Building a Basic DaemonSet

This page demonstrates how to build a basic DaemonSet that runs a Pod on every node in a Kubernetes cluster. It covers a simple use case of mounting a file from the host, logging its contents using an init container, and utilizing a pause container.

Before you begin

You need to have a Kubernetes cluster, and the kubectl command-line tool must be configured to communicate with your cluster. It is recommended to run this tutorial on a cluster with at least two nodes that are not acting as control plane hosts. If you do not already have a cluster, you can create one by using minikube or you can use one of these Kubernetes playgrounds:

A Kubernetes cluster with at least two nodes (one control plane node and one worker node) to demonstrate the behavior of DaemonSets.

Define the DaemonSet

In this task, a basic DaemonSet is created which ensures that the copy of a Pod is scheduled on every node. The Pod will use an init container to read and log the contents of /etc/machine-id from the host, while the main container will be a pause container, which keeps the Pod running.

  1. application/basic-daemonset.yaml
  1. apiVersion: apps/v1
  2. kind: DaemonSet
  3. metadata:
  4. name: example-daemonset
  5. spec:
  6. selector:
  7. matchLabels:
  8. app.kubernetes.io/name: example
  9. template:
  10. metadata:
  11. labels:
  12. app.kubernetes.io/name: example
  13. spec:
  14. containers:
  15. - name: pause
  16. image: registry.k8s.io/pause
  17. initContainers:
  18. - name: log-machine-id
  19. image: busybox:1.37
  20. command: ['sh', '-c', 'cat /etc/machine-id > /var/log/machine-id.log']
  21. volumeMounts:
  22. - name: machine-id
  23. mountPath: /etc/machine-id
  24. readOnly: true
  25. - name: log-dir
  26. mountPath: /var/log
  27. volumes:
  28. - name: machine-id
  29. hostPath:
  30. path: /etc/machine-id
  31. type: File
  32. - name: log-dir
  33. hostPath:
  34. path: /var/log
  1. Create a DaemonSet based on the (YAML) manifest:

    1. kubectl apply -f https://k8s.io/examples/application/basic-daemonset.yaml
  2. Once applied, you can verify that the DaemonSet is running a Pod on every node in the cluster:

    1. kubectl get pods -o wide

    The output will list one Pod per node, similar to:

    1. NAME READY STATUS RESTARTS AGE IP NODE
    2. example-daemonset-xxxxx 1/1 Running 0 5m x.x.x.x node-1
    3. example-daemonset-yyyyy 1/1 Running 0 5m x.x.x.x node-2
  3. You can inspect the contents of the logged /etc/machine-id file by checking the log directory mounted from the host:

    1. kubectl exec <pod-name> -- cat /var/log/machine-id.log

    Where <pod-name> is the name of one of your Pods.

Cleaning up

To delete the DaemonSet, run this command:

  1. kubectl delete --cascade=foreground --ignore-not-found --now daemonsets/example-daemonset

This simple DaemonSet example introduces key components like init containers and host path volumes, which can be expanded upon for more advanced use cases. For more details refer to DaemonSet.

What’s next