-
Notifications
You must be signed in to change notification settings - Fork 0
EN_CS_StatefulSet
somaz edited this page Mar 30, 2026
·
1 revision
Question: Explain the difference between StatefulSet and Stateless applications in Kubernetes. Cover deployment methods, scaling strategies, Persistent Volumes, Headless Services, and Pod Identity.
| Term | Description |
|---|---|
| StatefulSet | K8s object for managing stateful applications |
| Stateless Application | App with no internal state; each request handled independently |
| Headless Service | Service with clusterIP: None — returns Pod IPs directly |
| Ordinal Index | Ordered number assigned to each StatefulSet Pod (pod-0, pod-1, ...) |
| PVC | PersistentVolumeClaim — Pod's request for a PersistentVolume |
| volumeClaimTemplates | Automatically creates a PVC per Pod in a StatefulSet |
Stateless (Deployment): Stateful (StatefulSet):
┌──────┐ ┌──────┐ ┌──────┐ ┌──────────┐ ┌──────────┐ ┌──────────┐
│Pod A │ │Pod B │ │Pod C │ │ mysql-0 │ │ mysql-1 │ │ mysql-2 │
│ │ │ │ │ │ │ (Master) │ │ (Slave) │ │ (Slave) │
└──────┘ └──────┘ └──────┘ │ PV-0 │ │ PV-1 │ │ PV-2 │
↓ └──────────┘ └──────────┘ └──────────┘
Shared External DB └────── Replication ──────┘
| Aspect | Stateless (Deployment) | Stateful (StatefulSet) |
|---|---|---|
| Pod Name | Random (nginx-abc123) | Ordered (mysql-0, mysql-1) |
| Creation Order | Simultaneous | Sequential (0 → 1 → 2) |
| Deletion Order | Simultaneous | Reverse (2 → 1 → 0) |
| Network Identity | Unstable (Pod IP changes) | Stable (DNS preserved) |
| Storage | Shared or none | Dedicated PVC per Pod |
| Service Type | ClusterIP / LoadBalancer | Headless + ClusterIP |
| Use Cases | Web servers, APIs | Databases, messaging, distributed storage |
Setting clusterIP: None returns Pod IPs directly instead of a VIP.
apiVersion: v1
kind: Service
metadata:
name: mysql-headless
spec:
clusterIP: None
selector:
app: mysql
ports:
- port: 3306DNS lookup result:
mysql-headless → 10.244.1.10 (mysql-0)
→ 10.244.2.20 (mysql-1)
→ 10.244.3.30 (mysql-2)
Per-Pod DNS (stable across restarts):
mysql-0.mysql-headless.default.svc.cluster.local → 10.244.1.10
- Deleting a StatefulSet does NOT delete PVCs — data is preserved
- Pod recreated → reattaches to existing PVC
- Manual cleanup required:
kubectl delete pvc mysql-data-mysql-0
| Strategy | Behavior |
|---|---|
| RollingUpdate (default) | Sequential update in reverse order (2 → 1 → 0) |
| OnDelete | No auto-update; new version applied only when Pod is manually deleted |
| Parallel | All Pods created/deleted simultaneously (when order doesn't matter) |