DaemonSet in Kubernetes: What It Is and How It Works
DaemonSet in Kubernetes ensures that a copy of a specific Pod runs on all or selected nodes in a cluster. It automatically adds the Pod to new nodes when they join and removes it when nodes leave.How It Works
A DaemonSet works like a manager who makes sure every worker in a factory has the same tool. In Kubernetes, this means it runs one copy of a Pod on each node you want. When a new node joins the cluster, the DaemonSet automatically adds the Pod there, so no node is left without it.
Think of it as a delivery service that guarantees every house on a street gets a package. If a house is built later, the service delivers the package there too. If a house is removed, the delivery stops. This keeps your system consistent and ready.
Example
This example creates a DaemonSet that runs a simple nginx web server on every node in the cluster.
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: nginx-daemonset
labels:
app: nginx
spec:
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.23.3
ports:
- containerPort: 80
When to Use
Use a DaemonSet when you need a Pod to run on all or specific nodes for tasks like monitoring, logging, or networking. For example, you might run a log collector on every node to gather system logs or a network agent to manage traffic.
It is also useful for hardware-specific tasks, like running a Pod only on nodes with GPUs or special storage. This ensures consistent service and management across your cluster.
Key Points
- A
DaemonSetruns one Pod copy per selected node automatically. - It updates Pods on new nodes and removes them from deleted nodes.
- Commonly used for system services like monitoring and logging.
- You can limit which nodes run the Pod using labels and selectors.