0
0
KubernetesHow-ToBeginner · 4 min read

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.matchLabels must exactly match the labels in spec.template.metadata.labels. Otherwise, the DaemonSet controller will reject the manifest.
  • Using latest image tag: Avoid using latest for 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 nodeSelector or tolerations to 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

FieldDescription
apiVersionAPI version, usually apps/v1
kindResource type, must be DaemonSet
metadata.nameName of the DaemonSet
spec.selector.matchLabelsLabels to select pods managed by DaemonSet
spec.template.metadata.labelsLabels assigned to pods, must match selector
spec.template.spec.containersContainer specs to run on each node
spec.template.spec.nodeSelectorOptional: restrict nodes by labels
spec.template.spec.tolerationsOptional: 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.