Configuration, secrets, storage and access · 5 MIN READ

Volumes and persistent storage: emptyDir, PersistentVolumeClaims and StorageClasses

Choose between ephemeral and persistent volumes, request storage with a PersistentVolumeClaim, read binding and provisioning status, and diagnose a pod stuck Pending on its claim.

Everything a container writes to its own filesystem disappears when the container is replaced. Volumes give pods storage with a defined lifetime: some last as long as the pod, others outlive every pod that ever used them. Most storage incidents come from not knowing which lifetime you asked for, or from a claim that can never be bound.

kubernetes · A MAP OF THE IDEA

  1. Pick a lifetime: pod or beyond
  2. Choose class and access mode
  3. Claim storage with a PVC
  4. Check the reclaim policy

Useful when

  • Pods request storage without knowing the backend.
  • Claims and their data survive pod replacement.

Watch out

  • The Delete reclaim policy erases data with the claim.
  • ReadWriteOnce volumes cannot be shared across nodes.

Implementation & operation cost: Provisioned disks cost money until their claims, and with Retain their PVs, are deleted.

01

Ephemeral storage: the container layer and emptyDir

A container's writable layer is discarded when the container restarts, so a crash loop also wipes every file it wrote. An emptyDir volume is created empty when the pod is assigned to a node, survives container restarts, is shared by all containers in the pod, and is deleted when the pod leaves the node for any reason. It suits caches, scratch space and files handed from an init container or sidecar to the main container. medium: Memory puts it on tmpfs, which is fast but counts against the containers' memory limits, and sizeLimit lets the kubelet evict a pod whose emptyDir grows too large. None of this survives the pod being replaced or rescheduled; for that you need a PersistentVolume.

02

PersistentVolume, PersistentVolumeClaim and StorageClass

A PersistentVolume (PV) is a cluster-scoped object that represents real storage: a cloud disk, an NFS export, a directory on a node. A PersistentVolumeClaim (PVC) is a namespaced request for storage with a size and an access mode, and a pod refers only to the claim. A StorageClass names a provisioner and its parameters. With dynamic provisioning, a claim that names a class makes the provisioner create a matching PV, and the two are bound one to one. A claim without storageClassName gets the default class, marked by the storageclass.kubernetes.io/is-default-class annotation, while storageClassName: "" opts out and binds only to a pre-created PV. kind installs one default class, standard, backed by the local-path provisioner.

yaml
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: retained
provisioner: rancher.io/local-path
reclaimPolicy: Retain
volumeBindingMode: WaitForFirstConsumer
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: reports
spec:
  storageClassName: retained
  accessModes: ["ReadWriteOnce"]
  resources:
    requests:
      storage: 2Gi
Read the code

The class uses kind's local-path provisioner but keeps volumes after their claim is deleted. The claim asks that class for 2Gi of ReadWriteOnce storage. Because of WaitForFirstConsumer it stays Pending until a pod that uses it is scheduled; only then is a PV created on that pod's node and bound to the claim.

03

Access modes and reclaim policy

Access modes describe how a volume may be attached, not file permissions. ReadWriteOnce (RWO) allows read-write mounting by one node, so several pods on that node can share it; ReadOnlyMany (ROX) allows many nodes to mount it read-only; ReadWriteMany (RWX) needs a shared filesystem such as NFS, CephFS or a cloud file service; ReadWriteOncePod (RWOP) restricts the volume to a single pod in the whole cluster and works only with CSI drivers. Block-storage classes usually offer RWO only. The reclaim policy decides what happens to a dynamically provisioned PV when its claim is deleted: Delete removes the volume and its data, while Retain keeps both and moves the PV to Released, where an administrator must recover or clean up the data before the storage is reused.

04

WaitForFirstConsumer and claims stuck Pending

With volumeBindingMode: Immediate a volume is provisioned as soon as the claim exists, possibly in a zone or on a node where the pod can never run. WaitForFirstConsumer delays provisioning until the scheduler has placed a pod that uses the claim, so the volume is created where that pod lands; until then the claim is Pending with the event waiting for first consumer to be created before binding, which is normal. Abnormal cases have recognisable messages. A pod whose claim does not exist fails scheduling with persistentvolumeclaim "data" not found. A claim that names a missing StorageClass collects ProvisioningFailed events, and its pod reports pod has unbound immediate PersistentVolumeClaims. A claim still used by a pod keeps the kubernetes.io/pvc-protection finalizer and stays Terminating until that pod is gone.

05

StatefulSet claims outlive their pods

A StatefulSet's volumeClaimTemplates create one claim per replica, named from the template, the set and the ordinal: data-cache-0, data-cache-1. When cache-0 is replaced or rescheduled it gets the same claim and therefore the same data back. By default neither deleting the pods nor deleting the StatefulSet removes these claims, which protects a database from a careless delete and also leaves storage you keep paying for. persistentVolumeClaimRetentionPolicy, stable since Kubernetes 1.32, can set whenDeleted and whenScaled to Delete for data that is disposable. Scaling down and back up with the default Retain reattaches the existing claims.

yaml
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: cache
spec:
  serviceName: cache
  replicas: 2
  selector:
    matchLabels:
      app: cache
  persistentVolumeClaimRetentionPolicy:
    whenDeleted: Retain
    whenScaled: Delete
  template:
    metadata:
      labels:
        app: cache
    spec:
      containers:
      - name: redis
        image: redis:7
        volumeMounts:
        - name: data
          mountPath: /data
  volumeClaimTemplates:
  - metadata:
      name: data
    spec:
      accessModes: ["ReadWriteOnce"]
      resources:
        requests:
          storage: 1Gi
Read the code

Each replica gets its own 1Gi claim from the default class, mounted at /data: data-cache-0 and data-cache-1. whenScaled: Delete removes the claim of a replica that a scale-down removes, while whenDeleted: Retain keeps every claim if the StatefulSet itself is deleted, so recreating the set finds its data again.

06

Try it in the lab terminal

Inspect the StorageClass kind installs, then apply a claim together with a pod that uses it, plus an emptyDir for scratch files. A second claim names a StorageClass that does not exist, so its pod cannot be scheduled; read the events of both objects to see where each part of the problem is reported. Finally delete the pod and then the claim, and watch what the Delete reclaim policy does to the volume.

bash
kind create cluster
kubectl get storageclass
cat > data.yaml <<'EOF'
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: data
spec:
  accessModes: ["ReadWriteOnce"]
  resources:
    requests:
      storage: 1Gi
---
apiVersion: v1
kind: Pod
metadata:
  name: writer
spec:
  containers:
  - name: app
    image: busybox
    command: ["sleep", "3600"]
    volumeMounts:
    - name: data
      mountPath: /data
    - name: scratch
      mountPath: /tmp/scratch
  volumes:
  - name: data
    persistentVolumeClaim:
      claimName: data
  - name: scratch
    emptyDir: {}
EOF
kubectl apply -f data.yaml
kubectl wait --for=condition=Ready pod/writer
kubectl get pvc,pv
cat > fast.yaml <<'EOF'
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: fast-data
spec:
  storageClassName: fast-ssd
  accessModes: ["ReadWriteOnce"]
  resources:
    requests:
      storage: 5Gi
---
apiVersion: v1
kind: Pod
metadata:
  name: fast-writer
spec:
  containers:
  - name: app
    image: busybox
    command: ["sleep", "3600"]
    volumeMounts:
    - name: data
      mountPath: /data
  volumes:
  - name: data
    persistentVolumeClaim:
      claimName: fast-data
EOF
kubectl apply -f fast.yaml
cluster tick 10s
kubectl events --for pvc/fast-data
kubectl events --for pod/fast-writer
kubectl delete pod writer
kubectl get pvc data
kubectl delete pvc data
cluster tick 30s
kubectl get pv
Read the code

get storageclass shows standard (default), provisioner rancher.io/local-path, reclaim policy Delete and WaitForFirstConsumer. Once writer is Ready, get pvc,pv shows data Bound to a generated pvc-<uid> volume of 1Gi, RWO, claimed by default/data. fast-writer stays Pending: the claim's events say ProvisioningFailed, storageclass.storage.k8s.io "fast-ssd" not found, and the pod's say pod has unbound immediate PersistentVolumeClaims. Deleting writer leaves the claim Bound, because claims outlive pods. Deleting the claim lets the Delete policy remove the volume, so the last command prints No resources found.

TAKE THIS WITH YOU

Use emptyDir for data that may vanish with the pod and a PersistentVolumeClaim for data that must outlive it. Check the StorageClass, access mode and reclaim policy before relying on a volume, and read the claim's events when a pod stays Pending.

Check your understanding

A pod stays Pending with "pod has unbound immediate PersistentVolumeClaims", and its claim shows ProvisioningFailed events. What is the likely cause?

Go to the source

Progress stays in this browser. No account needed.