Kubernetes DaemonSets

Essential commands and configuration for DaemonSets

What is a DaemonSet?

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.

Node Coverage

Ensures all Nodes (or a subset) run an instance of a Pod

Controllers

Scheduled by the scheduler controller and run by the daemon controller

Auto Scaling

As nodes are added to the cluster, Pods are automatically added to them

Typical Use Cases

  • Running a cluster storage daemon - like glusterd, ceph on each node
  • Running a logs collection daemon on every node - like fluentd or logstash
  • Running a node monitoring daemon on every node - like Prometheus Node Exporter

kubectl Commands for DaemonSets

Create a DaemonSet

kubectl apply -f [definition.yaml]

List DaemonSets

kubectl get ds

Get Detailed Info

kubectl describe ds [daemonSetName]

Delete a DaemonSet

kubectl delete -f [definition.yaml]
kubectl delete ds [daemonSetName]

Two ways to delete: using the YAML file or the DaemonSet name

DaemonSet YAML Configuration

Here's an example DaemonSet configuration that avoids scheduling on the master node:

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"

Note: The toleration prevents scheduling on the master node. Without this, the DaemonSet would run on all nodes including the master.

Key Configuration Elements:

  • apiVersion: apps/v1 (for current Kubernetes versions)
  • selector.matchLabels: Must match the pod template's labels
  • tolerations: Controls which nodes the pods can be scheduled on
  • containers: Defines the container(s) to run on each node

Additional Tips

Node Selectors

Use nodeSelectors to run DaemonSet pods only on nodes with specific labels:

spec:
  template:
    spec:
      nodeSelector:
        disktype: ssd

Update Strategies

DaemonSets support two update strategies:

  • OnDelete: Pods are updated when they are manually deleted
  • RollingUpdate: Pods are updated in a rolling fashion (default)