Explain the difference between a Deployment and a StatefulSet.
Both manage Pods, but they make different guarantees.
Deployment:
- Manages stateless, interchangeable replicas identified by a random suffix.
- Supports rolling updates and rollbacks, and Pods are created and deleted in any order.
- Uses a ReplicaSet under the hood.
StatefulSet:
- Manages stateful workloads with stable identity. Pods get ordinal names such as web-0 and web-1, stable network identities through a headless Service, and stable PersistentVolumeClaims.
- Pods are created, scaled, and deleted in order, which matters for clustered databases.
apiVersion: apps/v1
kind: StatefulSet
metadata: {name: db}
spec:
serviceName: db
replicas: 3
template: {spec: {containers: [{name: db, image: postgres:16}]}}
Use Deployments for stateless web apps and StatefulSets for databases, queues, and anything needing stable storage or ordering.