Kubernetes DaemonSet

Master Kubernetes DaemonSets for cluster-wide node agents, log collectors, monitoring daemons, update strategies, tolerations, and interview scenarios.

A DaemonSet ensures that all eligible nodes in a Kubernetes cluster run a single copy of a specified Pod. As new nodes join the cluster, the DaemonSet controller automatically schedules Pods onto them, and when nodes are removed, those Pods are reclaimed by the garbage collector. DaemonSets are the foundation for running cluster-level background infrastructure such as storage daemons, log shippers, and node monitoring agents.

Core Concepts and Architecture

Unlike a Deployment, which schedules Pods based on overall resource availability and replica count across the cluster, a DaemonSet enforces a one-Pod-per-node topology (or one Pod per matching node subset). Modern Kubernetes versions use the default scheduler to place DaemonSet Pods using node affinity rules rather than directly writing the nodeName field.

Dimension DaemonSet Deployment StatefulSet
Pod Placement Exactly one Pod per matching node Dispersed across cluster by scheduler Dispersed with deterministic ordinal identity
Scaling Target Tied directly to node count Configured via spec.replicas or HPA Configured via spec.replicas
Primary Use Case Node-level logging, metrics, networking agents Stateless web apps, APIs, microservices Databases, distributed consensus systems
Storage Association Typically host paths (hostPath) or local storage Shared PVCs or ephemeral storage Dedicated volumeClaimTemplates per ordinal
Update Strategy RollingUpdate or OnDelete RollingUpdate or Recreate RollingUpdate (in reverse ordinal) or OnDelete

Node Selection and Tolerations

By default, DaemonSet Pods run on all worker nodes. However, Kubernetes control-plane nodes and specialized nodes (such as GPU nodes) usually carry taints (node-role.kubernetes.io/control-plane:NoSchedule). If an infrastructure agent (such as a CNI plugin or cluster-wide log collector) must run across all nodes including the control plane, you must declare explicit tolerations in the Pod template.

DaemonSet Manifest

The following manifest demonstrates a production-grade Prometheus node-exporter DaemonSet configured with host filesystem mounts, control-plane tolerations, and a rolling update strategy:

apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: node-exporter
  namespace: monitoring
  labels:
    app.kubernetes.io/name: node-exporter
spec:
  selector:
    matchLabels:
      app.kubernetes.io/name: node-exporter
  updateStrategy:
    type: RollingUpdate
    rollingUpdate:
      maxUnavailable: 1
  template:
    metadata:
      labels:
        app.kubernetes.io/name: node-exporter
    spec:
      tolerations:
        - key: node-role.kubernetes.io/control-plane
          operator: Exists
          effect: NoSchedule
        - key: CriticalAddonsOnly
          operator: Exists
      hostNetwork: true
      hostPID: true
      containers:
        - name: node-exporter
          image: quay.io/prometheus/node-exporter:v1.8.0
          args:
            - --path.procfs=/host/proc
            - --path.sysfs=/host/sys
            - --path.rootfs=/host/root
          resources:
            requests:
              cpu: 50m
              memory: 64Mi
            limits:
              cpu: 150m
              memory: 128Mi
          volumeMounts:
            - name: proc
              mountPath: /host/proc
              readOnly: true
            - name: sys
              mountPath: /host/sys
              readOnly: true
            - name: root
              mountPath: /host/root
              readOnly: true
      volumes:
        - name: proc
          hostPath:
            path: /proc
        - name: sys
          hostPath:
            path: /sys
        - name: root
          hostPath:
            path: /

Managing DaemonSets with kubectl

Useful operational commands for inspecting and rolling out updates to DaemonSets:

# Check DaemonSet status across nodes
kubectl get daemonset node-exporter -n monitoring
# NAME            DESIRED   CURRENT   READY   UP-TO-DATE   AVAILABLE   NODE SELECTOR   AGE
# node-exporter   5         5         5       5            5           <none>          2d

# Track the progress of a rolling update
kubectl rollout status ds/node-exporter -n monitoring

# View rollout revision history
kubectl rollout history ds/node-exporter -n monitoring

# Roll back to the previous revision if an update is unhealthy
kubectl rollout undo ds/node-exporter -n monitoring

Common Mistakes

  • Missing control plane tolerations: Omitting tolerations for node-role.kubernetes.io/control-plane prevents cluster agents from collecting telemetry or routing traffic on master nodes.
  • Unbounded resource limits: Failing to set memory limits on a DaemonSet agent can allow memory leaks to consume node RAM, triggering system-wide node OOM evictions.
  • Setting maxUnavailable to 0: Setting maxUnavailable: 0 in rollingUpdate creates a deadlock because a DaemonSet cannot create an extra Pod per node during a rollout.
  • Writable host mounts without security controls: Mounting root host paths (/) without readOnly: true exposes node filesystems to potential container breakout risks.

Best Practices

  • Define explicit requests and limits so that node daemons do not starve user workloads or suffer eviction.
  • Set updateStrategy.rollingUpdate.maxUnavailable appropriately (typically 1 or a small percentage like 10%) to maintain node monitoring coverage during upgrades.
  • Use nodeSelector or affinity when an agent is only needed on a subset of nodes, such as storage nodes or GPU nodes.
  • Mount host volumes with readOnly: true whenever write access is not strictly required.

Interview Questions

1. What is a DaemonSet and when should you choose it over a Deployment?

A DaemonSet guarantees that a single instance of a Pod runs on every eligible node (or filtered subset of nodes) in a Kubernetes cluster. Use a DaemonSet for node-level system agents, such as log collectors (Fluentd, Fluent Bit), monitoring agents (Prometheus Node Exporter), or networking plugins (Calico, Cilium). Use a Deployment when managing horizontally scalable application workloads where the exact host placement per node does not need a 1:1 mapping.

2. How does Kubernetes schedule DaemonSet Pods?

In modern Kubernetes (1.12+), DaemonSet Pods are scheduled by the standard Kubernetes default scheduler rather than having the DaemonSet controller directly set spec.nodeName. The DaemonSet controller generates the Pod with NodeAffinity requirements targeting the specific node. This allows DaemonSet Pods to respect scheduler features such as node affinities, taints, tolerations, and preemption priorities.

3. How do you restrict a DaemonSet to a subset of nodes?

You can constrain a DaemonSet using spec.template.spec.nodeSelector or spec.template.spec.affinity.nodeAffinity. For example, adding nodeSelector: disktype: ssd ensures the DaemonSet controller only schedules Pods on nodes labeled with disktype=ssd.

4. What update strategies do DaemonSets support?

DaemonSets support two update strategies in spec.updateStrategy.type: RollingUpdate (the default) and OnDelete. With RollingUpdate, old DaemonSet Pods are killed and new ones created automatically, respecting maxUnavailable. With OnDelete, new Pods are only created when old Pods are manually deleted or restarted by an operator.

5. Why can't maxSurge be used with a DaemonSet?

maxSurge cannot be used because a DaemonSet enforces at most one Pod per node (with matching host ports and local daemon bindings). Creating a second copy of a DaemonSet Pod on the same node would cause port conflicts and violate the one-Pod-per-node constraint. Hence, only maxUnavailable is configurable.

6. What happens to DaemonSet Pods when a node is cordoned or drained?

When a node is cordoned (kubectl cordon), no new application Pods are scheduled, but existing DaemonSet Pods remain running. When a node is drained (kubectl drain), kubectl requires the --ignore-daemonsets flag; otherwise, the drain command fails because DaemonSets cannot be permanently evicted from an active node.

Summary

  • DaemonSets ensure exactly one Pod runs per matching node across the cluster lifecycle.
  • Common use cases include cluster logging, host metrics collection, and CNI/CSI node daemons.
  • Tolerations are essential for running DaemonSet Pods on control-plane and tainted worker nodes.
  • Rolling updates only support maxUnavailable to preserve the 1:1 Pod-to-node topology.

Continue Learning

Previous: StatefulSet | Next: Job and CronJob

Was this guide helpful?

CodeWithVenu is free to read, with no paywall. If this guide saved you time or helped you prepare, a small voluntary contribution helps me write more practical tutorials, architecture guides, and interview-prep content.