[經驗交流] k8s mount 文件到容器目錄

docker 的 volume 能夠 mount 單個文件(好比單個配置文件)到容器目錄、同時保留原目錄的內容。放到 k8s 中,結果卻變成了這樣:k8s 的 volume 把文件mount 到容器目錄後,該目錄下的其它文件卻消失了(若是 mount 到 /etc 下,只有 hostname,resolv.conf, passwd 等文件被保留)。html

 

這個連接給出瞭解決方法:git

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

 

下面的例子已測試經過(k8s v1.5): docker

先建立一個configmap,存儲一個配置文件 test-file,內容是一個單詞 'very':api

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

 

再建立一個 pod,按照下述配置掛載 configmap,注意 mountPath, subPath, path 的配置:bash

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

相關文章
相關標籤/搜索