Skip to content

Commit bac18a5

Browse files
authored
Merge pull request #48655 from abhilasha2418/daemonSet
Create new task for building basic daemon set
2 parents 620a5db + b2a315f commit bac18a5

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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

0 commit comments

Comments
 (0)