PersistentVolumes and claims
Storage that outlives the Pod.
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: adee882ca1e760cdace016e34665f37bf52d40afd3b0c219f08e31b935c029afAn 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:
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:
# 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: v1kind: PersistentVolumeClaimmetadata:name: dataspec:accessModes:- ReadWriteOnceresources:requests:storage: 1Gi
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:
# A Pod that mounts the claim "data" at /data.apiVersion: v1kind: Podmetadata:name: appspec:terminationGracePeriodSeconds: 1containers:- name: shellimage: busybox:1.38.0command: ["sleep", "3600"]volumeMounts:- name: datamountPath: /datavolumes:- name: datapersistentVolumeClaim:claimName: data
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
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:
# A second Pod that mounts the claim "data" at /data.apiVersion: v1kind: Podmetadata:name: app2spec:terminationGracePeriodSeconds: 1containers:- name: shellimage: busybox:1.38.0command: ["sleep", "3600"]volumeMounts:- name: datamountPath: /datavolumes:- name: datapersistentVolumeClaim:claimName: data
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
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:
# Hands-on: a claim and a Pod that writes one line into it, then waits.apiVersion: v1kind: PersistentVolumeClaimmetadata:name: notesspec:accessModes:- ReadWriteOnceresources:requests:storage: 500Mi---apiVersion: v1kind: Podmetadata:name: notesspec:terminationGracePeriodSeconds: 1containers:- name: shellimage: busybox:1.38.0command: ["sh", "-c", "echo remember the milk > /notes/todo.txt && sleep 3600"]volumeMounts:- name: notesmountPath: /notesvolumes:- name: notespersistentVolumeClaim: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.
Pending. No Pod uses it yet. What is going on?waiting for first consumer to be created before binding.standard, and the claim used it.lab-worker2. For a single Pod there is ReadWriteOncePod.Retain does. With Delete the PV went too.Retain policy, and they are not reused automatically.