Reusing released pv (with reclaim Policy "Retain") with the same name? (mock exam 2 - #4)

Hi, all!

When dealing with the mack exam2 #4, I accidentally made a pvc with wrong name. And the given PV was bound with the wrong-named PVC. After I delete the PVC, the PV was released since it’s reclaim policy was “Retain”.

I deleted the PV and made a new PV with the same name as the given PV, but the newly generated PV’s status was still released.

How can I deal with this issue?

Thanks in advance.

How did you recreate the PV?

If you simply took the output of

kubectl get pv pv-1 -o yaml

and then recreated it from that, then this is your problem.

The issue is this

  claimRef:
    apiVersion: v1
    kind: PersistentVolumeClaim
    name: my-pvc
    namespace: default
    resourceVersion: "2359"
    uid: f1902ca0-c8f1-454d-99c6-81c175a2b937

…which makes it remember the previous binding state. You need to remove that block when recreating the PV, then the state of the new PV will be Available

Or even quicker (and this is worth remembering for the exam), simply patch out the claim ref:

controlplane ~ ➜  kubectl get pv
NAME   CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS     CLAIM            STORAGECLASS   VOLUMEATTRIBUTESCLASS   REASON   AGE
pv-1   10Mi       RWO            Retain           Released   default/my-pvc                  <unset>                          9m40s

Remove the binding:

controlplane ~ ➜  kubectl patch pv pv-1 -p '{"spec": {"claimRef": null}}'
persistentvolume/pv-1 patched

And check

controlplane ~ ➜  kubectl get pv
NAME   CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM   STORAGECLASS   VOLUMEATTRIBUTESCLASS   REASON   AGE
pv-1   10Mi       RWO            Retain           Available                          <unset>                          10m

Both your answer worked!

I found that using kubectl edit pv pv-1 and erase all the spec.claimRef field also worked!

Thanks Alistair! :slight_smile: