Tutorial: Learn How To Mount A Local Directory Into A Pod In minikube

Tutorial: Learn How To Mount A Local Directory Into A Pod In minikube

The use case for this guide is as follows: A software developer needs to mount a local directory into a pod in minikube since pod storage is ephemeral and will be deleted when the pod is deleted. In the following article, we're using Minikube (v1.14.2) with Kubectl (1.19) running on Ubuntu 20.04.1 LTS.

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 file system — this could be a requirement, for example, when you have a database running in the pod but the storage is located on the local filesystem.

In this article, we’ll cover several examples that demonstrate how to mount 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'

In the next section we’ll take a look at several use cases regarding why a software developer would want to mount a local directory in a pod in Kubernetes and/or minikube and following that we’ll take a look at a few solutions.

Use Cases for mounting a local directory in a pod in Kubernetes

When setting up a local Kubernetes cluster using minikube, you can utilize hostPath volumes to mount specific files or directories from the host machine into your pods, and define volumeMounts with mountPath in your pod specification to efficiently manage file and directory access within your containers.

For detailed management and troubleshooting of these volumes, use ‘minikube ssh‘ to access the cluster’s command line.

Mounting a local directory into a pod in Kubernetes can be useful for several reasons and I’ve included five use cases below.

At a high level, these benefits include data sharing and accelerating the development and testing of software.

Configuration Injection

You can mount a local directory containing configuration files or environment variables into a pod.

This allows you to dynamically adjust the behavior of your application without modifying the container image.

As you update the configuration on the host machine, the changes are immediately reflected in the pod thereby facilitating easy configuration management.

Data Sharing and Backup

Mounting a shared local directory allows you to share data between containers within the same pod.

Mounting a shared local directory enables seamless communication and data exchange, such as sharing log files or database backups between containers.

Additionally, you can use local directories as temporary storage for data backups and then mount the local directories into pods for further processing.

Persistent Data Storage

You can mount a local directory into a pod when one or more applications require persistent storage.

This approach can be particularly useful during development and testing, and allows you to use your local machine’s file system as a data storage solution.

Local storage is not recommended for production scenarios due to its limitations and lack of scalability however it is perfectly suitable for development and testing purposes.

Debugging and Troubleshooting

When debugging an application within a pod, mounting a local directory with debugging tools, scripts, or diagnostic utilities can help you quickly investigate issues.

Mounting a local directory allows the developer access to debugging tools and logs directly from the local machine and streamlines the troubleshooting process.

Development and Testing

Mounting a local directory can accelerate the iteration process when it comes to developing and testing software.

Instead of building and pushing container images for every code change, you can mount the directory containing the source code into the pod and enable real-time testing and observation of the changes as development progresses.

Keep in mind that while mounting a local directory can be helpful for specific use cases, it’s essential to consider the security and portability implications as it pertains to production software.

Using ConfigMaps, Configuration Secrets, or network-attached storage solutions such as Persistent Volumes (see also Configure a Pod to Use a PersistentVolume for Storage) and Persistent Volume Claims (PVCs)) might be more appropriate for production environments, where data needs to be managed securely and scaled effectively.

In the next section we’ll take a look at what the minikube mount command does and then we’ll take a look at three solutions that we can use to mount a host directory in a pod in minikube.

What is minikube mount?

The minikube mount command allows the developer to mount a local directory on the host machine into a running Minikube cluster, thereby making it accessible to containers running in the cluster.

In the following examples we do not use the minikube mount command directly — instead we apply a configuration yaml file which has the mount specification details included.

See also the page on mounting filesystems regarding how to mount a host directory into the virtual machine (VM) on k8s.io for more details regarding how minikube mount is used.

Solution #1: Mount a subdirectory within user's home into a pod in minikube)

In the first solution, which is also the easiest solution in this article, we mount a subdirectory within the user’s home directory into a pod in minikube.

This solution was inspired by #1 (Akshay Sood, specifically) and 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 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.

The "/Users" directory has been mounted in minikube.

Step One: Modify the hostPath in the pod configuration below to point to the user home directory.

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 (todo: double-check this image as it may not be the correct one to use here):

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

Step Two: Start minikube

The second step requires us to start minikube using the hyperv-virtual-switch option as follows:

minikube start --hyperv-virtual-switch "My Virtual Switch" --v=4

In this example the –hyperv-virtual-switch parameter is described on k8s.io as:

Name of the virtual switch the minikube VM should use. Defaults to first found

and we’re using “My Virtual Switch” as the name. The –v option sets the log level verbosity.

minikube start cluster using the hyperkit driver.

Step Three: Apply the configuration.

In the script below we apply the configuration file.

kubectl apply -f ./nginx-hp-minimounted.yaml

We can see the output when the kubectl apply command is executed below.

Output when mounting a local directory to a pod.

Step Four (optional): Test that the path is available in the pod in minikube

We can test that the path is available by getting command line access to the pod in the myns namespace and checking that the path exists as follows:

kubectl exec nginx-minimounted -n myns -it -- /bin/sh ls -la /etc/minimounted

Below I’ve included the complete script for running and testing this solution.

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

If everything worked as we expect it to we should see something that looks like what appears below.

Example output for mounting a directory in a pod in Minikube within user's home directory

And that’s it for this example.

Solution #2: Mount a directory outside of user home into a pod in minikube

Refer to solution #2 in the original article for the complete example.

Solution #3: Mount a directory other than user home, without restart, into a pod in minikube

Refer to solution #3 in the original article for the complete example.

Tutorial Conclusion

If you liked this instructional then you may also like the following articles:

See the Kubernetes category and the containerization category for other articles that may be of interest.

Questions and comments are welcomed.