Kubernetes Field Guide · Chapter 7 of 16
Storage: volumes, PersistentVolumes and claims
A container's filesystem is thrown away with the container. Anything that must survive a restart, a rollout or a dead node needs a volume. Learn which storage survives what, how claims get real disks, StatefulSets, and whether your database belongs in the cluster at all.
- 35 min
- Intermediate
- Hands-on
- Free
By Joshua Sequeira, founding engineer running Kubernetes in production. Updated .
By the end you can
0 of 5
Live lab in this chapter
A real-feeling terminal and a live cluster map, right in your browser. Every command block below has a ▶ Run in lab button.
Why containers forget
A container writes to a thin layer on top of its image. When the container is replaced, that layer is gone. Kubernetes replaces containers all the time, so anything written there is temporary by design. Try the three places a pod can write:
Interactive
Where does the data survive?
pod db-0 on node-1
Disk
- emptyDir is scratch space shared by the containers in one pod: caches, temporary files, the sidecar log example from chapter 3.
- A PersistentVolumeClaim is for data that must outlive the pod.
- hostPath mounts a directory from the node. Avoid it for app data: the data is stuck on one node, and it is a common security hole.
PersistentVolumes and claims
Storage is split in two so apps do not need to know about disks:
- A PersistentVolume (PV) is a real piece of storage: an EBS volume, a Hetzner volume, an NFS share.
- A PersistentVolumeClaim (PVC) is a request for storage: "5Gi, mounted by one node at a time". The app's pod references the claim; Kubernetes binds the claim to a volume.
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: uploads
spec:
accessModes: ["ReadWriteOnce"]
storageClassName: standard
resources:
requests:
storage: 5Gi- Mountable read-write by one node at a time. Block disks such as EBS only support this.
- Which kind of storage to create. Leave it out to use the default class.
- You are billed for the size you request, not what you use.
| Access mode | Means | Typical backend |
|---|---|---|
| ReadWriteOnce (RWO) | One node mounts it read-write | EBS, Persistent Disk, local disks |
| ReadWriteOncePod (RWOP) | Exactly one pod, cluster-wide | CSI block storage |
| ReadWriteMany (RWX) | Many nodes read-write at once | EFS, NFS, CephFS |
| ReadOnlyMany (ROX) | Many nodes, read-only | Shared datasets |
StorageClasses and dynamic provisioning
Nobody creates PVs by hand any more. A StorageClass names a provisioner (a CSI driver), and when a claim asks for that class, the driver creates a disk and a PV for it.
Step through it
Dynamic provisioning: from a claim to a mounted disk
- Poduses a claim
- PVCI need 10Gi
- StorageClassgp3, Delete
- CSI drivertalks to the cloud
- PVthe disk
- Kubeletmounts it
Step 0 of 6
Press Play, or step through with Next.
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: gp3
provisioner: ebs.csi.aws.com
parameters:
type: gp3
reclaimPolicy: Delete
volumeBindingMode: WaitForFirstConsumer
allowVolumeExpansion: true- The CSI driver that creates the disks. Here, AWS EBS.
- Deleting the claim deletes the disk. Retain keeps it, and its bill, until someone cleans up.
- Create the disk only once the pod is scheduled, in that node's zone.
- Grow a claim by editing its size. Shrinking is never supported.
StatefulSets
Deployments treat pods as interchangeable. Databases, queues and anything clustered need the opposite: each replica has its own identity and its own data. A StatefulSet gives you:
- Stable names:
notes-0,notes-1, kept across restarts, with DNS names through a headless Service. - A volume per replica from
volumeClaimTemplates.notes-0always gets claimdata-notes-0back. - Ordered start-up and shutdown, so a primary can start before its replicas.
See why a Deployment is the wrong tool for a database. Scale both to three, then delete the second pod:
Interactive
A database as a Deployment vs a StatefulSet
Deployment one shared claim
StatefulSet a claim per pod
In production: databases and cost
Kubernetes can run databases, and operators such as CloudNativePG make it much easier. The question is whether you want to own backups, point-in-time recovery, failover, upgrades and zone-aware storage. For most small teams, a managed database is cheaper once you count engineering time.
Storage costs to watch:
- Provisioned, not used. A 100Gi claim holding 2Gi of data bills 100Gi. Start small; you can expand later but never shrink.
- Orphans. Retained volumes and leftover StatefulSet claims keep billing after the app is gone. List them regularly:
kubectl get pvand look forReleased. - Snapshots and IOPS. Snapshots are billed separately, and some disk types charge for provisioned IOPS and throughput.
Hands-on: data that survives
The lab is docked at the bottom of your screen.
Live lab · no install
Data that survives
- Tasks0/7
Find the default StorageClass
It decides what kind of disk a claim gets. On kind it is the node's own disk.
kubectl get storageclassCreate the StatefulSet
notes.yaml gives each pod its own claim from a volumeClaimTemplate.
kubectl apply -f notes.yaml kubectl rollout status statefulset/notesnotes-0 started first, then notes-1: StatefulSets start in order.
See one claim per pod
kubectl get pvcDelete notes-0 and read its file
Same name, same claim: the line written by the first pod is still there.
kubectl delete pod notes-0 kubectl wait --for=condition=Ready pod/notes-0 kubectl exec notes-0 -- cat /data/log.txtCompare with a container's own filesystem
Write a file outside the volume, delete the pod, and look for it.
kubectl exec notes-1 -- sh -c "echo temp > /tmp/scratch.txt" kubectl delete pod notes-1 kubectl wait --for=condition=Ready pod/notes-1 kubectl exec notes-1 -- cat /tmp/scratch.txtGone. Only what is on the volume survives a restart.
Delete the StatefulSet and find what is left
Claims outlive their pods on purpose. On a cloud they also keep billing.
kubectl delete -f notes.yaml kubectl get pvc,pvClean up the volumes
kubectl delete pvc data-notes-0 data-notes-1- Lab complete. Keep experimenting, or press Reset to start again.
- ready
- starting / not ready
- failing
- terminating
Prefer your own machine? The same steps on a local cluster
kind comes with a default StorageClass called standard that creates volumes on the node's disk. Save the manifest as notes.yaml:
apiVersion: v1
kind: Service
metadata:
name: notes
spec:
clusterIP: None
selector:
app: notes
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: notes
spec:
serviceName: notes
replicas: 2
selector:
matchLabels:
app: notes
template:
metadata:
labels:
app: notes
spec:
containers:
- name: app
image: busybox:1.37
command: ["sh", "-c", "echo \"$(hostname) started at $(date)\" >> /data/log.txt; sleep 3600"]
volumeMounts:
- name: data
mountPath: /data
volumeClaimTemplates:
- metadata:
name: data
spec:
accessModes: ["ReadWriteOnce"]
resources:
requests:
storage: 100Mikubectl apply -f notes.yaml
kubectl rollout status statefulset/notes
kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
data-notes-0 Bound pvc-3b1f0c2e-6a4d-4f0e-9c1a-7d2e8b5f4a10 100Mi RWO standard 30s
data-notes-1 Bound pvc-9e7d6c5b-4a3f-4e2d-8c1b-0a9f8e7d6c5b 100Mi RWO standard 20sDelete a pod and check the file it wrote:
kubectl delete pod notes-0
kubectl wait --for=condition=Ready pod/notes-0
kubectl exec notes-0 -- cat /data/log.txt
notes-0 started at Sat Nov 14 10:02:11 UTC 2026
notes-0 started at Sat Nov 14 10:04:37 UTC 2026notes-0- Same name, same claim, so the earlier line is still there.
Mini mission
Find the leftover volumes
Delete the StatefulSet. Predict what remains, check it, then clean up completely.
Reveal one safe solution
kubectl delete -f notes.yaml
kubectl get pvc,pv
kubectl delete pvc data-notes-0 data-notes-1
The claims and volumes survive the StatefulSet. That protects your data, and on a cloud it also keeps billing. Deleting the claims removes the volumes because the class's reclaim policy is Delete.
Check yourself
Quiz
0 of 4 answered