使用 minikube 的 HostPath - Kubernetes [英] HostPath with minikube - Kubernetes

查看:75
本文介绍了使用 minikube 的 HostPath - Kubernetes的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:我连接到 minikubevm,我看到我的主机目录已安装,但那里没有文件.此外,当我在那里创建文件时,它不会在我的主机中.它们之间有任何联系

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

我尝试挂载一个主机目录以使用 kubernetes 开发我的应用程序.

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

按照文档的建议,我正在使用 minikube 在我的电脑上运行我的 kubernetes 集群.目标是使用 docker 和 kubernetes 创建一个开发环境来开发我的应用程序.我想挂载一个本地目录,这样我的 docker 就会从那里读取代码应用程序.但这是行不通的.任何帮助将不胜感激.

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.

我的测试应用(server.js):

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);

我的 Dockerfile:

my Dockerfile:

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

我的 pod kubernetes 配置:(pod-configuration.yaml)

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

我的文件夹是:

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

当我在没有 hostPath 卷的情况下运行我的 docker 映像时,它当然可以工作,但问题是每次更改时我都必须重新创建对开发来说确实不强大的映像,这就是我需要卷 hostPath 的原因.

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 ?

感谢您的帮助.

推荐答案

看起来解决方案是使用 privilaged container,或者手动挂载你的主文件夹以允许 MiniKube VM 从你的 hostPath 中读取 -- https://github.com/boot2docker/boot2docker#virtualbox-guest-additions.(感谢 Eliel 解决这个问题).

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).

绝对可以使用 minikube 配置 hostPath 卷 - 但是有很多怪癖并且对这个特定问题没有很好的支持.

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.

尝试从 Dockerfile 中删除 ADD code//code.Docker 的添加"指令正在将代码从您的主机复制到您的容器的 /code 目录.这就是重建图像成功更新代码的原因.

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.

当 Kubernetes 尝试将容器的 /code 目录挂载到主机路径时,它发现该目录已经充满了烘焙到镜像中的代码.如果您将其从构建步骤中移除,Kubernetes 应该能够在运行时成功挂载主机路径.

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.

还要确保检查主机上 code/ 目录的权限.

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

我唯一的其他想法与安装在根目录中有关.在将 Kubernetes hostPath 卷挂载到/从根目录中的目录中时遇到问题(我认为这是与权限相关的).因此,要尝试的其他方法是像 /var/www/html 这样的 mountPath.

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.

这是一个功能性 hostPath 卷的示例:

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

这篇关于使用 minikube 的 HostPath - Kubernetes的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆