灰灰鼠精灵 发表于 2018-1-5 20:38:51

[经验交流] 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
页: [1]
查看完整版本: [经验交流] k8s mount 文件到容器目录