Kubernetes Volume and PVC
Kubernetes Volumes and PersistentVolumeClaims provide durable storage for containerized workloads, decoupling storage provisioning from Pod lifecycles.
Container filesystems are ephemeral by default — when a container restarts, any data written to it is gone. Kubernetes Volumes solve this by giving Pods access to storage that outlasts the container lifecycle. For durable, cluster-wide storage that persists beyond the Pod lifecycle, Kubernetes uses a three-layer abstraction: PersistentVolume (PV), PersistentVolumeClaim (PVC), and StorageClass.
Storage Concepts
| Concept | Description |
|---|---|
Volume |
A storage resource defined in a Pod spec; tied to the Pod's lifecycle (disappears when Pod is deleted) |
PersistentVolume (PV) |
A cluster-scoped storage resource provisioned by an admin or dynamically by a StorageClass. Its lifecycle is independent of any Pod. |
PersistentVolumeClaim (PVC) |
A namespace-scoped request for storage by a user or application. Kubernetes binds it to a suitable PV. |
StorageClass |
Defines the type of storage (provisioner, parameters, reclaim policy) and enables dynamic provisioning. |
Volume Types
| Type | Use Case |
|---|---|
emptyDir |
Temporary scratch space; shared between containers in a Pod; deleted when the Pod is deleted |
hostPath |
Mounts a file or directory from the node's filesystem; avoid in production (ties Pod to a specific node) |
configMap / secret |
Injects ConfigMap or Secret data as files into a container |
persistentVolumeClaim |
Mounts a PVC (backed by durable cloud or network storage) into a container |
nfs |
Mounts an NFS share |
emptyDir Example
An emptyDir volume is useful for sharing data between a main container and a sidecar, or for caching within a Pod's lifetime:
apiVersion: v1
kind: Pod
metadata:
name: shared-volume-pod
spec:
volumes:
- name: cache-vol
emptyDir: {}
containers:
- name: writer
image: busybox:1.36
command: ['sh', '-c', 'echo "data" > /cache/data.txt && sleep 3600']
volumeMounts:
- name: cache-vol
mountPath: /cache
- name: reader
image: busybox:1.36
command: ['sh', '-c', 'cat /cache/data.txt && sleep 3600']
volumeMounts:
- name: cache-vol
mountPath: /cache
PersistentVolume and PersistentVolumeClaim
PersistentVolume (admin-provisioned)
apiVersion: v1
kind: PersistentVolume
metadata:
name: postgres-pv
spec:
capacity:
storage: 10Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
storageClassName: standard
hostPath:
path: /mnt/data # for local testing only; use cloud storage in production
PersistentVolumeClaim
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: postgres-pvc
namespace: default
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
storageClassName: standard
Using the PVC in a Pod
apiVersion: v1
kind: Pod
metadata:
name: postgres
spec:
volumes:
- name: postgres-storage
persistentVolumeClaim:
claimName: postgres-pvc
containers:
- name: postgres
image: postgres:16
env:
- name: POSTGRES_PASSWORD
valueFrom:
secretKeyRef:
name: postgres-secret
key: POSTGRES_PASSWORD
volumeMounts:
- name: postgres-storage
mountPath: /var/lib/postgresql/data
Dynamic Provisioning with StorageClass
With a StorageClass, you do not need to pre-create PVs. Kubernetes automatically provisions a new volume when a PVC is created:
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: app-data-pvc
namespace: default
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 20Gi
storageClassName: gp3 # use the StorageClass name from your cloud provider
kubectl get storageclass
# NAME PROVISIONER RECLAIMPOLICY VOLUMEBINDINGMODE AGE
# gp3 (default) ebs.csi.aws.com Delete WaitForFirstConsumer 10d
# standard kubernetes.io/no-provisioner Retain Immediate 10d
kubectl get pvc
# NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
# app-data-pvc Bound pvc-abc123-def456 20Gi RWO gp3 10s
Access Modes
| Mode | Abbreviation | Meaning |
|---|---|---|
ReadWriteOnce |
RWO | Mounted read-write by one node at a time |
ReadOnlyMany |
ROX | Mounted read-only by many nodes |
ReadWriteMany |
RWX | Mounted read-write by many nodes (requires NFS or similar) |
ReadWriteOncePod |
RWOP | Mounted read-write by exactly one Pod (Kubernetes 1.22+) |
Reclaim Policies
| Policy | Behavior after PVC is deleted |
|---|---|
Delete |
PV and the underlying storage are deleted automatically |
Retain |
PV is kept (in a Released state); must be manually reclaimed |
Recycle |
Deprecated; do not use |
Common Mistakes
- Deleting a StatefulSet without deleting PVCs. StatefulSet PVCs are not deleted automatically when you delete the StatefulSet. You must delete them manually. If left behind, they continue to consume storage and may prevent new StatefulSet Pods from binding to a fresh volume.
- Using
hostPathin production. AhostPathvolume binds a Pod to a specific node. If the Pod is rescheduled to another node, the data is gone. Use cloud-backed PVCs or network storage instead. - Not setting a
StorageClass. If nostorageClassNameis specified and no defaultStorageClassexists, the PVC will remainPendingindefinitely. - Using
ReadWriteManywhen you need it but your provider does not support it. AWS EBS (gp2/gp3) only supportsReadWriteOnce. ForReadWriteMany, you need NFS, AWS EFS, or Azure Files. - Forgetting that
emptyDirdata is lost when a Pod is deleted. It is shared between containers in a Pod, but it is not persistent storage. Use a PVC if the data must survive Pod restarts.
Best Practices
- Use dynamic provisioning via
StorageClassinstead of manually creating PVs. - Set the
persistentVolumeReclaimPolicytoRetainfor production databases;Deleteis convenient but risks accidental data loss. - Use
StatefulSetwithvolumeClaimTemplatesfor stateful applications — it creates a dedicated PVC for each replica automatically. - Always request only the storage you need; over-provisioning wastes money, and under-provisioning causes application errors.
- Monitor PVC usage and set up alerts before volumes fill up, as a full volume can cause Pod crashes and data corruption.
Interview Questions
1. What is the difference between a Volume, a PersistentVolume, and a PersistentVolumeClaim?
A Volume is defined in a Pod spec and shares the Pod's lifecycle. A PersistentVolume (PV) is a cluster-scoped storage resource whose lifecycle is independent of any Pod — it persists after the Pod is deleted. A PersistentVolumeClaim (PVC) is a namespace-scoped request by an application for a PV matching its size and access mode requirements. Kubernetes binds the PVC to a suitable PV.
2. What is dynamic provisioning and how does it work?
With dynamic provisioning, when a PVC is created with a storageClassName, Kubernetes calls the provisioner specified in the StorageClass (e.g., ebs.csi.aws.com for AWS EBS) to automatically create a new PV and bind it to the PVC. This eliminates the need to pre-create PVs manually.
3. What are PV access modes?
ReadWriteOnce (RWO) allows the volume to be mounted read-write by a single node. ReadOnlyMany (ROX) allows read-only mounts by many nodes. ReadWriteMany (RWX) allows read-write mounts by many nodes — supported by NFS, EFS, and Azure Files, but not by block storage like AWS EBS.
4. What happens to PVCs when a StatefulSet is deleted?
PVCs created by a StatefulSet's volumeClaimTemplates are not deleted when the StatefulSet is deleted. They persist and must be deleted manually. This is intentional — it protects data from accidental deletion. If you want the data removed, explicitly delete the PVCs after deleting the StatefulSet.
5. What is the persistentVolumeReclaimPolicy?
It controls what happens to a PV when its PVC is deleted. Delete removes the PV and underlying storage (common for cloud volumes with automated cleanup). Retain keeps the PV in a Released state, preserving the data for manual recovery or rebinding. Retain is recommended for production databases.
6. What is an emptyDir volume and when would you use it?
An emptyDir volume is created when a Pod is assigned to a node and exists as long as the Pod runs. It is shared between all containers in the Pod and is useful for temporary data sharing (e.g., a sidecar reading files produced by the main container) or caching. The data is deleted when the Pod is deleted; it is not persistent storage.
7. How do you resize a PersistentVolumeClaim?
Edit the PVC's spec.resources.requests.storage to a larger value using kubectl edit pvc <name> or kubectl patch. The StorageClass must have allowVolumeExpansion: true. After the PVC is updated, the volume expands automatically (for most CSI drivers). For file systems like ext4/xfs, the resize happens when the Pod remounts the volume or restarts.
Summary
- Container filesystems are ephemeral; use
PersistentVolumeClaimbacked by aStorageClassfor durable storage. - PVs are cluster-scoped; PVCs are namespace-scoped requests that Kubernetes binds to PVs.
- Dynamic provisioning via
StorageClassis the standard approach and eliminates manual PV management. emptyDiris for temporary inter-container sharing;persistentVolumeClaimis for data that must outlast the Pod.- Deleting a StatefulSet does not delete its PVCs — clean them up manually to avoid orphaned storage costs.
Continue Learning
Previous: Namespace | Next: StatefulSet
Was this guide helpful?
CodeWithVenu is free to read, with no paywall. If this guide saved you time or helped you prepare, a small voluntary contribution helps me write more practical tutorials, architecture guides, and interview-prep content.