Lesson 35 · Section 3: Scheduling
Lesson 35: DaemonSets: one Pod on every node
What a DaemonSet is, how its controller and the scheduler place exactly one Pod per eligible node, why taints decide where it runs, and how to create, update, roll back and debug one quickly. · 18 min read · 4 min video
Official competencies this lesson covers
Narrated by the local narrator voice. Keys: Space plays and pauses, J and L step, C captions, F full screen.
Why DaemonSets exist
Most workloads do not care where they run. A web application needs three copies somewhere in the cluster, and a Deployment plus the scheduler take care of that. Some software is different: it does its work on the node it runs on, so every node needs its own copy.
| Kind of node agent | Examples | What it needs from the node |
|---|---|---|
| Networking | kube-proxy, the CNI plugin's agent (Calico, Cilium, Flannel) | The node's network namespace and kernel |
| Log collection | Fluent Bit, Fluentd | The log files under /var/log |
| Monitoring | Prometheus node-exporter | The node's /proc and /sys |
| Storage | The node part of a CSI driver | The kubelet's directories and the node's devices |
You could try this with a Deployment whose replica count equals the number of nodes, but it breaks in three ways. The scheduler may put two replicas on one node and none on another. A node that joins later gets nothing until somebody scales up. And a node that leaves keeps counting as a replica that has to run elsewhere.
A DaemonSet is the workload object built for this case. The name comes from the Unix word daemon: a background process that runs on a machine for as long as the machine is up.
Parts:
- DaemonSet (log-agent)
- Pod log-agent-7k2xq (Running)
- Pod log-agent-m9d4c (Running)
- Pod log-agent-zr8wh (created on join)
- node01 (worker)
- node02 (worker)
- node03 (just joined)
- controlplane (tainted: no Pod)
Connections:
- DaemonSet to Pod log-agent-7k2xq
- DaemonSet to Pod log-agent-m9d4c: owns
- DaemonSet to Pod log-agent-zr8wh
- Pod log-agent-7k2xq to node01: runs on
- Pod log-agent-m9d4c to node02: runs on
- Pod log-agent-zr8wh to node03: runs on
Everything else in this lesson is an answer to one question: which nodes are eligible?
What a DaemonSet is made of
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: log-agent
namespace: logging
spec:
selector:
matchLabels:
app: log-agent
template:
metadata:
labels:
app: log-agent
spec:
containers:
- name: agent
image: busybox:1.36
command: ["sh", "-c", "while true; do ls /host/var/log/pods | wc -l; sleep 60; done"]
volumeMounts:
- name: varlog
mountPath: /host/var/log
readOnly: true
volumes:
- name: varlog
hostPath:
path: /var/log
This small agent prints how many Pod log directories its node holds, once a minute. A real log collector is the same
shape with a different image: a hostPath volume that reaches into the node, and one Pod per node to read it.
If you know the ReplicaSet and Deployment manifests from lessons 17 and 18, you already know this one. The differences are short:
| Field | In a DaemonSet | Good to know |
|---|---|---|
spec.replicas | Does not exist | The nodes decide the count. kubectl scale does not work on a DaemonSet. |
spec.selector | Required, immutable | Must match the template's labels, or the API server rejects the object. |
spec.template | An ordinary Pod template | restartPolicy must be Always (the default). Node selectors, affinity and tolerations go here. |
spec.updateStrategy | RollingUpdate (default) or OnDelete | The Deployment field is called strategy; here it is updateStrategy. |
spec.minReadySeconds | How long a new Pod must be ready before it counts as available | Default 0. Slows a rollout down on purpose. |
spec.revisionHistoryLimit | How many old template versions to keep for rollbacks | Default 10. |
The status reads differently too, because there is no replica number to compare with. This is what
kubectl get ds prints:
| Column | Status field | Meaning |
|---|---|---|
DESIRED | desiredNumberScheduled | Nodes that should run a Pod: the eligible nodes |
CURRENT | currentNumberScheduled | Eligible nodes that have a Pod |
READY | numberReady | Pods that pass their readiness probe |
UP-TO-DATE | updatedNumberScheduled | Pods made from the current template |
AVAILABLE | numberAvailable | Pods ready for at least minReadySeconds |
NODE SELECTOR | the template's nodeSelector | <none> means no node is filtered out by labels |
DESIRED is the number to read first. It is not something you set, it is the controller's answer to "how many nodes
are eligible?". If it is lower than you expect, the cause is on the nodes (taints, labels), not in the Pods.
How it works: the controller picks the node, the scheduler binds
Parts:
- DS controller (controller-manager)
- kube-scheduler (default scheduler)
- Pod log-agent-m9d4c (affinity: node02)
- node01 (has its own Pod)
- node02 (kubelet starts it)
- node03 (has its own Pod)
Connections:
- DS controller to Pod log-agent-m9d4c: creates
- kube-scheduler to Pod log-agent-m9d4c: binds
- Pod log-agent-m9d4c to node02: runs on
Like a ReplicaSet, a DaemonSet is only a record in etcd. The work is done by the DaemonSet controller, one of
the loops inside kube-controller-manager. It watches DaemonSets, Pods and Nodes, and whenever one of them
changes it walks through the nodes:
- List all nodes
- Per node: should it run a Pod? (selector, affinity, taints)
- Should, but has none: create a Pod for that node
- Should not, but has one: delete it
- More than one on a node: delete the extras
- Write the counts to status
The interesting part is how a Pod ends up on the right node. The controller does not start anything itself. It creates a Pod from the template and adds one thing to it: a node affinity that names the target node.
# kubectl -n logging get pod log-agent-m9d4c -o yaml (part of spec)
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchFields:
- key: metadata.name
operator: In
values:
- node02
From there the Pod takes the normal road you saw in the kube-scheduler lesson. It is created without a
spec.nodeName, the default scheduler picks it up, finds that only node02 satisfies the affinity, runs its
usual checks on that node (taints, resources, ports) and binds the Pod. The kubelet on node02 then starts the
containers.
Why this split? Because it gives DaemonSet Pods the same rules as every other Pod. One component decides whether a
Pod fits a node, so taints, resource requests and Pod priority all mean the same thing for a DaemonSet as for a
Deployment. If a node is full, the scheduler can even preempt lower priority Pods to make room for the agent,
which is why critical DaemonSets such as kube-proxy set priorityClassName: system-node-critical.
Under the hood · "DaemonSets bypass the scheduler": true once, not any more
Older courses and blog posts say that the DaemonSet controller sets spec.nodeName itself and that the scheduler
never sees these Pods. That was true in early Kubernetes. Since version 1.12 the controller adds the node affinity
shown above and the default scheduler does the binding.
Three things follow from the current behaviour:
- A DaemonSet Pod can be
Pending, for example when its node lacks the CPU or memory it requests. Describe the Pod and read theFailedSchedulingevent, as for any other Pod. - If
kube-scheduleris down, new DaemonSet Pods stayPendingtoo. Only static Pods start without a scheduler. - A node affinity you write in the template is still respected: the controller uses it to decide which nodes are eligible, then replaces it on each Pod with the affinity for that Pod's one node.
Which nodes get a Pod
A node is eligible when the template's node selector and node affinity match it and the Pod tolerates the node's taints. Those three topics have their own lessons in this section; here is how each one applies to a DaemonSet.
Fewer nodes: nodeSelector and node affinity
Add a nodeSelector (or a nodeAffinity) to the Pod template and the DaemonSet only covers the nodes that match:
spec:
template:
spec:
nodeSelector:
disktype: ssd
The controller keeps following the labels afterwards. Label another node disktype=ssd and it gets a Pod within
seconds; remove the label from a node and the Pod on it is deleted. That is different from an ordinary Pod,
which stays where it is when labels change after scheduling.
More nodes: tolerations
A taint on a node keeps away every Pod that does not tolerate it. The DaemonSet controller adds a set of tolerations to every Pod it creates, so that node agents stay on nodes that are in trouble:
| Toleration added for you | Effect | What it means for the agent |
|---|---|---|
node.kubernetes.io/not-ready | NoExecute | Not evicted when the node is not ready |
node.kubernetes.io/unreachable | NoExecute | Not evicted when the node stops answering |
node.kubernetes.io/disk-pressure | NoSchedule | Still placed on a node that is short of disk |
node.kubernetes.io/memory-pressure | NoSchedule | Still placed on a node that is short of memory |
node.kubernetes.io/pid-pressure | NoSchedule | Still placed on a node that is short of process ids |
node.kubernetes.io/unschedulable | NoSchedule | Still placed on a cordoned node |
node.kubernetes.io/network-unavailable | NoSchedule | Only for Pods with hostNetwork: true, so a network plugin can start before the network works |
Compare the first two rows with what you learned about ordinary Pods: those tolerate not-ready and unreachable
for 300 seconds and are then evicted. DaemonSet Pods tolerate both without a time limit, because moving a node agent
to another node would be pointless.
What is not on the list matters just as much:
Agents that must run everywhere, whatever happens, tolerate every taint with a single entry. Look at kube-proxy:
kubectl -n kube-system get ds kube-proxy -o jsonpath='{.spec.template.spec.tolerations}{"\n"}'
# [{"operator":"Exists"}]
A toleration with operator: Exists and no key matches all taints, which is why kube-proxy runs on control plane
nodes while your own DaemonSet does not.
Nodes that come and go
| Event | What the DaemonSet does |
|---|---|
| A node joins the cluster | Creates a Pod for it, if it is eligible |
| A node is deleted | Its Pod is garbage collected; DESIRED drops by one |
| A node is cordoned | Nothing. The Pod stays, and a missing one is still created (the unschedulable toleration) |
A node gets a NoExecute taint the Pod does not tolerate | The Pod is evicted and not recreated there |
| You delete a DaemonSet Pod | A new Pod is created on the same node within seconds |
Updating and rolling back
Parts:
- DaemonSet (template: 1.37)
- Pod on node01 (1.37, ready)
- Pod on node02 (being replaced)
- Pod on node03 (1.36, waiting)
Connections:
- DaemonSet to Pod on node01: done
- DaemonSet to Pod on node02: now
- DaemonSet to Pod on node03: next
Unlike a bare ReplicaSet, a DaemonSet rolls out template changes by itself. spec.updateStrategy.type chooses how:
| Strategy | What happens after you change the template | Use it when |
|---|---|---|
RollingUpdate (default) | Old Pods are replaced automatically, a few nodes at a time | Almost always |
OnDelete | Nothing, until you delete a Pod yourself; its replacement uses the new template | You want to update node by node at your own pace |
Two settings under rollingUpdate control the pace. Both take a number or a percentage of DESIRED:
| Setting | Default | Meaning |
|---|---|---|
maxUnavailable | 1 | How many nodes may be without a ready Pod during the update |
maxSurge | 0 | On how many nodes a new Pod may start next to the old one before the old one is removed |
With the defaults, the update deletes the old Pod on one node, creates the new one, waits until it is ready and
only then moves to the next node. Note the order: delete first, then create. A Deployment starts new Pods
before it removes old ones; a DaemonSet by default does the opposite, because two copies of a node agent on one
node often clash (the same host port, the same files). The price is a short gap per node. If the agent can run
twice on a node, set maxSurge: 1 and maxUnavailable: 0 to close the gap. The two cannot both be 0.
The rollout commands you know from Deployments work here as well:
kubectl -n logging set image ds/log-agent agent=busybox:1.37
kubectl -n logging rollout status ds/log-agent
kubectl -n logging rollout history ds/log-agent
kubectl -n logging rollout undo ds/log-agent # back to the previous template
kubectl -n logging rollout undo ds/log-agent --to-revision=1 # or to a chosen revision
kubectl -n logging rollout restart ds/log-agent # replace every Pod, same template
Under the hood · Where a DaemonSet keeps its history
A Deployment keeps its old versions as ReplicaSets scaled to zero. A DaemonSet has no ReplicaSets, so it stores each version of its Pod template in a small object called a ControllerRevision:
kubectl -n logging get controllerrevisions
# NAME CONTROLLER REVISION AGE
# log-agent-6b8d7c5f4d daemonset.apps/log-agent 1 9m
# log-agent-85c9f6d7b8 daemonset.apps/log-agent 2 2m
Every Pod carries the hash of the revision it was made from in the label controller-revision-hash, which is how
the controller tells old Pods from new ones. kubectl rollout undo copies an old revision's template back into the
DaemonSet, and that starts an ordinary rolling update. revisionHistoryLimit (default 10) decides how many
revisions are kept. StatefulSets use the same mechanism.
A rollout that stops half way is nearly always a new Pod that does not become ready. With maxUnavailable: 1 the
controller will not touch the next node until that one is fixed, so the damage stays on a single node. Find the
Pod that is not ready, read its events, then fix the template or run rollout undo.
DaemonSet or something else?
| You want | Use |
|---|---|
| One copy of an agent on every node (or on every node of a kind) | DaemonSet |
| N copies of an application, anywhere | Deployment |
| A Pod on one node that must start even when the API server is down | Static Pod (next lesson) |
| A job to run once on every node | A DaemonSet is the usual tool, removed afterwards; a Job cannot target "each node" |
DaemonSet Pods and static Pods are easy to mix up, because both are tied to a node and neither is touched by
kubectl drain. The difference is who creates them. A static Pod is created by the kubelet from a file on the
node, and exists without any control plane. A DaemonSet Pod is created by a controller through the API server,
and can be updated, rolled back and deleted with kubectl. On a kubeadm cluster the control plane components are
static Pods; kube-proxy and the CNI agent are DaemonSets.
Using it
Example · Create a DaemonSet and read its counts
Save the manifest above as log-agent.yaml. The cluster has one control plane node and two workers.
kubectl create namespace logging
kubectl apply -f log-agent.yaml
kubectl -n logging get ds log-agent # DESIRED 2, CURRENT 2, READY 2
kubectl -n logging get pods -o wide # one Pod on node01, one on node02
kubectl -n logging logs ds/log-agent # output of one of the Pods
Two Pods on three nodes is correct: the control plane node is tainted and the template has no toleration for it.
The Pod names are the DaemonSet's name plus five random characters, and kubectl describe pod shows
Controlled By: DaemonSet/log-agent.
Example · Run it on the control plane nodes too
The task: "make sure log-agent runs on every node of the cluster". Check the taint first, then tolerate it.
kubectl describe node controlplane | grep -i taints
# Taints: node-role.kubernetes.io/control-plane:NoSchedule
kubectl -n logging edit ds log-agent
Add under spec.template.spec:
tolerations:
- key: node-role.kubernetes.io/control-plane
operator: Exists
effect: NoSchedule
kubectl -n logging get ds log-agent # DESIRED 3
kubectl -n logging get pods -o wide # a Pod on controlplane as well
Adding the toleration changes the Pod template, so the two existing Pods are replaced in a rolling update as well.
Example · Limit a DaemonSet to some nodes
Only the nodes with SSDs should run the agent.
kubectl label node node01 disktype=ssd
kubectl -n logging patch ds log-agent --type merge \
-p '{"spec":{"template":{"spec":{"nodeSelector":{"disktype":"ssd"}}}}}'
kubectl -n logging get ds log-agent # DESIRED 1, NODE SELECTOR disktype=ssd
kubectl label node node02 disktype=ssd
kubectl -n logging get ds log-agent # DESIRED 2, no change to the DaemonSet needed
The Pod on a node that does not match is deleted; a node that starts to match gets a Pod.
Example · Update the image, watch the rollout, roll back
kubectl -n logging set image ds/log-agent agent=busybox:1.37
kubectl -n logging rollout status ds/log-agent
# Waiting for daemon set "log-agent" rollout to finish: 1 out of 3 new pods have been updated...
# daemon set "log-agent" successfully rolled out
kubectl -n logging get pods -L controller-revision-hash # all Pods carry the new hash
kubectl -n logging rollout history ds/log-agent # revisions 1 and 2
kubectl -n logging rollout undo ds/log-agent # back to busybox:1.36
If the new image had a typo, rollout status would hang at "0 out of 3 new pods have been updated" with one Pod in
ImagePullBackOff and the other nodes still on the old version. rollout undo repairs it in one command.
Tricks
Common mistakes
- Writing
replicasin a DaemonSet. The field does not exist and the API server rejects the manifest. - Expecting a Pod on the control plane node. Without a toleration for
node-role.kubernetes.io/control-plane, the DaemonSet skips it andDESIREDis one lower than the node count. - Writing
strategyinstead ofupdateStrategy. The first belongs to Deployments. The types differ too:RollingUpdateandOnDeletehere,RollingUpdateandRecreatethere. - Deleting Pods to get rid of an agent. They come back on the same node. Delete the DaemonSet, or narrow its node selector.
- Assuming the scheduler is not involved. It is. A DaemonSet Pod can be
Pendingfor lack of resources, and a broken scheduler holds up DaemonSet Pods like any others. - Forgetting
--ignore-daemonsetsonkubectl drain. The command stops with an error before it evicts anything. - Putting the node selector or tolerations on the DaemonSet's own
spec. They belong in the Pod template:spec.template.spec.nodeSelectorandspec.template.spec.tolerations.
Recap
- A DaemonSet runs one Pod on every eligible node and has no
replicasfield; Pods are added and removed as nodes join, leave or change labels. - The DaemonSet controller creates each Pod with a node affinity for one node; the default scheduler binds it, so taints, resources and priority apply as usual.
- Eligible means: the template's
nodeSelectorand affinity match, and the Pod tolerates the node's taints. Node condition taints and the cordon are tolerated automatically, the control plane taint is not. - Template changes roll out with
RollingUpdate(maxUnavailable: 1,maxSurge: 0: delete, then create, node by node).OnDeletewaits for you. History lives in ControllerRevisions. - In the exam: generate the YAML from
kubectl create deployment --dry-run=client -o yaml, readDESIREDinkubectl get ds, check node taints, and usekubectl rollout status | history | undo ds/NAME.
Official docs
Quiz: check what you learned
5 questions on this lesson · about 3 min · instant feedback. Missed questions come back in your review queue.
Sign in to take the quiz and keep your score.
Practise: try it on a real cluster
2 hands-on labs for this lesson. You get goals, not steps: any solution that reaches them passes.
Run an agent on every node of the cluster, the tainted control plane included, and cut another DaemonSet back to the nodes that carry one label.
Build · Level 2 · standard · target 15 min
Section capstone on a three-node cluster. Reserve a node for one team, keep a search engine on fast disks, run an agent on every node whatever its taints, set resource defaults for a namespace, and place a Pod no scheduler will take.
Build · Level 3 · challenge · target 35 min
Discussion
Learners ask and answer questions here. Sign in to read and join the discussion.