1.ConfigMap基本概念
1.1 什么是ConfigMap
ConfigMap资源主要为容器注入相关的程序配置信息,用来定制程序的运行方式,比如Redis监听端口、最大客户端连接数。
当定义好一个ConfigMap资源后,如果Pod需要使用,可以通过 通过环境变量、或存储卷的形式将其挂载 并加载相关的配置,降低了配置与镜像文件的耦合关系。
1.2 为什么需要ConfigMap
将 应用配置信息与程序进行分离 ,这样可以使得应用程序被更好地复用,通过不同的配置能实现更灵活的功能,例如:在测试环境中Nginx提供test域名访问,且没配置压缩功能,而在生产环境中则需要提供www域名访问,且需要开启压缩功能,所以将应用容器与配置分离,根据不同的环境调用不同的 ConfigMap 配置,能有效的降低耦合度和复杂度。

2.创建ConfigMap
2.1 基于命令创建CM
1.使用 kubectl create configmap 命令使用 –-from-literal 选项给出键值对来创建 ConfigMap
[root@k8s-master ~]# kubectl create configmap nginx-command-cm --from-literal=nginx.host='0.0.0.0' --from-literal=nginx.port='8899'
configmap/nginx-command-cm created
2.通过 kubectl get configmap 查看 nginx-comand-cm 对象YAML格式可以看出,ConfigMap资源没有 sepc 和 status,而是直接使用data字段嵌套键值数据。
[root@k8s-master ~]# kubectl get configmaps
NAME DATA AGE
kube-root-ca.crt 1 58d
nginx-command-cm 2 46s
[root@k8s-master ~]# kubectl get configmaps nginx-command-cm -o yaml
apiVersion: v1
data:
nginx.host: 0.0.0.0
nginx.port: "8899"
kind: ConfigMap
metadata:
creationTimestamp: "2022-12-28T03:06:41Z"
name: nginx-command-cm
namespace: default
resourceVersion: "339704"
uid: a3347792-63d5-4735-9de0-2bc061f134c7
[root@k8s-master ~]# kubectl describe configmaps nginx-command-cm
Name: nginx-command-cm
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
nginx.host:
----
0.0.0.0
nginx.port:
----
8899
BinaryData
====
Events: <none>
从上配置得知,若要基于配置清单创建ConfigMap资源时,仅需要指定apiVersion、kind、metadata、data这四个字段;
2.2 基于文件创建CM
ConfigMap资源也可以为应用程序提供大段配置,这些大段配置通常保存在一个或多个文件中,可以使用 kubectl create configmap 命令,通过 --from-file 选项一次加载一个配置文件的内容为指定键的值。默认文件名为key ,文件内容为values
1.准备两个nginx配置文件
# 主位置文件
[root@k8s-master configmap]# cat myserver.conf
server {
listen 8080;
server_name my.lushirong.cn;
location / {
root /usr/share/nginx/html;
index index.html;
}
include /etc/nginx/conf.d/*.cfg;
}
# 用于开启Nginx状态模块配置
[root@k8s-master configmap]# cat myserver-status.cfg
location /ngx_status {
stub_status;
access_log off;
}
2.通过下面的命令可以把事先准备好的Nginx配置文件保存到 ConfigMap 对象的 nginx-confs 中,其中一个直接使用 myserver.conf 作为key名称 ,另一个 myserver-status.cfg 对应的key名称自定义为 status.cfg
[root@k8s-master ~]# kubectl create configmap nginx-confs \
--from-file=./myserver.conf \
--from-file=status.cfg=./myserver-status.cfg
3.我们通过 nginx-confs 对象的详情了解到各个键对应的值
[root@k8s-master ~/configmap]# kubectl describe configmaps nginx-confs
Name: nginx-confs
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
myserver.conf: # 键的名称
---- # 下面是键的值
server {
listen 8080;
server_name my.lushirong.cn;
location / {
root /usr/share/nginx/html;
index index.html;
}
include /etc/nginx/conf.d/*.cfg;
}
status.cfg:
----
location /ngx_status {
stub_status;
access_log off;
}
BinaryData
====
Events: <none>
2.3 基于目录创建CM
对于配置文件较多且无需自定义键名称的场景,可以直接在 kubectl create configmap 命令的 --from-file 选项上附加一个目录路径就能将该目录下的所有文件创建于同一个ConfigMap资源中,各文件名即为键名称。
1.准备多个nginx配置文件,都统一存储至nginx-conf.d目录中;
# 主位置文件
[root@k8s-master configmap]# cat myserver.conf
server {
listen 8080;
server_name my.lushirong.cn;
location / {
root /usr/share/nginx/html;
index index.html;
}
include /etc/nginx/conf.d/*.cfg;
}
# 用于开启Nginx状态模块配置
[root@k8s-master configmap]# cat myserver-status.cfg
location /ngx_status {
stub_status;
access_log off;
}
# 用于开启Nginx压缩功能
[root@k8s-master configmap]# cat myserver-gzip.cfg
gzip on;
2.通过下面命令将conf.d目录中的所有文件都保存到nginx-confs-files对象中。
[root@k8s-master ~/configmap]# mkdir conf.d
[root@k8s-master ~/configmap]# mv *.conf *.cfg conf.d/
[root@k8s-master ~/configmap]# ll conf.d/
total 12
-rw-r--r-- 1 root root 185 Dec 28 11:10 myserver.conf
-rw-r--r-- 1 root root 9 Dec 28 11:18 myserver-gzip.cfg
-rw-r--r-- 1 root root 62 Dec 28 11:11 myserver-status.cfg
[root@k8s-master ~/configmap]# kubectl create configmap nginx-confs-files --from-file=./conf.d/
3.此目录包含 myserver.conf、myserver-status.cfg、myserver-gzip.cfg 这3个配置文件,它们会被分别存储为3个键值数据。
[root@k8s-master ~/configmap]# kubectl describe configmaps nginx-confs-files
Name: nginx-confs-files
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
myserver-gzip.cfg: # 键名称1
----
gzip on;
myserver-status.cfg: # 键名称2
----
location /ngx_status {
stub_status;
access_log off;
}
myserver.conf: # 键名称3
----
server {
listen 8080;
server_name my.lushirong.cn;
location / {
root /usr/share/nginx/html;
index index.html;
}
include /etc/nginx/conf.d/*.cfg;
}
BinaryData
====
Events: <none>
2.4 配置清单创建CM
基于配置清单创建ConfigMap资源时,仅需要指定apiVersion、kind、metadata、data 这四个字段;
[root@k8s-master configmap]# cat demoapp-configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: demoapp-config
data:
host: 0.0.0.0 # key: value
port: "8888" # key: value
my-server.conf: | # key
server {
listen 80;
server_name k8s.lushirong.cn;
location / {
root /usr/share/nginx/html;
index index.html;
}
include /etc/nginx/conf.d/*.cfg;
}
myserver-status.cfg: |
location /ngx_status {
stub_status;
access_log off;
}
[root@k8s-master ~/configmap]# kubectl describe configmaps demoapp-config
Name: demoapp-config
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
my-server.conf:
----
server {
listen 80;
server_name k8s.lushirong.cn;
location / {
root /usr/share/nginx/html;
index index.html;
}
include /etc/nginx/conf.d/*.cfg;
}
myserver-status.cfg:
----
location /ngx_status {
stub_status;
access_log off;
}
port:
----
8888
host:
----
0.0.0.0
BinaryData
====
Events: <none>
若键值来自于文件或一个目录,会发现不如通过命令行创建来的有效,因此我们可以先使用命令行加载文件或目录的方式进行创建,而后在通过 kubectl get cm -o yaml 获取相关信息进行编辑和保存。
3.Pod引用ConfigMap
3.1 通过环境变量引用CM键值
3.1.1 env引用变量示例
pod清单中除了使用vaule字段直接给定变量之外,还支持valueFrom字段嵌套 configMapKeyRef 来引用 ConfigMap 对象的键值,具体格式如下
env:
- name: <string> # 要赋值的环境变量名称
valueFrom: # 定义变量的引用
configMapkeyRef: # 变量来自于configmap对象
name: <string> # configmap对象的名称(因为有很多configmap,需要指定具体的名称)
key: <string> # configmap的键名称
这种方式赋值环境变量与直接赋值环境变量方式并无区别,它们都可以用于容器的启动脚本或直接传递给容器应用等。
3.1.2 env引用变量实践
demoapp 容器支持通过环境变量 HOST、PORT 为其指定监听的地址和端口。
1.定义configmap资源
[root@k8s-master configmap]# cat demoapp-configmap-env.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: demoapp-var-conf
data:
demoapp.host: "0.0.0.0"
demoapp.port: "8899"
2.创建一个Pod,然后通过env方式引用变量
[root@k8s-master configmap]# cat demoapp-pod-env-cm.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-env-cm
spec:
containers:
- name: app
image: registry.cn-shenzhen.aliyuncs.com/xiaoningmeng/demoapp:v1.0
env:
- name: HOST # HOST变量名
valueFrom:
configMapKeyRef:
name: demoapp-var-conf # 引用demoapp-var-conf资源中的demoap.host
key: demoapp.host
- name: PORT # PORT变量名
valueFrom:
configMapKeyRef:
name: demoapp-var-conf # 引用demoapp-var-conf资源中的demoap.port
key: demoapp.port
3.验证与测试
[root@k8s-master ~/configmap]# kubectl exec pod-env-cm -- netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:8899 0.0.0.0:* LISTEN 1/python3
注意:被引用的ConfigMap资源必须事先存在,否则无法在Pod对象中引用ConfigMap资源,另外ConfigMap属于名称空间级别的资源,它必须与引用它的Pod资源在同一名称空间。
3.2 通过卷挂载方式引用CM
使用环境变量方式导入 ConfigMap 对象中来源较长的文件内容,会导致占据过多的内存空间,同时也不支持内容的动态更新。其次该类数据主要用于为容器提供配置文件,所以将其内容直接通过挂载的方式进行引用,会是一种更好的选择。
3.2.1 引用整个存储卷
将ConfigMap对象的每个键名转为容器挂载点路径下的一个文件名,所以每个键名应该设计为对容器应用加载的配置文件名称。
1.启动一个NginxPod,然后将此前创建的 nginx-confs-files 引用至容器的 /etc/nginx/conf.d 目录中;
[root@k8s-master configmap]# cat nginx-pod-volume-cm-1.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod-volume-cm-1
spec:
containers:
- name: nginx
image: nginx:1.20
imagePullPolicy: IfNotPresent
volumeMounts:
- name: nginxconfs # 将nginxconfs这个资源挂载到/etc/nginx/conf.d/
mountPath: /etc/nginx/conf.d/
volumes: # 将configmap中的资源通过volumes方式引入到nginxconfs这个资源中来
- name: nginxconfs
configMap:
name: nginx-confs-files
2.此Pod资源引用了nginx-confs-files中包含3个配置文件
[root@k8s-master ~/configmap]# kubectl exec -it nginx-pod-volume-cm-1 -- ls /etc/nginx/conf.d/
myserver-gzip.cfg myserver-status.cfg myserver.conf
# myserver.conf定义主站,然后include包含了所有.cfg文件
[root@k8s-master ~/configmap]# kubectl exec -it nginx-pod-volume-cm-1 -- cat /etc/nginx/conf.d/myserver.conf
server {
listen 8080;
server_name my.lushirong.cn;
location / {
root /usr/share/nginx/html;
index index.html;
}
include /etc/nginx/conf.d/*.cfg;
}
3.访问Pod对应8080端口对应的/ngx_status,看是否能打开对应的nginx状态页
[root@k8s-master ~/configmap]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-pod-volume-cm-1 1/1 Running 0 3m33s 10.244.3.22 k8s-node2 <none> <none>
[root@k8s-master ~/configmap]# curl 10.244.3.22:8080/ngx_status
Active connections: 1
server accepts handled requests
1 1 1
Reading: 0 Writing: 1 Waiting: 0
3.2.2 引用存储卷部分键值
有些应用场景中,用户可能期望仅向容器中挂载指定的几个键,例如前面创建的一个名为 demoapp-config 里面有4个键,其中 host、port能为demoapp容器定义监听地址及端口,而 myserver.conf、myserver-status.cfg 能为nginx提供一个虚拟主机站点以及该虚拟站点的状态信息。

1.编写Pod,运行两个容器,分别调用不同的configmap配置
[root@k8s-master configmap]# cat nginx-pod-volume-cm-2.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod-volume-cm-2
spec:
containers:
- name: nginx
image: nginx:1.20
imagePullPolicy: IfNotPresent
volumeMounts:
- name: nginxconfs # 将nginxconfs这个资源挂载到/etc/nginx/conf.d/
mountPath: /etc/nginx/conf.d/
- name: demoapp
image: registry.cn-shenzhen.aliyuncs.com/xiaoningmeng/demoapp:v1.0
env:
- name: HOST
valueFrom:
configMapKeyRef:
name: demoapp-config
key: host
- name: PORT
valueFrom:
configMapKeyRef:
name: demoapp-config
key: port
volumes: # 将configmap中的资源通过volumes方式引入到nginxconfs这个资源中来
- name: nginxconfs
configMap:
name: demoapp-config
items:
- key: my-server.conf # 要引用的键名称(必写)
path: k8s.lushirong.cn.conf # 对应的键在挂载点目录中映射的文件名称(必写)
mode: 0644 # 文件权限(0~0777
- key: myserver-status.cfg
path: myserver-status.cfg
mode: 0644
2.查看容器状态
[root@k8s-master ~/configmap]# kubectl get pod nginx-pod-volume-cm-2 -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-pod-volume-cm-2 2/2 Running 0 22s 10.244.3.23 k8s-node2 <none> <none>
3.验证nginx容器
[root@k8s-master ~/configmap]# curl http://10.244.3.23/ngx_status
Active connections: 1
server accepts handled requests
1 1 1
Reading: 0 Writing: 1 Waiting: 0
4.验证demoapp容器
[root@k8s-master ~/configmap]# curl http://10.244.3.23:8888
iKubernetes demoapp v1.0 !! ClientIP: 10.244.0.0, ServerName: nginx-pod-volume-cm-2, ServerIP: 10.244.3.23!
[root@k8s-master ~/configmap]# kubectl exec -it nginx-pod-volume-cm-2 -c demoapp -- netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN -
tcp 0 0 0.0.0.0:8888 0.0.0.0:* LISTEN 1/python3
# demoapp和nginx容器都在一个Pod中,且他们是共享网络命名空间,所以在demoapp容器中能看到nginx的80端口
[root@k8s-master ~/configmap]# kubectl exec -it nginx-pod-volume-cm-2 -c nginx -- ls /etc/nginx/conf.d/
k8s.lushirong.cn.conf myserver-status.cfg
3.2.3 引用存储卷单个键值
前面两种方式中,无论是装在ConfigMap对象中的所有文件还是部分文件,挂载点目录下原有的文件都会被隐藏。(打开刚才创建的nginx容器验证,看默认的default.conf配置文件是否还存在)
对于期望将ConfigMap 对象提供的配置文件补充在挂载点目录下的需求来说,这种方法难以实现,好在我们可以通过容器上的volumeMounts字段 subpath 来解决
1.运行一个 NginxPod ,将 demoapp-config 中 myserver.conf、myserver-status.cfg 挂载进来测试
[root@k8s-master configmap]# cat nginx-pod-volume-cm-3.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod-volume-cm-3
spec:
containers:
- name: nginx
image: nginx:1.20
imagePullPolicy: IfNotPresent
volumeMounts:
- name: nginxconfs
mountPath: /etc/nginx/conf.d/k8s.lushirong.cn.conf # 挂载容器对应的路径
subPath: my-server.conf # ConfigMap 的 Key 名称
- name: nginxconfs
mountPath: /etc/nginx/conf.d/k8s-status.cfg
subPath: myserver-status.cfg
volumes: # 将configmap中的资源通过volumes方式引入到nginxconfs这个资源中来
- name: nginxconfs
configMap:
name: demoapp-config
2.验证容器/etc/nginx/conf.d/目录中原有的 default.conf 文件是否能够得以保留
[root@k8s-master ~/configmap]# kubectl exec -it nginx-pod-volume-cm-3 -- ls -l /etc/nginx/conf.d/
total 12
-rw-r--r-- 1 root root 1093 Aug 13 2019 default.conf
-rw-r--r-- 1 root root 63 Dec 28 06:09 k8s-status.cfg
-rw-r--r-- 1 root root 156 Dec 28 06:09 k8s.lushirong.cn.conf
3.访问测试
[root@k8s-master configmap]# curl 10.244.2.161
<html>
...
<h1>Welcome to nginx!</h1>
...
</html>
# 只能通过 k8s.lushirong.cn 域名方式访问到对应的状态页面,其他方式访问会出现404
[root@k8s-master ~/configmap]# curl -HHost:k8s.lushirong.cn 10.244.2.161/ngx_status
Active connections: 1
server accepts handled requests
2 2 2
Reading: 0 Writing: 1 Waiting: 0
4.Redis结合ConfigMap实践
4.1 场景说明
- 使用 Redis 配置的值创建一个 ConfigMap文档
- 创建一个 Redis Pod,挂载并使用创建的 ConfigMap
- 验证配置已经被正确应用。
4.2 创建ConfigMap
首先创建一个 ConfigMap 配置文件,写入一些基本的配置信息
[root@k8s-master configmap]# cat redis-config.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: example-redis-conf
data:
redis-config: |
bind 0.0.0.0
port 6380
4.3 创建Redis-Pod
[root@k8s-master configmap]# cat redis-server-cm.yaml
apiVersion: v1
kind: Pod
metadata:
name: redis-server-cm
spec:
containers:
- name: redis
image: redis
imagePullPolicy: IfNotPresent
command:
- redis-server
- "/redis-master/redis.conf"
volumeMounts:
- name: config # 2.将config卷,挂载到 /redis-master/redis.conf 这个文件中
mountPath: /redis-master/redis.conf
subPath: redis-config # 3.将configMap中redis-config这个key的内容挂载到对应的路径
volumes:
- name: config # 1.申明config卷,内容来自于configmap对象中的example-redis-conf
configMap:
name: example-redis-conf
4.4 检查Redis配置
使用 `kubectl exec` 进入 pod,运行 `redis-cli` 工具检查当前配置:
[root@k8s-master ~/configmap]# kubectl exec -it redis-server-cm -- /bin/bash
root@redis-server-cm:/data# redis-cli
Could not connect to Redis at 127.0.0.1:6379: Connection refused
not connected> exit
root@redis-server-cm:/data# redis-cli -p 6380
127.0.0.1:6380>
1.查看maxclients,最大运行客户端连接数
127.0.0.1:6380> config get maxclients
1) "maxclients"
2) "10000"
2.查看maxmemory,最大能使用的内存
127.0.0.1:6380> config get maxmemory
1) "maxmemory"
2) "0"
4.4 更新ConfigMap
接下来,向 example-redis-config ConfigMap 更新一些配置信息
[root@k8s-master configmap]# cat redis-config.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: example-redis-conf
data:
redis-config: |
bind 0.0.0.0
port 6380
maxclients 200000
maxmemory 1024mb
requirepass lushirongpwd
4.5 验证容器更新
修改后,容器中对应配置值并未更改,因此需要重新启动 Pod 才能从关联的 ConfigMap 中获取更新的值。
1.删除并重新创建 Pod:
[root@k8s-master ~]# kubectl delete pod redis-server-cm
[root@k8s-master ~]# kubectl apply -f redis-server-cm.yaml
2.重新登录Redis应用:
[root@k8s-master ~/configmap]# kubectl exec -it redis-server-cm -- /bin/bash
root@redis-server-cm:/data# redis-cli -p 6380 -a lushirongpwd
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6380>
3.查看 maxclients
127.0.0.1:6380> config get maxclients
1) "maxclients"
2) "200000"
4.查看 maxmemory ,它的值应该是 1073741824 bytes ,相当于2Mb
127.0.0.1:6380> config get maxmemory
1) "maxmemory"
2) "1073741824"






桂公网安备 45010702001945号