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

2024-05-02

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

下面是nginx.yaml



apiVersion: v1
kind: Pod
metadata:
    name: nginx
spec:
    volumes:
      - name: webconf
        hostPath:
          path: /home/ubuntu/workspace/web.conf
    containers:
      - image:  nginx
        name: nginx
        ports:
          - containerPort: 18001
            protocol: TCP                               
        volumeMounts:
          - mountPath: /etc/nginx/conf.d/web.conf
            name: web
  

虽然它仅映射为文件夹



$ kubectl create -f nginx.yaml
pod "nginx" created
$ kubectl exec -it nginx -- bash
root@nginx:/app# ls -al /etc/nginx/conf.d/
total 12
drwxr-xr-x 1 root root 4096 Aug  3 12:27 .
drwxr-xr-x 1 root root 4096 Aug  3 11:46 ..
-rw-r--r-- 2 root root 1093 Jul 11 13:06 default.conf
drwxr-xr-x 2 root root    0 Aug  3 11:46 web.conf
  

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

我怎样才能在 kubernetes 中做到这一点?

顺便说一句:我使用 minikube0.21.0 on Ubuntu 16.04 LTS with kvm


尝试使用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

Example:

$ 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.
...
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用 hostPath 将单个文件映射到 kubernetes pod 中? 的相关文章

随机推荐