问题描述
configmap 挂载在容器的路径中会覆盖掉容器路径下原有的文件,如何支持 configmap 的每个 key-value 挂载在容器中,且不会覆盖掉原目录下的文件。
问题分析
可以通过 subpath,挂载到容器的子路径中。
解决方案
- configmap yaml文件
apiVersion: v1
kind: ConfigMap
metadata:
name: configmap-subpath
namespace: default
data:
example.subpath.1: subpath1
example.subpath.2: subpath2
example.subpath.file: |-
property.1=value-1
property.2=value-2
property.3=value-3
- 创建 configmap
$ kubectl apply -f subpath-cm.yaml
- 查看创建的 configmap
$ kubectl get cm configmap-subpath -n default
NAME DATA AGE
configmap-subpath 3 31s
- pod yaml 文件
apiVersion: v1
kind: Pod
metadata:
name: configmap-subpath-test
namespace: default
labels:
purpose: configmap-subpath-test
spec:
containers:
- name: configmap-subpath-test
image: nginx
volumeMounts:
- name: config-volume
mountPath: /etc/nginx/conf.d/example.subpath.file
subPath: example.subpath.file
volumes:
- name: config-volume
configMap:
name: configmap-subpath
- 创建 pod
$ kubectl apply -f pod-configmap.yaml
- 查看 pod
$ kubectl get pod -n default
NAME READY STATUS RESTARTS AGE
configmap-subpath-test 1/1 Running 0 9m38s
- 验证
# 查看/etc/nginx/nginx.conf被覆盖
$ kubectl exec -it -n default configmap-subpath-test -- cat /etc/nginx/nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
# 查看挂载文件
$ kubectl exec -it -n default configmap-subpath-test -- cat /etc/nginx/conf.d/example.subpath.file
property.1=value-1
property.2=value-2
property.3=value-3
如果您有其他问题,欢迎您联系火山引擎技术支持服务