[經驗交流] 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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM