kubernetes的配置管理configmap管理
配置管理
配置介绍
生产中的应用程序,一般都会涉及到配置文件,而配置文件经常会有变更,比如: 数据库连接,代码版本号,证书更新等
典型场景:
项目经历开发环境、测试环境、预发布环境、线上环境才能完成发布,而每个环境都有定义其独立的各
种配置,这些配置手工操作很繁杂,一些流行的开源项目,比如: Nacos,Consul,Apollo等可以实现配置管
理, 还有大公司专门开发了专用配置管理中心,如百度的disconf等。但通常此方法需要开发者参与,通
过开发者写代码实现调用,并且和某种开发语言深度绑定
在容器化大行其道的当前,在为容器化应用提供配置信息方法一般有下面方法
- 制作镜像时,提前将定义好的配置文件拷贝进镜像之中
- 制作镜像时,内置环境变量,在启动容器时通过环境变量向容器传递配置数据,容器启动后将无法传递新的变量
- 启动容器时,基于存储卷向容器传递配置文件,但需要提前准备外部的存储
- 启动容器时,传递自定义的启动的命令,选项,参数
kubernetes作为分布式容器调度平台,同样会遇到配置变更的问题,如果将资源删除,重新修改配置再重新创建,这种方法太繁琐。
kubernetes提供了对 Pod 容器应用可以实现集中式的配置管理功能的相关资源:
- ConfigMap
- Secret
- downwardAPI
- Projected
通过这些组件来实现向pod中的容器应用中注入配置信息的机制,从而避免了开发者参与
注意:对于运行中容器的配置改变,还需要通过应用程序重载相关配置才能生效
配置组件简介
https://kubernetes.io/zh-cn/docs/concepts/configuration/
Configmap
Configmap是Kubernetes集群中非常重要的一种配置管理资源对象。
借助于ConfigMap API可以向pod中的容器中注入配置信息。
ConfigMap不仅可以保存环境变量或命令行参数等属性,也可以用来保存整个配置文件或者JSON格式的文件。
各种配置属性和数据以 k/v或嵌套k/v 样式 存在到Configmap中
注意:所有的配置信息都是以明文的方式来进行保存,实现资源配置的快速获取或者更新。
Secret
Kubernetes集群中,有一些配置属性信息是非常敏感的,所以这些信息在传递的过程中,是不希望其他人能够看到的
Kubernetes提供了一种加密场景中的配置管理资源对象Secret。
它在进行数据传输之前,会对数据进行编码,在数据获取的时候,会对数据进行解码。从而保证整个数据传输过程的安全。
注意:这些数据通常采用Base64机制保存,所以安全性一般
DownwardAPI
downwardAPI 为运行在pod中的应用容器提供了一种反向引用。让容器中的应用程序了解所处pod或
Node的一些基础外部属性信息。
从严格意义上来说,downwardAPI不是存储卷,它自身就存在。
相较于configmap、secret等资源对象需要创建后才能使用,而downwardAPI引用的是Pod自身的运行
环境信息,这些信息在Pod启动的时候就存在。
Projected
一个 projected Volumes 投射卷可以将若干现有的卷源映射到容器内的同一个目录之上。
应用的配置
环境变量 $remote_addr
配置文件
configmap
表现为Pod
1)环境变量
2)配置文件
ConfigMap
ConfigMap 说明
https://kubernetes.io/zh-cn/docs/concepts/configuration/configmap/
https://kubernetes.io/zh-cn/docs/tasks/configure-pod-container/configure-podconfigmap/
Kubernetes提供了对pod中容器应用的集中配置管理组件:ConfigMap。
通过ConfigMap来实现向pod中的容器中注入配置信息的机制。

可以把configmap理解为Linux系统中的/etc目录,专门用来存储配置文件的目录
Kubernetes借助于ConfigMap对象实现了将配置信息从容器镜像中解耦,从而增强了工作负载的可移樟
性、使其配置更易于更改和管理并避免了将配置数据硬编码到Pod配置清单中
ConfigMap不仅仅可以保存单个属性,也可以用来保存整个配置文件。
虽然configmap可以对各种应用程序提供定制配置服务,但是一般不用它来替代专门的配置文件,
从Kubernetes v1.19版本开始,ConfigMap和Secret支持使用immutable字段创建不可变实例,实现不
可变基础设施效果
注意: Configmap 属于名称空间级别,只能被同一个名称空间的Pod引用

#环境变量
kubectl explain pod.spec.containers.env.valueFrom
#配置文件
kubectl explain pod.spec.volumes.configMap
基本属性
#kubectl explain cm binaryData #二进制数据 data #文本数据,支持变量和文件 immutable <boolean> #设为true,不能被修改只能删除,默认为nil可以随时被修改 #注意:基于data的方式传递信息的话,会在pod的容器内部生成一个单独的数据文件
命令行创建方式案例
范例:命令行创建基于key/value形式的变
kubectl create configmap cm-test1 --from-literal=key1='value1' --from-literal=key2='value2'
[root@master1 sc-nfs]# kubectl get cm NAME DATA AGE cm-test1 2 7s envoy-config 1 10d kube-root-ca.crt 1 14d
root@master1 sc-nfs]# kubectl get cm cm-test1 -o yaml apiVersion: v1 data: key1: value1 key2: value2 kind: ConfigMap metadata: creationTimestamp: "2026-05-24T09:07:03Z" name: cm-test1 namespace: default resourceVersion: "316769" uid: f650fe51-bdf6-44ea-b539-73996e641540
[root@master1 sc-nfs]# kubectl describe cm cm-test1 Name: cm-test1 Namespace: default Labels: <none> Annotations: <none> Data ==== key1: ---- value1 key2: ---- value2 BinaryData ==== Events: <none>
#删除CM
kubectl delete cm cm-test1
范例: 命令行创建基于key/value形式的变量配置
#创建CM
kubectl create configmap pod-test-config --from-literal=host="127.0.0.1" --from-literal=port="8888"
#验证创建成功
[root@master1 config]# kubectl get cm NAME DATA AGE envoy-config 1 10d kube-root-ca.crt 1 14d pod-test-config 2 16s
#显示CM内容
root@master1 config]# kubectl get cm pod-test-config -o yaml apiVersion: v1 data: host: 127.0.0.1 port: "8888" kind: ConfigMap metadata: creationTimestamp: "2026-05-24T09:17:49Z" name: pod-test-config namespace: default resourceVersion: "318069" uid: 66a0f215-6fb9-4213-8ee7-bd6a1ad4d2f1
#删除CM
kubectl delete cm pod-test-config
范例: 命令行创建基于环境变量文件的变量配置
#如果变量较多,使用上面方式一个个的设定环境变量太繁琐,可以全部添加到环境变量文件中然后基于它来创建CM。
#定制环境变量文件
[root@master1 config]# cat env a=1 b=2 c=3
#注意:env文件中所有的配置项以 "属性名=属性值" 格式定制
#将所有环境变量添加到到configmap中
kubectl create configmap cm-test2 --from-env-file=env
[root@master1 config]# kubectl get cm cm-test2 -o yaml apiVersion: v1 data: a: "1" b: "2" c: "3" kind: ConfigMap metadata: creationTimestamp: "2026-05-24T09:33:47Z" name: cm-test2 namespace: default resourceVersion: "319989" uid: 3ebb41cb-81c0-45ff-89b7-de9a2aaefc93
范例:命令行创建基于配置文件的文件形式CM
#直接将多个配置文件创建为一个ConfigMap
mkdir conf.d
[root@master1 conf.d]# ls app1.conf app2.conf app3.conf
[root@master1 conf.d]# cat app1.conf [app1] config1
[root@master1 conf.d]# cat app2.conf [app2] config2
#文件名自动成为key名
kubectl create configmap cm-test3 --from-file=./conf.d/app1.conf --from-file=./conf.d/app2.conf
[root@master1 config]# kubectl get cm cm-test3 -o yaml apiVersion: v1 data: app1.conf: | [app1] config1 app2.conf: | [app2] config2 kind: ConfigMap metadata: creationTimestamp: "2026-05-24T10:27:12Z" name: cm-test3 namespace: default resourceVersion: "321196" uid: f581c47a-ee5b-4b76-ac61-6c43a3e6f832
范例: 命令行创建基于目录的CM
[root@master1 config]# ls conf.d/ app1.conf app2.conf app3.conf [root@master1 config]#
[root@master1 ~]#cat conf.d/app1.conf
[app1]
config1
[root@master1 ~]#cat conf.d/app2.conf
[app2]
config2
[root@master1 ~]#cat conf.d/app3.conf
[app3]
config3
#直接将一个目录下的所有配置文件创建为一个ConfigMap
kubectl create configmap cm-test4 --from-file=./conf.d/
#结果显示:多个文件之间,属性名是文件名,属性值是文件内容
root@master1 config]# kubectl get cm cm-test4 -o yaml apiVersion: v1 data: app1.conf: | [app1] config1 app2.conf: | [app2] config2 app3.conf: | [app3] config3 kind: ConfigMap metadata: creationTimestamp: "2026-05-24T10:34:02Z" name: cm-test4 namespace: default resourceVersion: "322014" uid: 04788ce7-fc65-412c-8432-cb3e681d771d
[root@master1 config]# cat 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 create cm cm-nginx --from-file nginx.conf
kubectl get cm cm-nginx -o yaml
[root@master1 config]# kubectl get cm cm-nginx -o yaml apiVersion: v1 data: nginx.conf: |2 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; } kind: ConfigMap metadata: creationTimestamp: "2026-05-24T10:41:25Z" name: cm-nginx namespace: default resourceVersion: "322902" uid: 895fd07d-5123-4ada-9700-0a770c9fba66
ConfigMap 使用
使用ConfigMap主要有两种方式:
- 通过环境变量的方式直接传递pod
- 使用volume的方式挂载入到pod内的文件中
注意:
- ConfigMap必须在Pod之前创建
- 与ConfigMap在同一个namespace内的pod才能使用ConfigMap,即ConfigMap不能跨命名空间调用。
- ConfigMap通常存放的数据不要超过1M
- CM 可以支持实时更新,在原来的pod里面直接看到效果
通过环境变量的方式直接传递 Pod
引用ConfigMap对象上特定的key,以valueFrom赋值给Pod上指定的环境变量
也可以在Pod上使用envFrom一次性导入ConfigMap对象上的所有的key-value,key(可以统一附加特定前缀)即为环境变量,value自动成为相应的变量值
环境变量是容器启动时注入的,容器启动后变量的值不会随CM更改而发生变化,即一次性加载configmap,除非删除容器重建
通过环境变量脚本的方式,预处理这些配置信息
注意:环境变量的名称如果要用横线的话,最好使用下划线 _
环境变量格式
方式1:env 对指定的变量一个一个赋值
kubectl explain pod.spec.containers.env name #手工定制环境变量时,设置环境变量的名称,必选字段 value #手工定制环境变量时,直接设置环境变量的属性值,不通过CM获取配置,可选字 段 valueFrom #手工定制环境变量时,设置环境变量的属性来源,可以支持从 CM,Secret,downwordAPI获取 kubectl explain pod.spec.containers.env.valueFrom.configMapKeyRef name #引用指定的configmap key #引用指定的configmap中的具体哪个key optional #如果设置为false,标识该项是必选项,如果设置为true标识这个key是可选 的。默认false
#此方式实现过程
1)容器中自定义环境变量名
2)根据CM的名称和Key名,找到对应的value
3) 再将value赋值给容器的环境变量
ConfigMap 实战案例
范例:env 变量
#资源清单文件
[root@master1 config]# cat storage-configmap-simple-env.yaml apiVersion: v1 kind: ConfigMap metadata: name: cm-nginx-config data: port: "10086" #注意:只支持字符串,需要用引号引起来 user: "www" --- apiVersion: v1 kind: Pod metadata: name: configmap-env-test spec: containers: - name: configmap-env-test image: registry.cn-beijing.aliyuncs.com/wangxiaochun/nginx:1.20.0 env: - name: NGINX_HOST value: "10.0.0.100" #直接变量赋值 - name: NGINX_PORT valueFrom: configMapKeyRef: name: cm-nginx-config key: port optional: true - name: NGINX_USER valueFrom: configMapKeyRef: name: cm-nginx-config key: user optional: false
optional: true 时 这个变量有就有,没有都不会报错 没有都不会报错
optional: false 时 这个变量必须有,没有就报错
#配置解析:这里面我们可以使用两种方式在pod中传递变量
#资源创建
kubectl apply -f storage-configmap-simple-env.yaml
root@master1 config]# kubectl get cm NAME DATA AGE cm-nginx-config 2 27s envoy-config 1 10d kube-root-ca.crt 1 14d
[root@master1 config]# kubectl get pod NAME READY STATUS RESTARTS AGE configmap-env-test 1/1 Running 0 92s controller-deployment-demo-5f884bb8-2ppvf 1/1 Running 5 (3h57m ago) 44h controller-deployment-demo-5f884bb8-fscrl 1/1 Running 14 (3h57m ago) 7d23h controller-deployment-demo-5f884bb8-rfsbv 1/1 Running 13 (3h57m ago) 7d19h [root@master1 config]#
[root@master1 config]# kubectl exec -it configmap-env-test -- env PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin HOSTNAME=configmap-env-test TERM=xterm NGINX_PORT=10086 NGINX_USER=www NGINX_HOST=10.0.0.100 SERVICE_2_SERVICE_PORT=80 SERVICE_3_PORT_80_TCP=tcp://10.104.61.85:80 KUBERNETES_SERVICE_PORT_HTTPS=443 MYAPP_SERVICE_HOST=10.98.62.229 MYAPP_SERVICE_PORT=8080 MYAPP_PORT_8080_TCP_ADDR=10.98.62.229 SERVICE_2_PORT_80_TCP=tcp://10.98.59.151:80 SERVICE_3_PORT_80_TCP_PROTO=tcp SERVICE_2_SERVICE_HOST=10.98.59.151 SERVICE_3_SERVICE_PORT=80 KUBERNETES_SERVICE_HOST=10.96.0.1 MYAPP_SERVICE_PORT_8080_80=8080 SERVICE_2_PORT=tcp://10.98.59.151:80 KUBERNETES_PORT=tcp://10.96.0.1:443 KUBERNETES_PORT_443_TCP_PROTO=tcp MYAPP_PORT_8080_TCP_PORT=8080 SERVICE_2_PORT_80_TCP_PROTO=tcp KUBERNETES_PORT_443_TCP=tcp://10.96.0.1:443 SERVICE_2_PORT_80_TCP_ADDR=10.98.59.151 SERVICE_3_SERVICE_HOST=10.104.61.85 SERVICE_3_PORT=tcp://10.104.61.85:80 SERVICE_3_PORT_80_TCP_ADDR=10.104.61.85 KUBERNETES_PORT_443_TCP_ADDR=10.96.0.1 MYAPP_PORT_8080_TCP=tcp://10.98.62.229:8080 SERVICE_2_PORT_80_TCP_PORT=80 SERVICE_3_PORT_80_TCP_PORT=80 KUBERNETES_PORT_443_TCP_PORT=443 KUBERNETES_SERVICE_PORT=443 MYAPP_PORT=tcp://10.98.62.229:8080 MYAPP_PORT_8080_TCP_PROTO=tcp NGINX_VERSION=1.20.0 NJS_VERSION=0.5.3 PKG_RELEASE=1~buster HOME=/root
在线更新configmap 变量的值
kubectl edit cm cm-nginx-config
Please edit the object below. Lines beginning with a '#' will be ignored, # and an empty file will abort the edit. If an error occurs while saving this file will be # reopened with the relevant failures. # apiVersion: v1 data: port: "10086" user: www kind: ConfigMap metadata: annotations: kubectl.kubernetes.io/last-applied-configuration: | {"apiVersion":"v1","data":{"port":"10086","user":"www"},"kind":"ConfigMap","metadata":{"annotations":{},"name":"cm-nginx-config","namespace":"default"} } creationTimestamp: "2026-05-24T11:07:34Z" name: cm-nginx-config namespace: default resourceVersion: "326043"
# Please edit the object below. Lines beginning with a '#' will be ignored, # and an empty file will abort the edit. If an error occurs while saving this file will be # reopened with the relevant failures. # apiVersion: v1 data: port: "9927" user: www kind: ConfigMap metadata: annotations: kubectl.kubernetes.io/last-applied-configuration: | {"apiVersion":"v1","data":{"port":"10086","user":"www"},"kind":"ConfigMap","metadata":{"annotations":{},"name":"cm-nginx-config","namespace":"default"} } creationTimestamp: "2026-05-24T11:07:34Z"
pod没有改变 环境变量configmap修改了 不会动态更新到pod 要把pod删了重建
[root@master1 config]# kubectl exec -it configmap-env-test -- env PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin HOSTNAME=configmap-env-test TERM=xterm NGINX_PORT=10086 NGINX_USER=www NGINX_HOST=10.0.0.100 SERVICE_2_SERVICE_PORT=80 SERVICE_3_PORT_80_TCP=tcp://10.104.61.85:80 KUBERNETES_SERVICE_PORT_HTTPS=443 MYAPP_SERVICE_HOST=10.98.62.229 MYAPP_SERVICE_PORT=8080 MYAPP_PORT_8080_TCP_ADDR=10.98.62.229 SERVICE_2_PORT_80_TCP=tcp://10.98.59.151:80 SERVICE_3_PORT_80_TCP_PROTO=tcp SERVICE_2_SERVICE_HOST=10.98.59.151 SERVICE_3_SERVICE_PORT=80 KUBERNETES_SERVICE_HOST=10.96.0.1 MYAPP_SERVICE_PORT_8080_80=8080 SERVICE_2_PORT=tcp://10.98.59.151:80 KUBERNETES_PORT=tcp://10.96.0.1:443 KUBERNETES_PORT_443_TCP_PROTO=tcp MYAPP_PORT_8080_TCP_PORT=8080 SERVICE_2_PORT_80_TCP_PROTO=tcp KUBERNETES_PORT_443_TCP=tcp://10.96.0.1:443 SERVICE_2_PORT_80_TCP_ADDR=10.98.59.151 SERVICE_3_SERVICE_HOST=10.104.61.85 SERVICE_3_PORT=tcp://10.104.61.85:80 SERVICE_3_PORT_80_TCP_ADDR=10.104.61.85 KUBERNETES_PORT_443_TCP_ADDR=10.96.0.1 MYAPP_PORT_8080_TCP=tcp://10.98.62.229:8080 SERVICE_2_PORT_80_TCP_PORT=80 SERVICE_3_PORT_80_TCP_PORT=80 KUBERNETES_PORT_443_TCP_PORT=443 KUBERNETES_SERVICE_PORT=443 MYAPP_PORT=tcp://10.98.62.229:8080 MYAPP_PORT_8080_TCP_PROTO=tcp NGINX_VERSION=1.20.0 NJS_VERSION=0.5.3 PKG_RELEASE=1~buster HOME=/root
范例:volume 生成配置文件并更新生效
#configmap资源定义
[root@master1 config]# cat storage-configmap-simple-volume.yaml apiVersion: v1 kind: ConfigMap metadata: name: cm-volume data: author.txt: tom file.conf: | [app] config1 config2 --- apiVersion: v1 kind: Pod metadata: name: pod-volume-test spec: volumes: - name: volume-config #指定卷名 configMap: name: cm-volume #指定卷来自cm containers: - name: nginx image: registry.cn-beijing.aliyuncs.com/wangxiaochun/nginx:1.20.0 volumeMounts: - name: volume-config #调用前面定义的卷名 mountPath: /cmap/ #指定Pod中的挂载点目录
#应用
kubectl apply -f storage-configmap-simple-volume.yaml
#验证配置文件
root@master1 config]# kubectl exec -it pod-volume-test -- sh # ls /cmap author.txt file.conf # cat /cmap/author.txt tom# cat /cmap/file.conf [app] config1 config2
#在线更新配置文件中的内容
kubectl edit cm cm-volume
[root@master1 config]# kubectl edit cm cm-volume # Please edit the object below. Lines beginning with a '#' will be ignored, # and an empty file will abort the edit. If an error occurs while saving this file will be # reopened with the relevant failures. # apiVersion: v1 data: author.txt: tom file.conf: | [app] config1 config2 kind: ConfigMap metadata: annotations: kubectl.kubernetes.io/last-applied-configuration: | {"apiVersion":"v1","data":{"author.txt":"tom","file.conf":"[app]\nconfig1\nconfig2\n"},"kind":"ConfigMap","metadata":{"annotations":{},"name":"cm-volum e","namespace":"default"}} creationTimestamp: "2026-05-24T11:33:32Z" name: cm-volume namespace: default resourceVersion: "329194"
# Please edit the object below. Lines beginning with a '#' will be ignored, # and an empty file will abort the edit. If an error occurs while saving this file will be # reopened with the relevant failures. # apiVersion: v1 data: author.txt: jack file.conf: | [app] config1 config2 config3 kind: ConfigMap metadata: annotations: kubectl.kubernetes.io/last-applied-configuration: | {"apiVersion":"v1","data":{"author.txt":"tom","file.conf":"[app]\nconfig1\nconfig2\n"},"kind":"ConfigMap","metadata":{"annotations":{},"name":"cm-volum e","namespace":"default"}} creationTimestamp: "2026-05-24T11:33:32Z" name: cm-volume namespace: default resourceVersion: "329194" uid: c38717cb-0389-41c7-a306-9223505d2fef
#等一会儿进入pod可以看到配置文件变化
[root@master1 config]# kubectl exec -it pod-volume-test -- sh # # cat /cmap/author.txt tom# cat /cmap/file.conf [app] config1 config2 # cat /cmap/author.txt tom# cat /cmap/file.conf [app] config1 config2 # # cat /cmap/author.txt jack# # cat /cmap/file.conf [app] config1 config2 config3 #
#结果显示:无需重启pod对象,只要等待十几秒后,配置信息自动发生了变动
基于volume 生成配置文件 支持pod配置文件动态更新
环境变量不支持
#查看一下文件的存储方式
文件不是真实文件 是软链接 ..data是文件
# ls -la /cmap total 0 drwxrwxrwx 3 root root 94 May 24 11:39 . drwxr-xr-x 1 root root 51 May 24 11:33 .. drwxr-xr-x 2 root root 41 May 24 11:39 ..2026_05_24_11_39_41.690578659 lrwxrwxrwx 1 root root 31 May 24 11:39 ..data -> ..2026_05_24_11_39_41.690578659 lrwxrwxrwx 1 root root 17 May 24 11:33 author.txt -> ..data/author.txt lrwxrwxrwx 1 root root 16 May 24 11:33 file.conf -> ..data/file.conf
也是软链接 指向时间戳 时间戳一变会生成新的文件指向新的配置 发生变化的当前时间
# ls -la /cmap/..data lrwxrwxrwx 1 root root 31 May 24 11:39 /cmap/..data -> ..2026_05_24_11_39_41.690578659
再在线修改
kubectl edit cm cm-volume
# Please edit the object below. Lines beginning with a '#' will be ignored, # and an empty file will abort the edit. If an error occurs while saving this file will be # reopened with the relevant failures. # apiVersion: v1 data: author.txt: jack file.conf: | [app] config1 config2 config3 config4 kind: ConfigMap metadata: annotations: kubectl.kubernetes.io/last-applied-configuration: | {"apiVersion":"v1","data":{"author.txt":"tom","file.conf":"[app]\nconfig1\nconfig2\n"},"kind":"ConfigMap","metadata":{"annotations":{},"name":"cm-volum e","namespace":"default"}} creationTimestamp: "2026-05-24T11:33:32Z" name: cm-volume
[root@master1 config]# kubectl exec -it pod-volume-test -- sh # # ls -la /cmap/..data lrwxrwxrwx 1 root root 32 May 24 11:50 /cmap/..data -> ..2026_05_24_11_50_56.2938100345
# cat /cmap/file.conf [app] config1 config2 config3 config4
#结果显示:
#这些文件虽然看起来是在挂载的目录下,实际上,它是经过了两层的软连接才能找到真正挂载的文件
容器内挂载目录的生成的文件file --> ..data/file -->
..YYYY_MM_DD_HH_MM_SS.XXXXXXXX/file
#通过这种双层软连接的方式,只要容器支持重载技术,那么只需要更改配置文件就可以实现容器应用的变动
configmap表现磁盘文件支持动态更新 configmap文件改变了pod里面也改 双重软连接 最新数据放到新建带最新时间戳新目录下 实现自动更新, 通过时间戳机制实现
时间戳文件夹实现,k8s自动创建,把新的更改过的新数据 放在新建的时间戳新目录下,并且修改原有的文件软连接,删掉旧路径时间戳,指向新的时间戳
环境变量不支持
范例:volume 挂载CM中的全部内容
#命令行创建CM,创建nginx的配置信息
mkdir nginx-conf.d
[root@master1 nginx-conf.d]# cat default.conf server { listen 8080; server_name localhost; #access_log /var/log/nginx/host.access.log main; location / { root /usr/share/nginx/html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } }
[root@master1 nginx-conf.d]# cat myserver.conf server { listen 8888; server_name www.org; include /etc/nginx/conf.d/myserver-*.cfg; location / { root /usr/share/nginx/html; } }
[root@master1 nginx-conf.d]# cat myserver-gzip.cfg gzip on; gzip_comp_level 5; gzip_proxied expired no-cache no-store private auth; gzip_types text/plain text/css application/xml text/javascript;
[root@master1 nginx-conf.d]# cat myserver-status.cfg location /nginx-status { stub_status on; access_log off; }
#命令行创建CM
kubectl create configmap cm-nginx-conf-files --from-file=nginx-conf.d/default.conf
[root@master1 config]# kubectl create configmap cm-nginx-conf-files --from-file=nginx-conf.d/default.conf configmap/cm-nginx-conf-files created [root@master1 config]# kubectl get cm NAME DATA AGE cm-nginx-conf-files 1 4s envoy-config 1 10d kube-root-ca.crt 1 14d
#清单文件
[root@master1 config]# cat storage-configmap-nginx-file.yaml apiVersion: v1 kind: ConfigMap metadata: name: cm-nginx-index data: index.html: "Nginx Configmap page!" #单行内容的文件生成configmap --- apiVersion: v1 kind: Pod metadata: name: pod-nginx-conf-configmap spec: volumes: - name: nginx-conf configMap: name: cm-nginx-conf-files optional: false - name: nginx-index configMap: name: cm-nginx-index optional: false containers: - image: registry.cn-beijing.aliyuncs.com/wangxiaochun/nginx:1.20.0 name: nginx volumeMounts: - name: nginx-conf mountPath: /etc/nginx/conf.d/ readOnly: true - name: nginx-index mountPath: /usr/share/nginx/html/ readOnly: true
kubectl apply -f storage-configmap-nginx-file.yaml
[root@master1 config]# kubectl get cm NAME DATA AGE cm-nginx-conf-files 1 4m21s cm-nginx-index 1 13s envoy-config 1 10d kube-root-ca.crt 1 14d
root@master1 config]# kubectl get pod -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES controller-deployment-demo-5f884bb8-2ppvf 1/1 Running 5 (5h13m ago) 45h 10.244.1.192 node1.org <none> <none> controller-deployment-demo-5f884bb8-fscrl 1/1 Running 14 (5h13m ago) 8d 10.244.1.193 node1.org <none> <none> controller-deployment-demo-5f884bb8-rfsbv 1/1 Running 13 (5h13m ago) 7d21h 10.244.1.194 node1.org <none> <none> pod-nginx-conf-configmap 1/1 Running 0 70s 10.244.2.192 node2.org <none> <none>
[root@master1 config]# curl 10.244.2.192:8080 Nginx Configmap page![root@master1 config]#
在线修改configmap
kubectl edit cm cm-nginx-index
# Please edit the object below. Lines beginning with a '#' will be ignored, # and an empty file will abort the edit. If an error occurs while saving this file will be # reopened with the relevant failures. # apiVersion: v1 data: index.html: Nginx Configmap page! kind: ConfigMap metadata: annotations: kubectl.kubernetes.io/last-applied-configuration: | {"apiVersion":"v1","data":{"index.html":"Nginx Configmap page!"},"kind":"ConfigMap","metadata":{"annotations":{},"name":"cm-nginx-index","namespace":"d efault"}} creationTimestamp: "2026-05-24T12:24:54Z" name: cm-nginx-index namespace: default resourceVersion: "335381" uid: d2d3a858-109f-4b18-9150-5b97d5b3fe69
[root@master1 config]# kubectl edit cm cm-nginx-index # Please edit the object below. Lines beginning with a '#' will be ignored, # and an empty file will abort the edit. If an error occurs while saving this file will be # reopened with the relevant failures. # apiVersion: v1 data: index.html: Nginx Configmap page!2.0 kind: ConfigMap metadata: annotations: kubectl.kubernetes.io/last-applied-configuration: | {"apiVersion":"v1","data":{"index.html":"Nginx Configmap page!"},"kind":"ConfigMap","metadata":{"annotations":{},"name":"cm-nginx-index","namespace":"d efault"}} creationTimestamp: "2026-05-24T12:24:54Z" name: cm-nginx-index namespace: default
网页文件更新了
[root@master1 config]# curl 10.244.2.192:8080 Nginx Configmap page!2.0[root@master1 config]#
修改配置文件
kubectl edit cm cm-nginx-conf-files
# Please edit the object below. Lines beginning with a '#' will be ignored, # and an empty file will abort the edit. If an error occurs while saving this file will be # reopened with the relevant failures. # apiVersion: v1 data: default.conf: | server { listen 8080; server_name localhost; #access_log /var/log/nginx/host.access.log main; location / { root /usr/share/nginx/html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html {
# Please edit the object below. Lines beginning with a '#' will be ignored, # and an empty file will abort the edit. If an error occurs while saving this file will be # reopened with the relevant failures. # apiVersion: v1 data: default.conf: | server { listen 8888; server_name localhost; #access_log /var/log/nginx/host.access.log main; location / { root /usr/share/nginx/html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } }
改了配置文件
[root@master1 ~]# kubectl exec -it pod-nginx-conf-configmap -- cat /etc/nginx/conf.d/default.conf server { listen 8888; server_name localhost; #access_log /var/log/nginx/host.access.log main; location / { root /usr/share/nginx/html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } }
没有生效
[root@master1 config]# curl 10.244.2.192:8888 curl: (7) Failed to connect to 10.244.2.192 port 8888: Connection refused [root@master1 config]# curl 10.244.2.192:8080 Nginx Configmap page!2.0[root@master1 config]#
要重启可以生效
[root@master1 ~]# kubectl exec -it pod-nginx-conf-configmap -- bash root@pod-nginx-conf-configmap:/# ng ngettext nginx nginx-debug root@pod-nginx-conf-configmap:/# ng ngettext nginx nginx-debug root@pod-nginx-conf-configmap:/# nginx -s reload 2026/05/24 12:36:11 [notice] 49#49: signal process started
[root@master1 config]# curl 10.244.2.192:8888 Nginx Configmap page!2.0[root@master1 config]#
浙公网安备 33010602011771号