liveBook · Manning

Chapter 16: Handling stateful applications with StatefulSets

What you will learn in chapter 16 of Kubernetes in Action, Second Edition

The introduction uses the Quiz service’s scaling difficulty to motivate the chapter’s focus on stateful workloads.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The introduction uses the Quiz service’s scaling difficulty to motivate the chapter’s focus on stateful workloads.

Key terms: Deployment object, replicas, Quiz service, stateful workloads

16.1 Introducing StatefulSets

16.1.1 Understanding stateful workload requirements

Explains why stateful workloads are difficult to scale with Deployments, using a Quiz service backed by MongoDB to contrast shared storage with per-replica volumes and stable addresses.

Key terms: stateful workload, state, replicas, ReadWriteMany, ReadWriteOnce, PersistentVolumeClaim, PersistentVolume, data directory, MongoDB replica set, Service

16.1.2 Comparing StatefulSets with Deployments

This section contrasts Deployments and StatefulSets, explaining how StatefulSets preserve each replica’s identity and state, create pods in order by default, and handle storage when scaling.

Key terms: Pets vs. Cattle, stateless workloads, stateful workloads, network identity, state, Pod template, PersistentVolumeClaim template, ordinal number, PersistentVolumeClaims, StatefulSet controller

16.1.3 Creating a StatefulSet

This section creates a headless Service and a quiz StatefulSet, explaining how the Service gives pods individual DNS identities and how the manifest configures pod creation and storage.

Key terms: headless Service, network identity, serviceName, clusterIP, publishNotReadyAddresses, quiz-pods, volumeClaimTemplates, PersistentVolumeClaims, Pod template, podManagementPolicy

16.1.4 Inspecting the StatefulSet, Pods, and PersistentVolumeClaims

Shows how to inspect a newly created StatefulSet, diagnose why its pods are not ready, and identify the pod and storage resources it creates.

Key terms: readiness probe, quiz-api, MongoDB replica set, kubectl rollout status, kubectl describe, PersistentVolumeClaim template, Event, ordinal index, controller-revision-hash, statefulset.kubernetes.io/pod-name

16.1.5 Understanding the role of the headless Service

Explains how a headless Service lets StatefulSet pods discover one another through DNS, then uses that discovery to import quiz data into a replicated MongoDB set.

Key terms: peer discovery, headless Service, A or AAAA record, StatefulSet, SRV records, mongodb+srv, mongodb, connection string, quiz-data-importer Pod, mongoimport

16.2 Understanding StatefulSet behavior

This introduction previews tests of how the StatefulSet handles missing pods and node failures.

16.2.1 Understanding how a StatefulSet replaces missing pods

Explains how a StatefulSet replaces a deleted Pod while preserving its identity and storage, and how the volume type affects where the replacement can run.

Key terms: StatefulSet, PersistentVolumeClaim, replicas field, DNS records, PersistentVolume, network-attached volume, local volume

16.2.2 Understanding how a StatefulSet handles node failures

This section explains why a StatefulSet does not automatically replace a pod on an unreachable node and what can happen when an operator forces its replacement.

Key terms: at-most-one semantics, identity, StatefulSet controller, ReplicaSet, Terminating, Kubelet, Pod object, PersistentVolume, PersistentVolumeClaim, local volume

16.2.3 Scaling a StatefulSet

This section explains how StatefulSets scale pods and storage, and why scaling the pods alone may not make a stateful application ready.

Key terms: StatefulSet, replicas, ordinal number, kubectl scale, PersistentVolumeClaim, PersistentVolume, persistentVolumeClaimRetentionPolicy, MongoDB replica set, quorum, readiness probe

16.2.4 Changing the PersistentVolumeClaim retention policy

Explains how a StatefulSet’s PersistentVolumeClaim retention policy controls claim deletion during scale-down and StatefulSet deletion, and why automatic deletion risks losing data.

Key terms: persistentVolumeClaimRetentionPolicy, whenScaled, whenDeleted, Retain, Delete, reclaimPolicy, StorageClass, --cascade=orphan, PersistentVolumeClaims

16.2.5 Using the OrderedReady Pod management policy

Explains how OrderedReady sequences StatefulSet pods, why readiness can stall that sequencing, and where its ordering guarantees do and do not apply.

Key terms: podManagementPolicy, OrderedReady, Parallel, ordinal, minReadySeconds, readiness probe, quorum, Pod template, deadlock, scale-down

16.3 Updating a StatefulSet

Introduces declarative StatefulSet updates and distinguishes their update strategies from Deployment strategies.

Key terms: StatefulSet, Pod template, declarative updates, updateStrategy, RollingUpdate, OnDelete, Recreate

16.3.1 Using the RollingUpdate strategy

This section explains how a StatefulSet rolls out pod updates, what pauses a rollout, and how to inspect revisions or roll back.

Key terms: RollingUpdate, ordinal number, ready, maxSurge, maxUnavailable, readiness, readiness probe, minReadySeconds, rollout, kubectl edit

16.3.2 RollingUpdate with partition

Explains how the RollingUpdate partition field lets you stage, test, and complete a StatefulSet update while preserving the correct template for each pod.

Key terms: partition, ordinal number, RollingUpdate strategy, canary, replicas, Pod template, StatefulSet controller, previous template, new template

16.3.3 OnDelete strategy

This section explains how the OnDelete strategy gives you control over StatefulSet updates and rollbacks by replacing pods only when you delete them.

Key terms: OnDelete update strategy, Pod template, StatefulSet controller, ordinal number, kubectl rollout undo, OnDelete strategy, previous revision, pod readiness, OnDelete

16.4 Managing stateful applications with Kubernetes Operators

This section explains why StatefulSets alone may not fully manage a stateful application and introduces Kubernetes operators as a way to automate application-specific management.

Key terms: StatefulSet, MongoDB replica set, quorum, Kubernetes operator, application-specific controller, custom object types, Kubernetes API, Deployments, StatefulSets

16.4.1 Deploying the MongoDB community operator

This section explains how to install the MongoDB community operator and prepare the cluster API for MongoDBCommunity objects, without yet deploying MongoDB.

Key terms: CustomResourceDefinition, MongoDBCommunity, operator, object manifests, Deployment, mongodb namespace

16.4.2 Deploying MongoDB via the operator

This section shows how to deploy and manage a MongoDB replica set through a MongoDBCommunity custom object while the operator handles the underlying resources and MongoDB setup.

Key terms: MongoDBCommunity, custom object, manifest, kubectl apply, reconciliation loop, operator, ownerReferences, StatefulSet, Services, Secrets

16.4.3 Cleaning up

This section gives the cleanup sequence for an operator-managed MongoDB deployment.

Key terms: MongoDBCommunity, StatefulSet, Services, garbage collector, mongodb Namespace, CustomResourceDefinition

16.5 Summary

The section summarizes how StatefulSets provide stable identities and storage for stateful pods, control their lifecycle and updates, and fit into Operator-managed workloads.

Where this is used later

Concepts explained in this chapter

← Chapter 15: Automating application updates with Deployments Chapter 17: Deploying per-node workloads with DaemonSets →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.