DaemonSets: Running Background Tasks on Every Node

In Kubernetes, certain workloads need to run on every node in the cluster. Examples include monitoring agents, log collectors, or networking components. To achieve this, Kubernetes provides DaemonSets. A DaemonSet ensures that a copy of a Pod runs on all (or selected) nodes, making it ideal for background tasks and cluster-wide services.

What is a DaemonSet?

A DaemonSet is a Kubernetes controller that automatically deploys Pods across all nodes. When new nodes are added, DaemonSets ensure Pods are scheduled there. When nodes are removed, the Pods are cleaned up.

Key Features

  • Cluster-wide Deployment: Ensures Pods run on every node.
  • Automatic Scaling: New nodes automatically get the DaemonSet Pods.
  • Selective Scheduling: Can target specific nodes using labels and selectors.
  • Background Tasks: Ideal for monitoring, logging, and networking agents.

YAML Example: DaemonSet

apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: log-collector
spec:
  selector:
    matchLabels:
      app: log-collector
  template:
    metadata:
      labels:
        app: log-collector
    spec:
      containers:
      - name: log-collector
        image: fluentd:latest
        resources:
          requests:
            cpu: "100m"
            memory: "200Mi"

Explanation: This DaemonSet deploys a Fluentd log collector Pod on every node to gather logs.

Flowchart: DaemonSet Workflow


   DaemonSet created ---> Scheduler ensures Pod on each node
          |
          v
   New node added ---> Pod automatically scheduled
          |
          v
   Node removed ---> Pod cleaned up
  

Real-Time Example

In a production cluster:

  • Monitoring: A DaemonSet runs Prometheus Node Exporter on every node to collect metrics.
  • Logging: Fluentd or Filebeat DaemonSets gather logs from all nodes.
  • Networking: CNI plugins like Calico or Weave Net are deployed as DaemonSets to manage networking.

Common Mistakes

  • Using Deployments instead of DaemonSets for node-wide tasks.
  • Not setting resource requests/limits, causing resource contention.
  • Ignoring node selectors, leading to Pods running on undesired nodes.
  • Forgetting cleanup policies when nodes are removed.

Interview Notes

Q1: What is the purpose of a DaemonSet?

Answer: A DaemonSet ensures Pods run on every node, making it ideal for background tasks like logging and monitoring.

Q2: How does a DaemonSet differ from a Deployment?

Answer: A Deployment manages replicas across the cluster, while a DaemonSet ensures one Pod per node.

Q3: What happens when a new node is added?

Answer: The DaemonSet automatically schedules a Pod on the new node.

Q4: Example Interview Task

apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: node-monitor
spec:
  selector:
    matchLabels:
      app: node-monitor
  template:
    metadata:
      labels:
        app: node-monitor
    spec:
      containers:
      - name: monitor
        image: prom/node-exporter

Explanation: This DaemonSet runs Prometheus Node Exporter on every node for monitoring.

Advanced Notes

  • Node Selectors: Restrict DaemonSet Pods to specific nodes.
  • Taints and Tolerations: Allow DaemonSets to run on tainted nodes like masters.
  • Update Strategy: Configure rolling updates for DaemonSets.
  • Best Practices: Use DaemonSets for cluster-wide agents, monitor resource usage, and combine with RBAC for security.

Summary

DaemonSets are essential for running background tasks on every node in Kubernetes. They ensure Pods are deployed consistently across the cluster, making them ideal for monitoring, logging, and networking. By mastering DaemonSets, developers can build robust infrastructure services and confidently answer interview questions on Kubernetes resource management.