When to Use DaemonSet in Kubernetes: Key Use Cases Explained
DaemonSet in Kubernetes when you need to run a copy of a pod on every node or a specific group of nodes. This is useful for tasks like monitoring, logging, or network management that must run on all nodes consistently.How It Works
A DaemonSet ensures that a pod runs on all or some nodes in a Kubernetes cluster. Think of it like a delivery service that makes sure every house in a neighborhood gets a package. When a new node joins the cluster, the DaemonSet automatically adds a pod to that node, and when a node leaves, the pod is removed.
This is different from regular pods that run only where the scheduler decides. DaemonSets are perfect for running background tasks that need to be present everywhere, like monitoring tools or log collectors. They keep the cluster healthy by making sure these essential pods are always running on the right nodes.
Example
This example creates a DaemonSet that runs a simple logging agent on every node.
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: simple-logger
labels:
app: logger
spec:
selector:
matchLabels:
app: logger
template:
metadata:
labels:
app: logger
spec:
containers:
- name: logger
image: busybox
command: ["/bin/sh", "-c", "while true; do echo Logging from $(hostname); sleep 30; done"]When to Use
Use a DaemonSet when you need a pod to run on all or specific nodes for consistent background tasks. Common real-world uses include:
- Monitoring: Running tools like Prometheus node exporters to collect metrics from every node.
- Logging: Deploying log collectors such as Fluentd or Logstash on all nodes to gather logs.
- Networking: Running network proxies or agents like Calico or Weave for network management.
- Security: Running security agents or scanners on every node.
DaemonSets are not for workloads that need scaling or load balancing but for node-level services that must be present everywhere.
Key Points
- A
DaemonSetruns one pod copy per node automatically. - It ensures essential services run on all or selected nodes.
- Pods are added or removed as nodes join or leave the cluster.
- Ideal for monitoring, logging, networking, and security agents.
- Not meant for user applications that require scaling or load balancing.