Skip to main content

DaemonSets & StatefulSets

Specialized Controllers

Picture a city: some services must run in every neighborhood - like garbage collection or street lighting. Others require unique identities - like postal addresses or bank accounts. Kubernetes faced the same dual challenge. Some workloads needed to run on every node, while others demanded stable identities and persistence.

To solve this, Kubernetes introduced DaemonSets and StatefulSets - specialized controllers designed for unique workload patterns.


DaemonSets – One Pod Per Node

  • Definition: A DaemonSet ensures that a copy of a pod runs on every node in the cluster.
  • Use Cases:
    • Log collection agents (e.g., Fluentd, Filebeat).
    • Monitoring tools (e.g., Prometheus Node Exporter).
    • Networking components (e.g., CNI plugins).
  • Behavior: When new nodes join the cluster, DaemonSets automatically schedule pods on them.
  • Analogy: DaemonSets are like streetlights - every street (node) must have one, ensuring visibility across the city.

StatefulSets – Persistence

  • Definition: A StatefulSet manages pods that require stable identities and persistent storage.
  • Use Cases:
    • Databases (e.g., MySQL, MongoDB).
    • Message queues (e.g., Kafka, RabbitMQ).
    • Applications needing ordered deployment and scaling.
  • Behavior: Pods in a StatefulSet have unique names (e.g., db-0, db-1) and retain their identity even after restarts.
  • Analogy: StatefulSets are like apartments with fixed addresses - each resident (pod) has a unique identity and mailbox, even if they move in and out.

Global Context

  • Enterprises: Use DaemonSets for cluster‑wide monitoring and networking, and StatefulSets for mission‑critical databases.
  • Cloud Providers: Managed Kubernetes services integrate StatefulSets with persistent volumes for reliable storage.
  • Community: These controllers are taught worldwide as essential patterns for specialized workloads.

Hands‑On Exercise

  1. Reflect: How do DaemonSets guarantee cluster‑wide coverage, while StatefulSets guarantee stability and persistence?

Create a StatefulSet for a database:

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: db
spec:
  serviceName: "db-service"
  replicas: 3
  selector:
    matchLabels:
      app: db
  template:
    metadata:
      labels:
        app: db
    spec:
      containers:
      - name: mysql
        image: mysql:5.7
        volumeMounts:
        - name: data
          mountPath: /var/lib/mysql
  volumeClaimTemplates:
  - metadata:
      name: data
    spec:
      accessModes: [ "ReadWriteOnce" ]
      resources:
        requests:
          storage: 1Gi
kubectl apply -f statefulset.yaml
kubectl get pods

Create a DaemonSet for monitoring:

apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: node-monitor
spec:
  selector:
    matchLabels:
      app: monitor
  template:
    metadata:
      labels:
        app: monitor
    spec:
      containers:
      - name: monitor
        image: prom/node-exporter
kubectl apply -f daemonset.yaml
kubectl get pods -o wide

The Hacker’s Notebook

  • DaemonSets are guardians - ensuring every node runs critical services.
  • StatefulSets are anchors - providing stability and persistence for stateful apps.
  • Lesson for engineers: Choose the right controller for the workload - stateless vs. stateful, node‑wide vs. unique identity.
  • Hacker’s mindset: Treat DaemonSets and StatefulSets as specialized tools. Master them, and you can run everything from monitoring agents to global databases with confidence.

Tips, Tricks, Roadmaps, Resources, Networking, Motivation, Guidance, and Cool Stuff ♥

Updated on Dec 29, 2025