Kubernetes storage: volumes, claims and StatefulSets
Where data lives in a cluster, how pods ask for it, and how stateful apps keep it when they move.
Anything a container writes to its own file system disappears when it restarts. Volumes fix that. Some last only as long as the pod (emptyDir, handy for scratch files). For data that must outlive pods, the pod mounts a PersistentVolumeClaim, and Kubernetes binds it to a PersistentVolume, usually created on demand by a StorageClass.
Each class has a provisioner (a CSI driver such as Azure Disk or Azure Files), parameters (disk type), a reclaim policy (Delete or Retain) and a binding mode. WaitForFirstConsumer waits until a pod uses the claim, so the disk is made in the same zone as the pod. Access modes matter: a disk is usually ReadWriteOnce (one node), a file share can be ReadWriteMany.
StatefulSets give each replica a fixed name and its own claim from a template, so db-0 always gets data-db-0 back. Backups still need a plan: volume snapshots, a backup tool, or the database's own backups.
Cost. Free on kind. Cloud disks and file shares are charged by size and performance tier while they exist.
Use it when
- you run databases, queues or anything that writes data.
- a claim is stuck in Pending.
- you're choosing between disks and file shares.
How it works
- Claim
- A pod's request: size, access mode, optional class.
- Class
- How to make the volume: driver, tier, reclaim policy, binding mode.
- Binding
- Kubernetes matches a claim to a volume, making one if needed.
- StatefulSet
- Fixed names and one claim per replica that survives rescheduling.
- Snapshots
- Point-in-time copies of volumes, if the driver supports them.
Options
- emptyDir
- Scratch space for one pod's life.
- Disk (RWO)
- Fast block storage for one node at a time.
- File share (RWX)
- Shared files for many pods across nodes.
- Managed database
- Often simpler than running a database in the cluster at all.
Example. Quayhurst Health keeps its document store on an Azure Files class (ReadWriteMany) so several pods can read scans, and its audit database on a premium disk StatefulSet with nightly volume snapshots.
Don't confuse it with
- Deployment with a claim
- Every replica shares one claim, which most disks don't allow; fine for one replica only.
- hostPath
- A folder on the node; ties the pod to that node and is risky. Avoid it for apps.
Words to know
- PersistentVolume
- A piece of storage in the cluster, made by hand or by a class.
- CSI driver
- A plugin that lets Kubernetes create and attach a storage system's volumes.
- Reclaim policy
- What happens to a volume when its claim is deleted.
- volumeClaimTemplate
- The part of a StatefulSet that creates one claim per replica.
Where to look
- kubectl get pvc,pv,storageclass
- kubectl describe pvc
Practise it
- Keep data with volumes, claims and a StatefulSetHow-to, Free
- Final project: run a shop on Kubernetes from start to finishHow-to, Free
Read it in the guide, next to the tasks and labs.
Open in DevOps, Hands On