Kubernetes · Chapter 22 of 28
StatefulSets
By the end of this chapter you can run a database on Kubernetes with a StatefulSet: pods with stable names, each with its own disk from a claim template, started and stopped in order, reached by their own DNS names through a headless Service; and you can scale one and say what happens to its data. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: More workloads and traffic
- 4 tasks
- Kubernetes
- In the all labs plan
The first 2 chapters of Kubernetes are free. This one comes with the all labs plan.
Why you need this
Every one of Jhatpat's 62,000 orders a day is a row in PostgreSQL, and on Dhanteras evening about 2,40,000. Until now the database ran on one of Tushar's virtual machines. Ammar wants it in the cluster with everything else, and next year a second copy that follows the first, so a failed disk does not stop orders.
Chapter 21 gave you a Deployment with a PersistentVolumeClaim, and for a database it falls apart in three places. Its pods have random names (orders-67b566db84-wb7hv), so nothing can say "the first database" and mean the same pod tomorrow. All its replicas name the same claim: two PostgreSQL servers writing into one data folder destroy it, and a ReadWriteOnce disk cannot even reach two nodes. And a rollout starts the new pod before the old one stops, which for a database is two servers on one folder again. A database needs each copy to have a name of its own, a disk of its own that follows that name, and a careful order of start and stop.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
postgres.yaml: run PostgreSQL with its own disk
Run PostgreSQL as a StatefulSet, so its data stays on a disk of its own.
1. Open postgres.yaml in the chapter folder. Its notes list every name and number.
2. Complete the Service postgres: clusterIP None, port 5432, and the selector app=postgres.
3. Complete the StatefulSet postgres: 1 replica, image postgres:17, and env from the Secret postgres-auth.
4. Add a volumeClaimTemplate named data (1Gi, ReadWriteOnce). Mount it at /var/lib/postgresql/data.
5. Apply postgres.yaml with kubectl apply -f. Then run pg_isready inside the pod postgres-0.
To pass: in ch22, the Service postgres is headless on port 5432 and the StatefulSet postgres has serviceName postgres. The container takes POSTGRES_USER, POSTGRES_PASSWORD and POSTGRES_DB from the Secret postgres-auth. The claim data-postgres-0 is Bound, postgres-0 is ready, and pg_isready in it says the database is accepting connections.
- Task 2
orders.yaml: connect orders to postgres-0 by its name
The orders app still uses the old database host, so it crashes. Give it the new host.
1. Do task 1 first. Without postgres-0 there is nothing to connect to.
2. Read the log of the orders Deployment. It names the host it cannot find.
3. In orders.yaml, change only the host in DATABASE_URL to postgres-0.postgres.ch22.svc.cluster.local.
4. Apply orders.yaml. Wait for the new pod, then read its log again.
To pass: the host in DATABASE_URL is postgres-0.postgres.ch22.svc.cluster.local. The user orders, port 5432 and the database orders stay as they are. A new orders pod runs, and its log says it connected to the database as orders.
- Task 3
redis.txt: three Redis pods, each with its own data
See that each pod of a StatefulSet keeps its own data.
1. Scale the StatefulSet redis to 3 replicas. Wait until redis-0, redis-1 and redis-2 are ready.
2. In each pod, use redis-cli to SET the key whoami to that pod's own name.
3. List the claims. Find the claim that holds each pod's data.
4. In redis.txt, write one line for each pod: the pod's name, a space, then its claim's name. For example: web-9 www-web-9.
To pass: redis has 3 replicas, all 3 pods are ready, and whoami in each pod gives that pod's own name. redis.txt has a line for each of the 3 pods with the right claim. Lines that start with # are notes and do not count.
- Task 4 · interview question
Interview: StatefulSet or Deployment with a PVC
Interview question: when do you use a StatefulSet and not a Deployment with a PVC?
1. Ammar will ask: "Tushar says a Deployment with a PVC is enough for PostgreSQL. What would you tell him?"
2. Write your answer in one or two sentences of your own.
3. Save it in answer.txt, below the notes.
To pass: answer.txt says what stays the same for each pod of a StatefulSet, says what happens to the storage, and compares it with a Deployment. Lines that start with # do not count.
What you will learn
- StatefulSet: pods with stable names (web-0, web-1), made and removed in order.
- volumeClaimTemplates: one claim per pod (www-web-0), which stays with that name.
- The headless Service: no cluster IP, and a DNS name for each pod (web-1.web.ch22.svc.cluster.local).
- Scaling: pods go in reverse order, their claims stay.
- Rolling updates and partition: updating one pod at a time, from the highest.
More in More workloads and traffic
Build "StatefulSets" yourself
Jhatpat delivers groceries in ten minutes from 14 dark stores across Lucknow, and its hand-run servers fall over on festival evenings. You move it onto Kubernetes. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Kubernetes are free. This one comes with the all labs plan.