Most Kubernetes workloads are stateless, and for those workloads Deployments are the right tool. A web server, an API, a worker that pulls jobs from a queue — these pods can be created, destroyed, and replaced without anyone caring which one handled a particular request. But some applications are not like that. A database, a message broker, or a distributed cache needs to remember who it is, where its data lives, and which peers it can talk to.
This is the gap StatefulSets fill. A StatefulSet is a workload controller that gives each pod a stable, predictable identity: the same name, the same network address, and the same storage volume, every time it is created. That stability is what stateful applications need to form clusters, elect leaders, and keep their data intact across restarts.
By the end, you will know how stable identity and ordinals work, why a headless Service is required, how volumeClaimTemplates manage storage, how ordered scaling and rolling updates behave, and when a StatefulSet is the wrong choice and an Operator or a managed service is better. The examples here are written against the current Kubernetes release: version 1.37, codenamed “Garhwal”, with the latest patch 1.37.3 released on 25 November 2026. Version 1.38 is planned for 16 December 2026 but has not been released yet, and the supported minor versions are 1.37, 1.36, and 1.35.
Why Deployments Fail Stateful Applications
A Deployment manages identical, interchangeable pods. When a pod dies, the ReplicaSet creates a replacement with a new, random name and a new IP address. When you scale up, new pods appear in no particular order. When you roll out an update, pods are replaced in an order the controller chooses, often several at once. For stateless applications, all of this is a feature. For stateful applications, each of those behaviors breaks something fundamental.
Consider a three-node database cluster. Each node holds a portion of the data, or a full copy in a replicated setup, and the nodes know each other by name. If one node restarts and comes back with a different name and a different address, the other nodes cannot find it, and the cluster has to be reconfigured. If the replacement pod attaches to a fresh, empty volume instead of the original node’s disk, the data is gone, or at best the node has to spend hours re-syncing from its peers. And if an update replaces all three nodes at the same time, the cluster loses quorum and goes down entirely.
These are not edge cases. They are the normal behavior of a Deployment applied to a workload it was never designed for. The Kubernetes documentation is direct about this: Deployments are for stateless applications, and applications that need stable identity and persistent storage should use StatefulSets instead. Our Kubernetes Deployment YAML tutorial walks through the Deployment side of this picture in detail, so if you are comfortable with Deployments, you already know the baseline that StatefulSets build on. The rest of this guide explains what changes when identity and storage start to matter.
The core idea is simple: a StatefulSet treats each pod as an individual, not as a member of an anonymous pool. Pod zero is not interchangeable with pod one. Each pod gets an ordinal index, a stable hostname, a stable DNS entry, and a dedicated persistent volume that follows it through restarts and rescheduling. The controller also creates, scales, and updates pods in a defined order, so the application can start up, shut down, and change version without losing its cluster state.
Stable Identity: How StatefulSets Name Pods
Every pod created by a StatefulSet gets a name built from the StatefulSet name plus an ordinal index: web-0, web-1, web-2, and so on. The ordinal is assigned when the pod is created and it never changes for that replica slot. If web-1 is deleted, the replacement pod is also called web-1. If web-1 is rescheduled to a different node, it keeps the same name. This is the first piece of stable identity, and it is what lets other pods and external clients address a specific replica reliably.
The ordinals also define the order in which pods are created and deleted. When a StatefulSet scales up from two replicas to four, the controller creates web-2 first, waits until it is Running and Ready, and only then creates web-3. When it scales down from four to two, it deletes web-3 first, waits for it to terminate completely, and only then deletes web-2. This ordering is the default behavior, and it exists because many stateful applications need to initialize in sequence: a secondary node cannot join the cluster until the primary is ready, and a primary should not be removed while secondaries still depend on it.
Each pod also gets a stable hostname that matches its pod name, set through the pod’s hostname field by the StatefulSet controller. The application inside the pod can discover its own identity from its hostname and use it to decide its role. A common pattern is for the entrypoint script to read the hostname, extract the ordinal, and use it to decide whether this instance should initialize as the primary or join as a replica.
That said, stable identity is not the same as immortality. If a StatefulSet is deleted, its pods are deleted too. If a pod is deleted manually, the controller replaces it with a new pod that has the same name and the same storage, but any in-memory state is lost. Stable identity protects the pod’s name, network address, and disk — not the contents of memory — and it does not make a single pod highly available. High availability still comes from running enough replicas and from the application itself handling failover.
The Headless Service and Stable DNS Names
A StatefulSet requires a headless Service. This is not optional: the StatefulSet’s serviceName field must point to a Service, and that Service must be headless, meaning its clusterIP is set to None. A normal Service gets a single cluster IP and load-balances traffic across all matching pods. A headless Service has no cluster IP, and instead of load-balancing, it lets clients resolve the DNS name of each individual pod.
When a headless Service exists, Kubernetes creates a DNS record for the Service itself and a DNS record for each pod behind it. For a StatefulSet named web with a headless Service named nginx in the namespace default, the pods are reachable at addresses like web-0.nginx.default.svc.cluster.local and web-1.nginx.default.svc.cluster.local. These addresses are stable: they follow the pod name, so a client that connects to web-0.nginx always reaches the pod with ordinal zero, no matter which node it is running on or how many times it has restarted.
This is the second piece of stable identity, and it is what makes clustering work. A database replica can be configured with the DNS names of its peers. A client can connect directly to the primary by name instead of going through a load balancer that might send it to a read-only replica. Without the headless Service, the stable pod names would still exist, but nothing could resolve them into addresses, so the names would be useless for networking. If you need a single entry point that distributes traffic across all replicas, create a second, regular Service alongside the headless one — the headless Service is for pod-to-pod communication and for clients that need to address a specific replica.
There is one more detail worth knowing. DNS records for StatefulSet pods are created when the pod is created, but a pod’s DNS entry only becomes resolvable once the pod is Ready. This is the default behavior of the endpoints controller, and it means a pod that is still starting up will not receive traffic through its DNS name. For applications that need peers to discover each other before they are fully ready, the Service can be configured to publish addresses for pods that are not yet ready, but the default behavior is the safe one for most cases.
volumeClaimTemplates and the Storage Lifecycle
Stable identity would be useless without stable storage. A pod that keeps its name but loses its data every time it restarts is no better than a Deployment pod. StatefulSets solve this with volumeClaimTemplates, a field that defines a template for a PersistentVolumeClaim. When the StatefulSet creates a pod, it also creates a PersistentVolumeClaim from that template, one claim per pod, named after the pod it belongs to. Pod web-0 gets a claim named something like www-web-0, where www is the name of the volume claim template. That claim binds to a PersistentVolume, the pod mounts it, and the data written to it persists independently of the pod’s lifecycle.
The key behavior is that the claim follows the pod’s ordinal, not the pod’s lifetime. If web-1 is deleted and recreated, the new web-1 mounts the same PersistentVolumeClaim, and the same data is there. If web-1 is rescheduled to another node, the claim moves with it, provided the underlying storage class supports it. This is what lets a database survive a pod restart without losing its data or needing a lengthy resync.
Scaling down, however, does not delete the claims. If you scale a StatefulSet from four replicas to two, the pods web-3 and web-2 are deleted, but their PersistentVolumeClaims remain, along with the data on them. This is deliberate and protective: deleting a pod should never silently destroy data. The claims are only removed when you explicitly delete them, or when the StatefulSet’s persistentVolumeClaimRetentionPolicy tells Kubernetes to do so. That policy, which controls whether claims are deleted when a StatefulSet is scaled down or deleted, became beta in Kubernetes 1.27 and reached stable status in Kubernetes 1.32. By default, the policy is to retain claims in both cases, which is the safe choice for production data.
Storage classes matter here. The volume claim template references a StorageClass, and the cluster’s provisioner creates a volume of the requested size and access mode. Most database workloads use ReadWriteOnce volumes, which can be mounted by a single node at a time. That matches the StatefulSet model, where each pod has its own dedicated volume. Shared storage, where multiple pods mount the same volume, is a different pattern and usually not what you want for a database. You can learn more about how volumes, claims, and storage classes fit together in the official documentation on PersistentVolumes, which explains the binding lifecycle and the access modes in detail.
One practical consequence of the one-claim-per-pod model is that storage is provisioned per replica, and the cost scales with the replica count. A three-replica database with 20 Gi volumes each is 60 Gi of provisioned storage, and that storage exists whether the pods are running or not, because the claims persist after scale-down. Plan capacity accordingly, and make sure your storage class supports volume expansion if you expect data to grow.
Ordered Scaling, Updates, and Pod Management Policies
The StatefulSet controller’s ordering guarantees are what make it safe for applications that cannot tolerate all their members changing at once. By default, the controller uses the OrderedReady pod management policy: pods are created one at a time, in ordinal order, and each must be Running and Ready before the next is created. Deletion happens in reverse ordinal order, one at a time, and each pod must terminate fully before the next is deleted. Scaling and updates both follow these rules, which means a StatefulSet of five replicas never has more than one pod in transition at a time.
Rolling updates work the same way. When you change the StatefulSet’s pod template, for example by updating the container image, the controller updates pods in reverse ordinal order, starting with the highest ordinal. It deletes the pod, waits for the replacement to become Running and Ready, and only then moves on to the next one. This is the opposite of the order used for creation, and it is the safe direction for most clustered applications: the newest replicas are updated first, and the lowest ordinal, which is often the primary, is updated last.
The update strategy can be tuned. The default strategy is RollingUpdate, and it supports a partition value: only pods with an ordinal at or above the partition are updated, which lets you stage a rollout — update the higher ordinals first, verify the new version works, then lower the partition to update the rest. This is the StatefulSet equivalent of a canary deployment, and it is the recommended approach for updating a production database, where you want to prove the new version works on one replica before touching the primary. The other strategy is OnDelete, which does nothing until you manually delete a pod, giving you full control over the timing of each update — useful when manual steps such as running a migration are needed between replica updates.
There is also a second pod management policy, Parallel, which creates and deletes all pods at once with no ordering guarantees. This is useful for applications that do not need ordered startup, or for speeding up the initial creation of a large StatefulSet, but it removes the safety that ordering provides. If your application truly needs a StatefulSet, it usually needs the ordering too, so the Parallel policy is the exception rather than the rule.
A Working Example: A Stateful Database
The example below creates a small database deployment using a headless Service and a StatefulSet with a volume claim template. It is a teaching example, not a production setup: it uses a single replica and omits the clustering configuration, backups, and resource limits a production database would need. What it demonstrates is the structure every StatefulSet shares, and you can run it on any cluster to see stable identity and persistent storage working.
The first manifest is the headless Service. Notice clusterIP: None, which is what makes it headless, and the selector, which must match the labels on the StatefulSet pods. The Service also exposes the database port, so clients inside the cluster can reach the database through the Service name as well as through individual pod DNS names.
apiVersion: v1
kind: Service
metadata:
name: postgres
labels:
app: postgres
spec:
ports:
- port: 5432
name: postgres
clusterIP: None
selector:
app: postgres
The second manifest is the StatefulSet itself. The serviceName field points to the headless Service, which is what gives the pods their stable DNS entries. The volumeClaimTemplates section defines a 10 Gi volume using the cluster’s default storage class, and the pod mounts it at the database data directory. A readiness probe is included because ordered creation depends on readiness: the controller will not create the next pod until this one reports Ready, so an accurate readiness probe is what makes the ordering meaningful.
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: postgres
spec:
serviceName: postgres
replicas: 1
selector:
matchLabels:
app: postgres
template:
metadata:
labels:
app: postgres
spec:
containers:
- name: postgres
image: postgres:16
ports:
- containerPort: 5432
name: postgres
env:
- name: POSTGRES_PASSWORD
valueFrom:
secretKeyRef:
name: postgres-secret
key: password
volumeMounts:
- name: data
mountPath: /var/lib/postgresql/data
readinessProbe:
exec:
command:
- pg_isready
- -U
- postgres
initialDelaySeconds: 5
periodSeconds: 5
volumeClaimTemplates:
- metadata:
name: data
spec:
accessModes: ["ReadWriteOnce"]
resources:
requests:
storage: 10Gi
A few things in this manifest deserve attention. The database password comes from a Secret, not from a hardcoded value in the YAML, which is the correct pattern for credentials. Our guide on ConfigMaps versus Secrets explains when to use each one and how Secrets are handled differently from plain configuration. The image is pinned to a specific major version, postgres:16, rather than using the latest tag, because an unpinned tag can change under you and trigger an unexpected update. And the readiness probe uses the database’s own pg_isready command, which checks that the database is actually accepting connections, not just that the process started.
Apply both manifests and verify the result. The commands below create the resources, list the pods and the claims, and confirm the pod’s DNS name resolves. If you are new to running these commands, our Kubernetes tutorial for beginners covers installing kubectl, connecting to a cluster, and reading the output of these commands step by step.
kubectl apply -f postgres-service.yaml
kubectl apply -f postgres-statefulset.yaml
# Watch the pod become Ready, then check the claim
kubectl get pods -l app=postgres
kubectl get pvc -l app=postgres
# Confirm the pod's stable DNS name resolves
kubectl run -it --rm dns-test --image=busybox:1.36 \
-- nslookup postgres-0.postgres.default.svc.cluster.local
To prove that the storage is persistent, write some data, delete the pod, and check that the data is still there after the replacement pod starts. This is the test that matters: it is the difference between a StatefulSet and a Deployment, and it is worth running yourself rather than taking on trust.
# Write a row into the database
kubectl exec -it postgres-0 -- psql -U postgres \
-c "CREATE TABLE test (id int); INSERT INTO test VALUES (42);"
# Delete the pod and wait for the replacement
kubectl delete pod postgres-0
kubectl wait --for=condition=Ready pod/postgres-0 --timeout=120s
# The replacement pod has the same name and the same data
kubectl exec -it postgres-0 -- psql -U postgres \
-c "SELECT * FROM test;"
If the row is still there, the StatefulSet is working as designed: the replacement pod mounted the same volume, and the data survived the restart. Try the same experiment with a Deployment and an emptyDir volume, and the row will be gone, which is exactly the failure mode this guide is about.
When NOT to Use a StatefulSet
StatefulSets are powerful, but they are not the answer to every stateful problem, and using one where it does not fit creates complexity without benefit. The most common mistake is reaching for a StatefulSet for an application that is actually stateless. If your pods do not need stable names, stable network addresses, or persistent per-pod storage, a Deployment is simpler and faster to update. Stable identity is a constraint as well as a feature: it makes scaling slower and updates more deliberate, and you should only pay that cost when the application genuinely needs it.
The second mistake is using a StatefulSet as a substitute for a database operator. A StatefulSet gives you stable pods and stable storage, but it does not know anything about your database. It cannot run backups, it cannot fail over a primary, it cannot reconfigure replication after a node failure, and it cannot handle the split-brain scenarios that distributed databases have to survive. For production databases, the recommended approach is usually an Operator: a controller that understands the specific database and automates the operational tasks around it. The StatefulSet is often the foundation the Operator builds on, but the Operator is what makes the database safe to run in production. The official documentation on StatefulSets describes the controller’s guarantees and limits, and it is worth reading alongside the documentation for whichever Operator you choose.
There are also workloads where even a StatefulSet is the wrong layer entirely. If your application stores its state in an external service, such as a managed database or an object store, the pods themselves can be stateless and a Deployment is the right choice. If your application needs shared storage that multiple pods write to at the same time, a StatefulSet’s per-pod volumes will not help. And if your application is a single instance that must never have two copies running, a StatefulSet does not guarantee that on its own: you still need to think about pod disruption budgets and node failures.
A good rule is to ask three questions before choosing a StatefulSet. Does each replica need a stable, unique identity that other replicas can address by name? Does each replica need its own persistent storage that survives restarts? And does the application need pods to start, stop, and update in a defined order? If the answer to all three is yes, a StatefulSet is the right tool. If the answer to one or more is no, look at Deployments, DaemonSets, or an Operator first, and use a StatefulSet only for the part of the system that genuinely needs it.
Frequently Asked Questions
What is the difference between a StatefulSet and a Deployment?
A Deployment manages identical, interchangeable pods with random names, dynamic IP addresses, and no guaranteed storage. A StatefulSet gives each pod a stable name with an ordinal index, a stable DNS entry through a headless Service, and a dedicated PersistentVolumeClaim that follows the pod through restarts. Deployments create and update pods in any order, while StatefulSets create, scale, and update pods in a defined order. Use Deployments for stateless applications and StatefulSets for applications where each replica has its own identity and data.
Do StatefulSets require a headless Service?
Yes. A StatefulSet requires a headless Service, specified in the StatefulSet’s serviceName field, with the Service’s clusterIP set to None. The headless Service is what creates the stable DNS records for each pod, such as web-0.nginx.default.svc.cluster.local. Without it, the pods would still have stable names, but other pods and clients could not resolve those names to addresses, so the stable identity would be useless for networking.
What happens to my data when I scale down a StatefulSet?
Scaling down deletes the pods but does not delete their PersistentVolumeClaims by default, so the data remains. The claims, and the volumes behind them, stay in the cluster until you delete them manually or set the StatefulSet’s persistentVolumeClaimRetentionPolicy to delete claims on scale-down or on StatefulSet deletion. That policy became beta in Kubernetes 1.27 and stable in 1.32. The default behavior of retaining claims is deliberate: deleting a pod should never silently destroy data.
Can I use a StatefulSet for a production database?
You can, but a bare StatefulSet is usually not enough on its own. A StatefulSet provides stable identity and persistent storage, but it does not handle backups, failover, replication configuration, or recovery from split-brain scenarios. For production databases, the usual recommendation is to use an Operator built for your specific database, which automates those operational tasks and often uses a StatefulSet underneath. A plain StatefulSet is a good fit for learning, for development environments, and for simpler stateful workloads where the application handles its own clustering.
How do rolling updates work in a StatefulSet?
When you update the pod template, the StatefulSet controller updates pods in reverse ordinal order, starting with the highest ordinal, and waits for each replacement to become Running and Ready before moving to the next one. This means the lowest ordinal, which is often the primary, is updated last. You can control the rollout with a partition: only pods with an ordinal at or above the partition value are updated, so you can test a new version on higher ordinals before rolling it out to the rest. The alternative strategy, OnDelete, waits until you manually delete each pod before recreating it with the new template.
Conclusion
StatefulSets exist because some applications cannot be treated as interchangeable. A database, a message broker, or any clustered service needs to know who it is, where its data lives, and how to reach its peers by name. A Deployment cannot provide any of those guarantees. A StatefulSet can, through stable ordinals, a headless Service with predictable DNS names, volume claim templates that bind storage to each replica, and ordered scaling and updates.
The key is to use that power where it fits and nowhere else. If your application needs stable identity, per-pod storage, and ordered operations, a StatefulSet is the right controller, and the example in this guide is a solid starting point. If your application is stateless, a Deployment is simpler. And if you are running a production database, pair the StatefulSet with an Operator or a managed service that handles the operational work a controller alone cannot do.



