K8s掛載文件到目錄,不清空原有目錄文件


問題

如果只是使用mount功能來掛載一個文件到某個路徑下,則這個路徑下原有的文件將會被清空。

掛載config.toml到/etc/apt/下

apiVersion: apps/v1
kind: Deployment
    spec:
      volumes:
        - name: runner-config-demo1
          configMap:
            name: runner-config-demo1
            items:
              - key: config.toml
                path: config.toml
      containers:
        - name: lcdp-runner
          image: gitlab-runner:v13.5.0
          volumeMounts:
            - name: runner-config-demo1
              mountPath: /etc/apt/

掛載前

$ ls /etc/apt/
apt.conf.d  auth.conf.d  preferences.d  sources.list  sources.list.d  trusted.gpg.d

掛載后

$ ls /etc/apt/
config.toml

解決

使用subPath功能即可

apiVersion: apps/v1
kind: Deployment
    spec:
      volumes:
        - name: runner-config-demo1
          configMap:
            name: runner-config-demo1
            items:
              - key: config.toml
                path: config.toml
      containers:
        - name: lcdp-runner
          image: gitlab-runner:v13.5.0
          volumeMounts:
            - name: runner-config-demo1
              # 注意,區別只在此處,另外,mountPath和subPath都要加上config.toml。
              mountPath: /etc/apt/config.toml
              subPath: config.toml

掛載后

$ ls /etc/apt/
apt.conf.d  auth.conf.d  config.toml  preferences.d  sources.list  sources.list.d  trusted.gpg.d

備注

Docker Swarm也有類似的掛載問題,還沒找到解決方案,有大佬知道解決方案的,希望可以不吝賜教,在評論區留下解決方案可否


免責聲明!

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



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