Kubernetes: How to set ReclaimPolicy for PersistentVolumeClaim

Kim Wuestkamp
FAUN — Developer Community 🐾
3 min readMay 31, 2019

--

PV = PersistentVolume
PVC = PersistentVolumeClaim

Since some time now we can use dynamic provisioning with Kubernetes so we don’t have to create a PV ourself but can do this automatically when requesting it via a PVC.

But how can we prevent the automatically created PV from being removed if we removed the PVC? Because by default the PV and all its data is deleted. We can do this by setting the PVs Reclaim Policy, but how can we configure the Reclaim Policy using a PVC?

TL;DR

Create a custom StorageClass.

Reclaim Policy

Persistent Volumes that are dynamically created by a storage class will have the reclaim policy specified in the reclaimPolicy field of the class, which can be either Delete or Retain. If no reclaimPolicy is specified when a StorageClass object is created, it will default to Delete.

Persistent Volumes that are created manually and managed via a storage class will have whatever reclaim policy they were assigned at creation.

https://kubernetes.io/docs/concepts/storage/storage-classes/

The Default Situation

Let’s say we create a simple PVC like this:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name:
pvc1
spec:
accessModes:
- ReadWriteOnce
volumeMode: Filesystem
resources:
requests:
storage:
40Mi

This would result in a PVC and a PV created:

If we now delete that PVC (kubectl delete pvc pvc1), then the PV is removed as well because of its Reclaim Policy is by default set to Delete.

Change the Reclaim Policy

We could simply wait for the PV to be created and then update it’s Reclaim Policy. But what if we would like to do this directly through the PVC creation? For this we can use a custom StorageClass.

Create a custom StorageClass

By default a PVC used the StorageClass set as default, you can see yours with kubectl get sc. So let’s create a new one:

apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name:
custom
provisioner: docker.io/hostpath
reclaimPolicy: Retain
volumeBindingMode: Immediate

With this we create a StorageClass named custom. As I do this in DockerForDestktop I did set the provisioner to docker.io/hostpath.

Read more about Kubernetes StorageClasses here.

Use the custom StorageClass

All we have to do is to change our PVC yaml to include a storageClassName:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pvc1
spec:
accessModes:
- ReadWriteOnce
volumeMode: Filesystem
resources:
requests:
storage: 40Mi
storageClassName: custom # ADD THIS

Now when we create this PVC, we can see our custom StorageClass and that the created PV has the Retain Reclaim Policy:

Now after kubectl delete pvc pvc1 our PV still exists with status Released.

Why all this?

With the “Retain” policy, if a user deletes a PersistentVolumeClaim, the corresponding PersistentVolume is not be deleted. Instead, it is moved to the Releasedphase, where all of its data can be manually recovered.

Follow us on Twitter 🐦 and Facebook 👥 and join our Facebook Group 💬.

To join our community Slack 🗣️ and read our weekly Faun topics 🗞️, click here⬇

If this post was helpful, please click the clap 👏 button below a few times to show your support for the author! ⬇

--

--