SubPath解决挂载覆盖问题

当挂载ConfigMap或Secret到容器内部时,会覆盖原有目录。比如将nginx.conf挂载到/etc/nginx/nginx.conf,如果使用ConfigMap挂载,就会把/etc/nginx这个目录完全覆盖掉,只会留下一个nginx.conf,使得文件的全限定名变为了/etc/nginx.conf。

可以使用subPath字段解决挂载覆盖的问题。

SubPath主要用于将同一个Volume的数据挂载到不同的路径,同时也能用subPath的功能解决挂载覆盖的问题,比如有一个保存了Nginx配置文件的ConfigMap需要挂载到Nginx容器的/etc/nginx目录,那么定义Deployment的Volume和VolumeMounts的格式如下:

containers:
  ...
  volumeMounts:
  - name: nginx-config
    mountPath: /etc/nginx/nginx.conf
    subPath: etc/nginx/nginx.conf
volumes: 
  - name: nginx-config
    configMap:
      name: nginx-config-cm
      defaultMode: 0420
      items:
      - key: nginx.conf
        path: etc/nginx/nginx.conf

最后更新于