如何将K8s Secret文件挂载到Pod指定路径且保留原有文件?
解决Kubernetes Secret单文件挂载路径问题
你需要使用subPath字段来实现单文件挂载,避免将Secret挂载为目录。具体配置如下:
正确的Deployment配置片段
spec: containers: - name: your-container-name image: your-tomcat-image volumeMounts: - name: secret-volume mountPath: /opt/tomcat/conf/secret.txt # 直接指定目标文件路径 subPath: secret.txt # 指定Secret中要挂载的文件键名 volumes: - name: secret-volume secret: secretName: mysecret # 你的Secret名称
原理说明
mountPath直接设为你想要的最终文件路径/opt/tomcat/conf/secret.txtsubPath指定Secret中存储的文件对应的键名(也就是你创建Secret时用的文件名,比如用kubectl create secret generic mysecret --from-file=secret.txt创建的话,键名就是secret.txt)- 这种方式只会将Secret中的
secret.txt文件挂载到目标路径,不会覆盖/opt/tomcat/conf目录下的其他文件(比如creds.txt),完全满足你的需求。
内容的提问来源于stack exchange,提问作者Mayank Singh Rathore
相关产品推荐
相关产品推荐

