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

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

问题描述

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

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

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

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

var http = require('http');var handleRequest = 函数(请求,响应){response.writeHead(200);response.end("Hello World!");}var www = http.createServer(handleRequest);www.listen(8080);

我的 Dockerfile:

FROM 节点:最新工作目录/代码添加代码//代码曝光 8080CMD server.js

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

apiVersion: v1种类:豆荚元数据:名称:apiserver规格:容器:- 名称:节点图像:我的用户名/节点测试:v1端口:- 容器端口:8080卷挂载:- 名称:api-server-code-files挂载路径:/代码卷:- 名称:api-server-code-files主机路径:路径:/home//Projects/nodetest/api-server/code

我的文件夹是:

/home//Projects/nodetest/- pod-configuration.yaml- api服务器/- Dockerfile- 代码/- server.js

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

有什么想法吗?为什么我没有成功挂载我的本地目录?

感谢您的帮助.

解决方案

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

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

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

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

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

我唯一的其他想法与在根目录中安装有关.我在将 Kubernetes hostPath 卷安装到/从根目录中的目录安装时遇到问题(我认为这与权限相关).因此,可以尝试使用类似 /var/www/html 的 mountPath.

以下是功能性 hostPath 卷的示例:

apiVersion: v1种类:豆荚元数据:名称:示例规格:卷:- 名称:示例卷主机路径:路径:'/Users/example-user/code'容器:- 名称:示例容器图像:示例图像卷挂载:- 挂载路径:'/var/www/html'名称:示例卷

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.

解决方案

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

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

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