How to access PersistentVolume files on docker-for-desktop?

2/13/2019

I'd like to access and edit files in my Kubernetes PersistentVolume on my local computer (macOS), but I cannot understand where to find those files!

I'm pointing my hostPath to /tmp/wordpress-volume but I cannot find it anywhere. What is the hidden secret I'm missing

I'm using the following configuration on a docker-for-desktop cluster Version 2.0.0.2 (30215).

PersistentVolume

kind: PersistentVolume
metadata:
  name: wordpress-volume
spec:
  # ...
  hostPath:
    path: /tmp/wordpress-volume

PersistentVolumeClaim

kind: PersistentVolumeClaim
metadata:
  name: wordpress-volume-claim
# ...

Deployment

kind: Deployment
metadata:
  name: wordpress
# ...
spec:
  containers:
  - image: wordpress:4.8-apache
    # ...
    volumeMounts:
    - name: wordpress-volume
      mountPath: /var/www/html
  volumes:
  - name: wordpress-volume
    persistentVolumeClaim:
      claimName: wordpress-volume-claim
-- a.barbieri
docker
docker-volume
kubernetes
persistent-volumes

3 Answers

2/9/2020

In case of MacOS and Kubernetes inside Docker for Mac. How to find a real location of dir-based local volume into VM

1) Create a new PersistentVolume with unique path:

blablabla.yml:

kind: PersistentVolume
apiVersion: v1
metadata:
  name: blablabla
spec:
  storageClassName: manual
  capacity:
    storage: 1G
  accessModes:
    - ReadWriteMany
  hostPath:
    path: "/mnt/blablabla"

kubectl apply -f blablabla.yml

2) Log into VM:

screen ~/Library/Containers/com.docker.docker/Data/vms/0/tty
# then press Enter

3) Find your volume:

find / -name blablabla
/containers/services/docker/rootfs/mnt/blablabla # <= got it!
/containers/services/docker/tmp/upper/mnt/blablabla

4) Exit from screen: Ctrl-a k y, Detach from screen: Ctrl-a d

Note

Sometimes you have a chance to get a broken screen session, it seems like a garbaged stdout with messed symbols, stdin still works fine. In that case, try to terminate all screen sessions and reconnect to the first one. Or just restart you docker for mac.

-- a0s
Source: StackOverflow

2/13/2019

Thanks to @aman-tuladhar and some hours lost on the internet I've found out that you just need to make sure storageClassName is set for you PersistentVolume and PersistentVolumeClaim.

As per documentation if you want to avoid that Kubernetes dynamically generetes PersistentVolumes without considering the one you statically declared, you can just set a empty string " ".

In my case I've set storageClassName: manual.

PersistentVolume

kind: PersistentVolume
metadata:
  name: wordpress-volume
spec:
  # ...
  storageClassName: manual
  hostPath:
    path: /tmp/wordpress-volume

PersistentVolumeClaim

kind: PersistentVolumeClaim
metadata:
  name: wordpress-volume-claim
spec:
  storageClassName: manual
  # ...

This works out of the box with docker-for-desktop cluster (as long as mountPath is set to a absolute path).

References:

-- a.barbieri
Source: StackOverflow

2/13/2019

First this you need to remember is that Kubernetes is running on minikube cluster. minikube itself run on Virtual Machine. So that path won't be on you host machine, rather it is the path in Virtual Machine.

But with minikube we have easy way to do this. First you have to mount host directory to minikube.

(If you are using cloud providers you will have some way to create a storage. For GCE you have gcePersistentDisk)

minikube mount /path/to/dir/to/mount:/vm-mount-path

Now

kind: PersistentVolume
metadata:
  name: wordpress-volume
spec:
  # ...
  hostPath:
    path: /vm-mount-path

If you create this resource this should save file in your host machine.

Follow this minikube documentation for more detail

-- Aman Tuladhar
Source: StackOverflow