Mount local directory into pod in minikube
KubernetesMinikubeKubernetes Problem Overview
I am running minikube v0.24.1. In this minikube, I will create a Pod for my nginx application. And also I want to pass data from my local directory.
That means I want to mount my local $HOME/go/src/github.com/nginx
into my Pod
How can I do this?
apiVersion: v1
kind: Pod
metadata:
name: nginx
spec:
containers:
- image: nginx:0.1
name: nginx
volumeMounts:
- mountPath: /data
name: volume
volumes:
- name: volume
hostPath:
path: /data
Kubernetes Solutions
Solution 1 - Kubernetes
You can't mount your local directory into your Pod directly.
First, you need to mount your directory $HOME/go/src/github.com/nginx
into your minikube.
$ minikube start --mount-string="$HOME/go/src/github.com/nginx:/data" --mount
Then If you mount /data
into your Pod using hostPath, you will get you local directory data into Pod.
There is another way
Host's $HOME
directory gets mounted into minikube's /hosthome
directory. Here you will get your data
$ ls -la /hosthome/go/src/github.com/nginx
So to mount this directory, you can change your Pod's hostPath
hostPath:
path: /hosthome/go/src/github.com/nginx
Solution 2 - Kubernetes
I tried out aerokite's solution, but found out that I had to pass --mount
as well as --mount-string "local-path:minikube-path"
to mount a directory in minikube.
minikube start --mount-string ${HOME}/go/src/github.com/nginx:/data --mount
.
Spent some time figuring this out.
Solution 3 - Kubernetes
I found a way.
This way you can directly mount directory to container. You do not have to mount your directory to minikube first.
We can specify the directory we want to add into container by using hostPath
in volumes
volumeMounts:
- name: crypto-config
mountPath: <PATH IN CONTAINER>
- name: channel-artifacts
mountPath: /opt/gopath/src/github.com/hyperledger/fabric/peer/channel-artifacts
- name: chaincode
mountPath: /opt/gopath/src/github.com/chaincode
volumes:
- name: crypto-config
hostPath:
path: <YOUR LOCAL DIR PATH>
- name: channel-artifacts
hostPath:
path: /Users/akshaysood/Blockchain/Kubernetes/Fabric/network/channel-artifacts
- name: chaincode
hostPath:
path: /Users/akshaysood/Blockchain/Kubernetes/Fabric/network/chaincode
Solution 4 - Kubernetes
DefaultMountDir
Minikube already mounts by default home directory to VM:
- on Mac it mounts dir of all users -
/Users
- on Linux and Windows just the home of current user -
homedir.HomeDir()
You can see how it does this, if you browse through Minikube sources.
Here is the search for the moment, but result might change over time:
https://github.com/kubernetes/minikube/search?q=DefaultMountDir&unscoped_q=DefaultMountDir
The definition of the HomeDir()
is:
https://godoc.org/k8s.io/client-go/util/homedir
You can always do minikube ssh
into the Minikube VM and explore it:
$ df -hl
Filesystem Size Used Avail Use% Mounted on
...
/Users 466G 442G 25G 95% /Users
As Minikube is a single node Kubernetes cluster, you can then mount /Users/...
inside your pods.
Recommended way
> minikube mount /path/to/dir/to/mount:/vm-mount-path
is the recommended way to mount directories into minikube so that they can be used in your local Kubernetes cluster. The command works on all supported platforms.
See documentation and example: https://minikube.sigs.k8s.io/docs/tasks/mount/
Solution 5 - Kubernetes
After struggling to do this for an hour & reviewing these answers, I got something slightly similar to work:
First off, I am running minikube
with virtualbox
as the driver.
minikube version
minikube version: v1.9.2
My start command:
minikube start --mount=true --mount-string=$(HOME)/somedir/on/host/:/somedir/on/vm/
If you are struggling with this, I highly suggest running minikube help to see what the flags are. I suspect these could be changing for different versions/builds.
If you're looking to get this mounted directory into your pods, you must then establish a volume and volume mount in a manifest file. Here's a simplified version of mine.
apiVersion: v1
kind: Service
...
---
apiVersion: apps/v1
kind: Deployment
...
spec:
replicas: 1
...
template:
...
spec:
containers:
...
volumeMounts:
- name: someName
mountPath: /somedir/on/vm/
...
volumes:
- name: someName
hostPath:
path: /somedir/on/vm/
Hopefully the extra polish, details and organization helps other people move a little faster on this.
Revisiting this as it seems other people like it:
If you are tied to pure minikube
, I'd suggest using the above, but I have found much better ways to locally develop minikube clusters using skaffold and docker for mac. Docker for mac can run a minikube like k8s cluster locally (in containers) using your local machine's docker daemon (so mounting files and such is a breeze). It is lovely and using it with skaffold makes hot reload and all other heavenly features possible. Do check those technologies out if you're not satisfied with the heaviness of minikube.
Solution 6 - Kubernetes
For an already running minikube you can do the following:
nohup minikube mount <host-directory-path>:<desired-minikube-directory-path> &
Solution 7 - Kubernetes
to check which passes are mounted in the minikube run
minikube ssh
and then ls -la
Solution 8 - Kubernetes
Not sure if I joined the party late but I did a root:root mapping by doing following command:
minikube start --mount-string="/:/"
This just will just mount your local file system root to minikube and keeps things seamless between the systems.Hope it helps.