Learn How to Mount a Local Drive in a Pod in minikube (2021)

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 a software 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:

alias k='kubectl'

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 [1] (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.

Shell access to minikube with a pointer to the /Users directory, which is mounted by default.
The /Users directory has been mounted by default by minikube.

Here’s the pod configuration:

				
					apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: nginx-minimounted
  name: nginx-minimounted
  namespace: myns
spec:
  containers:
  - image: nginx
    name: nginx-minimounted
    resources: {}
    volumeMounts:
      - mountPath: /etc/minimounted
        name: minimounted
  volumes:
    - name: minimounted
      hostPath:
        path: /Users/thospfuller/k8s-study/minimounted
  dnsPolicy: ClusterFirst
  restartPolicy: Never
status: {}
				
			
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:

Terminal / Oh My Zsh output for Solution #1: Mount a subdirectory in a pod in Minikube within user home
We mount a subdirectory within the pod's user home and display the contents of the hello.txt file.
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 [1] (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.

				
					apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: nginx-minimounted
  name: nginx-minimounted
  namespace: myns
spec:
  volumes:
  - name: minimounted
    hostPath:
      path: /minimounted/
      type: DirectoryOrCreate
  containers:
  - image: nginx
    name: nginx-minimounted
    resources: {}
    volumeMounts:
      - mountPath: /etc/minimounted
        name: minimounted
  dnsPolicy: ClusterFirst
  restartPolicy: Never
status: {}
				
			

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.

Terminal shell output demonstrating that the /etc/minimounted/ directory is empty.

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.

Example bash shell output for a container running in minikube with the /etc/minimounted/ directory which now contains the hello.txt file.

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.

Conclusion

If you liked this article you may also like the following articles, also written by yours truly:

Please add comments below if you find an error with anything written here or have a question about this document.