Skip to Content

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 , founding engineer running Kubernetes in production. Updated .

pod: db-0newpostgrespod: db-0deletedpostgres ✕PVC data-db-010Gi, ReadWriteOnceStorageClassprovisionsPersistentVolumebound to data-db-0the actual diskEBS, local-path
The chapter in one pictureThe pod is disposable. The claim, the volume and the disk outlive it.

By the end you can

0 of 5

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.
    pvc.yaml
    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 modeMeansTypical backend
    ReadWriteOnce (RWO)One node mounts it read-writeEBS, Persistent Disk, local disks
    ReadWriteOncePod (RWOP)Exactly one pod, cluster-wideCSI block storage
    ReadWriteMany (RWX)Many nodes read-write at onceEFS, NFS, CephFS
    ReadOnlyMany (ROX)Many nodes, read-onlyShared 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

    1. Poduses a claim
    2. PVCI need 10Gi
    3. StorageClassgp3, Delete
    4. CSI drivertalks to the cloud
    5. PVthe disk
    6. Kubeletmounts it

    Step 0 of 6

    Press Play, or step through with Next.

    storageclass.yaml
    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-0 always gets claim data-notes-0 back.
    • 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 pv and look for Released.
    • Snapshots and IOPS. Snapshots are billed separately, and some disk types charge for provisioned IOPS and throughput.

    Hands-on: data that survives

    Live lab · no install

    Data that survives

    kind-learn
    1. Tasks0/7
    2. Find the default StorageClass

      It decides what kind of disk a claim gets. On kind it is the node's own disk.

      kubectl get storageclass
    3. Create the StatefulSet

      notes.yaml gives each pod its own claim from a volumeClaimTemplate.

      kubectl apply -f notes.yaml
      kubectl rollout status statefulset/notes

      notes-0 started first, then notes-1: StatefulSets start in order.

    4. See one claim per pod
      kubectl get pvc
    5. Delete 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.txt
    6. Compare 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.txt

      Gone. Only what is on the volume survives a restart.

    7. 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,pv
    8. Clean up the volumes
      kubectl delete pvc data-notes-0 data-notes-1
    9. Lab complete. Keep experimenting, or press Reset to start again.
    ↑↓ history · Tab complete · Ctrl+C stop
    Cluster map live
    • 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:

    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: 100Mi
    Terminal
    kubectl 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       20s

    Delete a pod and check the file it wrote:

    Terminal
    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 2026
    notes-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
    Terminal
    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

    1. 1. An app writes uploads to an emptyDir volume. Which event loses them?
    2. 2. Why use volumeBindingMode: WaitForFirstConsumer for cloud disks?
    3. 3. You delete a StatefulSet. What happens to its PersistentVolumeClaims by default?
    4. 4. For a small team, what is usually the better home for a production PostgreSQL database?

    Further reading