Overview

16 Handling Stateful Applications with StatefulSets

This chapter explains how Kubernetes handles stateful workloads and why StatefulSets are the preferred abstraction compared to Deployments for applications that require durable state and stable identities. It contrasts stateless scaling with the complexities of stateful scaling, highlighting storage constraints (for example, common lack of ReadWriteMany in many environments) and the need for per-replica volumes and stable network identities. Using the Kiada suite’s Quiz service as a running example backed by MongoDB, it shows how a StatefulSet paired with a headless Service provides ordered, stable Pod identities, per-replica PersistentVolumeClaims from templates, and per-Pod DNS (including SRV records), while moving data import to a one-shot Pod to avoid duplicate ingestion.

The chapter dives into core StatefulSet behavior: ordinal Pod naming and identity persistence across restarts, controlled Pod creation (Parallel vs OrderedReady), and at-most-one semantics that prevent duplicate identities during node outages. It demonstrates lifecycle scenarios such as replacing missing Pods, handling node failures (including when volumes are local vs network-attached), and when manual intervention (force-deleting Pods or claims) is required. It covers scaling rules (removing highest ordinals first), default retention of PVCs with an option to change retention policies, and the common pattern of exposing Pods through two Services—regular (ready-only) for clients and headless (includes unready) for peer discovery—to satisfy both application readiness and cluster membership needs.

For change management, the chapter details StatefulSet updates: RollingUpdate (one Pod at a time, highest ordinal first, optional minReadySeconds) and OnDelete (operator-driven, in any order). It shows how partitioned rolling updates enable staged and canary rollouts by freezing updates below a chosen ordinal, and discusses pitfalls where OrderedReady can deadlock if readiness depends on quorum, along with remedies (Parallel policy or adjusted probes). It concludes with Kubernetes Operators as higher-level, application-aware controllers: installing and using the MongoDB Community Operator to manage a replica set via a custom resource, while the operator creates the StatefulSet and Services, initializes replication, reconciles scaling, and automates the application’s operational tasks beyond what a raw StatefulSet provides.

All Pods from a Deployment use the same PersistentVolumeClaim and PersistentVolume.
Providing each replica with its own volume and address.
Treating entities as pets vs. as cattle
A StatefulSet, its Pods, and PersistentVolumeClaims
A headless Service used in combination with a StatefulSet
StatefulSets don’t delete PersistentVolumeClaims when scaling down; then they reattach them when scaling back up.
Comparison between the OrderedReady and Parallel Pod management policy
How the Pods are updated over time with different update strategies
Partitioning a rolling update
Managing an application through custom resources and operators

Summary

  • Stateful workloads are harder to manage than their stateless counterparts because managing state is difficult. However, with StatefulSets, managing stateful workloads becomes much easier because the StatefulSet controller automates most of the work.
  • With StatefulSets you can manage a group of Pods as pets, whereas Deployments treat the Pods like cattle. The Pods in a StatefulSet use ordinal numbers instead of having random names.
  • A StatefulSet ensures that each replica gets its own stable identity and its own PersistentVolumeClaim(s). These claims are always associated with the same Pods.
  • In combination with a StatefulSet, a headless Service ensures that each Pod receives a DNS record that always resolves to the Pod’s IP address, even if the Pod is moved to another node and receives a new IP address.
  • StatefulSet Pods are created in the order of ascending ordinal numbers, and deleted in reverse order.
  • The Pod management policy configured in the StatefulSet determines whether Pods are created and deleted sequentially or simultaneously.
  • The PersistentVolumeClaim retention policy determines whether claims are deleted or retained when you scale down or delete a StatefulSet.
  • When you update the Pod template in a StatefulSet, the controller updates the underlying Pods. This happens on a rolling basis, from highest to lowest ordinal number. Alternatively, you can use a semi-automatic update strategy, where you delete a Pod and the controller then replaces it.
  • Since StatefulSets don’t provide everything needed to fully manage a stateful workload, these types of workloads are typically managed via custom API object types and Kubernetes Operators. You create an instance of the custom object, and the Operator then creates the StatefulSet and supporting objects.

FAQ

What is a StatefulSet and how does it differ from a Deployment?A StatefulSet manages stateful workloads by giving each Pod a stable identity (name, network DNS, and storage) and by creating dedicated PersistentVolumeClaims per replica. Deployments treat Pods as interchangeable (cattle), while StatefulSets treat them as unique (pets), ensuring a Pod keeps its ordinal name and reattaches to the same storage when recreated.
Why can’t I just scale a stateful Deployment that uses a single PersistentVolumeClaim?Most clusters only support ReadWriteOnce (RWO) volumes, so a single PersistentVolume can’t be mounted read/write across multiple nodes. Even on a single node, many databases (e.g., MongoDB) won’t allow multiple instances to share the same data directory. Each replica typically needs its own volume.
Why do StatefulSets need a headless Service, and what does it provide?A headless Service (clusterIP: None) paired with a StatefulSet gives each Pod a resolvable, stable DNS name (for example, quiz-0.quiz-pods.namespace.svc.cluster.local). This enables peer discovery and stable client connections to specific replicas, which is essential for systems like database replica sets.
How do StatefulSet Pods get stable identities and dedicated storage?StatefulSets assign ordinal names (pod-0, pod-1, …) and create one or more PersistentVolumeClaims per replica via volumeClaimTemplates. A Pod with a given ordinal is always recreated with the same name and re-bound to the corresponding claim (for example, db-data-pod-1), preserving its state.
What’s the difference between the OrderedReady and Parallel podManagementPolicy?- OrderedReady (default): Creates/deletes Pods one at a time in order, waiting for readiness between steps. Safer for apps that require strict sequencing but can deadlock if the first Pod never becomes ready. - Parallel: Creates/deletes all Pods concurrently, faster but less strict. Choose based on your app’s startup/shutdown needs.
How does a StatefulSet handle node failures and “at-most-one” semantics?StatefulSets guarantee that at most one Pod with a given identity runs at a time. If a node becomes unreachable, the controller won’t immediately replace the Pod elsewhere to avoid duplicate instances. Operators may need to force-delete the Pod (for example, kubectl delete pod POD --force --grace-period 0) only after confirming the old instance is truly down and its volume can be safely reattached.
What happens to PersistentVolumeClaims when scaling a StatefulSet, and can I change that?By default, scaling down deletes Pods but retains their PVCs to avoid data loss. You can change this with spec.persistentVolumeClaimRetentionPolicy (whenScaled: Retain|Delete, whenDeleted: Retain|Delete). Use Delete only if data is disposable or safely retained elsewhere.
How do SRV records simplify connecting to stateful Pods like MongoDB replicas?With a headless Service that names its port appropriately (for MongoDB: name “mongodb”), the cluster adds SRV records. Clients can use a simplified URI (for example, mongodb+srv://quiz-pods.namespace.svc.cluster.local), and the driver discovers replica addresses and ports via DNS, avoiding hardcoding each Pod’s hostname.
How do I update a StatefulSet safely, and what is “partitioned” RollingUpdate?StatefulSets support two strategies: - RollingUpdate (default): Replaces Pods one-by-one from highest ordinal down, waiting for readiness; can be slowed with minReadySeconds. - OnDelete: Updates only when you manually delete Pods, giving full control. With RollingUpdate, rollingUpdate.partition lets you stage/canary updates (for example, set partition to replicas-1 to update only the highest-ordinal Pod first).
When should I use a Kubernetes Operator for stateful apps, and what does it automate?Use an Operator when application-specific lifecycle tasks exceed StatefulSet capabilities (for example, auto-initializing/reconfiguring database replica sets, safe scaling, failover logic). Operators add custom resources (CRDs) you manage (for example, MongoDBCommunity), and the Operator reconciles desired state by creating/updating StatefulSets, Services, and app config automatically.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$399.99
only $33.33 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Kubernetes in Action, Second Edition ebook for free
choose your plan

team

monthly
annual
$49.99
$399.99
only $33.33 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Kubernetes in Action, Second Edition ebook for free