Kubernetes: how to do dynamic PersistentVolumeClaim with persistentVolumeReclaimPolicy: Reclaim

5/22/2018

I have dynamic PersistentVolume provisioning using PersistentVolumeClaim.

I would like to keep the PV after the pod is done. So I would like to have what persistentVolumeReclaimPolicy: Reclaim does.

However, that is applicable to PersistentVolume, not PersistentVolumeClaim (AFAIK).

How can I change this behavior for dynamically provisioned PV's?

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
    name: {{ .Release.Name }}-pvc
spec:
    accessModes:
      - ReadWriteOnce
    storageClassName: gp2
    resources:
        requests:
            storage: 6Gi

---
kind: Pod
apiVersion: v1
metadata:
    name: "{{ .Release.Name }}-gatling-test"
spec:
    restartPolicy: Never
    containers:
      - name: {{ .Release.Name }}-gatling-test
        image: ".../services-api-mvn-builder:latest"
        command: ["sh", "-c", 'mvn -B gatling:test -pl csa-testing -DCSA_SERVER={{ template "project.fullname" . }} -DCSA_PORT={{ .Values.service.appPort }}']
        volumeMounts:
          - name: "{{ .Release.Name }}-test-res"
            mountPath: "/tmp/testResults"

    volumes:
      - name: "{{ .Release.Name }}-test-res"
        persistentVolumeClaim:
          claimName: "{{ .Release.Name }}-pvc"
          #persistentVolumeReclaimPolicy: Retain  ???
-- Ondra Žižka
kubernetes
persistent-volume-claims
persistent-volumes

2 Answers

4/5/2019

Workaround would be to create new StorageClass with reclaimPolicy: Retain and use that StorageClass every where.

kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
  name: gp2-retain
  annotations:
provisioner: kubernetes.io/aws-ebs
parameters:
  type: gp2
  fsType: ext4 
reclaimPolicy: Retain

PS: The reclaimPolicy of the existing StorageClass can't edited, but you can delete the StorageClass and recreate it with reclaimPolicy: Retain

-- Tummala Dhanvi
Source: StackOverflow

5/22/2018

There is an issue on Kubernetes Github about Reclaim Policy of dynamically provisioned volumes.

A short answer is "no" - you cannot set the policy.

Here is the small quote from the dialogue in the ticket on how to avoid the PV deletion:

Speedline: Stumbled upon this and I'm going through a similar issue. I want to create an Elasticsearch cluster but make sure that if the cluster goes down for whatever reason, the data stored on the persistent disks get maintained across the restart. I currently have one a PersistentVolumeClaim for each of the deployment of elasticsearch that carries data.

jsafrane: @speedplane: it is maintained as long as you don't delete the PVC. Reclaim policy is executed only if kuberenetes sees a PV that was bound to a PVC and the PVC does not exist.

@jsafrane okay, got it. So just have to be careful with the PVCs, deleting one is like deleting all the data on the disk.

-- Anton Kostenko
Source: StackOverflow