This gist demonstrates how to change the reclaim policy of a Persistent Volume (PV) in Kubernetes from "Delete" to "Retain".
- kubectl configured to your cluster
- Proper permissions to modify PV resources
-
List all PVCs in a specific namespace:
kubectl get pvc -n <namespace>
Replace
<namespace>
with your actual namespace (e.g., monitoring). -
Find the PVC name you're interested in from the output.
-
Get the PV name associated with the PVC:
kubectl get pvc <pvc-name> -n <namespace> -o jsonpath='{.spec.volumeName}'
Replace
<pvc-name>
with your PVC name and<namespace>
with the correct namespace. -
Once you have the PV name, use this command to change its reclaim policy:
kubectl patch pv <pv-name> -p '{"spec":{"persistentVolumeReclaimPolicy":"Retain"}}'
Changing the reclaim policy to "Retain" ensures that the PV and its data are not automatically deleted when the associated PersistentVolumeClaim (PVC) is deleted. This is useful for preserving data or when you want to manually manage the lifecycle of the volume.
After running the command, verify the change:
kubectl get pv <pv-name> -o jsonpath='{.spec.persistentVolumeReclaimPolicy}'
Replace <pv-name>
with your actual PV name.
This should output: Retain
Be cautious when changing reclaim policies. "Retain" policy means you'll need to manually reclaim the storage resource when it's no longer needed.