minikube + nginx + volumeMount not working

7/15/2017

I'm following example found here.

I'm simply trying to understand how volumes work with Kubernetes. I'm testing locally so I need to contend with minikube. I'm trying to make this as simple as possible. I'm using nginx and would like to have it display content that is mounted from a folder on my localhost.

Environment: macOS 10.12.5 minikube 0.20.0 + xhvve VM

I'm using the latest ngninx image from GitHub with no modifications.

This works perfectly when I run the docker image outside of minikube.

docker run --name flow-4 \
 -v $(pwd)/website:/usr/share/nginx/html:ro \
 -P -d nginx

But when I try to run it in minikube I get a 404 response when I visit the hosted page -always. Why?

Here are my kubernetes config files...
kubernets/deploy/deployment.yaml

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  labels:
    run: flow-4
  name: flow-4
spec:
  replicas: 1
  selector:
    matchLabels:
      run: flow-4
  template:
    metadata:
      labels:
        run: flow-4
    spec:
      containers:
      - image: nginx
        name: flow-4
        ports:
        - containerPort: 80

        volumeMounts:
        - mountPath: /usr/share/nginx/html
          name: flow-4-volume
      volumes:
      - name: flow-4-volume
        hostPath:
          path: /Users/myuser/website

kubernets/deploy/svc.yaml

apiVersion: v1
kind: Service
metadata:
  labels:
    run: flow-4
  name: flow-4
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
  selector:
    run: flow-4
  type: NodePort

Finally, I run it like this:

kubectl create -f kubernetes/deploy/
minikube service flow-4

When it opens in my browser, instead of seeing my index.html page in the website folder, I just get a '404 Not Found' message (above a nginx/1.13.3 footer)

Why am I getting 404? Is nginx not able to see the contents of my mounted folder? Does the VM hosting kubernetes not have access to my 'website' folder?

I suspect this is the problem. I ssh into the kubernetes pod

kubectl exec -it flow-4-1856897391-m0jh1 /bin/bash

When I look in the /usr/share/nginx/html folder, it is empty. If I manually add an index.html file, then I can see it in my browser. But why won't Kubernetes mount my local drive to this folder?

Update

There seems to be something wrong with mounting full paths from my /Users/** folder. Instead, I used the 'minikube mount' command to mount local folder container index.html into the minikube VM. Then in a separate terminal I started my deployment and it could see the index.html file just fine.

Here is my updated deployment.yaml file which has clearer file names to better explain the different folders and where they are mounted...

Here are my kubernetes config files...
kubernets/deploy/deployment.yaml

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  labels:
    run: flow-4
  name: flow-4
spec:
  replicas: 1
  selector:
    matchLabels:
      run: flow-4
  template:
    metadata:
      labels:
        run: flow-4
    spec:
      containers:
      - image: nginx
        name: flow-4
        ports:
        - containerPort: 80

        volumeMounts:
        - mountPath: /usr/share/nginx/html
          name: flow-4-volume
      volumes:
      - name: flow-4-volume
        hostPath:
          path: /kube-website

It's using the same svc.yaml file from earlier in the post.

I then ran the whole thing like this from my current directory.
1. mkdir local-website
2. echo 'Hello from local storage' > local-website/index.html
3. minikube mount local-website:/kube-website
Let this run....

In a new terminal, same folder...
4. kubectl create -f kubernetes/deploy/

Once all the pods are running...
5. minikube service flow-4

You should see the 'Hello from local storage' message great you in your browser. You can edit the local index.html file and then refresh your browser to see the contents change.

You can tear it all down with this... kubectl delete deployments,services flow-4

-- anschoewe
docker
kubernetes
minikube
nginx

1 Answer

7/18/2017

Probably the folder you created is not in kubernetes node (it is minikube vm).

Try to create folder inside vm and try again

ssh -i ~/.minikube/machines/minikube/id_rsa docker@$(minikube ip)
mkdir /Users/myuser/website

Also take a look at minikube host mount folder feature

-- Larry Cai
Source: StackOverflow