Reusing a PVC
12 Sep 2021 | tags: KubernetesRationale TL;DR
Let’s suppose we have a PV and we need to attach it to a new PVC. Let’s suppose the PVC was claimed by a POD which does not exist anymore.
The solution
In this situation, the PV should be in the Released state:
pvc-ee8e4b8e-28d6-40dd-bfe8-a5affb6bc9d9 10Gi RWO Retain Released my-namespace/grafana kops-ssd-1-17 352d
Of course, the Reclaim policy should be Retained otherwise there wouldn’t be the PV at all :)
We need to edit the PV and remove the claimRef field. That would make the volume Available:
pvc-ee8e4b8e-28d6-40dd-bfe8-a5affb6bc9d9 10Gi RWO Retain Available kops-ssd-1-17 352d
The next thing we need to do is create a yaml file file with the following contents (your milleage may vary, the relevant thing is to use the right PV in the claim):
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: grafana
namespace: my-namespace
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
volumeName: pvc-ee8e4b8e-28d6-40dd-bfe8-a5affb6bc9d9
That should create a claim and attach it to the old PV:
# kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
grafana Bound pvc-ee8e4b8e-28d6-40dd-bfe8-a5affb6bc9d9 10Gi RWO kops-ssd-1-17 10m