DaemonSets in Kubernetes – The Silent Guardians of Your Cluster πŸ›‘οΈ



This content originally appeared on DEV Community and was authored by Laxman Patel

Kubernetes is a bustling ecosystem of pods, services, and deployments. But what about those tasks that need to run on every node in your cluster?

Enter DaemonSetsβ€”the unsung heroes of Kubernetes. In this article, we’ll explore what DaemonSets are, why they’re essential, and how to use them effectively.

οΏ½ The Problem: Node-Level Tasks in Kubernetes

Imagine you need to:

  • Run a logging agent on every node.

  • Deploy a monitoring tool like Prometheus Node Exporter.

  • Ensure a security agent is always present on all nodes.

Using a regular Deployment or Pod won’t cut it because:

  • You can’t guarantee a pod will run on every node.

  • Scaling manually is tedious and error-prone.

  • New nodes won’t automatically get the required pods.

This is where DaemonSets come to the rescue.

🛠 What Are DaemonSets?

A DaemonSet is a Kubernetes controller that ensures a copy of a pod runs on every node (or a subset of nodes) in your cluster. If a new node is added, the DaemonSet automatically schedules a pod on it. If a node is removed, the pod is garbage-collected.

Key features:

  • Node-Level Coverage: Runs a pod on every node (or specific nodes using labels).

  • Automatic Scaling: Scales with your clusterβ€”no manual intervention needed.

  • Self-Healing: If a pod is deleted, the DaemonSet recreates it.

  • Resource Efficiency: Ensures only one pod runs per node (unless overridden).

🎯 Why Are DaemonSets Needed?

  1. Node-Specific Tasks: Perfect for logging, monitoring, and security agents.

  2. Cluster-Wide Consistency: Ensures every node has the required software.

  3. Automatic Scaling: Handles node additions and removals seamlessly.

  4. Resource Optimization: Avoids over-provisioning by running only one pod per node.

🛠 How to Use DaemonSets

Let’s create a DaemonSet to deploy a logging agent on every node in your cluster.

apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: logging-agent
  namespace: kube-system
  labels:
    app: logging-agent
spec:
  selector:
    matchLabels:
      name: logging-agent
  template:
    metadata:
      labels:
        name: logging-agent
    spec:
      containers:
      - name: logging-agent
        image: fluent/fluentd:latest
        resources:
          limits:
            memory: "200Mi"
            cpu: "100m"
          requests:
            memory: "100Mi"
            cpu: "50m"
      tolerations:
      - key: node-role.kubernetes.io/master
        effect: NoSchedule
  • selector: Matches the pods managed by this DaemonSet.

  • template: Defines the pod specification.

  • tolerations: Allows the DaemonSet to run on master nodes (optional).

Apply the DaemonSet:

kubectl apply -f daemonset.yaml

Now, a logging-agent pod will run on every node in your cluster. If you add or remove nodes, the DaemonSet will handle it automatically.

🧩 Advanced Use Cases

  1. Node-Specific Workloads: Use node labels to run DaemonSets on specific nodes.

    Example: Run a GPU monitoring tool only on GPU-enabled nodes.

  2. Custom Taints and Tolerations: Control which nodes the DaemonSet can run on.

  3. Rolling Updates: Update DaemonSet pods in a controlled manner using updateStrategy.

🎯 Key Takeaways

  • DaemonSets ensure a pod runs on every node in your cluster.

  • They’re perfect for node-specific tasks like logging, monitoring, and security.

  • They scale automatically with your cluster and handle node changes seamlessly.

  • Use them to maintain consistency and efficiency across your nodes.

So, the next time you need to run a pod on every node, think DaemonSetsβ€”your silent guardians in the Kubernetes world. 🛡


This content originally appeared on DEV Community and was authored by Laxman Patel