DEV Community

loading...
Cover image for Learn How to Mount a Local Drive in a Pod in Minikube (2021)
Coherent Logic Limited

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

thospfuller profile image Thomas P. Fuller Originally published at thospfuller.com Updated on ・3 min read

In the following article, we're using Minikube (v1.14.2) with Kubectl (1.19) running on Ubuntu 20.04.1 LTS.

The Problem

The background to this problem is as follows:

You're using Minikube and you want to mount a local drive in a pod and that's it.

This should be fairly straightforward however it's not exactly. We'll cover a fully working example here.

The real issue is that we need to mount a local directory to a directory in Minikube and then 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 example is not provided.

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 fix this issue.

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.

Now let's take a look at a solution.

The Solution

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/"
Enter fullscreen mode Exit fullscreen mode

We have /etc/minimount/ mounted as /minimount/ in Minikube and when we run the nginx-minimounted pod the /minimount/ directory will be mounted as /etc/minimounted/ in the pod; we can see the directory contents in the image below.

And that's it for this article.

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.

Hat Tip

Q McCallum

References

  1. Mount local directory into pod in minikube
  2. MountVolume.SetUp failed for volume "mongo" : hostPath type check failed: /mongo/data is not a directory
  3. hostpath : MountVolume.SetUp failed for volume "test-volume" : hostPath type check failed: /data is not a directory
  4. Invalid Error: .container.volumes: hostPath type check failed: "not a directory"

Discussion (0)

pic
Editor guide