Persistent volumes and claims

Kubernetes · lesson 13 of 32 · 4 min read

Give a pod storage that outlives it using PVCs, StorageClasses and the correct access mode.

Open this lesson in the learning hub

Key points

  • A container filesystem is wiped on every restart. A PersistentVolumeClaim asks for storage that survives.
  • You write the PVC, and a StorageClass provisions the real disk on demand. Hand-made PVs are rare on cloud.
  • ReadWriteOnce means one node at a time, which is all a cloud block disk can do.
  • Many pods writing at once needs a file share instead: NFS, EFS or Azure Files with ReadWriteMany.
  • Reclaim policy Delete destroys the disk when the PVC goes. Use Retain for anything you would miss.
  • A PVC that stays Pending almost always means no default StorageClass or no capacity in that zone.

Example

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: uploads
spec:
  accessModes:
    - ReadWriteOnce
  storageClassName: standard-rwo
  resources:
    requests:
      storage: 20Gi
---
# mount it in the pod template
spec:
  containers:
    - name: app
      image: ghcr.io/acme/web:1.4.2
      volumeMounts:
        - name: uploads
          mountPath: /var/data
  volumes:
    - name: uploads
      persistentVolumeClaim:
        claimName: uploads

Ask for storage with a PVC and let the StorageClass do the plumbing.

This is a reading copy. The full lesson — with the visual explainer, the interactive lab and a Run button for the code — lives in the Kubernetes course, and every lesson in it is listed on the Kubernetes contents page.