The Problem: You’re using minikube and you want to mount a local drive in a pod.
The background to this problem is as follows:
You’re using minikube and you want to mount a local drive in a pod.
The reason an engineer would need this is that pod storage is ephemeral and will be deleted when the pod is deleted. If we want the storage to survive a pod deletion or to be shared between pods, then we can mount a directory from the local filesystem. This could be a requirement, for example, when you have a database running in the pod but the storage is located on a local filesystem. Another example where this could be appropriate would be when an engineer keeps the project on their local drive but runs the application in a container.
In this article we’ll cover several examples that demonstrate how to accomplish mounting a local directory in a pod running in minikube. We rely on minikube (v1.15.1) with kubectl (1.19.4) on Ubuntu (20.04.1 LTS) as well as on Mac OSX (10.13.6 High Sierra).
The k command, which appears below, is defined as follows:
Let’s take a look at three solutions that address this requirement.
Solution #1: Mount a subdirectory within user home (easiest)
The first solution, which is also the easiest solution in this article, was inspired by  (Akshay Sood, specifically) facilitates the same result as Solution #2 however without the requirement to pass the –mount and –mount-string args. The pod configuration file looks very close to the one used in Solution #2 as well — direct your attention to lines 14 (volumeMounts) and 17 (volumes).
This works because Minikube mounts the /Users directory by default on Mac OSX, which is what we’re using for this specific solution.
Refer to the homedir package documentation as well as the minikube DefaultMountDir search results on GitHub for more information regarding the directory paths per specific supported operating systems (Apple OSX, Linux, and Microsoft Windows) [12 & 13]. I also have a simple script, which is written in Go, in GitHub, which displays the value of the user’s home directory.
In the image below we can see which directory has been mounted in minikube by invoking the df command with the h option, for displaying human readable output, and the l option, which reveals information about locally-mounted filesystems.
Here’s the pod configuration:
In this example we start minikube absent any of the mount-related args. When the pod is created the /etc/minimounted/ directory will exist and it will be correctly mapped to the local directory.
We can see the output of this example in the following image:
Finally we include the complete script for testing this solution here.
minikube start --hyperv-virtual-switch "My Virtual Switch" --v=4 kubectl apply -f ./nginx-hp-minimounted.yaml kubectl exec nginx-minimounted -n myns -it -- /bin/sh ls -la /etc/minimounted exit
Let’s take a look at Solution #2.
Solution #2: Mount a directory outside of user home
The second solution should be fairly straightforward however it’s not exactly given the pod configuration in the example included below. Solution #2 attempts to mount the /etc/minimount directory and since the /etc directory is not mounted by default, we need to add command line args when minikube is started to instruct minikube to mount a local directory to a directory in minikube and then when the pod is created it will mount that directory to a directory in the pod itself.
If we just follow an example for mounting a local directory in a pod in Kubernetes, when we have shell access to the pod and check the path, the directory will exist however it will be empty.
Reference  (Shahriar, specifically) explains the solution pretty closely however a fully working solution is not provided and we’ll deliver that here.
The following pod configuration file should work but, as we’ll see in a moment, it doesn’t.
Here’s an example of what the minikube start script might look like — we need to change this in order to get this working.
minikube start --hyperv-virtual-switch "My Virtual Switch" --v=4
The following image demonstrates the problem inside the pod — note the /etc/minimounted/ directory is empty whereas we’re expecting the hello.txt file to be there.
In order to make this work, we need to start minikube with the following mount and mount-string options set:
minikube start --hyperv-virtual-switch "My Virtual Switch" --v=4 --mount --mount-string="/etc/minimount/:/minimounted/"
We have /etc/minimount/ mounted as /minimounted/ in minikube and when we run the nginx-minimounted pod the /minimounted/ directory will be mounted as /etc/minimounted/ in the pod; we can see the directory contents in the image below.
Let’s take a look at Solution #3.
Solution #3: Mount a directory other than user home without restart
It is possible to mount the directory without restarting minikube and we’ll demonstrate this here. Unfortunately, however, we’ll need to recreate the pod otherwise the pod’s /etc/minimounted/ directory will still be empty. If you know how to achieve this same result without restarting the pod, please include the details in the comments.
Regarding the following image: first we restart minikube and can see that the /etc/minikube/ directory is empty.
Next we mount the directory. Keep in mind that this command must be executed as a background process or we’ll need to open another terminal.
Finally, we can see the contents of the /etc/minimounted/hello.txt file.
For convenience, we include the entire script here:
minikube stop && minikube start kubectl apply -f ./nginx-minimounted.yaml kubectl exec nginx-minimounted -n myns -it -- /bin/sh ls -la /etc/minimounted exit kubectl delete pods nginx-minimounted -n myns minikube mount /Users/thospfuller/k8s-study/minimounted:/minimounted/ & kubectl apply -f ./nginx-minimounted.yaml kubectl exec nginx-minimounted -n myns -it -- /bin/sh cat /etc/minimounted/hello.txt
And that’s it for Solution #3.
And that’s it for this article.
If you liked this article you may also like the following articles, also written by yours truly:
- Easily Install the Metrics Server on Minikube in 5 Steps
- Answers to Five Kubernetes CKAD Practice Questions (2020)
Please add comments below if you find an error with anything written here or have a question about this document.
- Mount local directory into pod in minikube
- MountVolume.SetUp failed for volume “mongo” : hostPath type check failed: /mongo/data is not a directory
- hostpath : MountVolume.SetUp failed for volume “test-volume” : hostPath type check failed: /data is not a directory
- Invalid Error: .container.volumes: hostPath type check failed: “not a directory”
- Kubernetes Volumes
- Mounting filesystems in minikube
- Configure a Pod to Use a Volume for Storage
- Configure a Pod to Use a PersistentVolume for Storage
- Intro to Windows support in Kubernetes
- juliohm1978 / kubernetes-cifs-volumedriver
- kubernetes / examples (staging volumes NFS)
- Kubernetes DefaultMountDir (constants in Golang for Darwin, Linux, and Windows)
- GoDoc package homedir