K8s by Example: StatefulSets
| StatefulSets provide stable Pod identities and persistent storage. Unlike Deployments, Pods are not interchangeable and keep the same name, DNS, and storage across restarts. Use for: databases, caches, Kafka. |
| statefulset.yaml | |
| StatefulSet requires | |
| headless-service.yaml | |
| Headless Service ( | |
| statefulset-storage.yaml | |
| Each Pod gets a persistent volume via | |
| statefulset-policy.yaml | |
| Pod management policies control startup order. | |
| statefulset-update.yaml | |
| Update strategies: | |
| statefulset-retention.yaml | |
| PVCs persist after StatefulSet deletion by default. | |
| terminal | |
| Scale up creates postgres-3, postgres-4 in order. Scale down removes postgres-4, postgres-3 in reverse and waits for each to terminate. PVCs are NOT deleted on scale down, so data-postgres-3 and data-postgres-4 still exist. | |
| terminal | |
| Debug StatefulSets by checking Pod ordering, PVC binding, and headless Service DNS. Common issues: PVC stuck pending (no storage), Pod stuck initializing (previous Pod not ready). | |