Volumes, PersistentVolumes, and StorageClasses: State That Survives Restarts

Persistent volumes in Kubernetes: PV, PVC and StorageClass, dynamic provisioning, access modes, reclaim policies, expansion and backups.

Executive Summary: Pods are disposable, so they lose anything they write unless it lives in a volume that outlives them. Pod-scoped volumes such as emptyDir survive container restarts but not pod deletion. For real persistence, a pod requests storage with a PersistentVolumeClaim; a StorageClass tells Kubernetes how to provision a matching PersistentVolume on demand, such as a cloud disk. Get three things right and most storage trouble disappears: the access mode, the zone the disk lives in, and the reclaim policy that decides whether deleting a claim deletes your data.
Isometric PersistentVolume cylinder stack linked to a claim card and a pod, with a StorageClass cloud above

A team moves a small service that stores uploaded files on local disk into Kubernetes. Everything works until the first deployment, when the uploads vanish: the new pods started with an empty filesystem. They add a PersistentVolumeClaim and scale to three replicas, and two of the pods stay stuck in ContainerCreating with a “Multi-Attach error”. They switch the cluster to a new node pool in another availability zone, and the pod can no longer start at all because its disk is in the old zone. Each of these is a storage concept, not a bug.

Kubernetes storage has two halves: volumes, which are directories mounted into a pod’s containers, and the persistent volume subsystem, which manages storage whose lifecycle is independent of any pod. The concepts mirror Docker volumes, adapted for a cluster where pods move between machines.

Pod-scoped volumes

Volume Lifetime Use
emptyDir As long as the pod exists on its node Scratch space, caches, sharing files between containers in a pod
emptyDir with medium: Memory Same, stored in RAM and counted against memory Fast temporary files, sensitive temporary data
configMap, secret, projected Managed by Kubernetes Configuration files and credentials
hostPath The node’s filesystem Node agents only; a security risk and not portable
persistentVolumeClaim Independent of the pod Durable data

An emptyDir survives container crashes and restarts within the pod, but Kubernetes deletes it when the pod goes away or moves to another node. That is the right behavior for a cache and the wrong one for user data.

PersistentVolumes, claims and StorageClasses

  • A PersistentVolume (PV) is a piece of storage in the cluster: a cloud disk, an NFS export, a volume on a storage array. It is a cluster-scoped object with a size, access modes and a reclaim policy.
  • A PersistentVolumeClaim (PVC) is a namespaced request for storage: “I need 20 GiB, mountable read-write by one node, of class fast-ssd“. Pods reference claims, never volumes directly.
  • A StorageClass describes a kind of storage and the provisioner that creates it. With dynamic provisioning, creating a claim causes the provisioner to create a new disk and a matching PV automatically.
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: fast-ssd
provisioner: ebs.csi.aws.com          # the cloud's CSI driver
parameters:
  type: gp3
reclaimPolicy: Retain
allowVolumeExpansion: true
volumeBindingMode: WaitForFirstConsumer
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: uploads
  namespace: billing
spec:
  storageClassName: fast-ssd
  accessModes: ["ReadWriteOnce"]
  resources:
    requests:
      storage: 20Gi
      volumes:
        - name: uploads
          persistentVolumeClaim:
            claimName: uploads
      containers:
        - name: api
          volumeMounts:
            - name: uploads
              mountPath: /app/uploads

Storage drivers today are almost all CSI (Container Storage Interface) drivers, maintained by cloud providers and storage vendors outside the Kubernetes codebase. Most clusters have a default StorageClass, used when a claim omits storageClassName; kubectl get storageclass marks it.

Access modes

Mode Meaning Typical storage
ReadWriteOnce (RWO) Mounted read-write by one node at a time (several pods on that node may share it) Cloud block disks such as EBS, Persistent Disk, Azure Disk
ReadWriteOncePod (RWOP) Mounted read-write by exactly one pod in the whole cluster Block disks, with CSI support
ReadOnlyMany (ROX) Mounted read-only by many nodes Shared file systems, pre-populated data
ReadWriteMany (RWX) Mounted read-write by many nodes Network file systems such as EFS, Filestore, Azure Files, NFS

Most cloud disks are ReadWriteOnce, which explains the Multi-Attach error above: three replicas on three nodes cannot all attach one disk. ReadWriteOncePod, which became generally available in Kubernetes 1.29, is stricter and safer for databases, guaranteeing a single writer. If several replicas genuinely need the same files, use a shared file system with ReadWriteMany, or better, object storage such as S3, which suits uploads far better than any volume.

A Deployment with a single ReadWriteOnce volume also has a rollout problem: the default rolling update starts the new pod before stopping the old one, and if they land on different nodes, the new pod waits for a disk that stays attached elsewhere. Use strategy: Recreate for such Deployments, or a StatefulSet, which suits per-pod storage.

Zones: storage has a location

Cloud block disks live in one availability zone and can only attach to nodes in that zone. Two settings prevent most zone problems:

  • volumeBindingMode: WaitForFirstConsumer delays creating the disk until the scheduler places a pod that uses the claim, so the disk appears in the pod’s zone. With the older Immediate mode, the provisioner might create the disk in a zone where the pod cannot run.
  • As a result, once a disk exists, the scheduler can only place pods that use it in its zone. Make sure every zone your storage uses has node capacity, especially when changing node pools.

Reclaim policy: who deletes the data

Then, when you delete a claim, its PV’s reclaim policy decides what happens. Delete, the default for most dynamically provisioned storage classes, deletes the PV and the underlying cloud disk. Retain keeps both, releasing the PV for manual recovery. Deleting a namespace deletes its claims, so with Delete, removing a namespace removes every disk in it. Use Retain for storage classes that hold important data, and accept the small cost of cleaning up retained volumes by hand.

Growing, snapshotting and backing up

  • Expansion: with allowVolumeExpansion: true, editing a claim’s requested size grows the disk and, for most drivers, the file system online. However, you cannot shrink volumes.
  • Snapshots: the VolumeSnapshot API, supported by most CSI drivers, takes point-in-time snapshots of a claim and can create new claims from them, which is useful before risky migrations.
  • Backups: snapshots in the same account and region are not a full backup strategy. Tools such as Velero back up both Kubernetes objects and volume data to another location. For databases, the database’s own backup tooling, or a managed database service, remains the most reliable option.

Frequently Asked Questions

What is the difference between a PersistentVolume and a PersistentVolumeClaim?

A PersistentVolume is an actual piece of storage in the cluster. A PersistentVolumeClaim is a namespaced request for storage of a certain size, access mode and class. Pods use claims, and Kubernetes binds each claim to a suitable volume, creating one dynamically through a StorageClass if needed.

What does a StorageClass do?

It describes a type of storage and the provisioner that creates it, including parameters such as disk type, the reclaim policy, whether you can expand volumes and when binding happens. As a result, claims that name a StorageClass get a volume automatically.

Why do I get a Multi-Attach error?

A ReadWriteOnce volume, such as a cloud block disk, can attach to only one node at a time. A second pod on a different node, from extra replicas or a rolling update, cannot attach it. Use one replica with the Recreate strategy, a StatefulSet, or ReadWriteMany storage.

Does deleting a PVC delete my data?

With the Delete reclaim policy, which is the default for most dynamic storage classes, yes: Kubernetes deletes the volume and the underlying disk. With Retain, Kubernetes keeps the disk for manual recovery.

Decide where state lives

The easiest storage to run on Kubernetes is storage you do not run on Kubernetes: managed databases and object storage. For what remains, request storage with claims, define StorageClasses with WaitForFirstConsumer and a deliberate reclaim policy, match the access mode to how many pods write, and back up data somewhere a cluster mistake cannot reach. For the full reference, see the PersistentVolumes documentation and the StorageClass documentation.

Originally posted on 2 April 2024 and post last modified on 2 April 2024

DevOps Kubernetes Persistent Volumes Storage

Share this article

Leave a Reply

Your email address will not be published. Required fields are marked *