Working with DaemonSets

1. Creating DaemonSet

Example: Node log agent DaemonSet

apiVersion: apps/v1
kind: DaemonSet
metadata: { name: fluent-bit, namespace: logging }
spec:
  selector: { matchLabels: { app: fluent-bit } }
  template:
    metadata: { labels: { app: fluent-bit } }
    spec:
      tolerations:
      - { operator: Exists, effect: NoSchedule }
      containers:
      - name: fb
        image: fluent/fluent-bit:3
        volumeMounts:
        - { name: varlog, mountPath: /var/log }
      volumes:
      - { name: varlog, hostPath: { path: /var/log } }

2. Listing DaemonSets

kubectl get ds -A
kubectl get ds fluent-bit -n logging -o wide
ColumnMeaning
DESIREDNodes matching nodeSelector/tolerations
CURRENTPods created
READYPods passing readiness
UP-TO-DATEPods matching latest spec

3. Describing DaemonSet Details

SectionInfo
Node-SelectorWhere pods are scheduled
Pods StatusRunning counts
Update StrategyRollingUpdate / OnDelete

4. Understanding DaemonSet Scheduling

MechanismDetail
SchedulerDefault kube-scheduler with affinity injection
New nodeDS controller adds pod when node joins
Node removalPod deleted with node

5. Using Node Selectors

spec:
  template:
    spec:
      nodeSelector:
        kubernetes.io/os: linux
        node-role: edge
UseExample
OSkubernetes.io/os=linux
Architecturekubernetes.io/arch=amd64
Custom rolenode-role=ingress

6. Using Node Affinity

affinity:
  nodeAffinity:
    requiredDuringSchedulingIgnoredDuringExecution:
      nodeSelectorTerms:
      - matchExpressions:
        - { key: gpu, operator: In, values: [nvidia] }
ModeBehavior
requiredHard requirement
preferredSoft preference w/ weight

7. Tolerating Taints

tolerations:
- operator: Exists                            # tolerate any taint
- key: node-role.kubernetes.io/control-plane
  operator: Exists
  effect: NoSchedule
UseDetail
Run on masterTolerate control-plane taint
Run on tainted edgeTolerate role-specific taint

8. Updating DaemonSets

updateStrategyBehavior
RollingUpdate (default)Replace pods node by node
OnDeleteUpdate only when pod manually deleted
kubectl rollout status ds/fluent-bit -n logging
kubectl rollout history ds/fluent-bit -n logging
kubectl rollout undo ds/fluent-bit -n logging

9. Setting Max Unavailable

updateStrategy:
  type: RollingUpdate
  rollingUpdate:
    maxUnavailable: 25%
    maxSurge: 0           # since 1.25; 0 by default for DS
FieldDefault
maxUnavailable1
maxSurge0 (no extra pod per node)

10. Deleting DaemonSets

CommandEffect
kubectl delete ds NAMECascading delete pods
kubectl delete ds NAME --cascade=orphanKeep pods

11. Troubleshooting DaemonSet Pods

SymptomLikely Cause
Not running on N nodesnodeSelector/affinity excludes them
Missing on masterMissing toleration for control-plane taint
Hostpath permission deniedSecurityContext / SELinux / mount options
Pods evictedResource pressure; lower priority class