What is a StatefulSet in Kubernetes?
Why Interviewers Ask This
Interviewers use this question to quickly assess whether a candidate has the foundational knowledge required for Kubernetes (K8s) development. It reveals whether you understand the building blocks that more complex concepts rely on.
Answer
A StatefulSet manages stateful applications — pods that require: stable, unique network identifiers; stable, persistent storage; ordered, graceful deployment and scaling. Unlike Deployments (pods are interchangeable), StatefulSet pods have a sticky identity (pod-0, pod-1, pod-2) that persists across restarts. Use cases: databases (MySQL, PostgreSQL, MongoDB, Cassandra), distributed systems (ZooKeeper, Kafka, Elasticsearch), any app requiring stable storage per instance. Key differences from Deployment: stable pod names (pod-0, pod-1, pod-2 — not random hashes); stable DNS per pod: pod-0.service-name.namespace.svc.cluster.local; ordered startup (pod-0 must be Running/Ready before pod-1 starts); ordered scale-down (reverse order); each pod gets its own PersistentVolumeClaim (via volumeClaimTemplates). StatefulSet spec: apiVersion: apps/v1 kind: StatefulSet metadata: name: mysql spec: serviceName: mysql selector: matchLabels: app: mysql replicas: 3 template: spec: containers: - name: mysql image: mysql:8.0 volumeMounts: - name: data mountPath: /var/lib/mysql volumeClaimTemplates: - metadata: name: data spec: accessModes: [ReadWriteOnce] resources: requests: storage: 10Gi storageClassName: gp3. Headless service: StatefulSets require a headless service (clusterIP: None) for DNS-based individual pod addressing. Update strategies: RollingUpdate (default — reverse order), OnDelete (manual trigger by deleting pods).
Pro Tip
Before answering, structure your response: one-line definition → real-world analogy → concrete example from a project. This makes even complex Kubernetes (K8s) answers easy to follow.