Kubernetes persistent volumes: a Pod, a claim and a disk
DevOps Engineering

Kubernetes Persistent Volumes: PV, PVC and StorageClass

How Kubernetes PVs, PVCs and StorageClasses fit together: YAML, access modes, reclaim policies, debugging.

Michael Zion

21 min read

A Kubernetes persistent volume is storage that outlives the Pod that uses it. You get one through 3 objects: a PersistentVolumeClaim (PVC) asks for storage, a PersistentVolume (PV) is the storage, and a StorageClass creates the PV when the claim shows up. 5 things to know before you put data on one:

Bottom line

  1. PVC = the request. PV = the record of a real disk. App teams write claims. The cluster creates the volume
  2. The StorageClass decides what you get. Disk type, when the disk is created, whether it can grow, and what happens to the data when the claim is deleted
  3. The default is to delete your data. Dynamically created volumes have the reclaim policy Delete. Delete the claim and the disk goes with it
  4. ReadWriteOnce does NOT mean 1 Pod. It means 1 node. Only ReadWriteOncePod limits a volume to 1 Pod
  5. A disk usually lives in 1 zone. Use a StorageClass with WaitForFirstConsumer, so the disk is created where the Pod runs

A claim and a Pod that uses it, on Kubernetes v1.37:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: data
spec:
  accessModes: ["ReadWriteOnce"]
  resources:
    requests:
      storage: 10Gi
---
apiVersion: v1
kind: Pod
metadata:
  name: web
spec:
  containers:
  - name: nginx
    image: nginx:1.29
    volumeMounts:
    - name: data
      mountPath: /usr/share/nginx/html
  volumes:
  - name: data
    persistentVolumeClaim:
      claimName: data

The claim has no storageClassName, so it uses the default StorageClass of the cluster. Check that one exists with kubectl get storageclass. With no default, the claim and the Pod stay Pending.

The 5 commands you will use most:

kubectl get pvc
kubectl describe pvc data
kubectl get pv
kubectl get storageclass
kubectl describe pod web

Disks nobody remembers creating, and a storage bill that only goes up? Get a free DevOps assessment: a senior engineer reviews your CI/CD, infra, Kubernetes, cloud cost and on-call setup.

Find your situation:

PV vs PVC: the request and the record

Storage in Kubernetes takes 3 objects and 1 field in the Pod. Each one answers 1 question:

ObjectQuestion it answersScopeWho writes it
Volume in the Pod specWhere does the container see the files?The PodThe app team
PersistentVolumeClaim (PVC)How much storage, and which kind?A namespaceThe app team
PersistentVolume (PV)Which real disk is it?The clusterThe StorageClass, or an admin
StorageClassHow are disks created?The clusterThe platform team

The Pod names a claim. The claim is bound to a volume. The volume points at a disk that lives outside the cluster: an EBS volume, a Persistent Disk, an Azure Disk, an NFS share.

The claim is the request. The volume is the record of a real disk.

Kubernetes PV vs PVC: a Pod uses a PersistentVolumeClaim, the claim is bound to a PersistentVolume created by a StorageClass, and the PersistentVolume points to a disk outside the cluster
Pod, claim, volume, disk. The StorageClass creates the volume.

The split exists so that the app team never has to know how storage is provided. A claim says "10Gi, ReadWriteOnce". It does not say which cloud, which disk ID or which zone. The same manifest works on every cluster that has a default StorageClass.

3 rules follow from the scopes:

  • A Pod can only use a claim from its own namespace
  • A PV has no namespace. An admin sees all of them with kubectl get pv
  • A dynamically created PV is named after the claim's UID, like pvc-b6efd8da-b7b5-11e6-9d58-0ed433a7dd94. You almost never type that name

My rule: app teams write claims, the platform team writes StorageClasses, and nobody writes PVs by hand.

What survives a restart, a deleted Pod and a dead node

Before you add a claim, check that you need one. Kubernetes has 3 places to put files, and a claim comes in 2 kinds. They differ in what they survive:

Where the files areContainer restartPod deletedNode lost
The container filesystemLostLostLost
An emptyDir volumeKeptLostLost
A claim on a network diskKeptKeptKept
A claim on a local volumeKeptKeptLost, or out of reach until the node is back

An emptyDir is created when the Pod lands on a node and is deleted for good when the Pod leaves that node. A container crash does not remove the Pod, so the data is safe across crashes.

That makes emptyDir the right place for caches, scratch space and files shared between containers of 1 Pod. A claim is for data you want back after the Pod is gone.

2 volume types look like shortcuts and cost you later:

  • hostPath mounts a directory of the node. The Kubernetes docs warn that it carries many security risks, and a hostPath PV only works on a single node cluster. Keep it for local testing
  • local volumes are real PVs on a node's own disk. They are fast, they need a nodeAffinity, they cannot be created dynamically, and when the node is unhealthy the data is out of reach

How a claim finds a volume

There are 2 ways for a volume to exist:

  1. Static: an admin creates PVs ahead of time, and claims pick from them
  2. Dynamic: nobody creates anything ahead of time. A claim arrives, and the StorageClass creates a disk and a PV for it

Dynamic is the normal way on any cloud. Static is for storage that already exists: an NFS export, a disk restored from a backup, a local SSD.

Either way, a control loop binds the claim to a volume. The binding rules:

  • 1 to 1. A volume is bound to 1 claim, and no other claim can have it
  • At least what you asked for. A 3Gi claim can bind a 10Gi volume, and kubectl get pvc then shows 10Gi
  • The class must match. A volume of class fast only binds a claim of class fast
  • The access mode must be offered. A ReadWriteMany claim does not bind a volume that only offers ReadWriteOnce
  • The volume mode must match. Filesystem (the default) gives you a directory. Block gives you a raw device
  • No match, no binding. The claim stays Pending until a matching volume appears

A bound claim looks like this:

kubectl get pvc data

NAME   STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
data   Bound    pvc-b6efd8da-b7b5-11e6-9d58-0ed433a7dd94   10Gi       RWO            fast           30s

storageClassName: empty and missing are 2 different things

This 1 field has 3 behaviors, and the difference matters:

In the claimWhat happens
storageClassName: fastThe class fast creates the volume
The field is missingThe default StorageClass is used. With no default, the claim waits, and it gets the class once a default appears
storageClassName: ""No dynamic provisioning. The claim only binds a pre-created PV that has no class

So "" is the way to say "I want that existing volume with no class, do not create a new one". Pair it with volumeName to pick the exact PV:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: restored-data
spec:
  storageClassName: ""
  volumeName: restored-pv
  accessModes: ["ReadWriteOnce"]
  resources:
    requests:
      storage: 100Gi

Without the "", the default class is filled in, the classes no longer match, and the claim stays Pending.

Access modes count nodes, with 1 exception

There are 4 access modes:

ModeShortWhat it allows
ReadWriteOnceRWORead and write from 1 node. Several Pods on that node can all use it
ReadOnlyManyROXRead only from many nodes
ReadWriteManyRWXRead and write from many nodes
ReadWriteOncePodRWOPRead and write from 1 Pod in the whole cluster

Only ReadWriteOncePod limits the volume to 1 Pod.

Kubernetes access modes: ReadWriteOnce (RWO) is 1 node, ReadOnlyMany (ROX) is many nodes read only, ReadWriteMany (RWX) is many nodes read and write, ReadWriteOncePod (RWOP) is 1 Pod in the whole cluster
3 modes count nodes. 1 counts Pods.

3 things about access modes that surprise people:

  1. RWO is about nodes. 2 Pods on the same node can both write to a ReadWriteOnce volume. If your database assumes it is alone, RWO does not protect it
  2. The modes do not enforce write protection. Kubernetes uses them to match claims with volumes. Once a volume is mounted, a ReadOnlyMany volume is not guaranteed to be read only
  3. The disk decides what is possible. A normal block disk (EBS, Azure Disk) attaches to 1 node. For ReadWriteMany you need a file service: EFS on AWS, Filestore on Google Cloud, Azure Files on Azure, or NFS

ReadWriteOncePod is stable since Kubernetes v1.29 and only works with CSI drivers. The Kubernetes tutorial on persistent volumes recommends it over ReadWriteOnce for production.

I treat ReadWriteOnce as a scheduling hint and ReadWriteOncePod as a lock. For a database, I want the lock.

ReadWriteMany looks like the easy answer for "3 replicas, 1 folder". It works, and you pay for it with a network filesystem: slower small writes and file locking that your app has to tolerate. If the app can use object storage (S3, GCS) for shared files, that is usually simpler.

The StorageClass decides what disk you get

A StorageClass is a template for disks. A claim names a class, and the class creates the volume.

apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: fast
  annotations:
    storageclass.kubernetes.io/is-default-class: "true"
provisioner: ebs.csi.aws.com
parameters:
  type: gp3
reclaimPolicy: Delete
allowVolumeExpansion: true
volumeBindingMode: WaitForFirstConsumer

The annotation makes this class the default. Leave it out if the cluster already has a default you want to keep.

The fields, with the defaults:

FieldWhat it doesDefault
provisionerThe driver that creates the disk. RequiredNone
parametersPassed to the driver: disk type, IOPS, encryption. Each driver has its own listNone
reclaimPolicyWhat happens to the disk when the claim is deletedDelete
allowVolumeExpansionWhether a claim of this class can growOff
volumeBindingModeWhen the disk is createdImmediate
allowedTopologiesLimits the zones. Rarely needed with WaitForFirstConsumerAll zones

3 of these defaults are wrong for production data: Delete, no expansion and Immediate. Set all 3 on purpose when you write the class.

You cannot fix them later. The API rejects a change to reclaimPolicy, volumeBindingMode, provisioner or parameters on an existing class. Only allowVolumeExpansion can be switched on afterwards. To change the rest, create a new class and move the default annotation to it.

The driver names you will see: ebs.csi.aws.com for Amazon EBS, pd.csi.storage.gke.io for Google Persistent Disk and disk.csi.azure.com for Azure Disk. The old drivers that were built into Kubernetes are gone: EBS and Azure Disk were removed in v1.27, and GCE PD in v1.28.

The default StorageClass

A claim with no storageClassName gets the default class. A class is the default when it carries the annotation storageclass.kubernetes.io/is-default-class: "true".

kubectl get storageclass

The default has (default) next to its name. 3 cases to know:

  • 0 defaults: a claim without a class waits. Since v1.28 it gets the class when a default appears, without you touching it
  • 1 default: what you want
  • 2 or more defaults: the most recently created one is used. Keep it that way only while you migrate

To move the default from one class to another:

kubectl patch storageclass standard -p '{"metadata": {"annotations":{"storageclass.kubernetes.io/is-default-class":"false"}}}'
kubectl patch storageclass fast -p '{"metadata": {"annotations":{"storageclass.kubernetes.io/is-default-class":"true"}}}'

This affects new claims, and claims that are still waiting with no class. Bound claims and existing volumes keep the class they were created with.

WaitForFirstConsumer: the disk follows the Pod

A cloud disk usually lives in 1 zone. A Pod can only use it from a node in that zone. The binding mode decides who picks the zone:

  • Immediate: the disk is created as soon as the claim exists, before anybody knows where the Pod will run
  • WaitForFirstConsumer: the claim waits until a Pod uses it. The scheduler picks a node first, using the Pod's requests, node selectors, affinity rules and taints. Then the disk is created

With WaitForFirstConsumer, the disk is created in the zone of the Pod.

Kubernetes volumeBindingMode: with Immediate the disk is created in zone a as soon as the claim exists and a Pod that cannot run there stays Pending, with WaitForFirstConsumer the disk is created in the zone of the Pod
Schedule the Pod first, then create the disk.

With Immediate, the Pod has to fit in whatever zone the disk landed in. When that zone has no room, or an anti-affinity rule sends the Pod elsewhere, the Pod stays Pending with a scheduler message about the volume's node affinity.

3 things to expect with WaitForFirstConsumer:

  1. A new claim sits in Pending with the event waiting for first consumer to be created before binding. That is normal. Create the Pod
  2. Do not set nodeName on the Pod. It skips the scheduler, and the claim stays Pending. Use a nodeSelector on kubernetes.io/hostname
  3. The zone is picked once. After a zonal disk exists, the Pod is tied to that zone for as long as it uses that volume. Zone-redundant disks (the built-in AKS classes in a multi-zone cluster, regional disks on GKE) are the exception

Number 3 is the part people forget. A stateful Pod cannot move to another zone when its own zone runs out of nodes. Plan capacity per zone for anything with a disk.

The reclaim policy decides what happens to your data

Every PV has a reclaim policy. It answers 1 question: the claim was deleted, now what?

PolicyWhen the claim is deletedDefault for
DeleteThe PV is removed, and so is the disk in the cloudDynamically created volumes
RetainThe PV stays, in the phase Released. The disk and the data stayPVs created by hand
RecycleDeprecated. Do not use itNothing

So on a default cloud setup, kubectl delete pvc data deletes the data. So does kubectl delete namespace, because the claims go with the namespace.

(A wrong kubectl context is the usual way this happens.)

You can change the policy of a volume that already exists:

kubectl patch pv pvc-b6efd8da-b7b5-11e6-9d58-0ed433a7dd94 -p '{"spec":{"persistentVolumeReclaimPolicy":"Retain"}}'

For new volumes, use a StorageClass with reclaimPolicy: Retain. The field cannot be changed on an existing class, so this means a new class. Volumes that already exist keep their policy: patch each of them with the command above.

Retain keeps the disk and the bill until you delete both by hand.

Kubernetes reclaim policies when a PersistentVolumeClaim is deleted: with Delete the volume and the disk are deleted, with Retain the volume is Released and the disk keeps the data and is still billed
Delete removes the disk. Retain keeps it.

A Released volume is not available to a new claim, because the old data is still on it. You have 2 options:

  • Throw it away: delete the PV, then delete the disk in the cloud console. Deleting the PV alone leaves the disk
  • Use it again: remove the claimRef from the PV, and it becomes Available. Then bind it with a claim that sets volumeName
kubectl patch pv pvc-b6efd8da-b7b5-11e6-9d58-0ed433a7dd94 -p '{"spec":{"claimRef":null}}'

2 things to get right when you reuse a volume:

  • The storageClassName of the new claim has to equal the class on the PV. For a volume that a class created, that is the class name. Use "" only when the PV has no class
  • An Available volume can be taken by any claim that matches it. Create your claim right after the patch, or set the claimRef of the PV to the namespace and name of your new claim to reserve it

My rule: Retain for anything I would be sad to lose, Delete for everything else, and backups for both. Retain protects you from a deleted claim. It does nothing for a dropped table.

A claim in use is not deleted right away

Kubernetes will not pull a disk from under a running Pod. If you delete a claim that a Pod still uses, the claim shows Terminating and stays there until the Pod is gone. The same goes for a PV that is bound to a claim.

The delete is only postponed. When the Pod goes away (a restart, a rollout, a node drain), the claim is removed, and with the Delete policy the disk too. If the delete was a mistake, patch the PV to Retain before anything restarts the Pod.

The mechanism is a finalizer: kubernetes.io/pvc-protection on the claim and kubernetes.io/pv-protection on the volume.

2 more finalizers guard the Delete policy. On older clusters, deleting the PV before the claim could leave the disk behind in the cloud, with nothing in the cluster pointing at it. The fix is stable since v1.33: the disk is removed whatever the order.

The phases you will see

ObjectPhaseMeaning
PVAvailableFree, no claim yet
PVBoundBound to a claim
PVReleasedThe claim was deleted, the data is still there
PVFailedThe automatic reclaim failed
PVCPendingNo volume yet
PVCBoundReady to use
PVCLostThe volume it was bound to no longer exists

Grow a volume by editing the claim

When a disk fills up, you edit the claim and ask for more:

kubectl patch pvc data -p '{"spec":{"resources":{"requests":{"storage":"20Gi"}}}}'

The rules:

  • The StorageClass needs allowVolumeExpansion: true, and the driver has to support it
  • The same disk is resized. No new PV is created
  • A claim that a Pod is using grows online, with no restart, when the driver supports online expansion. Most cloud disk drivers do. A claim that no Pod uses finishes growing when a Pod mounts it
  • The filesystem has to be XFS, Ext3 or Ext4
  • You cannot shrink a volume. Ever
  • Never edit the size on the PV. Kubernetes then assumes the resize was done by hand and skips it

If the cloud refuses the new size (a quota, a size the disk type does not allow), the resize is retried forever. You can then lower the request to a smaller value, as long as it is still above the current size, and the resize is tried again. This recovery is stable since v1.34.

The full procedure, with the checks before and after, is in our guide on resizing persistent volumes without downtime.

One more detail on size: 10Gi is 10 times 1024 cubed bytes, and 10G is 10 times 1000 cubed. And the request is the size of the disk. The space you can write to is a little lower, because the filesystem takes its share.

Snapshots and clones copy a volume

A VolumeSnapshot is a point in time copy of a volume. It works like claims and volumes do: a VolumeSnapshot is the request, a VolumeSnapshotContent is the real snapshot, and a VolumeSnapshotClass says how to make it.

apiVersion: snapshot.storage.k8s.io/v1
kind: VolumeSnapshot
metadata:
  name: data-snap
spec:
  volumeSnapshotClassName: csi-snapclass
  source:
    persistentVolumeClaimName: data

To restore, you create a new claim that names the snapshot as its source:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: data-restored
spec:
  storageClassName: fast
  dataSource:
    name: data-snap
    kind: VolumeSnapshot
    apiGroup: snapshot.storage.k8s.io
  accessModes: ["ReadWriteOnce"]
  resources:
    requests:
      storage: 10Gi

A restore never overwrites the old volume. You get a new claim, and you point the app at it.

3 things to check before you count on snapshots:

  1. Snapshots are not part of core Kubernetes. They are custom resources, and they need a snapshot controller in the cluster and a CSI driver that supports them. Run kubectl get volumesnapshotclass to see if your cluster has them
  2. A snapshot covers a deleted claim. It does not cover a deleted namespace: the VolumeSnapshot lives in the namespace, and with deletionPolicy: Delete on the VolumeSnapshotClass the snapshot in the cloud is deleted with it. Use deletionPolicy: Retain for snapshots you count on. And a snapshot usually lives in the same cloud account as the disk, so it does not cover a deleted account
  3. A snapshot of a running database is only as consistent as a power cut. Test a restore

A clone skips the snapshot. The new claim names another claim as its dataSource, with kind: PersistentVolumeClaim. The source has to be in the same namespace, and the clone has to be at least as large as the source. It is handy for a copy of production data in a test Pod.

A Pod with a disk: Deployment or StatefulSet

All replicas of a Deployment share 1 claim. That gives you 2 cases:

  • Several replicas on 1 claim: works only with ReadWriteMany storage. With a ReadWriteOnce disk, the replicas on other nodes cannot attach it
  • 1 replica on 1 claim: works, with a catch on every rollout

The catch: a rolling update starts the new Pod before it stops the old one. If the new Pod lands on another node, it waits for a disk that the old Pod still holds, and the old Pod is not stopped until the new one is ready. The rollout hangs.

With ReadWriteOncePod it happens on every rollout. The scheduler refuses the new Pod on every node, the old node included, and the new Pod stays Pending with node(s) unavailable due to PersistentVolumeClaim with ReadWriteOncePod access mode already in-use by another pod.

2 ways out:

  1. Set strategy.type: Recreate on the Deployment. The old Pod stops first, at the price of a short downtime on each release
  2. Use a StatefulSet. Its volumeClaimTemplates give every replica its own claim, and it replaces Pods 1 at a time

For anything with more than 1 replica and a disk per replica, the StatefulSet is the only option. The details are in our Kubernetes StatefulSet guide, and the rollout mechanics are in the Kubernetes Deployment guide.

fsGroup can make a big volume mount slowly

A fresh disk is owned by root. A container that runs as another user cannot write to it. The usual fix is fsGroup in the Pod's security context:

spec:
  securityContext:
    runAsUser: 1000
    fsGroup: 2000
    fsGroupChangePolicy: OnRootMismatch

With fsGroup, Kubernetes changes the group of the files on the volume when it mounts it. By default it walks every file, on every mount. On a volume with millions of files, the Pod sits in ContainerCreating for minutes, and the events show a mount that timed out.

fsGroupChangePolicy: OnRootMismatch only walks the files when the top directory has the wrong owner. In practice that means once. It is stable since v1.23, and I add it every time I set fsGroup.

2 related mount options:

  • readOnly: true on a volume mount makes it read only for that container. Other containers can still write
  • subPath mounts 1 folder of the volume, which lets 2 containers share a disk without seeing each other's files

Storage classes on EKS, GKE and AKS

CloudClasses you getBinding modeShared files (ReadWriteMany)
EKSEKS Auto Mode creates no StorageClass. You write one, with the provisioner ebs.csi.eks.amazonaws.com. Without Auto Mode, you install the EBS CSI driver (ebs.csi.aws.com) and write a gp3 class for it. Older clusters may still carry a gp2 classYours to set. The AWS sample uses WaitForFirstConsumer and gp3The EFS CSI driver
GKEstandard-rwo (balanced disk) and premium-rwo (SSD). ReadWriteOnce claims default to standard-rwo. Google's driver page still describes an older default class on Standard clusters, so check yoursWaitForFirstConsumer on standard-rwoFilestore. From GKE 1.37.1, ReadWriteMany claims default to regional-rwx
AKSdefault and managed-csi (Standard SSD), managed-csi-premium (Premium SSD)WaitForFirstConsumer, with reclaim policy Delete and expansion onAzure Files

Do not trust a table for your own cluster, this one included. Run this and read 4 columns: the provisioner, the reclaim policy, the binding mode and the expansion flag.

kubectl get storageclass

2 notes for AWS. The Auto Mode provisioner and the standard EBS CSI driver manage their volumes separately, and moving volumes from one to the other goes through snapshots. And EBS volumes do not work on Fargate.

Limits, quotas and the bill

A node takes a limited number of disks

Every node has a maximum number of volumes it can attach. The CSI driver reports the number, and the scheduler respects it. Some numbers from the docs:

  • AWS: most Nitro instance types support 28 attachments, shared between EBS volumes, network interfaces and NVMe instance store volumes. An m5.xlarge with 1 network interface takes 27 EBS volumes, the root volume included. Each extra network interface takes 1 away, and EKS nodes usually have several
  • Google Cloud: up to 127 volumes per node, depending on the node type
  • Azure: up to 64 disks per node, depending on the node type

Many small Pods with 1 disk each on a few large nodes will hit this before they hit CPU. The Pod then stays Pending with node(s) exceed max volume count.

Quotas stop a namespace from eating the storage

A ResourceQuota can cap storage per namespace:

apiVersion: v1
kind: ResourceQuota
metadata:
  name: storage
spec:
  hard:
    persistentvolumeclaims: "10"
    requests.storage: 200Gi
    fast.storageclass.storage.k8s.io/requests.storage: 50Gi

That allows 10 claims and 200Gi in total, of which at most 50Gi on the class fast. A LimitRange of type PersistentVolumeClaim adds a minimum and a maximum size per claim.

Volumes without a Pod still cost money

A claim lives longer than the workload that created it. Delete a Deployment, scale down a StatefulSet, and the claims stay. So do the disks, and AWS and Azure both state it plainly: a disk that is no longer attached is still charged until you delete it.

Kubernetes v1.37 makes these easier to find. Claims now carry an Unused condition (beta, on by default) with the time since no Pod has used them:

kubectl get pvc -A -o custom-columns='NS:.metadata.namespace,NAME:.metadata.name,UNUSED:.status.conditions[?(@.type=="Unused")].status,SINCE:.status.conditions[?(@.type=="Unused")].lastTransitionTime'

And list the volumes whose claim is already gone:

kubectl get pv | grep Released

I run both once a month. There is always something.

What changed for volumes from v1.33 to v1.37

VersionChangeWhy you care
v1.33The fix for PVs deleted before their claim is stableA Delete policy volume no longer leaves an orphan disk in the cloud
v1.33The scheduler message for a zone mismatch changed to node(s) didn't match PersistentVolume's node affinityOlder guides and alerts search for volume node affinity conflict
v1.34Recovery from a failed volume expansion is stableYou can lower a resize request that the cloud refused
v1.34VolumeAttributesClass is stableChange the IOPS or throughput of a live volume by editing the claim, when the driver supports it (the EBS CSI driver does)
v1.36Volume group snapshots are stableSnapshot several volumes at the same point in time
v1.37The Unused condition on claims is beta and on by defaultFind claims that no Pod uses, and since when
v1.37SELinux mount labeling is on by default, for CSI drivers that opt inOn SELinux nodes, 2 Pods with different labels sharing a volume can fail to start

Debug a PVC: 11 symptoms and the first command

Start with kubectl describe pvc for a claim and kubectl describe pod for a Pod. The events at the bottom name the cause.

SymptomLikely causeFirst command
Claim Pending: no persistent volumes available for this claim and no storage class is setNo default StorageClass, and no pre-created PV that matcheskubectl get storageclass
Claim Pending: waiting for first consumer to be created before bindingNormal with WaitForFirstConsumer. No Pod uses the claim yetCreate the Pod
Claim Pending with a ProvisioningFailed eventThe driver could not create the disk: missing cloud permissions, a quota, a wrong parameterkubectl describe pvc data
Pod Pending: pod has unbound immediate PersistentVolumeClaimsThe claim is not bound. The problem is on the claimkubectl describe pvc data
Pod Pending: node(s) didn't match PersistentVolume's node affinity (before v1.33: node(s) had volume node affinity conflict)The disk is in a zone where the Pod cannot runkubectl describe pv, and compare with the zones of your nodes
Pod Pending: node(s) exceed max volume countThe nodes are at their attach limitkubectl describe pod web
Pod Pending during a rollout: PersistentVolumeClaim with ReadWriteOncePod access mode already in-use by another podA Deployment with a rolling update and a ReadWriteOncePod claimSet strategy.type: Recreate
Pod in ContainerCreating: Multi-Attach error for volumeThe disk is still attached to another node: an old Pod is still running, or a node died without detaching. After a node failure, expect about 6 minutes before the disk is force detachedkubectl get pods -o wide
Pod in ContainerCreating: a mount that timed outfsGroup on a volume with a huge number of filesSet fsGroupChangePolicy: OnRootMismatch
Claim stuck in TerminatingA Pod still uses itkubectl describe pvc data, and read the "Used By" line
Claim LostThe PV object is gone: somebody removed its finalizer to force a delete, or the cluster was restored without itkubectl get pv

For the Pod-level statuses (CrashLoopBackOff, ImagePullBackOff, OOMKilled), use the table in our Kubernetes Pod guide.

Checklist before a volume takes real data

  • The claim names its StorageClass, or you checked which class is the default
  • The class uses WaitForFirstConsumer
  • The class has allowVolumeExpansion: true
  • The reclaim policy is a decision: Retain for data you cannot recreate
  • The access mode is ReadWriteOncePod where 1 writer is the assumption
  • A Deployment with a ReadWriteOnce or ReadWriteOncePod claim uses the Recreate strategy, or it is a StatefulSet
  • fsGroupChangePolicy: OnRootMismatch is set next to fsGroup
  • Each zone has enough node capacity for the Pods whose disks live there
  • The nodes have room under their volume attach limit
  • A ResourceQuota caps storage per namespace
  • Backups run outside the volume, and a restore was tested
  • An alert fires before the disk is full
  • Somebody looks for unused claims and Released volumes every month

FAQ

What is a persistent volume in Kubernetes?

A PersistentVolume (PV) is a piece of storage in the cluster whose life does not depend on any Pod. It points at a real disk or file share, and a Pod uses it through a PersistentVolumeClaim.

What is the difference between a PV and a PVC?

A PVC is a request for storage, written by the app team inside a namespace: a size, an access mode and a class. A PV is the cluster-wide object that represents the real disk. A claim is bound to exactly 1 volume.

What does PVC stand for in Kubernetes?

PersistentVolumeClaim. It is the object a Pod refers to when it needs storage that survives the Pod.

What is the difference between a volume and a persistent volume?

A volume is a directory mounted into a Pod. Some volume types, like emptyDir, are deleted with the Pod. A persistent volume is a separate object that stays when the Pod is deleted.

How do I create a persistent volume in Kubernetes?

On a cluster with a StorageClass, you create a PersistentVolumeClaim and the volume is created for you. Creating a PV by hand is only needed for storage that already exists, like an NFS share.

Why is my PVC stuck in Pending?

The 3 common causes: the cluster has no default StorageClass, the class uses WaitForFirstConsumer and no Pod uses the claim yet, or the driver failed to create the disk. kubectl describe pvc shows which one in its events.

What happens to the data when I delete a PVC?

It depends on the reclaim policy of the volume. With Delete, the default for dynamically created volumes, the disk and the data are deleted. With Retain, the volume moves to Released and the data stays.

Can 2 Pods use the same PVC?

Yes, when they are in the same namespace and the access mode allows it. With ReadWriteOnce they have to run on the same node. With ReadWriteMany they can run on different nodes. With ReadWriteOncePod only 1 Pod can use it.

A recap

If you take away 1 thing, let it be this: the claim is yours, the disk is the cloud's, and the StorageClass is the contract between them.

Everything in this guide follows from that:

  • A claim asks, a volume records, a class creates
  • The access mode counts nodes, except ReadWriteOncePod
  • The binding mode picks the zone
  • The reclaim policy decides if a deleted claim is a deleted database
  • Claims outlive workloads, and so does the bill

You can set all of this up yourself with the YAML and the checklist above.

If you would like a second pair of eyes on your storage setup, get a free DevOps assessment. Next reads: Kubernetes StatefulSet, Kubernetes Pod and Kubernetes Deployment.