Skip to content
Lucas Mauro

What is a Kubernetes DaemonSet?

And when to use it?

kubernetes 1 min read

A DaemonSet is a Kubernetes object that ensures a copy of a pod runs on all (or a subset of) nodes in the cluster. Unlike a Deployment, which distributes pods across the cluster without caring which specific nodes they land on, a DaemonSet guarantees one pod per node.

This is particularly useful for node-level tasks: logging agents, monitoring exporters, network plugins, storage daemons, and anything that needs to run on every machine in the cluster.

apiVersion: apps/v1
kind: DaemonSet
metadata:
name: log-collector
spec:
selector:
matchLabels:
app: log-collector
template:
metadata:
labels:
app: log-collector
spec:
containers:
- name: fluentd
image: fluentd:latest
volumeMounts:
- name: varlog
mountPath: /var/log
volumes:
- name: varlog
hostPath:
path: /var/log

Let’s break this down:

  • selector.matchLabels defines which pods this DaemonSet owns;
  • template is the pod template (there is no replicas field because Kubernetes automatically creates one pod per matching node);
  • hostPath mounts the node’s /var/log directory into the container, giving the logging agent access to the node’s log files.

There is no explicit replica count to set. The DaemonSet controller watches the cluster and automatically creates a pod on every node that matches the nodeSelector or affinity rules (or on all nodes if none are specified).

When a new node joins the cluster, the DaemonSet places a pod on it. When a node is removed, the corresponding pod is cleaned up. This means we never have to worry about manually scaling a DaemonSet.

DaemonSets are the right choice for workloads that need to run once per node rather than N times across the cluster. Common examples:

  • Log collection (Fluentd, Filebeat);
  • Monitoring agents (Prometheus Node Exporter, Datadog Agent);
  • Network plugins (Calico, Cilium);
  • Storage daemons (Ceph, GlusterFS).

If we need a pod on every node, a DaemonSet is the answer. If we need a specific number of pods regardless of node count, a deployment is what we want.

Comments