TExcelTAD
Home › Kubernetes › DaemonSets

DaemonSets

A DaemonSet runs exactly one Pod on every node (or every node matching a selector). When a node joins the cluster, it gets the Pod; when the node leaves, the Pod is garbage-collected. You don't set replicas: the number of nodes decides.

Typical DaemonSets are node-level agents:

Purpose Examples
Networking kube-proxy, Calico/Cilium agents
Logs Fluent Bit, Vector, Promtail
Metrics Prometheus node-exporter, Datadog agent
Storage CSI node plugins
Security Falco, intrusion detection
yaml
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: node-exporter
  namespace: monitoring
spec:
  selector:
    matchLabels: {app: node-exporter}
  updateStrategy:
    type: RollingUpdate
    rollingUpdate: {maxUnavailable: 1}
  template:
    metadata:
      labels: {app: node-exporter}
    spec:
      nodeSelector:
        kubernetes.io/os: linux
      tolerations:
        - key: node-role.kubernetes.io/control-plane
          operator: Exists
          effect: NoSchedule
      hostNetwork: true
      containers:
        - name: node-exporter
          image: quay.io/prometheus/node-exporter:v1.9.1
          ports: [{containerPort: 9100}]
          resources:
            requests: {cpu: 50m, memory: 32Mi}
bash
kubectl get ds -n monitoring
kubectl get pods -n monitoring -o wide -l app=node-exporter

Sample output:

text
NAME            DESIRED   CURRENT   READY   UP-TO-DATE   AVAILABLE   NODE SELECTOR            AGE
node-exporter   3         3         3       3            3           kubernetes.io/os=linux   3m

Which nodes get a Pod?

A node is eligible if it matches the DaemonSet's nodeSelector/node affinity and the Pod tolerates the node's taints. Control-plane nodes usually carry a NoSchedule taint, so agents that should also run there need a toleration (like the one above). A model of the DaemonSet controller's decision:

python · try it yourself
nodes = [
    {"name": "cp-1", "labels": {"kubernetes.io/os": "linux"},
     "taints": [("node-role.kubernetes.io/control-plane", "NoSchedule")]},
    {"name": "worker-1", "labels": {"kubernetes.io/os": "linux"}, "taints": []},
    {"name": "worker-2", "labels": {"kubernetes.io/os": "linux", "gpu": "true"}, "taints": [("gpu", "NoSchedule")]},
    {"name": "win-1", "labels": {"kubernetes.io/os": "windows"}, "taints": []},
]

def eligible(node, selector, tolerations):
    if any(node["labels"].get(k) != v for k, v in selector.items()):
        return False, "nodeSelector mismatch"
    for key, effect in node["taints"]:
        if (key, effect) not in tolerations:
            return False, f"untolerated taint {key}:{effect}"
    return True, "run Pod"

daemonsets = {
    "node-exporter": ({"kubernetes.io/os": "linux"}, {("node-role.kubernetes.io/control-plane", "NoSchedule")}),
    "gpu-plugin": ({"gpu": "true"}, {("gpu", "NoSchedule")}),
}
for ds, (selector, tolerations) in daemonsets.items():
    print(ds)
    for n in nodes:
        ok, why = eligible(n, selector, tolerations)
        print(f"  {n['name']:9} {'yes' if ok else 'no ':3}  {why}")
node-exporter
  cp-1      yes  run Pod
  worker-1  yes  run Pod
  worker-2  no   untolerated taint gpu:NoSchedule
  win-1     no   nodeSelector mismatch
gpu-plugin
  cp-1      no   nodeSelector mismatch
  worker-1  no   nodeSelector mismatch
  worker-2  yes  run Pod
  win-1     no   nodeSelector mismatch

Following the node list

Because the desired set is "one per eligible node", the reconcile loop compares two sets of node names instead of counting replicas:

python · try it yourself
def reconcile(eligible_nodes, pods_on):
    to_create = sorted(set(eligible_nodes) - set(pods_on))
    to_delete = sorted(set(pods_on) - set(eligible_nodes))
    return to_create, to_delete

pods_on = ["worker-1", "worker-2"]
events = [
    ("initial", ["worker-1", "worker-2", "worker-3"]),
    ("worker-4 joins", ["worker-1", "worker-2", "worker-3", "worker-4"]),
    ("worker-2 removed", ["worker-1", "worker-3", "worker-4"]),
]
for label, eligible_nodes in events:
    create, delete = reconcile(eligible_nodes, pods_on)
    pods_on = sorted((set(pods_on) - set(delete)) | set(create))
    print(f"{label:17} create={create} delete={delete} -> pods on {pods_on}")
initial           create=['worker-3'] delete=[] -> pods on ['worker-1', 'worker-2', 'worker-3']
worker-4 joins    create=['worker-4'] delete=[] -> pods on ['worker-1', 'worker-2', 'worker-3', 'worker-4']
worker-2 removed  create=[] delete=['worker-2'] -> pods on ['worker-1', 'worker-3', 'worker-4']

Updates

updateStrategy.type Behaviour
RollingUpdate (default) Replace Pods node by node, maxUnavailable at a time (can also use maxSurge to start the new Pod before stopping the old)
OnDelete New template applies only when you delete a Pod yourself; for careful manual rollouts

DaemonSet Pods often use hostNetwork, hostPath volumes or privileged mode because they manage the node itself. Treat them as highly privileged software and review them carefully.

Tip: To run on only some nodes, label those nodes (kubectl label node worker-2 role=ingress) and add a matching nodeSelector.

Test yourself

How many Pods does a DaemonSet with no node selector create in a cluster with 5 schedulable, untainted nodes?

  1. 1
  2. 3
  3. 5
  4. It depends on replicas

Why might a DaemonSet Pod not run on control-plane nodes?

  1. DaemonSets never run there
  2. Control-plane nodes have a NoSchedule taint the Pod doesn't tolerate
  3. They have no kubelet
  4. Because of resource quotas

Which is a typical DaemonSet workload?

  1. A web front end
  2. A log collector like Fluent Bit
  3. A nightly backup job
  4. A PostgreSQL primary