Sub topic 4
How the controller places one Pod per node
This chapter assumes you know Pods, labels and selectors, namespaces, and how Deployments and ReplicaSets work. It also assumes you can apply YAML with kubectl. Here you will learn how the DaemonSet controller decides where Pods go, and what happens when nodes change.
The placement model
A Deployment asks for a number of replicas and lets the scheduler pick nodes. A DaemonSet works the other way around. It has no replica count. The controller looks at the list of nodes, decides which ones should run a Pod, and makes sure each of them has exactly one.
The controller runs this loop whenever a node, a DaemonSet, or one of its Pods changes:
- List all nodes.
- For each node, check whether the Pod template should run there. This depends on
nodeSelector, node affinity, and taints. - Create a Pod on each eligible node that has none.
- Delete Pods on nodes that are no longer eligible, and delete any extra Pods on a node.
How a Pod gets bound to its node
The controller does not leave the Pod's node open for the scheduler to choose. When it creates a Pod, it adds a node affinity term to that Pod. The term matches the target node by its name, using the field metadata.name. Because of this, the default scheduler still does the binding, but it can only choose the one node the controller picked.
You can see this on a running Pod:
kubectl get pod -n kube-system -l app=node-log-agent \
-o jsonpath='{.items[0].spec.affinity.nodeAffinity}'The output contains a matchFields entry for metadata.name. Any affinity you wrote in the template is kept, and the controller's term is added to it.
The controller also adds some tolerations automatically, so DaemonSet Pods are not evicted from nodes that are not ready or are under resource pressure. The next chapters cover taints in detail.
What changes trigger placement
| Event | Controller action |
|---|---|
| A node joins the cluster | Creates a Pod on it if eligible |
| A node is removed | The Pod is garbage collected |
| A node label changes so it no longer matches `nodeSelector` | Deletes the Pod |
| A node label changes so it now matches | Creates a Pod |
| A node gets a taint the template does not tolerate with `NoExecute` | The Pod is evicted |
| A DaemonSet Pod is deleted by hand | Creates a replacement |
This is why a DaemonSet suits cluster add-ons. You do not scale it. Adding a node adds a Pod.
Reading the status
Run the commands from the earlier example and look at the counts:
kubectl get daemonset node-log-agent -n kube-systemThe columns that matter here are:
DESIRED: the number of nodes that should run the Pod.CURRENT: the number of nodes that have a Pod.READY: how many of those Pods pass their readiness check.UP-TO-DATE: how many run the current template.AVAILABLE: how many are ready for the minimum time.NODE SELECTOR: the selector from the template, if any.
DESIRED is computed from eligible nodes, not from the total number of nodes. If you have five nodes and a nodeSelector matches two, DESIRED is 2.
Common errors
- A node is missing a Pod. The node is probably tainted, and the template has no matching toleration. Check
kubectl describe node <name>for taints. - A Pod stays Pending on one node. The target node may lack CPU or memory for the Pod's requests. Because the Pod is pinned to that node, the scheduler cannot place it elsewhere.
- Duplicate agents. Some clusters also run the same add-on through another mechanism, such as a managed add-on. Check for overlap before creating your own DaemonSet.
- Editing the selector. The
spec.selectorof a DaemonSet cannot be changed after creation inapps/v1. Create a new DaemonSet instead.
Exercise
A cluster has six nodes. Two are labeled disktype=ssd, and the DaemonSet template has nodeSelector disktype=ssd. You label a third node disktype=ssd. What happens to DESIRED, and what does the controller do?
Show answer
DESIRED rises from 2 to 3. The controller sees that the third node is now eligible and has no Pod, so it creates one there. The other three nodes are unaffected.