HostPath with minikube - Kubernetes

前端 未结 3 885
刺人心
刺人心 2020-12-05 10:32

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 li

相关标签:
3条回答
  • 2020-12-05 11:00

    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
    
    0 讨论(0)
  • 2020-12-05 11:01

    They have now given the minikube mount which works on all environment

    https://github.com/kubernetes/minikube/blob/master/docs/host_folder_mount.md

    Tried on Mac:

    $ minikube mount ~/stuff/out:/mnt1/out
    Mounting /Users/macuser/stuff/out into /mnt1/out on the minikube VM
    This daemon process needs to stay alive for the mount to still be accessible...
    ufs starting
    

    And in pod:

    apiVersion: v1
    kind: Pod
    metadata:
      name: myServer
    spec:
      containers:
      - name: myServer
        image: myImage
        volumeMounts:
        - mountPath: /mnt1/out
          name: volume
        # Just spin & wait forever
        command: [ "/bin/bash", "-c", "--" ]
        args: [ "while true; do sleep 30; done;" ]
      volumes:
      - name: volume
        hostPath:
          path: /mnt1/out
    
    0 讨论(0)
  • 2020-12-05 11:10

    Best practice would be building the code into your image, you should not run an image with code just coming from the disk. Your Dockerfile should look more like:

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

    Then you run the Image on Kubernetes without any volumes. You need to rebuild the image and update the pod every time you update the code.

    Also, I'm currently not aware that minikube allows for mounts between the VM it creates and the host you are running it on.

    If you really want the extreme fast feedback cycle of changing code while the container is running, you might be able to use just Docker by itself with -v /path/to/host/code:/code without Kubernetes and then once you are ready build the image and deploy and test it on minikube. However, I'm not sure that would work if you're changing the main .js file of your node app.

    0 讨论(0)
提交回复
热议问题