Sub topic 5
Running on a subset of nodes
This chapter assumes you can write a basic DaemonSet manifest, know how labels and selectors work, and have read the previous chapter on how the controller places one Pod per node. By default a DaemonSet targets every node. Often you want only some of them, such as nodes with SSD disks, GPU nodes, or one node pool.
Two ways to narrow the target
The DaemonSet controller reads node-selection rules from the Pod template. It creates a Pod only on nodes that satisfy them. You have two tools:
nodeSelector: a map of labels. A node must have all of them.- Node affinity: a richer rule with operators such as
In,NotIn,Exists, andGt.
Both go under spec.template.spec, not at the top level of the DaemonSet.
Using nodeSelector
First label the nodes you want:
kubectl label node worker-1 disktype=ssd
kubectl label node worker-2 disktype=ssdThen add the selector to the Pod template:
spec:
template:
spec:
nodeSelector:
disktype: ssdOnly worker-1 and worker-2 get a Pod. Other nodes are not counted in the DaemonSet's DESIRED number, so kubectl get daemonset shows DESIRED 2 even in a larger cluster.
The controller keeps watching node labels. If you label a third node disktype=ssd, a Pod appears there within seconds. If you remove the label from a node, the controller deletes the Pod that was running on it.
kubectl label node worker-3 disktype=ssd
kubectl label node worker-1 disktype-The second command removes the label, because the trailing - means "delete this key".
Using node affinity
Use node affinity when equality is not enough. DaemonSets support only the required form, requiredDuringSchedulingIgnoredDuringExecution. This example runs on nodes in two zones and skips nodes marked as spot instances:
spec:
template:
spec:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: topology.kubernetes.io/zone
operator: In
values:
- eu-west-1a
- eu-west-1b
- key: node-lifecycle
operator: NotIn
values:
- spotExpressions inside one matchExpressions list are combined with AND. Separate entries in nodeSelectorTerms are combined with OR. If you also set nodeSelector, a node must satisfy both.
Common errors
DESIREDis 0. No node carries the label. Check withkubectl get nodes --show-labelsorkubectl get nodes -l disktype=ssd. A typo in the key or value is the usual cause.- Selector in the wrong place. Putting
nodeSelectorunderspecinstead ofspec.template.specis rejected as an unknown field or ignored by tooling that is not strict. The DaemonSet's ownspec.selectoris unrelated. It only matches Pods by label. - Pods missing on labeled nodes. The node probably has a taint the template does not tolerate. Selection and taints are checked separately, and both must pass. The next chapter covers tolerations.
- Expecting
preferredaffinity to work. A soft preference makes no sense when the goal is "run here or not at all". Use the required form. - Changing the selector and expecting instant change. Editing the template is an update like any other, and it follows your update strategy. Pods on nodes that no longer match are removed, and new ones appear on nodes that now match.
Checking the result
kubectl get daemonset node-log-agent -n kube-system
kubectl get pods -n kube-system -l app=node-log-agent -o wideThe NODE column in the second command shows where each Pod landed. Compare it with the output of kubectl get nodes -l disktype=ssd. The two lists should match.
Exercise
A DaemonSet has nodeSelector: {disktype: ssd}. Five nodes carry the label, but kubectl get daemonset shows DESIRED 4. The fifth node is healthy. What is the most likely cause, and how do you confirm it?
Show answer
The fifth node probably has a taint that the Pod template does not tolerate, so the controller does not count it as a target. Confirm with kubectl describe node <name> and look at the Taints field. Then add a matching toleration to the Pod template, or remove the taint.
Exercise
You want a DaemonSet to run on nodes whose region label is us-east or us-west. Which mechanism do you use, and why is nodeSelector not enough?
Show answer
Use node affinity with operator: In and the values us-east and us-west. A nodeSelector can only require one exact value per key, so it cannot express "either of these".