Minikube
Kubernetes
Pod
Local Directory
Volume Mount

Mount local directory into pod in minikube

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Mounting a local directory into a pod in Minikube is an efficient way to make local files accessible to applications running in a Kubernetes pod. This is particularly helpful for local development and testing when you need to provide your applications with easy access to configuration files, code, or other data located on your host machine. In this article, we'll delve into the process of mounting local directories into pods with Minikube, and include both technical explanations and practical examples to guide you through the steps.

Introduction to Minikube

Minikube is a tool that enables you to run a Kubernetes cluster locally on your machine. It creates a virtual machine (VM) and deploys a simple one-node Kubernetes cluster. Minikube is widely used for local development and learning Kubernetes concepts without the need for a multi-node cluster.

Why Mount Directories?

There are several reasons to mount a local directory into a pod:

  1. Code Synchronization: While developing applications, you may want to automatically sync changes in the source code on your local development machine with the application running in your pod.
  2. Configuration Management: Often, you may have configuration files or environment variables that should be shared between your development environment and your applications.
  3. Data Access: You may need to test your application with local data files without copying them into Docker images or remote storage.

Pre-requisites

Before proceeding, ensure the following is set up:

  • Minikube: Installed and running on your system.
  • kubectl: Command-line tool to manage Kubernetes clusters.
  • Virtualization Software: Such as VirtualBox or Docker Desktop, to run Minikube.

Configuring Minikube

Typically, to mount a local directory into a pod running in Minikube, you perform two primary steps: enabling the Minikube mount feature and configuring your pod to use the mounted directory.

Step 1: Start Minikube

Start Minikube with the desired driver and resource settings:

bash
minikube start --driver=docker

Step 2: Enable the Mount Feature

Minikube allows you to mount directories from your local machine into the Minikube VM. Use the minikube mount command to achieve this:

bash
minikube mount /path/to/local/dir:/path/to/mount/in/minikube
  • /path/to/local/dir: The local directory you wish to mount.
  • /path/to/mount/in/minikube: The target directory path in the Minikube environment.

Keep this terminal window open, as closing it terminates the mount.

Step 3: Configure a Pod to Use Mounted Directory

Create a Kubernetes manifest file (pod.yaml) that specifies a Kubernetes pod with a volume pointing to the mounted directory:

yaml
1apiVersion: v1
2kind: Pod
3metadata:
4  name: demo-pod
5spec:
6  containers:
7  - name: demo-container
8    image: busybox
9    command: ['sh', '-c', 'while true; do sleep 3600; done;']
10    volumeMounts:
11    - mountPath: /data
12      name: local-data
13  volumes:
14  - name: local-data
15    hostPath:
16      path: /path/to/mount/in/minikube
  • mountPath: Where your application accesses the mounted data within the container.
  • path: Same as /path/to/mount/in/minikube defined earlier in the minikube mount command.

Deploy the pod using:

bash
kubectl apply -f pod.yaml

Validating the Setup

Once the pod is running, you can validate our setup by executing commands within the pod. Access the pod using:

bash
kubectl exec -it demo-pod -- sh

Inside the pod, navigate to /data, where you should now see the contents of /path/to/local/dir from your local machine:

bash
cd /data
ls

Table Summary

StepCommand/Description
1. Start Minikubeminikube start --driver=docker
2. Enable Mountminikube mount /path/to/local/dir:/path/mount/in/minikube
3. Pod ConfigCreate pod.yaml with volume & mountPath
4. Deploy Podkubectl apply -f pod.yaml
5. Validatekubectl exec -it demo-pod -- sh Navigate to /data

Additional Considerations

  • Persistent Changes: Minikube's mount is not persistent across reboots. You need to re-run the minikube mount command whenever you restart Minikube.
  • Security: Be cautious with what directories you expose to your pods, especially when dealing with sensitive data.
  • Performance: Mounting directories may have performance implications depending on the number and size of files.

Conclusion

Mounting a local directory into a Minikube pod is a straightforward yet powerful technique for local development and testing. By following the steps outlined in this guide, you can ensure seamless access to local files within your Kubernetes applications, enhancing your development workflow significantly.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.