带minikube的HostPath-Kubernetes [英] HostPath with minikube - Kubernetes

查看:311
本文介绍了带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在我的PC上运行kubernetes集群.目标是使用docker和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.

我的测试应用(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 ?

感谢您的帮助.

推荐答案

看来解决方案是使用特权容器,或手动安装主文件夹以允许MiniKube VM从您的hostPath中读取-

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的"ADD"指令正在将代码从主机复制到您的容器的/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天全站免登陆