Sub topic 13
DaemonSet or something else?
This chapter helps you decide when a DaemonSet is the right workload type. It assumes you know Pods, labels and selectors, Deployments and ReplicaSets, and that a cluster is made of nodes. It also assumes you have read the earlier chapters on DaemonSet placement and taints.
The deciding question
Ask what drives the number of Pods. A DaemonSet ties the Pod count to the node count: one Pod per matching node, added and removed as nodes join and leave. A Deployment ties it to a number you choose. A StatefulSet ties it to a number you choose and also gives each Pod a stable identity and storage. A static Pod is not tied to the API server at all.
Comparison
| Property | DaemonSet | Deployment | StatefulSet | Static Pod |
|---|---|---|---|---|
| Pod count | One per matching node | Fixed `replicas` | Fixed `replicas` | One per manifest file on a node |
| Placement | Controller creates a Pod bound to each node | Scheduler picks nodes | Scheduler picks nodes | The kubelet on that node |
| Identity | Random name suffix | Random name suffix | Ordered, stable names | Node name appended |
| Scaling | Add or remove nodes | Change `replicas` | Change `replicas` | Add or remove files |
| Managed through API | Yes | Yes | Yes | Only a read-only mirror Pod |
| Typical use | Log, metrics, network, storage agents | Stateless services | Databases, quorum systems | Control plane components |
Common mistakes
Using a DaemonSet to get "many replicas"
A DaemonSet does not take a replica count. If you want exactly three copies of a web service, use a Deployment. A DaemonSet on a 40-node cluster gives you 40 copies, and the count changes whenever the autoscaler changes the node pool.
Using a Deployment for a node agent
A Deployment with replicas: 5 on a five-node cluster may put two Pods on one node and none on another. Anti-affinity can reduce that, but you then have to keep replicas in step with the node count yourself. A DaemonSet does this for you.
Using a StatefulSet for per-node state
If each node needs its own local data, a DaemonSet with a hostPath volume or a local volume fits. A StatefulSet gives a Pod a persistent volume claim that follows the Pod's identity, not the node. Use a StatefulSet when the data must stay with a named replica wherever it is scheduled.
Expecting a DaemonSet to survive without the control plane
A DaemonSet Pod is created by the DaemonSet controller, so it needs a working API server and controller manager to be created. Components that must start before the cluster exists, such as the API server itself, run as static Pods. The kubelet reads their manifests from a directory on the node, usually /etc/kubernetes/manifests on kubeadm clusters. It starts them even when no API server is reachable.
Cases that look ambiguous
- A node-level cache that many Pods read. Run it as a DaemonSet and reach it through the node's IP or a host port, as covered earlier. A Deployment behind a Service sends traffic to any node, which defeats the point of a local cache.
- A batch job that must run once on every node. A DaemonSet restarts its Pods forever. For a one-time task, the Pod would need to finish and stay finished, which a DaemonSet does not do. Use a Job per node, or run the work as an init step inside a DaemonSet Pod and then sleep.
- A service that needs only some nodes. Still a DaemonSet. Limit it with
nodeSelectoror node affinity rather than switching workload types. - Operational cost. Every DaemonSet Pod consumes resources on every node. Ten small agents at 100m CPU each reserve one full CPU per node. Review the requests before adding another one.
Quick decision rules
- Needed on every node, or on a labeled subset, and it should follow node changes: DaemonSet.
- Stateless, scalable by count: Deployment.
- Needs stable names, ordered startup, or its own volume per replica: StatefulSet.
- Must run before the API server is available: static Pod.
- Runs to completion once: Job or CronJob.
Exercise
A team runs a metrics exporter with a Deployment of replicas: 6 on a six-node cluster. After the cluster autoscaler adds two nodes, those two nodes have no exporter, and one older node has two copies. Which workload type should they use, and why?
Show answer
A DaemonSet. The exporter is needed once per node, and the Pod count should follow the node count. A Deployment's replica count is fixed and the scheduler may stack Pods on one node, so it cannot track nodes joining or leaving. A DaemonSet creates a Pod on each new matching node automatically and removes it when the node goes away.
Exercise
You need a component that starts before the API server is running on a control plane node. Can a DaemonSet do this? What do you use instead?
Show answer
No. DaemonSet Pods are created by the DaemonSet controller through the API server, so they cannot exist before it. Use a static Pod. The kubelet reads its manifest from a local directory and starts it without the API server.