123DevOps··9 min read

Kubernetes storage in practice: volumes, PV, PVC, StorageClass, StatefulSet, and CSI

Ephemeral and projected volumes, PV and PVC, access modes, StorageClass with WaitForFirstConsumer, dynamic provisioning, reclaim policy, StatefulSet with volumeClaimTemplates, EBS CSI with gp3, expansion, snapshots, and VolumeAttributesClass, all tested on a kind cluster.

Kubernetes

Containers are disposable; data isn’t. When a pod restarts, everything in the container’s filesystem is gone, and that’s where Kubernetes volumes come in. In this guide, we start with ephemeral volumes, go through PV, PVC, and StorageClass, and end up at StatefulSets and cloud CSI drivers. The examples ran on a kind cluster with Kubernetes 1.37 (one control plane and two workers), whose default provisioner is rancher.io/local-path; the c- prefix on names is just the lab’s convention.

Ephemeral volumes: emptyDir, configMap, and secret

Not every volume needs to persist. An emptyDir is created empty with the pod and dies with it, which is useful for caches and temporary files shared between containers. A projected volume combines several sources (ConfigMap, Secret, ServiceAccount token, downward API) into a single directory:

apiVersion: v1
kind: Pod
metadata:
  name: volumes-demo
  namespace: c-storage
spec:
  containers:
  - name: app
    image: busybox:1.37
    command: ["sh", "-c", "echo cache > /cache/tmp.txt && sleep 3600"]
    volumeMounts:
    - name: cache
      mountPath: /cache
    - name: config
      mountPath: /etc/app
      readOnly: true
  volumes:
  - name: cache
    emptyDir:
      sizeLimit: 100Mi
  - name: config
    projected:
      sources:
      - configMap:
          name: app-config
      - secret:
          name: app-secret
          items:
          - key: db-password
            path: secrets/db-password
$ kubectl -n c-storage exec volumes-demo -- ls -la /etc/app/
lrwxrwxrwx    1 root     root            32 Nov 25 04:37 ..data -> ..2026_11_25_04_37_55.1437328224
lrwxrwxrwx    1 root     root            21 Nov 25 04:37 app.properties -> ..data/app.properties
lrwxrwxrwx    1 root     root            14 Nov 25 04:37 secrets -> ..data/secrets

The symlinks to ..data are the mechanism that lets the kubelet update mounted ConfigMaps and Secrets atomically. This doesn’t apply to subPath mounts, which don’t receive updates.

PV and PVC: the storage contract

For data that must outlive the pod, Kubernetes separates two roles:

  • PersistentVolume (PV): the actual volume (an EBS disk, a directory on the node, an NFS share), a cluster-level resource;
  • PersistentVolumeClaim (PVC): the application’s request (“I want 1Gi, ReadWriteOnce”), a namespaced resource.

The controller matches the PVC to a compatible PV or asks a provisioner to create one. The pod only references the PVC and doesn’t need to know where the disk comes from. The access mode says how the volume can be mounted:

Access mode Short Meaning
ReadWriteOnce RWO Read-write by a single node (several pods on that node can use it)
ReadOnlyMany ROX Read-only by many nodes
ReadWriteMany RWX Read-write by many nodes (NFS, EFS, CephFS)
ReadWriteOncePod RWOP Read-write by a single pod in the whole cluster

Cloud block disks such as EBS are RWO. If you need RWX, you need a network file system.

StorageClass and dynamic provisioning

The lab’s default StorageClass

A StorageClass describes “a kind of disk”: which provisioner to use, with which parameters, what to do on deletion, and when to create the volume.

kubectl get sc
NAME                 PROVISIONER             RECLAIMPOLICY   VOLUMEBINDINGMODE      ALLOWVOLUMEEXPANSION   AGE
standard (default)   rancher.io/local-path   Delete          WaitForFirstConsumer   false                  10m

The storageclass.kubernetes.io/is-default-class: "true" annotation makes it the default: PVCs without a storageClassName land on it.

WaitForFirstConsumer: Pending until a pod exists

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: app-data
  namespace: c-storage
spec:
  accessModes:
  - ReadWriteOnce
  storageClassName: standard
  resources:
    requests:
      storage: 1Gi
$ kubectl -n c-storage get pvc app-data
NAME       STATUS    VOLUME   CAPACITY   ACCESS MODES   STORAGECLASS   VOLUMEATTRIBUTESCLASS   AGE
app-data   Pending                                      standard       <unset>                 2s

Events:
  Normal  WaitForFirstConsumer  2s    persistentvolume-controller  waiting for first consumer to be created before binding

Pending here isn’t an error. With volumeBindingMode: WaitForFirstConsumer, the volume is only created after the scheduler picks the pod’s node. This avoids the classic problem of creating a disk in one availability zone while the pod lands in another. Immediate mode creates the volume right away, without knowing where the pod will run.

Writing a file that survives the pod

apiVersion: apps/v1
kind: Deployment
metadata:
  name: app
  namespace: c-storage
spec:
  replicas: 1
  strategy:
    type: Recreate        # RWO: the new pod starts only after the old one releases the volume
  selector:
    matchLabels: { app: app }
  template:
    metadata:
      labels: { app: app }
    spec:
      containers:
      - name: app
        image: busybox:1.37
        command: ["sh", "-c", "sleep 3600"]
        volumeMounts:
        - name: app-data
          mountPath: /data
      volumes:
      - name: app-data
        persistentVolumeClaim:
          claimName: app-data

As soon as the pod is created, the PVC goes through ExternalProvisioning, Provisioning, and ProvisioningSucceeded and becomes Bound. We write a file, delete the pod, and read it again from the pod the Deployment recreated:

kubectl -n c-storage get pvc app-data
kubectl -n c-storage exec deploy/app -- sh -c 'echo "order-42 written at $(date -u +%FT%TZ)" > /data/orders.txt'
kubectl -n c-storage delete pod -l app=app
kubectl -n c-storage exec deploy/app -- cat /data/orders.txt
NAME       STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS
app-data   Bound    pvc-1b85c023-cf26-45d3-9bb7-034525719483   1Gi        RWO            standard
pod "app-558fbd96fc-8qkbs" deleted from c-storage namespace
order-42 written at 2026-11-25T04:38:10Z

The data survived. One local-path detail: the PV gets a nodeAffinity for blog-lab-worker, so the pod is pinned to that node. Local disks are fast and simple, but they don’t follow the pod if the node dies; that’s what cloud network and block volumes are for.

StatefulSet with volumeClaimTemplates

A Deployment with several replicas would share the same PVC. Databases and queues need one volume per replica, with a stable identity. That’s the StatefulSet’s job:

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: db
  namespace: c-storage
spec:
  serviceName: db          # headless Service (clusterIP: None) created separately
  replicas: 2
  selector:
    matchLabels: { app: db }
  template:
    metadata:
      labels: { app: db }
    spec:
      containers:
      - name: db
        image: busybox:1.37
        command: ["sh", "-c", "echo \"I am $(hostname)\" >> /data/log.txt && sleep 3600"]
        volumeMounts:
        - name: data
          mountPath: /data
  volumeClaimTemplates:
  - metadata:
      name: data
    spec:
      accessModes: ["ReadWriteOnce"]
      storageClassName: c-local-retain
      resources:
        requests:
          storage: 1Gi

The c-local-retain StorageClass uses the same provisioner, but with reclaimPolicy: Retain (see the next section). Each replica got its own PVC, <template>-<pod>, and when we deleted db-0, the recreated pod mounted the same volume:

NAME        STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS
data-db-0   Bound    pvc-01ae5f66-de29-4686-91c1-cad95aa11c21   1Gi        RWO            c-local-retain
data-db-1   Bound    pvc-a6117201-c184-4477-877f-c19f2e708a94   1Gi        RWO            c-local-retain

$ kubectl -n c-storage exec db-0 -- cat /data/log.txt
I am db-0
I am db-0

The two lines show the two lives of pod db-0 on the same disk. Deleting the StatefulSet doesn’t delete the PVCs; that’s by design.

Reclaim policy: Delete or Retain

The reclaimPolicy decides what happens to the PV when the PVC is deleted:

  • Delete (the default for most StorageClasses): the volume and the disk behind it are deleted;
  • Retain: the PV becomes Released and the disk stays until someone cleans it up by hand.
kubectl -n c-storage delete sts db
kubectl -n c-storage delete pvc data-db-0 data-db-1
kubectl get pv
NAME                                       CAPACITY   RECLAIM POLICY   STATUS     CLAIM                 STORAGECLASS
pvc-01ae5f66-de29-4686-91c1-cad95aa11c21   1Gi        Retain           Released   c-storage/data-db-0   c-local-retain
pvc-1b85c023-cf26-45d3-9bb7-034525719483   1Gi        Delete           Bound      c-storage/app-data    standard
pvc-a6117201-c184-4477-877f-c19f2e708a94   1Gi        Retain           Released   c-storage/data-db-1   c-local-retain

A Released PV isn’t reused on its own: it keeps the reference to the old PVC. To reuse the data, you have to remove the claimRef or create a new PV pointing at the same disk. In the lab, after deleting the PVs, the directories were still on the nodes and had to be removed manually. Use Retain for production data that’s hard to recreate, and monitor orphaned disks, which keep costing money in the cloud.

Volume expansion

Growing a PVC is just a matter of editing spec.resources.requests.storage, as long as the StorageClass has allowVolumeExpansion: true and the driver supports it. In the lab, standard doesn’t allow it:

kubectl -n c-storage patch pvc app-data -p '{"spec":{"resources":{"requests":{"storage":"2Gi"}}}}'
Error from server (Forbidden): persistentvolumeclaims "app-data" is forbidden: only dynamically provisioned pvc can be resized and the storageclass that provisions the pvc must support resize

With cloud CSI drivers and the flag enabled, expansion usually happens while the pod is running, and the kubelet grows the file system. Shrinking a volume isn’t supported.

CSI in the cloud: EBS on EKS

CSI (Container Storage Interface) is the standard for volume drivers: each vendor ships its own driver outside the Kubernetes code. On EKS, the EBS driver (ebs.csi.aws.com, v1.68.1 at the time of writing) is installed as the aws-ebs-csi-driver add-on; on EKS Auto Mode it comes built in (check the docs for the provisioner used in that mode). A typical gp3 StorageClass:

apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: gp3
  annotations:
    storageclass.kubernetes.io/is-default-class: "true"
provisioner: ebs.csi.aws.com
parameters:
  type: gp3
  encrypted: "true"
volumeBindingMode: WaitForFirstConsumer   # creates the volume in the AZ where the pod was scheduled
allowVolumeExpansion: true
reclaimPolicy: Delete
$ kubectl apply -f gp3.yaml --dry-run=client
storageclass.storage.k8s.io/gp3 created (dry run)

The manifest was only validated with --dry-run=client, because the lab has no AWS. If another default class already exists (such as gp2 on older clusters), remove its annotation so you don’t end up with two. To build the cluster, see EKS with Terraform.

Snapshots and VolumeAttributesClass

VolumeSnapshot (snapshot.storage.k8s.io/v1) takes a point-in-time copy of a PVC, but it doesn’t come with the cluster: it requires the CRDs and controller from the external-snapshotter project (v8.7.1 at the time of writing) and a CSI driver that supports it. Group snapshots (VolumeGroupSnapshot), for several volumes consistent with each other, reached GA in Kubernetes 1.36. The lab doesn’t have these CRDs, so the example below wasn’t validated:

apiVersion: snapshot.storage.k8s.io/v1
kind: VolumeSnapshot
metadata:
  name: data-before-migration
  namespace: c-storage
spec:
  volumeSnapshotClassName: ebs-snapclass   # VolumeSnapshotClass created for the driver
  source:
    persistentVolumeClaimName: app-data

VolumeAttributesClass (storage.k8s.io/v1, GA since 1.34) solves a different problem: changing performance attributes, such as IOPS and throughput, of a volume in use by switching the PVC’s volumeAttributesClassName. The API exists in the lab, and the object passed --dry-run=server:

apiVersion: storage.k8s.io/v1
kind: VolumeAttributesClass
metadata:
  name: c-gp3-fast
driverName: ebs.csi.aws.com
parameters:
  iops: "6000"
  throughput: "250"
volumeattributesclass.storage.k8s.io/c-gp3-fast created (server dry run)

Accepted parameters depend on the driver; check the current EBS CSI docs before using it, as well as EBS’s own modification limits. To understand how the scheduler picks the node that triggers provisioning, see scheduling and resources; for a PVC stuck in Pending, see troubleshooting and debugging.

Checklist

  • Data that must persist lives in a PVC, never in the container’s filesystem
  • A single default StorageClass, with WaitForFirstConsumer on multi-AZ clusters
  • reclaimPolicy: Retain for critical data and a routine to clean up orphaned PVs
  • Databases and queues in a StatefulSet with volumeClaimTemplates
  • allowVolumeExpansion: true and a disk usage alert before it fills up
  • Snapshots tested with a restore, not just created

Kubernetes storage is a contract: the PVC says what the application needs, the StorageClass says how to deliver it, and the reclaim policy says what’s left when it’s all over.

Get updates on new posts and stay in the loop!