PersistentVolumes and claims

Storage that outlives the Pod.

Beginner20 min · lesson 18 of 21
Lesson files
The scripts, test data and local test servers this lesson uses, exactly as they ran on the lab machine (5 files, 1 KB): pvc.tar.gz. The lab VM shares no folders with your computer, so fetch them inside the VM: cd ~/lab && curl -fsSLO https://secopslog.com/lab-files/k8s-fund/pvc.tar.gz && tar -xzf pvc.tar.gz, which creates ~/lab/pvc/. SHA-256: adee882ca1e760cdace016e34665f37bf52d40afd3b0c219f08e31b935c029af

An emptyDir volume from lesson 17 (Volumes) survives a container restart, but it is deleted together with its Pod. A database, an upload folder or a queue needs more than that. Its data has to be there after the Pod is deleted, replaced by a rollout or moved to another node.

Kubernetes splits that job in two. A PersistentVolume (PV) is a piece of real storage: a cloud disk, an NFS share, or a folder on a node. A PersistentVolumeClaim (PVC) is your app's request for storage: "1 GiB, writable". You write the claim, and Kubernetes finds or creates a volume to match it. That way the app's YAML never names a particular disk.

Who makes the volumes

Nobody wants to create disks by hand for every claim. A StorageClass names a provisioner, a program that creates a volume whenever a claim asks for one. kind comes with one:

ubuntu@secopslog-k8s:~/lab/pvc · Kubernetes 1.37
$ kubectl get storageclass
NAME PROVISIONER RECLAIMPOLICY VOLUMEBINDINGMODE ALLOWVOLUMEEXPANSION AGE standard (default) rancher.io/local-path Delete WaitForFirstConsumer false 22s

There is one class called standard, marked (default), so any claim that names no class gets it. Its provisioner is rancher.io/local-path, which makes a folder on a node's disk. RECLAIMPOLICY is Delete: when the claim is deleted, the volume and its data go too. VOLUMEBINDINGMODE is WaitForFirstConsumer, and the next step shows what that means.

A claim waits for a Pod

This claim asks for 1 GiB with access mode ReadWriteOnce:

pvc.yaml
# Ask for 1 GiB that one node at a time can mount read-write.
# No storageClassName, so the cluster's default StorageClass is used.
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: data
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
ubuntu@secopslog-k8s:~/lab/pvc · Kubernetes 1.37
$ kubectl apply -f pvc.yaml && kubectl get pvc data
persistentvolumeclaim/data created NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS VOLUMEATTRIBUTESCLASS AGE data Pending standard <unset> 0s
$ kubectl events --for pvc/data
LAST SEEN TYPE REASON OBJECT MESSAGE 0s Normal WaitForFirstConsumer PersistentVolumeClaim/data waiting for first consumer to be created before binding

The claim is Pending and the VOLUME column is empty. That is not an error. The event says the provisioner is waiting for first consumer to be created before binding. A folder on a node is only useful on that node, so creating it before the scheduler has picked a node for the Pod could put the data in the wrong place. Cloud disks have the same problem with zones. Many cloud StorageClasses wait in the same way.

Now give the claim a consumer. The Pod mounts it at /data:

app.yaml
# A Pod that mounts the claim "data" at /data.
apiVersion: v1
kind: Pod
metadata:
name: app
spec:
terminationGracePeriodSeconds: 1
containers:
- name: shell
image: busybox:1.38.0
command: ["sleep", "3600"]
volumeMounts:
- name: data
mountPath: /data
volumes:
- name: data
persistentVolumeClaim:
claimName: data
ubuntu@secopslog-k8s:~/lab/pvc · Kubernetes 1.37
$ kubectl apply -f app.yaml && kubectl wait --for=condition=Ready pod/app --timeout=120s
pod/app created pod/app condition met
$ kubectl get pvc data && kubectl get pv
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS VOLUMEATTRIBUTESCLASS AGE data Bound pvc-826fbd44-4edf-435b-b008-edca41678e7b 1Gi RWO standard <unset> 8s NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS VOLUMEATTRIBUTESCLASS REASON AGE pvc-826fbd44-4edf-435b-b008-edca41678e7b 1Gi RWO Delete Bound default/data standard <unset> 3s
$ kubectl get pod app -o wide && kubectl describe pv | grep -A 4 "Node Affinity"
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES app 1/1 Running 0 9s 10.244.2.3 lab-worker2 <none> <none> Node Affinity: Required Terms: Term 0: kubernetes.io/hostname in [lab-worker2] Message: Source:

Within seconds the claim went to Bound, and a PV named pvc- plus a long ID appeared, 1Gi, RWO, reclaim policy Delete, claimed by default/data. You never wrote that PV: the provisioner made it. Its Node Affinity names the node the Pod landed on, here lab-worker2. From now on, any Pod that uses this claim has to run on that node, because that is where the folder lives.

Data that outlives the Pod

ubuntu@secopslog-k8s:~/lab/pvc · Kubernetes 1.37
$ kubectl exec app -- sh -c 'echo "written by the first Pod" > /data/note.txt && ls -l /data'
total 4 -rw-r--r-- 1 root root 25 Nov 11 05:47 note.txt
$ kubectl delete pod app && kubectl get pvc data
pod "app" deleted from default namespace NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS VOLUMEATTRIBUTESCLASS AGE data Bound pvc-826fbd44-4edf-435b-b008-edca41678e7b 1Gi RWO standard <unset> 12s
$ kubectl apply -f app.yaml && kubectl wait --for=condition=Ready pod/app --timeout=120s && kubectl exec app -- cat /data/note.txt
pod/app created pod/app condition met written by the first Pod

The file was written, the Pod was deleted, and the claim stayed Bound. A new Pod from the same YAML read back written by the first Pod. The data belongs to the claim, not to the Pod, so it is there for whichever Pod mounts the claim next.

ReadWriteOnce means one node

ReadWriteOnce sounds like "one Pod at a time". It is not. It means the volume can be mounted read-write by one node at a time, and any number of Pods on that node may share it. A second Pod with the same claim shows it. app2.yaml is app.yaml with the Pod renamed to app2:

app2.yaml
# A second Pod that mounts the claim "data" at /data.
apiVersion: v1
kind: Pod
metadata:
name: app2
spec:
terminationGracePeriodSeconds: 1
containers:
- name: shell
image: busybox:1.38.0
command: ["sleep", "3600"]
volumeMounts:
- name: data
mountPath: /data
volumes:
- name: data
persistentVolumeClaim:
claimName: data
ubuntu@secopslog-k8s:~/lab/pvc · Kubernetes 1.37
$ kubectl apply -f app2.yaml && kubectl wait --for=condition=Ready pod/app2 --timeout=120s && kubectl get pods -o wide
pod/app2 created pod/app2 condition met NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES app 1/1 Running 0 1s 10.244.2.4 lab-worker2 <none> <none> app2 1/1 Running 0 0s 10.244.2.5 lab-worker2 <none> <none>
$ kubectl exec app2 -- cat /data/note.txt
written by the first Pod

Both Pods are Running, both on lab-worker2, and the second one reads the first one's file. The scheduler put app2 there because the volume's node affinity left it no choice. If you really need one Pod only, Kubernetes has the access mode ReadWriteOncePod. ReadWriteMany lets Pods on different nodes share a volume, but only storage built for that, such as NFS, supports it. The local-path provisioner does not.

Deleting the claim

ubuntu@secopslog-k8s:~/lab/pvc · Kubernetes 1.37
$ PV=$(kubectl get pvc data -o jsonpath="{.spec.volumeName}") kubectl delete pod app app2 && kubectl delete pvc data && kubectl wait --for=delete "pv/$PV" --timeout=2m kubectl get pvc,pv
pod "app" deleted from default namespace pod "app2" deleted from default namespace persistentvolumeclaim "data" deleted from default namespace persistentvolume/pvc-826fbd44-4edf-435b-b008-edca41678e7b condition met No resources found

kubectl wait --for=delete returned condition met for the PV, and kubectl get pvc,pv found nothing. Deleting the claim deleted the volume and the data, exactly as the Delete reclaim policy says. With the policy Retain, the PV would stay behind in the Released state with its data, waiting for an admin. Most dynamic StorageClasses default to Delete, so treat deleting a PVC like deleting the data.

Hands-on

Find out where a volume really lives on kind. This file holds a 500Mi claim called notes and a Pod that writes one line into it:

notes.yaml
# Hands-on: a claim and a Pod that writes one line into it, then waits.
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: notes
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 500Mi
---
apiVersion: v1
kind: Pod
metadata:
name: notes
spec:
terminationGracePeriodSeconds: 1
containers:
- name: shell
image: busybox:1.38.0
command: ["sh", "-c", "echo remember the milk > /notes/todo.txt && sleep 3600"]
volumeMounts:
- name: notes
mountPath: /notes
volumes:
- name: notes
persistentVolumeClaim:
claimName: notes

1. Apply it, wait for the Pod, and check the claim: kubectl apply -f notes.yaml && kubectl wait --for=condition=Ready pod/notes --timeout=120s && kubectl get pvc notes. kubectl wait --for=condition=Ready returns as soon as the Pod is ready.

2. Save the name of the Pod's node in a variable: NODE=$(kubectl get pod notes -o jsonpath="{.spec.nodeName}"), and print it with echo "node: $NODE".

3. List the provisioner's folder inside that node's container, then print the file: docker exec "$NODE" ls /var/local-path-provisioner, then docker exec "$NODE" sh -c "cat /var/local-path-provisioner/*/todo.txt".

4. Note the PV name, delete both objects and wait for the PV to go: PV=$(kubectl get pvc notes -o jsonpath="{.spec.volumeName}"), then kubectl delete -f notes.yaml && kubectl wait --for=delete "pv/$PV" --timeout=2m.

5. List the folder again, hidden entries included: docker exec "$NODE" ls -A /var/local-path-provisioner.

What you should see: step 1 shows notes as Bound with capacity 500Mi. Step 2 prints the node, for example node: lab-worker. In step 3 the folder holds one directory named after the PV, the namespace and the claim (pvc-..._default_notes), and the file says remember the milk: a "persistent volume" on kind is just a directory on one node. Step 4 reports both objects deleted and the PV condition met. Step 5 prints nothing, because the directory is gone.

Remember

Apps ask for storage with a PersistentVolumeClaim, and a StorageClass creates the PersistentVolume, on kind only once a Pod needs it. The data follows the claim, not the Pod. ReadWriteOnce limits a volume to one node, and with the Delete reclaim policy, deleting the claim deletes the data.

Quick check
01You apply a PVC on a fresh kind cluster and it stays Pending. No Pod uses it yet. What is going on?
Correct — The event said waiting for first consumer to be created before binding.
Incorrect — kind ships the default class standard, and the claim used it.
Incorrect — A rejected claim would show an error event, not this one.
Incorrect — With dynamic provisioning nobody writes PVs. The provisioner made one as soon as a Pod used the claim.
02Two Pods mount the same ReadWriteOnce claim, and both are Running. How can that be?
Incorrect — The access mode applies to every mount, not only the first.
Incorrect — The second Pod read the file through the same volume, on the same node.
Correct — Both landed on lab-worker2. For a single Pod there is ReadWriteOncePod.
Incorrect — There is no conflict to see. Both kept running.
03A teammate deletes a PVC to "clean up" a test app. The StorageClass has reclaimPolicy Delete. What happened to the data?
Incorrect — That is what Retain does. With Delete the PV went too.
Correct — The PV disappeared, and the hands-on showed the folder on the node removed.
Incorrect — Released PVs belong to the Retain policy, and they are not reused automatically.
Incorrect — No Pod used the claim any more, and even so the claim is what holds the data.

Related