gpt4 book ai didi

kubernetes - 如何使用 hostPath 将单个文件映射到 kubernetes pod?

转载 作者:行者123 更新时间:2023-12-02 11:34:51 26 4
gpt4 key购买 nike

我有一个自己的nginx配置/home/ubuntu/workspace/web.conf由脚本生成。我更喜欢将它放在 /etc/nginx/conf.d 下除了default.conf
以下是nginx.yaml
api版本:v1
种类: pod
元数据:
名称:nginx
规范:
卷:
- 名称:网络 session
主机路径:
路径:/home/ubuntu/workspace/web.conf
容器:
- 图片:nginx
名称:nginx
端口:
- 容器端口:18001
协议(protocol):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 中做到这一点?

顺便说一句:我使用 minikube 0.21.0Ubuntu 16.04 LTSkvm

最佳答案

尝试使用 subPath键入您的volumeMounts像这样:

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

关于kubernetes - 如何使用 hostPath 将单个文件映射到 kubernetes pod?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45484366/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com