How to remove mounted volumes? PV/PVC won't delete|edit|patch

5/12/2020

I am using kubectl apply -f pv.yaml on this basic setup:

apiVersion: v1
kind: PersistentVolume 
metadata:
  name: pv-demo
spec:
  storageClassName: "normal"
  capacity:
    storage: 1Gi
  persistentVolumeReclaimPolicy: Delete
  accessModes: 
  - ReadWriteOnce
  hostPath:
    path: /home/demo/
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pvc-demo
spec:
  storageClassName: "normal"
  resources:
    requests:
      storage: 200Mi
  accessModes:
  - ReadWriteOnce
---
apiVersion: v1
kind: Pod
metadata:
  name: nginx-demo
  labels:
    name: nginx-demo
spec:
  containers:
  - image: nginx
    name: nginx
    volumeMounts:
    - mountPath: /usr/share/nginx/html
      name: pv-demo
  volumes:
  - name: pv-demo
    persistentVolumeClaim:
      claimName: pvc-demo 

Now I wanted to delete everything so I used: kubectl delete -f pv.yaml However, the volume still persists on the node at /home/demo and has to be removed manually.

So I tried to patch and remove protection before deletion:

kubectl patch pv pv-demo -p '{"metadata":{"finalizers":null}}'

But the mount still persists on the node. I tried to edit and null Finalizers manually, although it said 'edited'; kubectl get pv shows Finalizers unmodified.

I don't understand what's going on, Why all of the above is not working? I want when delete, the mount folder on the node /home/demo gets deleted as well.

-- x300n
kubernetes
persistent-volume-claims
persistent-volumes

2 Answers

5/12/2020

May be you can try with hostpath under /tmp/ enter image description here

-- Shailesh
Source: StackOverflow

5/12/2020

This is expected behavior when using hostPath as it does not support deletion as to other volume types. I tested this with kubeadm and gke clusters and the mounted directory and files remain intact after removal the pv and pvc.

Taken from the manual about reclaim policies:

Currently, only NFS and HostPath support recycling. AWS EBS, GCE PD,
Azure Disk, and Cinder volumes support deletion.

While recycle is mentioned in documentation as deprecated since version 1.5 it still works and can cleanup your files but it won`t delete your mounted directory. It is not ideal but that is the closest workaround.

IMPORTANT: To successfully use recycle you cannot delete PV itself. If you delete PVC then controller manager creates recycyler pod that cleans up the volumes and this volume become available for binding to the next PVC.

When looking at the control-manager logs you can see that host_path deleter rejects the /home/demo/ dir deletion and it supports only deletion of the /tmp/.+ directory. However after testing this tmp is also not being deleted.

'Warning' reason: 'VolumeFailedDelete' host_path deleter only supports /tmp/.+ but received provided /home/demo/```
-- acid_fuji
Source: StackOverflow