问题:传统上直接挂载会直接覆盖原目录
问题还原
挂载配置
重启容器挂了
进入容器查看
文件不见了,只剩下挂载文件了,目录被覆盖了
问题解决
subpath:
1、当挂载目录存在多个文件,不会覆盖所有文件;
2、挂载单个文件而不是整个目录。
修改配置
subpath热更新问题:
1. cm或secret以subpath挂载,那么它们是不 会感知configmap或secret的更新;
2. 变量内容也不会感知,不会更新pod中的变量。
解决思路:
可以把这个步骤放到初始化容器完成
要确保在nginx进程启动之前完成软链接,不然加载不上
具体的执行顺序如下:
- 容器启动
postStart
钩子执行,创建软链接/mnt/nginx.conf
->/usr/local/nginx/conf/
- Nginx 进程启动,加载配置文件
/usr/local/nginx/conf/nginx.conf
将configmap或secret挂载到其他目录,然后在容器启动前执行命令,做软连接至需要挂载的目录。
postStart: # 容器启动前执行命令 exec: command: [ "/bin/sh", "-c", "ln -sv /mnt/nginx.conf /usr/local/nginx/conf/" ]
清单
apiVersion: apps/v1 kind: Deployment metadata: name: nginx spec: replicas: 1 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx:latest ports: - containerPort: 80 volumeMounts: - name: nginx-config mountPath: /mnt subPath: nginx.conf lifecycle: postStart: exec: command: ["/bin/sh", "-c", "ln -sv /mnt/nginx.conf /usr/local/nginx/conf/"] volumes: - name: nginx-config configMap: name: nginx-config
热更新
验证自动同步功能
修改configmap 文件value
可以看到已同步到容器内
当configmap配置文件发生修改后,虽然容器内应用服务的配置文件发生了改变,还得重新启动服务使配置文件生效
![](/i/l/?n=20&i=blog/2170925/202111/2170925-20211121171405595-980869193.png)
![](/i/l/?n=20&i=blog/2170925/202111/2170925-20211121171419795-72028010.png)
标签:configmap,name,容器,subpath,nginx,secret,conf,挂载 From: https://www.cnblogs.com/rtnb/p/18220605