問題
如果只是使用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也有類似的掛載問題,還沒找到解決方案,有大佬知道解決方案的,希望可以不吝賜教,在評論區留下解決方案可否