How to Bind a PVC to an Existing Released PV in Kubernetes?

A PersistentVolume with persistentVolumeReclaimPolicy: Retain (not applicable for Delete) used by a PersistentVolumeClaim transitions from Bound to Released state when the attached PVC is deleted.

# PV is in Bound state with test/myclaim PVC
$ k get pv
NAME     CAPACITY   RECLAIM POLICY   STATUS   CLAIM
pv0001   50Gi       Retain           Bound    test/myclaim

$ k get pvc
NAME      STATUS   VOLUME   CAPACITY
myclaim   Bound    pv0001   50Gi

$ k delete pvc myclaim
persistentvolumeclaim "myclaim" deleted

# PV is in Released state now
$ k get pv
NAME     CAPACITY   RECLAIM POLICY   STATUS      CLAIM
pv0001   50Gi       Retain           Released    test/myclaim

If you try to create a new PVC and bind that to the same PV, the PVC will continue to be in Pending state and the PV in Released state.

# spec.volumeName: pv0001
$ k apply -f pvc.yaml
persistentvolumeclaim/myclaim created

# PVC is in pending state and will not bind to Released PV
$ k get pvc
NAME      STATUS    VOLUME   CAPACITY
myclaim   Pending   pv0001   0

# PV will continue to be Released
$ k get pv
NAME     CAPACITY   RECLAIM POLICY   STATUS      CLAIM
pv0001   50Gi       Retain           Released    test/myclaim

The reason behind this is mentioned in the docs:

The Retain reclaim policy allows for manual reclamation of the resource. When the PersistentVolumeClaim is deleted, the PersistentVolume still exists and the volume is considered “released”. But it is not yet available for another claim because the previous claimant’s data remains on the volume. An administrator can manually reclaim the volume with the following steps.

  1. Delete the PersistentVolume. The associated storage asset in external infrastructure (such as an AWS EBS or GCE PD volume) still exists after the PV is deleted.
  2. Manually clean up the data on the associated storage asset accordingly.
  3. Manually delete the associated storage asset.

If you want to reuse the same storage asset, create a new PersistentVolume with the same storage asset definition.

To reuse the PV, if you do not want to create a new PV altogether (with the same storage asset definition), there is a neat little trick where we can unset or remove the PV’s spec.claimRef that points to the PVC it was last/is currently bound to. This can be done using kubectl edit pv [pv_name] and removing the spec.claimRef block or by running kubectl patch pv [pv_name] -p '{"spec":{"claimRef": null}}'). Once this is done, new or existing PVCs can be attached to the existing/used PV.

# PV is Released
$ k get pv
NAME     CAPACITY   RECLAIM POLICY   STATUS   CLAIM
pv0001   50Gi       Retain           Released    test/myclaim

# Detach the PV from any references to PVCs
$ kubectl patch pv pv0001 -p '{"spec":{"claimRef": null}}'
persistentvolume/pv0001 patched

# PV becomes Available
$ k get pv
NAME     CAPACITY   RECLAIM POLICY   STATUS      CLAIM
pv0001   50Gi       Retain           Available

# Create a PVC that will get attached to the Available PV
# spec.volumeName: pv0001
$ k apply -f pvc.yaml
persistentvolumeclaim/myclaim created

# New PVC is bound to PV
$ k get pvc
NAME      STATUS   VOLUME   CAPACITY
myclaim   Bound    pv0001   50Gi

# PV is now Bound
$ k get pv
NAME     CAPACITY   RECLAIM POLICY   STATUS   CLAIM
pv0001   50Gi       Retain           Bound    test/myclaim

Leave a Reply

Your email address will not be published. Required fields are marked *