[经验交流] k8s mount 文件到容器目录

docker 的 volume 可以 mount 单个文件(比如单个配置文件)到容器目录、同时保留原目录的内容。放到 k8s 中,结果却变成了这样:k8s 的 volume 把文件mount 到容器目录后,该目录下的其它文件却消失了(如果 mount 到 /etc 下,只有 hostname,resolv.conf, passwd 等文件被保留)。

 

这个链接给出了解决方法:

https://github.com/dshulyak/kubernetes.github.io/commit/d58ba7b075bb4848349a2c920caaa08ff3773d70

 

下面的例子已测试通过(k8s v1.5): 

先创建一个configmap,存储一个配置文件 test-file,内容是一个单词 'very':

apiVersion: v1
kind: ConfigMap
metadata:
  name: test-config
  namespace: default
data:
  test-file: very                    

 

再创建一个 pod,按照下述配置挂载 configmap,注意 mountPath, subPath, path 的配置:

apiVersion: v1
kind: Pod
metadata:
  name: test-pod
spec:
  containers:
    - name: test-container
      image: busybox
      imagePullPolicy: IfNotPresent
      command: ["sleep", "10000"]
      volumeMounts:
      - mountPath: /etc/test-file
        name: config-volume
        subPath: path/to/test-file
  volumes:
    - name: config-volume
      configMap:
        name: test-config
        items:
        - key: test-file
          path: path/to/test-file
  restartPolicy: Always

 

这样就吧 test-file 成功插入到了 /etc 目录下,该目录下的其它文件都保留了下来,没有被替换:

kubectl exec test-pod -- ls /etc

group
hostname
hosts
localtime
mtab
passwd
resolv.conf
shadow
test-file

 

转载于:https://www.cnblogs.com/hahp/p/6760981.html

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值