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 | apiVersion: apps/v1 |
Create / Update / Delete
Before this, I had already set up a Kubernetes cluster with 3 master nodes and 3 worker 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:

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 | metadata: |
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:

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:

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:

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).