Skip to main content

Persistent Volumes

Persistent Volumes (PV) and Persistent Volume Claims (PVC) provide a way for Pods to use durable storage that persists beyond the Pod lifecycle.

  • PersistentVolume (PV): a piece of storage provisioned by an administrator or dynamically via a StorageClass
  • PersistentVolumeClaim (PVC): a request for storage by a user; binds to a PV that satisfies the claim

PersistentVolume Exampleโ€‹

apiVersion: v1
kind: PersistentVolume
metadata:
name: my-pv
spec:
capacity:
storage: 10Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
hostPath:
path: /data/my-pv

PersistentVolumeClaim Exampleโ€‹

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: my-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 5Gi

Using a PVC in a Podโ€‹

spec:
containers:
- name: app
image: my-app:latest
volumeMounts:
- name: data
mountPath: /app/data
volumes:
- name: data
persistentVolumeClaim:
claimName: my-pvc

Access Modesโ€‹

  • ReadWriteOnce (RWO): the volume can be mounted as read-write by a single node
  • ReadOnlyMany (ROX): the volume can be mounted as read-only by many nodes
  • ReadWriteMany (RWX): the volume can be mounted as read-write by many nodes
  • ReadWriteOncePod (RWOP): the volume can be mounted as read-write by a single Pod

Reclaim Policiesโ€‹

  • Retain: the PV is kept after the PVC is deleted; data must be manually cleaned up
  • Delete: the PV and its underlying storage are deleted when the PVC is deleted
  • Recycle (deprecated): basic scrub (rm -rf /volume/*) before making the PV available again

StorageClassesโ€‹

  • Enable dynamic provisioning of PVs
  • When a PVC requests a StorageClass, the provisioner automatically creates a PV
  • Eliminates the need for administrators to pre-provision PVs
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: fast
provisioner: kubernetes.io/aws-ebs
parameters:
type: gp3
reclaimPolicy: Delete

A PVC can request a specific StorageClass:

spec:
storageClassName: fast
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 20Gi