【问题标题】:How to map one single file into kubernetes pod using hostPath?如何使用 hostPath 将单个文件映射到 kubernetes pod?
【发布时间】:2018-01-11 01:37:54
【问题描述】:

我有一个自己的由脚本生成的 nginx 配置/home/ubuntu/workspace/web.conf。除了default.conf之外,我更喜欢在/etc/nginx/conf.d下拥有它

下面是nginx.yaml

api版本:v1 种类:豆荚 元数据: 名称:nginx 规格: 卷: - 名称:网络会议 主机路径: 路径:/home/ubuntu/workspace/web.conf 容器: - 图片:nginx 名称:nginx 端口: - 容器端口:18001 协议:TCP 卷装: - 挂载路径:/etc/nginx/conf.d/web.conf 名称:网络

虽然它仅映射为文件夹

$ kubectl 创建 -f nginx.yaml pod“nginx”创建 $ kubectl exec -it nginx -- bash root@nginx:/app# ls -al /etc/nginx/conf.d/ 共 12 个 drwxr-xr-x 1 根 4096 8 月 3 日 12:27 。 drwxr-xr-x 1 根 4096 8 月 3 日 11:46 .. -rw-r--r-- 2 root root 1093 Jul 11​​ 13:06 default.conf drwxr-xr-x 2 root root 0 8 月 3 日 11:46 web.conf

它适用于 docker 容器-v hostfile:containerfile

如何在 kubernetes 中做到这一点?

顺便说一句:我在 Ubuntu 16.04 LTS 上使用 minikube 0.21.0kvm

【问题讨论】:

  • 卷名与 web vs webconf 不匹配

标签: kubernetes kvm minikube


【解决方案1】:

尝试在您的volumeMounts 上使用subPath 键,如下所示:

apiVersion: v1
kind: Pod
metadata:
  name: singlefile
spec:
  containers:
  - image: ubuntu
    name: singlefiletest
    command:
      - /bin/bash
      - -c
      - ls -la /singlefile/ && cat /singlefile/hosts
    volumeMounts:
    - mountPath: /singlefile/hosts
      name: etc
      subPath: hosts
  volumes:
  - name: etc
    hostPath:
      path: /etc

例子:

$ kubectl apply -f singlefile.yaml
pod "singlefile" created
$ kubectl logs singlefile
total 24
drwxr-xr-x. 2 root root 4096 Aug  3 12:50 .
drwxr-xr-x. 1 root root 4096 Aug  3 12:50 ..
-rw-r--r--. 1 root root 1213 Apr 26 21:25 hosts
# /etc/hosts: Local Host Database
#
# This file describes a number of aliases-to-address mappings for the for 
# local hosts that share this file.
...

【讨论】:

  • 感谢您的快速响应,它实际上可以在没有subPath的情况下使用
【解决方案2】:

其实是由minikube使用的kvm引起的。

path: /home/ubuntu/workspace/web.conf

如果我登录到 minikube,它是 vm 中的文件夹。

$ ls -al /home/ubuntu/workspace # in minikube host
total 12
drwxrwxr-x 2 ubuntu ubuntu 4096 Aug  3 12:11 .
drwxrwxr-x 5 ubuntu ubuntu 4096 Aug  3 19:28 ..
-rw-rw-r-- 1 ubuntu ubuntu 1184 Aug  3 12:11 web.conf
$ minikube ssh
$ ls -al /home/ubuntu/workspace # in minikube vm
total 0
drwxr-xr-x 3 root root 0 Aug  3 19:41 .
drwxr-xr-x 4 root root 0 Aug  3 19:41 ..
drwxr-xr-x 2 root root 0 Aug  3 19:41 web.conf

我不知道为什么 kvm 主机文件夹会这样共享。

因此我改用minikube mount 命令,参见host_folder_mount.md,然后它按预期工作。

【讨论】:

  • 链接已损坏,如果仍然可用,您介意在此处添加实际代码吗?
猜你喜欢
  • 1970-01-01
  • 2018-12-16
  • 2018-11-28
  • 2019-09-27
  • 1970-01-01
  • 2023-02-20
  • 2018-01-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多