Building a Basic DaemonSet
Kubernetes Docs · نسخهٔ f284a9e7c87f · CC BY 4.0 برای مستندات و مجوزهای همراه
<!-- overview -->
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. 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.
{{% heading "prerequisites" %}}
{{< include "task-tutorial-prereqs.md" >}}
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.
{{% code_sample file="application/basic-daemonset.yaml" %}}
- Create a DaemonSet based on the (YAML) manifest:
kubectl apply -f https://k8s.io/examples/application/basic-daemonset.yaml- Once applied, you can verify that the DaemonSet is running a Pod on every node in the cluster:
kubectl get pods -o wideThe output will list one Pod per node, similar to:
NAME READY STATUS RESTARTS AGE IP NODE
example-daemonset-xxxxx 1/1 Running 0 5m x.x.x.x node-1
example-daemonset-yyyyy 1/1 Running 0 5m x.x.x.x node-2- You can inspect the contents of the logged
/etc/machine-idfile by checking
the log directory mounted from the host:
kubectl exec <pod-name> -- cat /var/log/machine-id.logWhere <pod-name> is the name of one of your Pods.
{{% heading "cleanup" %}}
To delete the DaemonSet, run this command:
kubectl delete --cascade=foreground --ignore-not-found --now daemonsets/example-daemonsetThis 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.
{{% heading "whatsnext" %}}
- See Performing a rolling update on a DaemonSet
- See Creating a DaemonSet to adopt existing DaemonSet pods