Search code examples
kuberneteskubectl

How to remove claim from a persistent volume when it was released?


After the PVC is deleted the associated PV still shows the claim. This might be expected behavior, but for my usecase it must turn to "Available" status again. When I remove the claim object in the PV manifest by hand, it can be claimed by a different pod again. This is what I wanted.

CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS     CLAIM
10Gi       RWO            Retain           Released   arc-runners/k8s-runner-set-5tsbc-runner-qpz97-runner-cache

CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS     CLAIM
10Gi       RWO            Retain           Available           

                                                       

Is there an option to so in a proper k8s manner?

More context:

In the pod spec I'm using ephemeral volumeClaimTemplate. I'm forced to do so because I need to integrate it to this helm chart. Here is the helm/values.yaml where the pod spec can be overridden.

...
  volumes:
    - name: runner-cache
      ephemeral:
        volumeClaimTemplate:
          spec:
            accessModes: [ "ReadWriteOnce" ]
            storageClassName: "cache-dir-dispatcher"
            resources:
              requests:
                storage: 10Gi

I got multiple static defined volumes like this

apiVersion: v1
kind: PersistentVolume
metadata:
  name: github-runner-slot1-pv
  labels:
    type: local
spec:
  persistentVolumeReclaimPolicy: Retain
  storageClassName: cache-dir-dispatcher
  ...

It claims properly till every PV is in the "Released" status. Once I delete the claim entry in the manifest of the PV, it turns to "Available" again which is working well.


Solution

  • This is an intended behavior for security reasons because if we accidentally delete the pod then when the reclaim policy is set to retain means that the system should not automatically release the PV and make it available. It indicates that there may be user action required to copy or purge the data from the disk. So it is up to the user to clear the ClaimRef from the PV when they are ready to make the disk available again. So there is no feature to automatically delete pvc after the pod is deleted. There are plenty of git issues on it.

    But using a stateful set you have a .spec.persistentVolumeClaimRetentionPolicy field which controls if and how PVCs are deleted during the lifecycle of a StatefulSet. You can find more information about this in this document.