|
| 1 | +--- |
| 2 | +title: Building a Basic DaemonSet |
| 3 | +content_type: task |
| 4 | +weight: 5 |
| 5 | +--- |
| 6 | +<!-- overview --> |
| 7 | + |
| 8 | +This page demonstrates how to build a basic {{< glossary_tooltip text="DaemonSet" term_id="daemonset" >}} that runs a Pod on every node in a Kubernetes cluster. |
| 9 | +It covers a simple use case of mounting a file from the host, logging its contents using |
| 10 | +an [init container](/docs/concepts/workloads/pods/init-containers/), and utilizing a pause container. |
| 11 | + |
| 12 | +## {{% heading "prerequisites" %}} |
| 13 | + |
| 14 | +{{< include "task-tutorial-prereqs.md" >}} |
| 15 | + |
| 16 | +A Kubernetes cluster with at least two nodes (one control plane node and one worker node) to demonstrate the behavior of DaemonSets. |
| 17 | + |
| 18 | +## Define the DaemonSet |
| 19 | + |
| 20 | +In this task, a basic DaemonSet is created which ensures that the copy of a Pod is scheduled on every node. |
| 21 | +The Pod will use an init container to read and log the contents of `/etc/machine-id` from the host, |
| 22 | +while the main container will be a `pause` container, which keeps the Pod running. |
| 23 | + |
| 24 | +{{% code_sample file="application/basic-daemonset.yaml" %}} |
| 25 | + |
| 26 | +1. Create a DaemonSet based on the (YAML) manifest: |
| 27 | + |
| 28 | + ```shell |
| 29 | + kubectl apply -f https://k8s.io/examples/application/basic-daemonset.yaml |
| 30 | + ``` |
| 31 | + |
| 32 | +1. Once applied, you can verify that the DaemonSet is running a Pod on every node in the cluster: |
| 33 | + |
| 34 | + ```shell |
| 35 | + kubectl get pods -o wide |
| 36 | + ``` |
| 37 | + |
| 38 | + The output will list one Pod per node, similar to: |
| 39 | + |
| 40 | + ``` |
| 41 | + NAME READY STATUS RESTARTS AGE IP NODE |
| 42 | + example-daemonset-xxxxx 1/1 Running 0 5m x.x.x.x node-1 |
| 43 | + example-daemonset-yyyyy 1/1 Running 0 5m x.x.x.x node-2 |
| 44 | + ``` |
| 45 | + |
| 46 | +1. You can inspect the contents of the logged `/etc/machine-id` file by checking the log directory mounted from the host: |
| 47 | + |
| 48 | + ```shell |
| 49 | + kubectl exec <pod-name> -- cat /var/log/machine-id.log |
| 50 | + ``` |
| 51 | + |
| 52 | + Where `<pod-name>` is the name of one of your Pods. |
| 53 | + |
| 54 | +## {{% heading "cleanup" %}} |
| 55 | + |
| 56 | + ``` |
| 57 | + kubectl delete --cascade=foreground --ignore-not-found --now daemonsets/example-daemonset |
| 58 | + ``` |
| 59 | + |
| 60 | +This simple DaemonSet example introduces key components like init containers and host path volumes, |
| 61 | +which can be expanded upon for more advanced use cases. For more details refer to |
| 62 | +[DaemonSet](/docs/concepts/workloads/controllers/daemonset/). |
| 63 | + |
| 64 | + |
| 65 | + |
0 commit comments