liveBook · Manning

Persistent volume claim

As explained in Kubernetes in Action, Second Edition

A persistentVolumeClaim volume is a pod volume definition that references a PersistentVolumeClaim by name.

A persistentVolumeClaim volume lets a pod use persistent storage by naming a PersistentVolumeClaim (PVC). Create the claim separately, then reference it in the pod’s `spec.volumes`. The pod does not refer directly to a PersistentVolume (PV): the claim is bound to one, and the pod’s container mounts the volume at a chosen path. section 10.1.1 section 10.2.2

The claim and the pod volume definition have different jobs:

  • The PVC requests a minimum capacity and an access mode. It can name a StorageClass or use the cluster’s default. Kubernetes binds it to a matching PV or provisions a new one. section 10.1.1 section 10.2.1
  • The pod’s `persistentVolumeClaim` definition needs a `claimName` matching the PVC’s name. Its optional `readOnly` field forces all mounts of that volume to be read-only. section 10.2.2
  • A container uses `volumeMounts` to choose where the volume appears in its filesystem. The PVC’s lifecycle is typically separate from the pod’s, so replacing a pod need not mean replacing its claim. section 10.2.2 section 10.1.1
Listing 10.3 A pod using a persistentVolumeClaim volume
apiVersion: v1
kind: Pod
metadata:
  name: quiz
spec:
  volumes:
  - name: quiz-data
    persistentVolumeClaim:      #1
      claimName: quiz-data      #1
  containers:
  - name: quiz-api
    image: luksa/quiz-api:0.1
    ports:
    - name: http
      containerPort: 8080
  - name: mongo
    image: mongo
    volumeMounts:             #2
    - name: quiz-data         #2
      mountPath: /data/db     #2

See the two references in one pod manifest: `claimName: quiz-data` selects the PVC, while the MongoDB container mounts the named pod volume at `/data/db`. section 10.2.2

Check the binding

After creating the pod, run `kubectl get pvc quiz-data` and `kubectl get pv`. In the worked example, both resources show `Bound`; the PVC reports a 1Gi volume and `ReadWriteOncePod` access. Whether binding waits for a pod depends on the StorageClass’s volume binding mode. section 10.2.2 section 10.2.5

This volume type can also reference a claim for a pre-provisioned, node-local PV. In a StatefulSet, each replica instead receives its own PVC from a template and remains associated with that claim when its pod is recreated. Neither the pod volume definition nor its `claimName` specifies how the underlying storage is provided. section 10.3.2 section 16.1.2 section 10.1.1

How Kubernetes in Action, Second Edition covers it

How deep it goes
Shows the pod fields, container mount, and binding checks; leaves storage provisioning to the claim, StorageClass, and provisioner.
Examples
Uses a quiz pod manifest that mounts a claimed volume into MongoDB, followed by `kubectl get pvc` and `kubectl get pv` checks.
What it assumes
Introduces claims, volumes, and their relationship to pods before demonstrating the reference; assumes a PVC has been created for the example.
What you can do after
Apply it via a pod manifest to mount an existing claim, then verify that the claim and volume are bound.
How it connects
Links PersistentVolumeClaims with PersistentVolumes, StorageClasses, container volume mounts, and StatefulSets.

Also explained in

Compare how 9 books explain persistent volume claim →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Kubernetes in Action, Second Edition. The section links point to the parts of the book it draws on.