🎓 A1Learner
🔍
0 of 51 lessons completed

Kubernetes: Persistent Volumes and Claims

A Persistent Volume is a disk that lives in the cluster. A Persistent Volume Claim is a pod asking for storage. The claim is what keeps the data safe when the pod dies.

A claim for one gigabyte

A claim asks for a size and a storage class. The cluster finds or creates a volume that fits. The pod never names a real disk, only the claim. The requests lines name what the Pod asks the cluster for.

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: db-data
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi
  storageClassName: standard

The claim is bound

A bound claim has a volume behind it. The volume is ready to mount. A claim that stays pending means the cluster has no volume to give it.

kubectl get pvc db-data

Mount the claim in a pod

A pod mounts the claim at a path. The app writes to that path as if it were a local folder. The data lives on the volume, not on the pod.

volumeMounts:
  - name: db-storage
    mountPath: /var/lib/postgresql/data
volumes:
  - name: db-storage
    persistentVolumeClaim:
      claimName: db-data

Example

kubectl apply -f db-pvc.yaml
kubectl get pvc db-data

Key points

  • PersistentVolume: a disk in the cluster that a pod can use
  • PersistentVolumeClaim: a request for a disk, naming the size and the class
  • StorageClass: the kind of disk the cluster hands out, like standard on minikube
  • Use a PVC when data must survive a pod restart or a pod replacement
  • Use emptyDir when data only needs to live as long as one pod
  • Use hostPath only for data that belongs to one node, like a node log

In this example, the first command creates a PersistentVolumeClaim called db-data from the file. The second command reads it back. The claim is bound to a volume and has 1Gi of storage, so a pod can now mount it.

Note

a PVC is a claim, not a disk, so it must be bound before a pod can mount it

Try it Yourself

Edit the code, then press Run.

Running is simulated · Rust Playground

Exercise

Why does a database need a PersistentVolumeClaim instead of writing to its own pod disk?