Table of contents
Open Table of contents
A database pod needs to come back as itself
Suppose you run a three-member database on Kubernetes. Each member has its own data and may be known to its peers by name. If Kubernetes replaces a failed Pod, the replacement must be able to find the same storage and network identity. A generic Deployment is designed for interchangeable replicas; a StatefulSet adds stable identity and storage to workloads that need them.
A StatefulSet is a Kubernetes controller. You describe a desired number of Pods, a Pod template, a governing Service, and optionally a template for persistent volume claims. The controller creates Pods with predictable ordinals such as db-0, db-1, and db-2. It also creates one persistent volume claim for each ordinal.
Headless Service: db
DNS domain for the Pods
|
v
StatefulSet: db, replicas=3 StatefulSet controller
template + claim template keeps desired Pods present
| and follows ordering rules
+------+------+------+
| | |
v v v
db-0 db-1 db-2
| | |
data-db-0 data-db-1 data-db-2
PVC PVC PVC
| | |
persistent volume per member's data
The key is the pairing: ordinal zero keeps the identity db-0 and uses its own claim, data-db-0. If that Pod is rescheduled onto a different node, the claim can be mounted again when the storage system allows it. StatefulSet does not promise that a Pod always runs on one particular node.
Three parts make the identity stable
Each StatefulSet Pod gets an ordinal, a stable network identity, and stable storage. These are related, but they come from different Kubernetes objects.
Ordinals name the members
For three replicas, Kubernetes assigns ordinals 0, 1, and 2 by default. The Pod names are db-0, db-1, and db-2. Kubernetes also adds the apps.kubernetes.io/pod-index label, which an application or monitoring system can use to identify the ordinal without parsing the Pod name.
A headless Service provides the DNS domain
The StatefulSet’s serviceName refers to a governing Service. It is commonly a headless Service, with clusterIP: None, so DNS can return addresses for the individual Pods instead of only one virtual IP. If the Service is named db in the default namespace, a member can have a stable DNS name such as:
db-0.db.default.svc.cluster.local
The Pod’s ordinal remains in the name when the Pod is replaced. DNS discovery still follows normal Kubernetes readiness and DNS behavior, so an application should retry lookups and connections as members start or move.
Volume claim templates give each member its own claim
The StatefulSet’s volumeClaimTemplates describes a claim to create for each Pod. For a template named data and a StatefulSet named db, the resulting PVCs are named data-db-0, data-db-1, and data-db-2. Each ordinal is mounted with its own claim.
That gives Kubernetes enough information to reconnect a replacement db-1 to data-db-1. A StatefulSet does not copy or replicate the database’s contents. Replication, membership changes, and data recovery remain responsibilities of the database and its storage system.
How the controller creates and scales members
With the default OrderedReady Pod management policy, creation proceeds from the lowest ordinal upward. The controller creates db-0 and waits for it to become Ready before creating db-1; it then waits for db-1 before creating db-2.
User applies replicas: 3
|
v
StatefulSet controller
create db-0
wait until Ready
create db-1
wait until Ready
create db-2
wait until Ready
If you scale from three replicas down to two, the highest ordinal, db-2, is removed first. Scaling back to three creates db-2 again and uses the claim associated with that ordinal, subject to the PVC retention policy and the storage system’s availability.
The Parallel Pod management policy lets the controller create or terminate Pods without waiting for each neighboring ordinal to become Ready. It is useful when the application manages its own membership and can safely start members concurrently. It does not change the StatefulSet’s rolling update order.
These ordering rules are helpful when an application expects members to join one at a time. They can also make progress wait: if db-0 never becomes Ready, the default policy will not move on to create db-1. Readiness probes should express when a member is ready for the next step, not merely when its process has started.
Rolling updates use ordinals as a safety control
The default RollingUpdate strategy updates StatefulSet Pods from the highest ordinal down. It waits for an updated Pod to become Ready before proceeding to the next one. For example, a template update begins with db-2, then db-1, then db-0.
The rollingUpdate.partition field can stage a rollout. With three replicas and a partition of 2, only Pods whose ordinal is 2 or higher receive the new template. You can check the canary member before lowering the partition to update the rest. With the OnDelete strategy, changing the template does not automatically replace existing Pods; each Pod is updated after you delete it and the controller recreates it.
StatefulSet ordering is not an application-level upgrade protocol. Kubernetes can wait for readiness, but it cannot know whether a database’s schema change is backward-compatible or whether a replica has safely joined consensus. The application may need its own operator or upgrade procedure.
What happens to claims when Pods are removed
By default, deleting or scaling down a StatefulSet does not delete the persistent volume claims created from its templates. That retention behavior helps protect data when a Pod is removed and later recreated.
The optional persistentVolumeClaimRetentionPolicy lets you choose separate behavior for scaling down and deleting the StatefulSet:
whenScaled: Retainkeeps claims for ordinals removed during scale-down.whenDeleted: Retainkeeps claims when the StatefulSet itself is deleted.- Either policy can be set to
Deleteif automatic claim removal is appropriate.
Deleting a PVC does not by itself specify how the underlying storage provider treats its PersistentVolume or data. The PersistentVolume reclaim policy and storage provider also matter. Retaining a claim is useful protection against accidental removal, but it is not a backup; it does not protect against corruption or deletion inside the database.
Settings to decide deliberately
| StatefulSet field | Typical choice | Effect |
|---|---|---|
serviceName | Name of a headless Service | Gives each Pod a stable DNS identity. |
volumeClaimTemplates | One claim template per data volume | Creates a separate PVC for each ordinal. |
podManagementPolicy | OrderedReady by default | Orders creation and deletion around Pod readiness; Parallel skips that waiting for create/delete. |
updateStrategy | RollingUpdate by default | Replaces Pods in a controlled order; OnDelete leaves replacement to the operator. |
rollingUpdate.partition | Omit for a full rollout | Limits an update to ordinals at or above the partition. |
persistentVolumeClaimRetentionPolicy | Retain claims by default | Controls claims on scale-down and StatefulSet deletion. |
Use a StatefulSet when ordinal identity and per-member storage matter. Use a Deployment when replicas can be replaced interchangeably. For a StatefulSet, identity and ordering are Kubernetes guarantees; service health, replication, backups, and safe data upgrades still need an application-level design.
References
- Kubernetes StatefulSets — stable identity, governing Services, ordered management, update strategy, and claim retention.
- Kubernetes Services: Headless Services — how
clusterIP: Noneexposes DNS records for individual Pods. - Kubernetes Persistent Volumes — PersistentVolumeClaims, storage classes, and reclaim policies.
- StatefulSet API reference — fields including
serviceName,volumeClaimTemplates, management policy, and update strategy.