DaemonSets for per-node workloads in Kubernetes - Time & Space Complexity
When using DaemonSets in Kubernetes, it's important to understand how the workload scales as the number of nodes grows.
We want to know how the number of operations changes when more nodes are added.
Analyze the time complexity of the following DaemonSet configuration.
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: example-daemonset
spec:
selector:
matchLabels:
app: example
template:
metadata:
labels:
app: example
spec:
containers:
- name: example-container
image: example-image
This DaemonSet ensures one pod runs on each node in the cluster.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Creating and managing one pod per node.
- How many times: Once for each node in the cluster.
As the number of nodes increases, the number of pods created and managed grows at the same rate.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 nodes | 10 pods managed |
| 100 nodes | 100 pods managed |
| 1000 nodes | 1000 pods managed |
Pattern observation: The operations grow linearly with the number of nodes.
Time Complexity: O(n)
This means the work grows directly in proportion to the number of nodes in the cluster.
[X] Wrong: "Adding more nodes does not increase workload because DaemonSet pods run independently."
[OK] Correct: Each node requires its own pod, so more nodes mean more pods to create and manage, increasing workload linearly.
Understanding how DaemonSets scale helps you explain resource management in Kubernetes clusters clearly and confidently.
What if the DaemonSet used node selectors to run pods only on a subset of nodes? How would the time complexity change?
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
