How to Create DaemonSet in Kubernetes: Step-by-Step Guide
To create a
DaemonSet in Kubernetes, define a YAML manifest specifying the DaemonSet kind with a pod template. Apply it using kubectl apply -f to ensure a pod runs on every node automatically.Syntax
A DaemonSet manifest includes apiVersion, kind, metadata, and spec. The spec contains a selector to match pods and a template describing the pod to run on each node.
The key parts are:
- apiVersion: Usually
apps/v1. - kind: Must be
DaemonSet. - metadata: Name and labels for the DaemonSet.
- spec.selector: Label selector to match pods.
- spec.template: Pod specification that runs on each node.
yaml
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: example-daemonset
spec:
selector:
matchLabels:
name: example-pod
template:
metadata:
labels:
name: example-pod
spec:
containers:
- name: example-container
image: busybox
command: ["sleep", "3600"]
Example
This example creates a DaemonSet named example-daemonset that runs a busybox container on every node. The container simply sleeps for 3600 seconds.
yaml
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: example-daemonset
spec:
selector:
matchLabels:
name: example-pod
template:
metadata:
labels:
name: example-pod
spec:
containers:
- name: example-container
image: busybox
command: ["sleep", "3600"]
Output
daemonset.apps/example-daemonset created
# After running kubectl get pods -o wide, you will see one pod per node running the busybox container.
Common Pitfalls
- Selector mismatch: The
spec.selector.matchLabelsmust exactly match the labels inspec.template.metadata.labels. Otherwise, the DaemonSet controller will reject the manifest. - Using
latestimage tag: Avoid usinglatestfor container images in production to prevent unpredictable updates. - Resource limits missing: Not setting resource requests and limits can cause scheduling issues.
- DaemonSet pods on master nodes: By default, DaemonSets run on all nodes including masters; use
nodeSelectorortolerationsto control this.
yaml
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: wrong-daemonset
spec:
selector:
matchLabels:
name: wrong-pod
template:
metadata:
labels:
name: example-pod # Mismatch here causes error
spec:
containers:
- name: example-container
image: busybox
command: ["sleep", "3600"]
# Corrected selector and labels:
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: correct-daemonset
spec:
selector:
matchLabels:
name: example-pod
template:
metadata:
labels:
name: example-pod
spec:
containers:
- name: example-container
image: busybox
command: ["sleep", "3600"]
Quick Reference
| Field | Description |
|---|---|
| apiVersion | API version, usually apps/v1 |
| kind | Resource type, must be DaemonSet |
| metadata.name | Name of the DaemonSet |
| spec.selector.matchLabels | Labels to select pods managed by DaemonSet |
| spec.template.metadata.labels | Labels assigned to pods, must match selector |
| spec.template.spec.containers | Container specs to run on each node |
| spec.template.spec.nodeSelector | Optional: restrict nodes by labels |
| spec.template.spec.tolerations | Optional: allow pods on tainted nodes |
Key Takeaways
A DaemonSet ensures a pod runs on every node matching its selector.
The selector labels must exactly match pod template labels to avoid errors.
Use
kubectl apply -f with a valid YAML manifest to create a DaemonSet.Control node placement with
nodeSelector and tolerations if needed.Avoid using
latest image tags and always set resource limits for stability.