QA
Intermediate LevelDocker & Deployment

When should you use a StatefulSet instead of a Deployment in Kubernetes?

❓ Interview Prompt: Compare StatefulSets vs Deployments regarding stable network identity, ordered startup/shutdown, and sticky persistent storage for databases.

Deployments (Stateless Workloads):

β€’
Pods are interchangeable β€” any Pod can serve any request.
β€’
Pod names are random: api-7d9f8b-xyz.
β€’
Pods can be created/deleted in any order.
β€’
Persistent storage is not guaranteed per-pod.
β€’
Best for: API servers, web frontends, worker pools.

StatefulSets (Stateful Workloads):

StatefulSets provide three guarantees that Deployments cannot:

1
Stable Pod Identity: Pods have ordinal names (db-0, db-1, db-2) that persist across rescheduling. DNS hostnames are also stable: db-0.db-svc.namespace.svc.cluster.local.
2
Ordered Deployment/Scaling: Pods are created/started in order (0 β†’ 1 β†’ 2) and deleted in reverse order (2 β†’ 1 β†’ 0). Databases need the primary to start before replicas.
3
Sticky Storage (volumeClaimTemplates): Each Pod gets its own dedicated PVC that is NOT deleted when the Pod is rescheduled. db-0 always gets the same pgdata-db-0 PVC.

Use StatefulSets For:

β€’
Databases (PostgreSQL, MySQL, MongoDB, Cassandra).
β€’
Message brokers (Kafka, RabbitMQ).
β€’
Distributed systems requiring stable cluster membership (Elasticsearch, ZooKeeper).

Code Implementation Example

yaml
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: db
spec:
  serviceName: db-svc     # headless service for DNS: db-0.db-svc
  replicas: 3
  selector:
    matchLabels:
      app: db
  template:
    metadata:
      labels:
        app: db
    spec:
      containers:
        - name: postgres
          image: postgres:16-alpine
          env:
            - name: POSTGRES_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: pg-secret
                  key: password
          volumeMounts:
            - mountPath: /var/lib/postgresql/data
              name: pgdata
  
  # Each pod gets its OWN PVC (db-0β†’pgdata-db-0, db-1β†’pgdata-db-1)
  volumeClaimTemplates:
    - metadata:
        name: pgdata
      spec:
        accessModes: ["ReadWriteOnce"]
        storageClassName: fast-ssd
        resources:
          requests:
            storage: 50Gi
πŸ’‘StatefulSet: ordinal pod names (db-0/1/2), ordered startup, and per-pod sticky PVCs that survive pod rescheduling.
🎯 Interview Key Takeaway

Use StatefulSets for databases/brokers requiring stable pod identity (db-0/1/2), ordered startup, and sticky per-pod PVCs. Use Deployments for stateless services.

#Kubernetes#StatefulSet#Deployment#Databases#Stable Identity#Intermediate
Back to all Docker & Deployment Questions→

More Docker & Deployment Interview Questions

Beginner LevelDocker & Deployment

How do Docker Multi-Stage Builds minimize image size and improve security?

Beginner LevelDocker & Deployment

What is the difference between a Docker Container and a Virtual Machine (VM)?

Beginner LevelDocker & Deployment

What is the difference between a Docker Image and a Docker Container?