HostPath with minikube - Kubernetes

Eliel Haouzi picture Eliel Haouzi · Jul 31, 2016 · Viewed 11.6k times · Source

UPDATE: I connected to the minikubevm and I see my host directory mounted but there is no files there. Also when I create a file there it will not in my host machine. Any link are between them

I try to mount an host directory for developing my app with kubernetes.

As the doc recommended, I am using minikube for running my kubernetes cluster on my pc. The goal is to create a develop environment with docker and kubernetes for develop my app. I want to mount a local directory so my docker will read the code app from there. But it is not work. Any help would be really appreciate.

my test app (server.js):

var http = require('http');
var handleRequest = function(request, response) {
response.writeHead(200);
response.end("Hello World!");
}
var www = http.createServer(handleRequest);
www.listen(8080);

my Dockerfile:

FROM node:latest
WORKDIR /code
ADD code/ /code
EXPOSE 8080
CMD server.js

my pod kubernetes configuration: (pod-configuration.yaml)

apiVersion: v1
kind: Pod
metadata:
  name: apiserver
spec:
  containers:
  - name: node
    image: myusername/nodetest:v1
    ports:
    - containerPort: 8080
    volumeMounts:
    - name: api-server-code-files
      mountPath: /code
  volumes:
  - name: api-server-code-files
    hostPath:
      path: /home/<myuser>/Projects/nodetest/api-server/code

my folder are:

/home/<myuser>/Projects/nodetest/
- pod-configuration.yaml
- api-server/
    - Dockerfile
    - code/
        - server.js

When I running my docker image without the hostPath volume it is of course works but the problem is that on each change I will must recreate my image that is really not powerful for development, that's why I need the volume hostPath.

Any idea ? why i don't success to mount my local directory ?

Thanks for the help.

Answer

springle picture springle · Aug 2, 2016

EDIT: Looks like the solution is to either use a privilaged container, or to manually mount your home folder to allow the MiniKube VM to read from your hostPath -- https://github.com/boot2docker/boot2docker#virtualbox-guest-additions. (Credit to Eliel for figuring this out).

It is absolutely possible to configure a hostPath volume with minikube - but there are a lot of quirks and there isn't very good support for this particular issue.

Try removing ADD code/ /code from your Dockerfile. Docker's "ADD" instruction is copying the code from your host machine into your container's /code directory. This is why rebuilding the image successfully updates your code.

When Kubernetes tries to mount the container's /code directory to the host path, it finds that this directory is already full of the code that was baked into the image. If you take this out of the build step, Kubernetes should be able to successfully mount the host path at runtime.

Also be sure to check the permissions of the code/ directory on your host machine.

My only other thought is related to mounting in the root directory. I had issues when mounting Kubernetes hostPath volumes to/from directories in the root directory (I assume this was permissions related). So, something else to try would be a mountPath like /var/www/html.

Here's an example of a functional hostPath volume:

apiVersion: v1
kind: Pod
metadata:
  name: example
spec:
  volumes:
    - name: example-volume
      hostPath:
        path: '/Users/example-user/code'
  containers:
    - name: example-container
      image: example-image
      volumeMounts:
        - mountPath: '/var/www/html'
          name: example-volume