Sub topic 10
Lifecycle phases and storage object protection
This chapter assumes you can apply YAML with kubectl and already know how a PersistentVolumeClaim (PVC) binds to a PersistentVolume (PV) and how reclaim policies work. Here we trace the phases a PV and PVC pass through, and the protection that stops you from deleting storage that a Pod is still using.
Phases of a PersistentVolume
A PV reports its state in status.phase. The kubectl get pv output shows it in the STATUS column.
| Phase | Meaning |
|---|---|
| Available | The volume is free and not bound to any claim. |
| Bound | The volume is bound to a claim. |
| Released | The claim was deleted, but the volume has not yet been reclaimed. |
| Failed | Automatic reclamation failed. |
A PVC has its own phases: Pending (no matching volume yet), Bound, and Lost. A claim is Lost when its PV no longer exists.
Following a volume through its life
The diagram shows the normal path for the Retain policy from the earlier example.
With Retain, a Released PV keeps its data, but it will not bind to a new claim. The PV still holds a claimRef that points to the old claim. To reuse the volume, an administrator copies off or cleans the data, then clears the reference:
kubectl get pv demo-pv -o jsonpath='{.status.phase}{"\n"}'
kubectl patch pv demo-pv --type=json -p '[{"op":"remove","path":"/spec/claimRef"}]'After the patch, the phase returns to Available.
Storage object in use protection
Deleting a PVC while a Pod mounts it could cut the Pod off from its data. Kubernetes prevents this with finalizers. A finalizer is a marker in metadata.finalizers that blocks final removal of an object until a controller clears it.
- A PVC carries
kubernetes.io/pvc-protection. If you delete a PVC that a Pod uses, the PVC entersTerminatingand stays there until no Pod uses it. - A PV carries
kubernetes.io/pv-protection. If you delete a PV that is still bound to a claim, the PV entersTerminatinguntil it is unbound.
You can see the protection at work:
kubectl delete pvc demo-pvc
kubectl get pvc demo-pvc
kubectl get pvc demo-pvc -o jsonpath='{.metadata.finalizers}{"\n"}'While demo-pod is running, the claim shows Terminating, and the finalizer list still contains kubernetes.io/pvc-protection. Delete the Pod and the claim disappears. The reclaim policy then applies to the PV.
Common errors
- A new claim stays
Pendingeven though a Released PV of the right size exists. The PV still has aclaimRefto the deleted claim, so it is notAvailable. - A claim stuck in
Terminatingalmost always has a Pod still mounting it. Runkubectl describe pvcand check the "Used By" field. - A PV in
Failedmeans the Delete policy could not remove the backing storage. Check the PV events, then fix or remove the storage manually.
Exercise
You delete a PVC named demo-pvc, but kubectl get pvc shows it as Terminating for several minutes. What is the most likely cause, and how do you resolve it?
Show answer
A Pod still mounts the claim, and the pvc-protection finalizer blocks deletion until no Pod uses it. Find the Pod with kubectl describe pvc demo-pvc ("Used By"), delete that Pod, and the claim is removed.
Exercise
A PV with the Retain policy is in the Released phase. Why won't a new PVC bind to it, and what makes it Available again?
Show answer
The PV still has a claimRef pointing to the deleted claim. After you clean up the data, remove spec.claimRef (for example with kubectl patch), and the PV returns to Available.