StatefulSet
StatefulSet manages stateful applications. Unlike Deployments, pods get stable network identities and persistent storage that follow them across rescheduling.
StatefulSet = assigned seating at a wedding. Each guest (pod) has a name card (app-0, app-1), a personal locker (PVC), and a reserved parking spot (stable DNS). Deployment guests are interchangeable — sit anywhere. StatefulSet guests are VIPs with permanent reservations.
Stable features: (1) Ordered pod names (app-0, app-1, app-2), (2) Stable DNS hostname per pod via headless service (app-0.svc.namespace.svc.cluster.local), (3) Dedicated PVC per pod that persists even when the pod is deleted. Ordered deployment: pods start sequentially (0, then 1, then 2) and terminate in reverse. Required: a headless service (clusterIP: None) for DNS. Use for: databases (MySQL, MongoDB), distributed systems (Kafka, ZooKeeper, etcd), any app needing stable identity.
VolumeClaimTemplates in the StatefulSet spec create a PVC per pod — the PVC is NOT deleted when the pod is deleted or the StatefulSet is scaled down. You must manually delete PVCs. This prevents accidental data loss. Pod Management Policy: OrderedReady (default, sequential) vs Parallel (all at once, for stateless-like stateful apps). Update strategy: RollingUpdate with partition N only updates pods with index >= N — useful for canary upgrades of stateful apps. Headless service DNS: each pod gets an A record. The StatefulSet's own service (if clusterIP != None) gets the normal VIP.
StatefulSets are for apps that need persistent identity — databases, message queues. Each pod gets a sticky name (app-0, app-1), a stable DNS hostname, and a dedicated PVC that survives pod restarts. This is how Kafka knows which broker is which across restarts. The key difference from Deployment: pods are NOT interchangeable. app-0 is always app-0 with its own data. PVCs are NOT auto-deleted when you scale down — you must clean them up manually.
Deleting a StatefulSet does NOT delete its PVCs — data is retained deliberately. You'll accumulate orphaned PVCs with their storage costs if you don't clean them up after deleting a StatefulSet.