From b2a315ffd0652bbd7e4332741595620e14fa65f0 Mon Sep 17 00:00:00 2001 From: abhilasha2418 Date: Wed, 6 Nov 2024 21:34:01 +0530 Subject: [PATCH] feature: Create new task for building basic daemon set --- .../tasks/manage-daemon/create-daemon-set.md | 65 +++++++++++++++++++ .../examples/application/basic-daemonset.yaml | 34 ++++++++++ 2 files changed, 99 insertions(+) create mode 100644 content/en/docs/tasks/manage-daemon/create-daemon-set.md create mode 100644 content/en/examples/application/basic-daemonset.yaml diff --git a/content/en/docs/tasks/manage-daemon/create-daemon-set.md b/content/en/docs/tasks/manage-daemon/create-daemon-set.md new file mode 100644 index 0000000000000..e859d1d051de7 --- /dev/null +++ b/content/en/docs/tasks/manage-daemon/create-daemon-set.md @@ -0,0 +1,65 @@ +--- +title: Building a Basic DaemonSet +content_type: task +weight: 5 +--- + + +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](/docs/concepts/workloads/pods/init-containers/), 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" %}} + +1. Create a DaemonSet based on the (YAML) manifest: + + ```shell + kubectl apply -f https://k8s.io/examples/application/basic-daemonset.yaml + ``` + +1. Once applied, you can verify that the DaemonSet is running a Pod on every node in the cluster: + + ```shell + kubectl get pods -o wide + ``` + + The 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 + ``` + +1. You can inspect the contents of the logged `/etc/machine-id` file by checking the log directory mounted from the host: + + ```shell + kubectl exec -- cat /var/log/machine-id.log + ``` + + Where `` is the name of one of your Pods. + +## {{% heading "cleanup" %}} + + ``` + 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](/docs/concepts/workloads/controllers/daemonset/). + + + diff --git a/content/en/examples/application/basic-daemonset.yaml b/content/en/examples/application/basic-daemonset.yaml new file mode 100644 index 0000000000000..f343544c29eeb --- /dev/null +++ b/content/en/examples/application/basic-daemonset.yaml @@ -0,0 +1,34 @@ +apiVersion: apps/v1 +kind: DaemonSet +metadata: + name: example-daemonset +spec: + selector: + matchLabels: + app.kubernetes.io/name: example + template: + metadata: + labels: + app.kubernetes.io/name: example + spec: + containers: + - name: pause + image: registry.k8s.io/pause + initContainers: + - name: log-machine-id + image: busybox:1.37 + command: ['sh', '-c', 'cat /etc/machine-id > /var/log/machine-id.log'] + volumeMounts: + - name: machine-id + mountPath: /etc/machine-id + readOnly: true + - name: log-dir + mountPath: /var/log + volumes: + - name: machine-id + hostPath: + path: /etc/machine-id + type: File + - name: log-dir + hostPath: + path: /var/log \ No newline at end of file