Lab 9: Storage & State¶
| Field | Value |
|---|---|
| Tier | 2 — Kubernetes Core |
| Estimated Time | 45 minutes |
| Prerequisites | k3s cluster, kubectl |
| Auto-Grade | Yes |
Scenario¶
The data engineering team needs a PostgreSQL instance running on Kubernetes with persistent storage that survives pod restarts. They tried deploying it as a plain Deployment but lost all their data when the pod restarted. They need you to set up a StatefulSet with a PersistentVolumeClaim, seed the database with initial data, verify the data survives a pod deletion, and implement a backup/restore workflow.
The catch: the cluster has a storage class configured, but the PVCs need to be
sized correctly. The StatefulSet needs a volumeClaimTemplate. And the backup
script needs to use pg_dump inside the pod and copy the dump file to local disk.
Objectives¶
- Create a PersistentVolumeClaim
pg-datawith 1Gi storage - Deploy PostgreSQL as a StatefulSet with the PVC mounted at
/var/lib/postgresql/data - Seed the database with a test table containing at least 5 rows
- Verify data persists after deleting and recreating the pod
- Create a backup using
pg_dumpand store it locally - Restore from the backup into a fresh database and verify data
Setup¶
Creates namespace lab-storage with a broken PostgreSQL deployment (not StatefulSet).
Hints¶
Hint 1: StatefulSet volumeClaimTemplates
Hint 2: PostgreSQL volume mount
Mount the PVC at `/var/lib/postgresql/data` and set the `PGDATA` environment variable to a subdirectory: `PGDATA=/var/lib/postgresql/data/pgdata`.Hint 3: Seeding data
Hint 5: Testing persistence
Delete the pod: `kubectl delete pod pg-0 -n lab-storage`. The StatefulSet controller will recreate it. Then check if the data is still there.Grading¶
Solution¶
See the solution/ directory for StatefulSet manifest and backup scripts.