Sub topic 1
What a DaemonSet does
Prerequisites
This chapter assumes you know the following:
- Basic Kubernetes objects: Pods, labels and selectors, and namespaces.
- How to read and apply YAML manifests with
kubectl. - What a Deployment and a ReplicaSet do.
- That a cluster is made of nodes, which are the machines that run Pods.
The guarantee
A DaemonSet is a workload controller that keeps one copy of a Pod running on every node in a cluster, or on a chosen subset of nodes. A Deployment asks "how many replicas do I want?" A DaemonSet asks "which nodes should have a Pod?"
The DaemonSet controller gives you three guarantees:
- When a node joins the cluster, the controller creates a Pod for it.
- When a node leaves the cluster, the Pod on that node is garbage collected.
- When a matching Pod is deleted or crashes, the controller replaces it on the same node.
Deleting the DaemonSet deletes all of its Pods. You never set a replica count. The count follows the number of eligible nodes.
Why this differs from a Deployment
A Deployment spreads replicas wherever the scheduler finds room. Two replicas may land on one node, and some nodes may get none. That is fine for a web service. It does not work for a program that must watch each machine, such as a log collector, a metrics exporter, or a network plugin. These programs need exactly one instance per node.
A first example
This DaemonSet runs a small log-reading Pod on every node, including control plane nodes, and mounts the node's /var/log read-only.
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: node-log-agent
namespace: kube-system
spec:
selector:
matchLabels:
app: node-log-agent
template:
metadata:
labels:
app: node-log-agent
spec:
tolerations:
- key: node-role.kubernetes.io/control-plane
operator: Exists
effect: NoSchedule
containers:
- name: agent
image: busybox:1.36
command:
- sh
- -c
- while true; do ls /host-logs | head -n 5; sleep 60; done
resources:
requests:
cpu: 20m
memory: 32Mi
limits:
memory: 64Mi
volumeMounts:
- name: varlog
mountPath: /host-logs
readOnly: true
volumes:
- name: varlog
hostPath:
path: /var/logThe manifest looks like a Deployment with two differences. The kind is DaemonSet, and there is no replicas field. The selector must match the labels in the Pod template, or the API server rejects the object. The tolerations entry lets the Pod run on control plane nodes, which are normally tainted. A later chapter covers taints in detail.
Apply it and check the result:
kubectl apply -f node-log-agent.yaml
kubectl get daemonset node-log-agent -n kube-system
kubectl get pods -n kube-system -l app=node-log-agent -o wideThe -o wide flag adds a NODE column. You should see exactly one Pod per node.
Reading the status columns
kubectl get daemonset prints several counters:
| Column | Meaning |
|---|---|
| DESIRED | Nodes that should run a Pod |
| CURRENT | Nodes that have a Pod |
| READY | Pods that pass their readiness check |
| UP-TO-DATE | Pods that match the latest template |
| AVAILABLE | Ready Pods that have stayed ready long enough |
| NODE SELECTOR | The node selector from the template, if any |
When DESIRED and READY differ, something is wrong on at least one node. The troubleshooting chapter shows how to find it.
Common mistakes
- Expecting control plane coverage by default. Control plane nodes usually carry a
NoScheduletaint. Without a toleration, no Pod runs there, and DESIRED is lower than your node count. - Selector and template labels that disagree. The API server rejects the manifest with a validation error.
- Omitting resource requests. Node agents run on every machine, so an unbounded agent can starve other workloads cluster-wide.
- Assuming a replica count. Setting
replicasin a DaemonSet is invalid. Control the count through node selection instead.
Exercise
A cluster has 5 worker nodes and 3 control plane nodes tainted with NoSchedule. You apply a DaemonSet with no tolerations and no node selector. What does DESIRED show, and what single change makes it show 8?
Show answer
DESIRED shows 5, because the control plane taint blocks the Pods there. Adding a toleration for the node-role.kubernetes.io/control-plane taint (operator Exists, effect NoSchedule) to the Pod template makes all 8 nodes eligible.
Exercise
Why does a DaemonSet manifest have no replicas field?
Show answer
The number of Pods is set by how many nodes match, not by a fixed number. The controller creates one Pod per eligible node, so the count changes as nodes join or leave.