BLOG08: Why we need statefulset?
What is a StatefulSet?
A StatefulSet is a Kubernetes workload controller used to manage stateful applications — applications that need stable identity, persistent storage, and ordered deployment.
Stable network identity → Pod names follow a predictable format: pod-name-0, pod-name-1, pod-name-2
Stable storage → Each Pod gets its own PersistentVolume that is not deleted on pod restart.
Ordered deployment & scaling → Pod-0 starts first, then Pod-1, etc.
Ordered updates & termination
When to Use a StatefulSet? (Real Use Cases)
StatefulSet is ideal when each pod must keep its own data or identity.
Real Use Cases
Databases
PostgreSQL with streaming replication
Distributed systems that need stable identity
Kafka brokers (kafka-0, kafka-1…)
Systems that maintain local state
Caches that cannot lose local data
Example: In Kafka, each broker must have a fixed ID and storage volume. A StatefulSet ensures that if kafka-2 is restarted, it still comes back as kafka-2 with its old data.
What is a DaemonSet?
A DaemonSet ensures that one copy of a Pod runs on every node (or on selected nodes) in the cluster.
Schedules exactly 1 pod per node
Automatically adds/removes pods as nodes join/leave
Ideal for node-level agents
When to Use a DaemonSet? (Real Use Cases)
DaemonSets are for workloads that must run on every node.
Real Use Cases
Networking Components
CNI plugins (Calico, Weave, Cilium)
Security Agents
Anti-virus / node scanners
Example: If you use Fluent Bit to collect logs from /var/log on every node, a DaemonSet ensures each node has a collector pod automatically.
StatefulSet vs DaemonSet (Simple Comparison)
Feature
StatefulSet
DaemonSet
Stateful apps, maintain identity
Node-level agent per node
Same name pattern on each node
Usually no persistent storage
Logging, monitoring, networking
Quick, Practical Mnemonic
StatefulSet = Stable Identity DaemonSet = One Pod Per Node
What is “Stable Network Identity”?
It means a Pod gets a permanent hostname and DNS name that does not change, even if:
✔ The pod is deleted ✔ The pod is rescheduled to another node ✔ The cluster restarts
This is critical for apps that need to identify each peer in a cluster.
If you deploy a StatefulSet named mysql with 3 replicas, Kubernetes creates:
Their DNS hostnames will be:
These DNS names never change as long as the StatefulSet exists.
Why Deployment/ReplicaSet CAN’T provide stable identity
A Deployment/ReplicaSet manages pods like cattle:
Pod Names Change
If a pod crashes, Deployment creates a new pod with a different name, for example:
These are random hashes, not predictable.
No Fixed DNS Entry
Each new pod gets a new IP, and because:
Pod names change randomly
You cannot rely on any pod to have a consistent identity.
ReplicaSet purpose
ReplicaSet only ensures N running replicas. It doesn’t care which pod is “pod-0” or “pod-1”.
How StatefulSet Achieves Stable Network Identity
StatefulSet has two mechanisms:
1. Fixed Pod Names (Ordinal Indexing)
Pods are created sequentially:
If pod-1 restarts, it returns as exactly the same name:
2. Stable DNS via Headless Service
StatefulSets require a Headless Service (clusterIP: None).
This creates a DNS entry for each pod:
Even after restarts or node failures, this DNS remains valid.
Why do stateful apps need this?
Example 1: Cassandra
Nodes form a ring: Each node is responsible for a specific token range.
If node names changed, the ring breaks.
Example 2: Kafka
Each broker has a permanent ID:
If Kafka pods kept getting new names (like in a Deployment), consumers and producers would lose connection to the brokers.
Example 3: MongoDB Replica Set
ReplicaSet members include hostnames:
If pod identity changed, the replica config becomes invalid.
Feature
StatefulSet
Deployment/ReplicaSet
Stable, predictable (app-0, app-1)
Per-pod persistent volume
Databases, message queues
One-Line Answer
Stable network identity means pods get fixed names and DNS records. StatefulSet maintains this using ordered creation, fixed naming, and a headless service — something Deployment/ReplicaSet cannot do because they treat pods as interchangeable.