4

I created a PV and a PVC on docker-desktop and even after removing the pv and pvc the file still remains. When I re-create it, it attaches the same mysql database to new pods. How do you manually delete the files created by the hostPath? I suppose one way is to just reset Kubernetes in the preferences but there has to be another less nuclear option.

apiVersion: v1
kind: PersistentVolume
metadata:
  name: mysql-pv-volume
  labels:
    type: local
spec:
  storageClassName: manual
  capacity:
    storage: 20Gi
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Delete
  hostPath:
    path: "/mnt/data"
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql-pv-claim2
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 20Gi
joshduffney
  • 389
  • 1
  • 4
  • 16
  • I found a good solution with docker-desktop. If you use /tmp or create a new share drive with docker for desktop you can go in and manually delete the files. I had expected that when you delete the volume it deletes the data but that's not the case with local and hostPath volumes. – joshduffney Sep 23 '19 at 15:11

3 Answers3

6

According to the docs, "...Recycle reclaim policy performs a basic scrub (rm -rf /thevolume/*) on the volume and makes it available again for a new claim". Also, "...Currently, only NFS and HostPath support recycling". So, try changing

persistentVolumeReclaimPolicy: Delete

to

persistentVolumeReclaimPolicy: Recycle
apisim
  • 4,036
  • 1
  • 10
  • 16
3

hostPath volumes are simply folders on one of your node's filesystem (in this case /mnt/data). All you need to do is delete that folder from the node that hosted the volume.

Alassane Ndiaye
  • 4,427
  • 1
  • 10
  • 19
  • 1
    that part makes sense, where I'm stuck is how to I get to the node? I know in windows docker runs on a linux vm from hyper-v. Happen to know where it runs from in macOS? – joshduffney Sep 21 '19 at 00:44
  • @duffney I am not familiar with those systems, do you know if ssh is installed on your node? – Alassane Ndiaye Sep 21 '19 at 00:57
  • I don't think so, I can't access by the name and IP shown with kubeclt cmds. I posed it in docker's forms. There has to be a way to access the hose created inside docker-desktop. https://forums.docker.com/t/kubernetes-delete-hostpath-files-docker-desktop/81526 – joshduffney Sep 21 '19 at 02:22
1

If you defined any node affinity to pod that you need to check. Then find out node where that pod is schedule. Delete PVC an PV Then delete data from /mnt/data directory.

kubectl get pod -o wide | grep <pod_name>

Here you will get on which node it is scheduled.

kubectl delete deploy or statefulset <deploy_name>

kubectl get pv,pvc

kubectl delete pv <pv_name>

kubectl delete pvc <pvc_name>

Now go on that node and delete that data from /mnt/data

One more way to do it you can define persistentVolumeReclaimPolicy to retain or delete

Sachin Arote
  • 907
  • 13
  • 22