0
0
KubernetesConceptBeginner · 3 min read

DaemonSet in Kubernetes: What It Is and How It Works

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

yaml
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
Output
daemonset.apps/nginx-daemonset created kubectl get pods -o wide NAME READY STATUS RESTARTS AGE NODE nginx-daemonset-abc 1/1 Running 0 1m node1 nginx-daemonset-def 1/1 Running 0 1m node2 nginx-daemonset-ghi 1/1 Running 0 1m node3
🎯

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 DaemonSet runs 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.

Key Takeaways

A DaemonSet ensures a Pod runs on all or selected nodes automatically.
It is ideal for running system-level services like monitoring or logging agents.
DaemonSets add Pods to new nodes and remove them from nodes that leave.
You can control which nodes run the DaemonSet using labels and selectors.