Keep data with volumes, claims and a StatefulSet
In this task you'll keep an app's data when its pod is replaced.
A container's own files vanish when it restarts. For data that must last, a pod mounts a PersistentVolumeClaim (PVC): a request for storage of a size and access mode. A StorageClass says how to create the matching PersistentVolume (PV) on demand; kind ships one (standard) that uses a folder on the node, and AKS has classes for Azure Disks and Azure Files.
A StatefulSet is for apps where each copy needs its own identity and disk, such as databases. Its pods get fixed names (db-0, db-1) and each gets its own PVC from a volumeClaimTemplate, which stays when the pod is replaced. A claim stuck in Pending usually means no class matches, the size or access mode can't be met, or the class waits for the first pod (WaitForFirstConsumer).
Example. Tollmere Logistics runs a small PostgreSQL for its depot app as a StatefulSet with one replica and a 10 GiB claim on a premium disk class. When the node was upgraded, db-0 moved to another node and its disk moved with it, so no data was lost.
Before you start
You need: Docker Desktop, kind and kubectl on your computer (all free), and a cluster from Run a practice cluster on your computer. Time: 35 minutes.
Where to look
- Terminal
- kubectl
Steps
- List storage classes with
kubectl get storageclassand note the default. - Create a PVC for 1Gi with access mode ReadWriteOnce, and see it stay Pending until a pod uses it.kind's class binds on first use.
- Run a pod that mounts the claim at /data and writes the date to /data/hello.txt; check the PVC is now Bound.
- Delete the pod, start a new one with the same claim, and read the file back.
- Create a StatefulSet
dbwith 2 replicas and a volumeClaimTemplate, and list the claims it made (data-db-0, data-db-1). - Delete pod db-1 and watch it come back with the same name and the same claim.
Commands
kubectl get storageclass
cat <<'EOF' | kubectl apply -f -
apiVersion: v1
kind: PersistentVolumeClaim
metadata: {name: notes}
spec:
accessModes: [ReadWriteOnce]
resources: {requests: {storage: 1Gi}}
EOF
kubectl get pvc notes
# Why is a claim Pending?
kubectl describe pvc notes | tail -5
Watch out for
- Cost
- Free on kind. In Azure, disks are charged by size while they exist, even when no pod uses them.
- Security
- Check the reclaim policy: Delete removes the data when the claim goes, Retain keeps it for you to clean up.
- Common mistakes
- Asking for ReadWriteMany from a disk class that only supports ReadWriteOnce, and deleting a StatefulSet expecting its claims to go too (they stay).
Clean up
Delete the pods, the StatefulSet and then the PVCs (they aren't deleted with the StatefulSet).
Words to know
- PersistentVolumeClaim
- A pod's request for storage of a given size and access mode.
- StorageClass
- A recipe for creating volumes on demand, such as a disk type.
- Access mode
- Who can mount a volume: one node read-write (RWO), or many nodes (RWX).
- StatefulSet
- A workload whose pods keep fixed names and their own disks.
Tick off each step and track your progress in the guide.
Open this task in DevOps, Hands OnKeep going
- Kubernetes roadmap: the skills behind the CKA and CKADExplained
- Kubernetes storage: volumes, claims and StatefulSetsExplained
- Kubernetes workload types: which one to useExplained
- Route web traffic with the Gateway APIPrevious in Kubernetes skills
- Give a team just enough access with RBACNext in Kubernetes skills
- Containers and KubernetesAll pages in this topic