StatefulSets: For Databases
- Identity: Assigns a sticky, unique network ID (e.g.,
web-0) to each Pod. - Persistence: Each Pod maintains its own PersistentVolume across restarts.
- Ordering: Starts and stops Pods in a predictable, sequential order.
A StatefulSet manages the deployment and scaling of a set of Pods, providing guarantees about the ordering and uniqueness of these Pods.
Unlike a Deployment, a StatefulSet maintains a sticky identity for each of its Pods. These pods are created from the same spec, but are not interchangeable: each has a persistent identifier that it maintains across any rescheduling.
Use Cases
StatefulSets are valuable for applications that require one or more of the following:
- Stable, unique network identifiers. (e.g.,
mysql-0,mysql-1) - Stable, persistent storage. (e.g., specific PVCs linked to specific Pods)
- Ordered, graceful deployment and scaling.
- Ordered, automated rolling updates.
Examples:
- Databases (PostgreSQL, MySQL, MongoDB)
- Distributed systems (Zookeeper, Kafka, Elasticsearch)
Key Features
1. Stable Network ID
Pods in a StatefulSet get a predictable name: $(statefulset-name)-$(ordinal).
If you define a StatefulSet named web with 3 replicas, you get:
web-0web-1web-2
2. Stable Storage
Kubernetes creates a PersistentVolumeClaim (PVC) for each Pod based on a volumeClaimTemplate. If web-0 dies and is rescheduled on a different node, it re-attaches to the same storage volume it had before.
3. Ordered Deployment
- Scale Up: 0 -> 1 -> 2 (Sequential)
- Scale Down: 2 -> 1 -> 0 (Reverse Sequential)