kuberneteskubernetes-pvc

StatefulSet vs Retain reclaim policy of a PersistentVolume


I have a question, what is the reason to use the Retain reclaim policy of a PersistentVolume for StatefulSet. Is StatefulSet not secure enough ?

As far as I know, when a StatefulSet-managed replica disappears, either because the StatefulSet is reducing its replica count, or because its StatefulSet is deleted, the PVC and its backing volume remains and must be manually deleted.


Solution

  • You can't directly set the PersistentVolume persistentVolumeReclaimPolicy: from a StatefulSet, and you probably don't want to change it from the default.

    The reclaim policy tells Kubernetes what to do with the actual storage behind a PersistentVolume when it is deleted. If you set persistentVolumeReclaimPolicy: Retain then whatever actual storage was in use, such as an AWS EBS volume, will simply be left behind and you'll be responsible for cleaning it up. This is very occasionally useful if you need to create a PersistentVolume that refers to some existing external storage, but it's not an option you should usually need.

    A PersistentVolumeClaim doesn't have this option. It is an object that tells Kubernetes that you'd like some specific storage to exist, but not where that storage actually is or what its physical properties are. A StatefulSet provides a template for a PersistentVolumeClaim, not the underlying PersistentVolumes, and so you can't set a reclaim policy on a StatefulSet-derived volume.

    An important property of StatefulSet-associated volumes is that each replica has its own volume, via its own PVC. That means the cluster dynamically creates PVCs, and the cluster's persistent volume provisioner dynamically allocates external storage and creates PVs. In turn, this means you almost certainly don't want to set persistentVolumeReclaimPolicy: Retain on these volumes: when you do kubectl delete pvc to clean up the storage, that option would leave the dynamically-created external storage behind, and you'd need to find that and clean it up by hand. The default Delete value will delete the external storage when the PersistentVolume is deleted, which is most likely what you want to happen.