Kubernetes · Orchestration under pressure
When do you need a StatefulSet?
Short answer
StatefulSets give pods stable ordinal names, stable DNS and per-pod persistent storage. Use them for databases, queues and clustered systems that need identity across restarts.
A StatefulSet creates pods named app-0, app-1 and so on, with a stable network identity through a headless Service. Each pod can claim its own PersistentVolume through volumeClaimTemplates, so storage follows the pod identity rather than the node.
- Creation and deletion are ordered by default; PodManagementPolicy can relax this
- Scaling down removes the highest ordinal first and keeps its PVC unless configured otherwise
- A headless Service provides per-pod DNS such as db-0.db.namespace.svc
- Rolling updates move from highest ordinal to lowest by default
- StatefulSets do not make an application highly available; quorum and replication are still your job
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: db
spec:
serviceName: db-headless
replicas: 3
selector:
matchLabels:
app: db
template:
metadata:
labels:
app: db
spec:
containers:
- name: db
image: registry.example.com/db:16.2
volumeMounts:
- name: data
mountPath: /var/lib/db
volumeClaimTemplates:
- metadata:
name: data
spec:
accessModes: ["ReadWriteOnce"]
resources:
requests:
storage: 50GiStateful workloads require more operational care: backups, restore drills, failover testing and PVC lifecycle. Running a database on Kubernetes can be reasonable, but only if you own those practices rather than assuming the StatefulSet provides them.
Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1How does a StatefulSet provide stable DNS?
- 2What happens to PVCs when you scale down or delete the StatefulSet?
- 3Why is ordered startup useful for consensus systems?
- 4Would you run your primary database on Kubernetes? Why or why not?
Say it in the interview
I reach for a StatefulSet when a workload needs stable identity, not just replicas. It gives ordinal pod names, per-pod DNS through a headless Service and a PVC per replica. That matters for databases and consensus systems, but it does not provide replication or backups. I would only run those on Kubernetes if the team owns restore drills and failover testing.
Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.
Related questions
Reading is step one
Now say it out loud.
Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live Kubernetes mock interview and get scored feedback on depth, clarity and confidence.