Reputation: 2616
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)
.
kind: PersistentVolume
metadata:
name: wordpress-volume
spec:
# ...
hostPath:
path: /tmp/wordpress-volume
kind: PersistentVolumeClaim
metadata:
name: wordpress-volume-claim
# ...
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
Upvotes: 8
Views: 14777
Reputation: 1510
I create a PersistentVolume
and use the storageClassName
(local-storage
in my example below). Make sure to replace the path
(/Users/user/data-pv1
) with an actual path on your Mac.
apiVersion: v1
kind: PersistentVolume
metadata:
name: local-pv1
spec:
capacity:
storage: 30Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
storageClassName: local-storage
local:
path: /Users/user/data-pv1
nodeAffinity:
required:
nodeSelectorTerms:
- matchExpressions:
- key: kubernetes.io/hostname
operator: In
values:
- docker-desktop
If you'd like to access the hostPath
, you need to use nsenter
:
docker run -it --rm --privileged --pid=host alpine:edge nsenter -t 1 -m -u -n -i sh
or nsenter1
docker run -it --rm --privileged --pid=host justincormack/nsenter1
and navigate to the following directory:
/var/lib/k8s-pvs
Upvotes: 2
Reputation: 506
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
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.
Upvotes: 3
Reputation: 2616
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
.
kind: PersistentVolume
metadata:
name: wordpress-volume
spec:
# ...
storageClassName: manual
hostPath:
path: /tmp/wordpress-volume
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:
Upvotes: 11
Reputation: 9110
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
Upvotes: 4