What if you never had to log into each computer again to run your helper program?
Why DaemonSets for per-node workloads in Kubernetes? - Purpose & Use Cases
Imagine you have a cluster with many computers (nodes), and you want to run a small helper program on each one to watch or manage something important.
You try to log into each computer one by one and start the program manually.
This manual way is slow and boring because you must repeat the same steps on every computer.
You might forget some computers or make mistakes, causing problems later.
Also, when new computers join the cluster, you have to remember to do it again.
DaemonSets automatically run a copy of your helper program on every computer in the cluster.
They keep track of new computers and make sure the program runs there too, without you lifting a finger.
ssh node1 start-helper ssh node2 start-helper ...
kubectl apply -f daemonset.yaml
# DaemonSet runs helper on all nodes automaticallyYou can easily ensure important tools run everywhere in your cluster, keeping your system healthy and consistent.
Running a monitoring agent on every node to collect data about CPU and memory usage, so you always know how your cluster is performing.
Manual setup on each node is slow and error-prone.
DaemonSets automate running programs on all nodes.
This keeps your cluster consistent and easier to manage.
Practice
DaemonSet in Kubernetes?Solution
Step 1: Understand DaemonSet functionality
A DaemonSet ensures that a copy of a pod runs on all or some nodes in the cluster.Step 2: Compare with other Kubernetes objects
Other objects like Jobs or Services do not guarantee one pod per node; DaemonSet does.Final Answer:
To run one pod on every node in the cluster -> Option CQuick Check:
DaemonSet = one pod per node [OK]
- Confusing DaemonSet with Deployment
- Thinking DaemonSet manages storage
- Assuming DaemonSet schedules batch jobs
Solution
Step 1: Recall DaemonSet selector syntax
The selector must usematchLabelsormatchExpressionsunderselector.Step 2: Identify correct syntax
selector: matchLabels: app: my-daemon usesmatchLabelscorrectly with key-value pairs. selector: labels: app: my-daemon is invalid because it useslabelsinstead ofmatchLabels. selector: matchExpressions: - key: app operator: Equals values: [my-daemon] has wrong operator case and structure. selector: matchLabels: app: my-daemon matchExpressions: - key: tier operator: In values: [backend] mixes both but DaemonSet selector only supports one selector type.Final Answer:
selector: matchLabels: app: my-daemon -> Option BQuick Check:
DaemonSet selector uses matchLabels [OK]
- Using 'labels' instead of 'matchLabels'
- Incorrect operator casing in matchExpressions
- Mixing matchLabels and matchExpressions in selector
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: log-agent
spec:
selector:
matchLabels:
app: log-agent
template:
metadata:
labels:
app: log-agent
spec:
containers:
- name: log-agent
image: log-agent:latest
Solution
Step 1: Understand DaemonSet behavior on new nodes
DaemonSets automatically create pods on every node that matches the selector, including new nodes joining the cluster.Step 2: Check node selector usage
This DaemonSet has no node selector restricting nodes, so it applies to all nodes.Final Answer:
A pod named log-agent will be created on the new node automatically -> Option AQuick Check:
DaemonSet pods auto-create on new nodes [OK]
- Assuming manual pod creation on new nodes
- Thinking node selector is mandatory
- Believing pods only run on labeled nodes
Solution
Step 1: Check DaemonSet selector and pod template labels
DaemonSet requires the selector to match the pod template labels exactly; otherwise, pods won't be created.Step 2: Evaluate other options
Missing container image causes pod creation failure but usually shows pod errors. No nodes means no pods but is rare. Wrong namespace means DaemonSet exists but unrelated to nodes.Final Answer:
The pod template labels do not match the DaemonSet selector -> Option DQuick Check:
Selector and pod labels must match [OK]
- Ignoring label mismatch between selector and template
- Assuming missing image stops pod creation silently
- Not verifying cluster node availability
Solution
Step 1: Identify how to target Linux nodes only
Kubernetes labels nodes withkubernetes.io/osset to their OS. UsingnodeSelectorwithkubernetes.io/os: linuxrestricts pods to Linux nodes.Step 2: Evaluate other options
Tolerations are for taints, not OS filtering. Selector matches pod labels, not nodes. Deployment does not guarantee one pod per node.Final Answer:
Add a nodeSelector with kubernetes.io/os: linux in the pod spec -> Option AQuick Check:
Use nodeSelector to filter nodes by OS [OK]
- Using selector to filter nodes instead of nodeSelector
- Confusing tolerations with node filtering
- Using Deployment instead of DaemonSet for per-node pods
Ready to prove it?
Pass the test and this pattern moves unknown
