Try Sailor Cloud - 25% off!

Claim Now
Back to all posts

Kubernetes DaemonSets: Run Pods on Every Node

Kubernetes DaemonSets: Run Pods on Every Node

DaemonSets ensure a pod runs on every (or selected) node in the cluster.

Use Cases

  • Log collection (Fluentd, Filebeat)
  • Monitoring agents (Prometheus Node Exporter, Datadog)
  • Network plugins (Calico, Weave)
  • Storage daemons (GlusterFS, Ceph)
  • Node configuration

Creating a DaemonSet

apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: node-exporter
  namespace: monitoring
spec:
  selector:
    matchLabels:
      app: node-exporter
  template:
    metadata:
      labels:
        app: node-exporter
    spec:
      containers:
      - name: node-exporter
        image: prom/node-exporter:latest
        ports:
        - containerPort: 9100
          hostPort: 9100

DaemonSet vs Deployment

FeatureDaemonSetDeployment
SchedulingOne per nodeBased on replicas
ScalingAutomatic with nodesManual/HPA
Use caseNode-level servicesApplication workloads

Node Selection

All Nodes (Default)

spec:
  template:
    spec:
      tolerations:
      - operator: Exists  # Tolerate all taints

Specific Nodes

spec:
  template:
    spec:
      nodeSelector:
        node-type: logging

Using Affinity

spec:
  template:
    spec:
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
            - matchExpressions:
              - key: zone
                operator: In
                values:
                - us-east-1a

Tolerating Control Plane Nodes

spec:
  template:
    spec:
      tolerations:
      - key: node-role.kubernetes.io/control-plane
        operator: Exists
        effect: NoSchedule

Update Strategy

spec:
  updateStrategy:
    type: RollingUpdate  # Default
    rollingUpdate:
      maxUnavailable: 1

Fluentd Log Collector Example

apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: fluentd
  namespace: logging
spec:
  selector:
    matchLabels:
      app: fluentd
  template:
    metadata:
      labels:
        app: fluentd
    spec:
      containers:
      - name: fluentd
        image: fluent/fluentd:latest
        volumeMounts:
        - name: varlog
          mountPath: /var/log
        - name: dockerlogs
          mountPath: /var/lib/docker/containers
          readOnly: true
      volumes:
      - name: varlog
        hostPath:
          path: /var/log
      - name: dockerlogs
        hostPath:
          path: /var/lib/docker/containers

Managing DaemonSets

# List DaemonSets
kubectl get daemonsets -A

# Check status
kubectl rollout status daemonset/node-exporter

# Update image
kubectl set image daemonset/node-exporter node-exporter=prom/node-exporter:v1.5.0

Practice DaemonSets

DaemonSets appear in CKA exams. Practice at Sailor.sh.

Start Free Practice