DaemonSet

A DaemonSet ensures that all (or some) nodes run a copy of a Pod. The word “Daemon” in computing refers to a background process.

When a new node is added to the cluster, the DaemonSet creates a new Pod on that node. When a node is removed from the cluster, those Pods are garbage collected. Deleting a DaemonSet will clean up all the Pods it created.

Simple Example

Run an nginx:1.16 Pod on every Node. GitHub link: https://raw.githubusercontent.com/chengqing-su/kubernetes-learning/master/daemon-set/nginx-daemon-set.yaml.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: nginx-daemon-set
spec:
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.16

Create / Update / Delete

Before this, I had already set up a Kubernetes cluster with 3 master nodes and 3 worker nodes.

nodes

Create

Run the command:

1
kubectl apply -f https://raw.githubusercontent.com/chengqing-su/kubernetes-learning/master/daemon-set/nginx-daemon-set.yaml

Result:

create

A DaemonSet manages Pods directly. Shown below is the metadata of nginx-daemon-set-d6c6l. Its metadata.ownerReferences points to the DaemonSet named nginx-daemon-set created above.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
metadata:
annotations:
cni.projectcalico.org/podIP: 10.42.5.218/32
creationTimestamp: "2020-02-25T12:27:10Z"
generateName: nginx-daemon-set-
labels:
app: nginx
controller-revision-hash: 59d5958c9f
pod-template-generation: "1"
name: nginx-daemon-set-d6c6l
namespace: default
ownerReferences:
- apiVersion: apps/v1
blockOwnerDeletion: true
controller: true
kind: DaemonSet
name: nginx-daemon-set
uid: fd40fc4d-c31f-42ca-bdc3-48c13e7348f1
resourceVersion: "11004234"
selfLink: /api/v1/namespaces/default/pods/nginx-daemon-set-d6c6l
uid: 2dde39cb-867c-46a9-b211-22d88e489d9a

Update

Upgrade the nginx container version to 1.17. A new manifest was created at: https://raw.githubusercontent.com/chengqing-su/kubernetes-learning/master/daemon-set/nginx-daemon-set-update.yaml

Run the command:

1
kubectl apply -f https://raw.githubusercontent.com/chengqing-su/kubernetes-learning/master/daemon-set/nginx-daemon-set-update.yaml

Result:

update

After a DaemonSet’s PodTemplate is updated, it automatically deletes the old Pods and then creates new ones based on the updated PodTemplate.

Delete

Delete a Pod from the DaemonSet

Delete the Pod named nginx-daemon-set-cpqqd from the DaemonSet above.

Run the command:

1
kubectl delete pod nginx-daemon-set-cpqqd

Result:

delete-a-pod

Once the Pod is deleted, the DaemonSet will start a new Pod on the same node where the old Pod was running.

Delete the DaemonSet

Run the command:

1
kubectl delete daemonset nginx-daemon-set

Result:

delete

When to Use

Some typical use cases for DaemonSet are:

  • Running a cluster storage daemon on every node, such as glusterd or ceph.

  • Running a log collection daemon on every node, such as fluentd or filebeat.

  • Running a node monitoring daemon on every node (for example, Prometheus Node Exporter, Flowmill, Sysdig Agent, collectd, Dynatrace OneAgent, AppDynamics Agent, Datadog agent, New Relic agent, Ganglia gmond, Instana agent, or Elastic Metricbeat).