Mastering Kubernetes DaemonSets: Deploy Pods Across Your Cluster
Tags: kubernetes, coding, programming, tutorial
Published: 2024-07-07 03:24:21


## Introduction

![MindMap](https://internal-api-drive-stream.feishu.cn/space/api/box/stream/download/authcode/?code=M2U5OWRlNDE0ZThiNWRlNWQxMjBhODE2NmUzMDM0MTlfMzQyN2ZjNjZhOThlMGQ0MGViNTg2ZGE0NTY2Nzk4YjBfSUQ6NzM4ODcyOTQ5Njk5MzYxMTc3OF8xNzIwMzIyNjU5OjE3MjA0MDkwNTlfVjM)

This article covers the following tech skills:

![Skills Graph](https://pub-a9174e0db46b4ca9bcddfa593141f230.r2.dev/kubernetes-running-pod-with-daemonsets-8454.jpg)

In Kubernetes, a DaemonSet is a type of controller that ensures a copy of a pod is running on every node in the cluster. This lab will guide you through the process of creating a DaemonSet to run replicas of a pod on every node in the cluster.


## Create a Pod

Create a simple pod that will be used as the template for the replicas. Create a file called `/home/labex/project/myapp-pod.yaml` with the following contents:

```yaml
apiVersion: v1
kind: Pod
metadata:
  name: myapp-pod
spec:
  containers:
    - name: myapp-container
      image: nginx
      ports:
        - containerPort: 80
```

Create the pod using the following command:

```shell
kubectl apply -f /home/labex/project/myapp-pod.yaml
```


## Create a Daemonset

Create a DaemonSet to run replicas of the `myapp-pod` on every node in the cluster. Create a file called `/home/labex/project/myapp-daemonset.yaml` with the following contents:

```yaml
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: myapp-daemonset
spec:
  selector:
    matchLabels:
      app: myapp
  template:
    metadata:
      labels:
        app: myapp
    spec:
      containers:
        - name: myapp-container
          image: nginx
          ports:
            - containerPort: 80
```

This DaemonSet uses the `myapp-pod` as the template for the replicas and sets the `matchLabels` selector to `app: myapp` to ensure that the replicas are created on every node.

Create the DaemonSet using the following command:

```shell
kubectl apply -f /home/labex/project/myapp-daemonset.yaml
```


## Verify the Daemonset

Verify that the DaemonSet has been created and that replicas of the `myapp-pod` are running on every node. Use the following command to list the nodes in the cluster:

```shell
kubectl get nodes
```

Use the following command to list the pods created by the DaemonSet:

```shell
kubectl get pods -l app=myapp
```

You should see one pod for each node in the cluster.


## Update the Daemonset

Update the DaemonSet to change the image used by the `myapp-container`. Create a file called `/home/labex/project/myapp-daemonsett-update.yaml` with the following contents:

```yaml
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: myapp-daemonset
spec:
  selector:
    matchLabels:
      app: myapp
  template:
    metadata:
      labels:
        app: myapp
    spec:
      containers:
        - name: myapp-container
          image: busybox
          command: ["sleep", "3600"]
```

This updated DaemonSet changes the image used by the `myapp-container` to `busybox` and sets the command to `sleep 3600`.

Update the DaemonSet using the following command:

```shell
kubectl apply -f /home/labex/project/myapp-daemonset-update.yaml
```

Verify that the DaemonSet has been updated and that replicas of the `myapp-pod` are running with the new image. Use the following command to list the pods created by the DaemonSet:

```shell
kubectl get pods -l app=myapp
```

You should see new pods created with the updated image.


## Summary

In this lab, you learned how to use a DaemonSet in Kubernetes to run replicas of a pod on every node in the cluster.

---

## Want to learn more?

- 🚀 Practice [Running Pod with Daemonsets](https://labex.io/tutorials/kubernetes-running-pod-with-daemonsets-8454)
- 🌳 Learn the latest [Kubernetes Skill Trees](https://labex.io/skilltrees/kubernetes)
- 📖 Read More [Kubernetes Tutorials](https://labex.io/tutorials/category/kubernetes)

Join our [Discord](https://discord.gg/J6k3u69nU6) or tweet us [@WeAreLabEx](https://twitter.com/WeAreLabEx) ! 😄