StatefulSets for Stateful Apps
Deploy and manage stateful applications like databases using StatefulSets, ensuring stable network identifiers and persistent storage.
StatefulSets for Stateful Apps is a free DevOps Bootcamp lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the DevOps Bootcamp learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
StatefulSets for Stateful Apps
Welcome! In this lesson, we'll dive into StatefulSets, a Kubernetes object designed to manage stateful applications.
Unlike stateless apps (which are easily scaled and replaced), stateful apps require unique identities and persistent storage for each instance. Think databases, message queues, or custom applications that store data locally.
Why Stateful? The Challenge
Traditional Deployments are great for stateless applications. If a Pod dies, a new one replaces it, and it doesn't matter which one serves the request.
But for stateful apps, each replica often needs a stable, unique identity and its own dedicated storage. If a database Pod restarts, it needs to reconnect to its specific data volume.
Stable Network & Pod Identity
A key feature of StatefulSets is providing stable, unique network identifiers for each Pod.
- Pods are named in a predictable, ordered way (e.g.,
web-0,web-1). - They maintain their hostnames across restarts.
- This allows other services to reliably connect to a specific replica.
Stable & Persistent Storage
StatefulSets ensure each Pod gets its own stable, persistent storage. This is handled using Persistent Volume Claims (PVCs).
When a StatefulSet Pod is created, it automatically provisions a PVC based on a volumeClaimTemplates definition. If the Pod dies and is recreated, it will re-attach to the same PVC and thus its original data.
Anatomy of a StatefulSet YAML
A StatefulSet YAML definition looks similar to a Deployment, but with some crucial additions. It includes:
apiVersion,kind,metadataspec.replicas,spec.selector,spec.template(like Deployments)spec.serviceName: Links to a headless service.spec.volumeClaimTemplates: Defines how PVCs are created for each Pod.
Headless Service for Stable IDs
For StatefulSets to provide stable network identities, they require a Headless Service. A headless service doesn't have a cluster IP. Instead, it directly returns the IP addresses of the Pods it selects.
The StatefulSet uses this service to register DNS entries for each of its Pods (e.g., pod-0.my-service.my-namespace.svc.cluster.local).
apiVersion: v1
kind: Service
metadata:
name: my-app-headless
spec:
ports:
- port: 80
name: web
clusterIP: None # This makes it a headless service
selector:
app: my-appVolumeClaimTemplates
The volumeClaimTemplates section is unique to StatefulSets. It's a template for the Persistent Volume Claims (PVCs) that will be created for each replica.
Each Pod in the StatefulSet (e.g., my-app-0, my-app-1) will get its own PVC (e.g., data-my-app-0, data-my-app-1) based on this template, ensuring dedicated storage.
volumeClaimTemplates:
- metadata:
name: data # This name is used in Pod's volumeMounts
spec:
accessModes: [ "ReadWriteOnce" ]
storageClassName: standard
resources:
requests:
storage: 1GiFull StatefulSet Example
Here's a complete example for a simple Nginx StatefulSet. Notice the serviceName linking to the headless service and the volumeClaimTemplates.
To deploy this, save it as nginx-statefulset.yaml and run kubectl apply -f nginx-statefulset.yaml.
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: web
spec:
serviceName: "nginx-headless" # Must match headless service name
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: k8s.gcr.io/nginx-slim:0.8
ports:
- containerPort: 80
name: web
volumeMounts:
- name: www # Mounts the volume from the PVC
mountPath: /usr/share/nginx/html
volumeClaimTemplates:
- metadata:
name: www # This name links to volumeMounts.name
spec:
accessModes: [ "ReadWriteOnce" ]
storageClassName: standard
resources:
requests:
storage: 1Gi
---
apiVersion: v1
kind: Service
metadata:
name: nginx-headless
labels:
app: nginx
spec:
ports:
- port: 80
name: web
clusterIP: None
selector:
app: nginxPod Naming & Ordering
Once deployed, you'll see Pods named like web-0, web-1. These indices are stable and unique.
StatefulSets enforce strict ordering for scaling operations. Pods are created in order (0, 1, 2...) and terminated in reverse order (..., 2, 1, 0).
This ordered approach is crucial for many distributed stateful systems to maintain consistency.
Scaling & Rolling Updates
You can scale a StatefulSet just like a Deployment, using kubectl scale statefulset web --replicas=3.
Rolling updates also work, but with the same ordered guarantees: Pods are updated one by one, in reverse ordinal order, ensuring the next Pod isn't updated until the previous one is ready.
This careful orchestration minimizes disruption for stateful applications.
Check Your Understanding
Which of the following are key characteristics or requirements for Kubernetes StatefulSets?
Recap: StatefulSets in Action
Great job! In this lesson, we explored StatefulSets, a powerful Kubernetes object for managing stateful applications.
- They provide stable network identities and ordered operations.
- They use
volumeClaimTemplatesfor dedicated, persistent storage per replica. - A headless service is essential for their stable network IDs.
StatefulSets are fundamental for running databases and other critical stateful workloads reliably on Kubernetes.
Frequently asked questions
Is the “StatefulSets for Stateful Apps” lesson free?
Yes — the full text of “StatefulSets for Stateful Apps” is free to read here on the web, and the DevOps Bootcamp course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the DevOps Bootcamp course, upgrade to CoddyKit PRO.
What will I learn in “StatefulSets for Stateful Apps”?
Deploy and manage stateful applications like databases using StatefulSets, ensuring stable network identifiers and persistent storage. You practise DevOps Bootcamp with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start DevOps Bootcamp?
No prior experience is required. DevOps Bootcamp on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “StatefulSets for Stateful Apps” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this DevOps Bootcamp lesson?
Yes. Every DevOps Bootcamp lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- StatefulSets for Stateful Apps
- DaemonSets for Node-Specific Tasks
- Understanding Kubernetes Operators
- Custom Resource Definitions (CRDs)