Kubernetes DaemonSet Practical: Hands-On Tutorial with BusyBox Example

Practical Kubernetes DaemonSet tutorial with step-by-step examples, YAML configuration, kubectl commands, tolerations, and node-level deployment using BusyBox.

DaemonSet Example

Download the YAML configuration file for the DaemonSet example

Download daemonset.yaml

Create the DaemonSet

kubectl apply -f daemonset.yaml

Applies the configuration from the YAML file to create the DaemonSet.

Get the Pods List

kubectl get pods --selector=app=daemonset-example -o wide

There should be one pod for each worker node in your cluster.

Cleanup

kubectl delete -f daemonset.yaml

Deletes all resources defined in the YAML configuration file.

YAML Configuration File

apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: daemonset-example
  labels:
    app: daemonset-example
spec:
  selector:
    matchLabels:
      app: daemonset-example
  template:
    metadata:
      labels:
        app: daemonset-example
    spec:
      tolerations:
      - key: node-role.kubernetes.io/master
        effect: NoSchedule
      containers:
      - name: busybox
        image: busybox
        args:
        - sleep
        - "10000"

YAML Configuration Explanation:

DaemonSet Structure:

  • apiVersion: apps/v1 → Specifies the Kubernetes API version for DaemonSet
  • kind: DaemonSet → Defines this as a DaemonSet resource
  • metadata.name: daemonset-example → Names the DaemonSet "daemonset-example"
  • metadata.labels.app: daemonset-example → Adds a label for selection

Selector:

  • selector.matchLabels.app: daemonset-example → Defines which pods the DaemonSet manages
  • This matches the labels defined in the pod template

Pod Template:

  • template.metadata.labels.app: daemonset-example → Labels for the pods created by this DaemonSet
  • tolerations → Allows pods to be scheduled on master nodes
  • - key: node-role.kubernetes.io/master → Tolerates the master node taint
  • effect: NoSchedule → Specifies the taint effect to tolerate

Container Specification:

  • name: busybox → Names the container "busybox"
  • image: busybox → Uses the lightweight BusyBox image
  • args: ["sleep", "10000"] → Runs the sleep command for 10000 seconds

How DaemonSet Works:

A DaemonSet ensures that all (or some) nodes run a copy of a pod. As nodes are added to the cluster, pods are added to them. As nodes are removed from the cluster, those pods are garbage collected. This is useful for deploying cluster-wide services like log collectors or monitoring agents.