kubernetes学习八:configMap创建的四种方式

1 创建configMap的四种方式

1.1 通过直接在命令行中指定configmap参数创建

--from-literal=key=value

下面测试基于kubernetes 1.23.3版本测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 创建configMap
[root@master ~]# kubectl create configmap nginx-config --from-literal=nginx_port=80 --from-literal=server_name=myapp.magedu.com

# 查看configmap
[root@master ~]# kubectl get cm
NAME DATA AGE
nginx-config 2 4s

# 查看configmap的具体信息,key:nginx_port 键值value:80
[root@master ~]# kubectl describe configmaps nginx-config
Name: nginx-config
Namespace: default
Labels: <none>
Annotations: <none>

Data
====
nginx_port:
----
80
server_name:
----
myapp.magedu.com
Events: <none>

pod应用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
apiVersion: v1
kind: Pod
metadata:
name: pod-cm-1 #name必须小写
namespace: default
labels:
app: myapp
tier: frontend
annotations:
create-by: tianpei.wang
spec:
containers:
- name: myapp
image: ikubernetes/myapp:v1
ports:
- name: http
containerPort: 80
env: # 引用为环境变量
- name: NGINX_SERVER_PORT
valueFrom:
configMapKeyRef:
name: nginx-config
key: nginx_port # 指定对应的key,键值value传递给变量NGINX_SERVER_PORT
- name: NGINX_SERVER_NAME
valueFrom:
configMapKeyRef:
name: nginx-config
key: server_name # 指定对应的key,键值value传递给变量NGINX_SERVER_NAME
1
2
3
4
5
6
7
8
9
10
11
12
# 创建configmap
[root@master configmap]# kubectl create configmap nginx-config --from-literal=nginx_port=80 --from-literal=server_name=myapp.magedu.com

# 创建pod通过configmap注入到pod内
[root@master configmap]# kubectl apply -f pod-cm-1.yaml
pod/pod-cm-1 created

# 可以看到已经成功注入到pod中了
[root@master configmap]# kubectl exec -it pod-cm-1 -- /bin/sh
/ # printenv | grep NGINX
NGINX_SERVER_PORT=80
NGINX_SERVER_NAME=myapp.magedu.com

警告
当以环境变量的方式注入pod时, 只在pod启动时加载, 后续更改configmap不会同步到pod内

1.2 通过指定文件或目录创建

即通过--from-file=File_Path参数将一个配置文件或目录下所有文件创建为一个ConfigMap,

说明

--from-file=File_Path 这里的File_Path可以是文件也可以是目录

文件适用于单个配置文件。生成单个配置文件;pod使用的话挂载为单个配置文件或覆盖到某个目录。

目录适用于多个配置文件。会将目录下的所有文件均创建为配置文件,包含到同一个configMap下。pod使用的话覆盖到某个目录,所有配置文件均放置到对应目录下。

1.2.1 指定文件创建

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# 文件内容
[root@master configmap]# cat manifests/configmap/www.conf
server {
server_name myapp.magedu.com;
listen 80;
root /data/web/html
}

# 通过文件创建configmap
[root@master configmap]# kubectl create configmap nginx-www --from-file=./manifests/configmap/www.conf

# 查看configmap 文件名为key, 文件内容为value
[root@master configmap]# kubectl describe configmaps nginx-www
Name: nginx-www
Namespace: default
Labels: <none>
Annotations: <none>

Data
====
www.conf:
----
server {
server_name myapp.magedu.com;
listen 80;
root /data/web/html
}

Events: <none>

pod应用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
apiVersion: v1
kind: Pod
metadata:
name: pod-cm-2 #name必须小写
namespace: default
labels:
app: myapp
tier: frontend
annotations:
create-by: tianpei.wang
spec:
containers:
- name: myapp
image: ikubernetes/myapp:v1
ports:
- name: http
containerPort: 80
volumeMounts:
- name: nginxconf
# 挂载路径,这种挂载方式是直接覆盖到config.d目录,最终结果是:/etc/nginx/config.d/www.conf (目录内只有这一个文件)
# config.d目录为空或只有一个要替换的问题可采用此方法
mountPath: /etc/nginx/config.d
readOnly: true # 只读
# config.d目录包含多个文件,只覆盖特定文件,使用如下方法
# mountPath: /etc/nginx/config.d/www.conf # www.conf为文件名,文件内容使用configMap中以www.conf为key的键值。
# subPath: www.conf # 指定key为www.conf的value(键值)作为内容挂载到上面www.conf文件中
volumes:
- name: nginxconf
configMap: # 挂载类型configMap
name: nginx-config
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 通过上边的yaml文件创建pod, 并将configmap以volumes的形式挂载到pod内
[root@master configmap]# kubectl apply -f pod-cm-2.yaml

# 可以看到已经生效了
[root@master configmap]# kubectl exec -it pod-cm-2 -- /bin/sh
/ # cd /etc/nginx/config.d/
/etc/nginx/config.d # ls
www.conf
/etc/nginx/config.d # cat www.conf
server {
server_name myapp.magedu.com;
listen 80;
root /data/web/html
}

1.2.2 指定目录创建

即将一个目录内的所有文件创建为一个ConfigMap,--from-file=File_PathFile_path为目录路径

创建configMap

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# 目录内容
[root@k8s-master geocms]# ls geocms_mb/
application-dev.properties authority-dev.properties dmb-prod.properties scene-dev.properties smb-prod.properties
application-prod.properties authority-prod.properties mb-dev.properties scene-prod.properties
application-user.properties dmb-dev.properties mb-prod.properties smb-dev.properties

# 通过目录创建configmap
[root@k8s-master geocms]# kubectl create configmap mb-config --from-file=/opt/geocms/geocms_mb/

# 查看configmap,可以看到含有多个以文件名为key,文件内容为键值的多个配置。
[root@k8s-master geocms]# kubectl describe configmaps -n geocms mb-config
Name: mb-config
Namespace: geocms
Labels: <none>
Annotations: <none>

Data
====
mb-prod.properties:
----
spring.profiles.include=user
spring.application.name=mb
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=ALWAYS
management.health.elasticsearch.enabled=false
management.health.redis.enabled=false
jx.datasource.es.type=ELASTICSEARCH
jx.datasource.es.host=192.168.0.39:9200
file.base.path=/
file.base.url=http://${SERVER_IP}:8000
file.local.rootPath=${file.base.path}/thumb
file.local.fileUrl=${file.base.url}/thumb

scene-dev.properties:
----
spring.profiles.include=user
spring.application.name=scene
server.servlet.context-path=/api/scene
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=ALWAYS
management.health.elasticsearch.enabled=false
management.health.redis.enabled=false
file.base.path=E:/tomcat-8.5.24/webapps
file.base.url=http://192.168.0.104:18900
file.local.rootPath=${file.base.path}/thumb
file.local.fileUrl=${file.base.url}/thumb

scene-prod.properties:
----
spring.profiles.include=user
spring.application.name=scene
server.servlet.context-path=/api/scene
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=ALWAYS
management.health.elasticsearch.enabled=false
management.health.redis.enabled=false
file.base.path=/
file.base.url=http://${SERVER_IP}:8000
file.local.rootPath=${file.base.path}/thumb
file.local.fileUrl=${file.base.url}/thumb

.... 中间内容省略....

smb-dev.properties:
----
spring.profiles.include=user
spring.application.name=smb
server.servlet.context-path=/api/smb
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=ALWAYS
management.health.elasticsearch.enabled=false
management.health.redis.enabled=false
jx.datasource.es.type=ELASTICSEARCH
jx.datasource.es.host=192.168.0.104:9200
spring.task.execution.pool.core-size=5
spring.task.execution.pool.max-size=10
spring.task.execution.pool.queue-capacity=5
spring.task.execution.pool.keep-alive=60
spring.task.execution.thread-name-prefix=mb-task-


BinaryData
====

Events: <none>

pod应用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
namespace: geocms
spec:
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.20.1
volumeMounts:
- name: nginx-html
mountPath: /usr/share/nginx/html
- name: test-config
# 只覆盖特定文件,采用如下写法。
mountPath: /home/application-dev.properties # 指定要覆盖的文件名。该文件名可自定义,和key不一致。
subPath: application-dev.properties # 指定key,key的键值作为内容挂载到上面文件内。只覆盖这一个文件,其他文件不覆盖。
# 所有文件均覆盖,直接覆盖/home目录
# mountPath: /home # 直接将mb-config内的所有配置文件覆盖到目录内,key为文件名,值为文件内容。
resources:
limits:
memory: "128Mi"
cpu: "500m"
ports:
- containerPort: 80
volumes:
- name: nginx-html
persistentVolumeClaim:
claimName: geocms-html-claim
- name: test-config
configMap:
name: mb-config
1
2
3
4
5
6
7
8
9
10
# 只覆盖指定文件,查看pod内文件
root@nginx-deployment-76964fff8f-6pf7m:/# cd /home/
root@nginx-deployment-566788f4fb-9j5bs:/home# ls
application-test.properties

# 覆盖整个目录,查看pod内文件
root@nginx-deployment-76964fff8f-6pf7m:/# cd /home/
root@nginx-deployment-76964fff8f-6pf7m:/home# ls
application-dev.properties application-user.properties authority-prod.properties dmb-prod.properties mb-prod.properties scene-prod.properties smb-prod.properties
application-prod.properties authority-dev.properties dmb-dev.properties mb-dev.properties scene-dev.properties smb-dev.properties

1.3 通过一个文件内多个键值对创建

使用参数--from-env-file=

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# ---------------------------------------- 单个键值对文件 ----------------------------------------------------
# 查看env.txt文件内容,具有多个键值对。
[root@master configmap_test]# cat << EOF > env.txt
db.host=10.0.0.50
db.port=3306
EOF
# 从键值对文件创建comfigMap
[root@master configmap_test]# kubectl create cm env-cm --from-env-file=env.txt


# --------------------------------------- 多个键值对文件 ----------------------------------------------------
# 如果有多个env文件, 只有最后一个env文件会生效(我使用这个版本,是都生效的,可能以前的老版本不支持多个文件)
# 查看game.properties键值对文件
[root@master configmap_test]# cat game.properties
enemies=aliens
lives=3
enemies.cheat=true
enemies.cheat.level=noGoodRotten
secret.code.passphrase=UUDDLRLRBABAS
secret.code.allowed=true
secret.code.lives=30
# 查看ui.properties键值对文件
[root@master configmap_test]# cat ui.properties
color.good=purple
color.bad=yellow
allow.textmode=true
how.nice.to.look=fairlyNice

# 执行命令创建configmap
[root@master configmap_test]# kubectl create configmap configmap-env --from-env-file=./game.properties --from-env-file=./ui.properties

# 可以看到, 只有ui.properties生效了(我实验这个版本,所有文件都生效了)
[root@master configmap_test]# kubectl get configmaps configmap-env -o yaml
apiVersion: v1
data:
allow.textmode: "true" # 看这里
color.bad: yellow # 看这里
color.good: purple # 看这里
enemies: aliens # 看这里
enemies.cheat: "true" # 看这里
enemies.cheat.level: noGoodRotten # 看这里
how.nice.to.look: fairlyNice # 看这里
lives: "3" # 看这里
secret.code.allowed: "true" # 看这里
secret.code.lives: "30" # 看这里
secret.code.passphrase: UUDDLRLRBABAS # 看这里
kind: ConfigMap
metadata:
creationTimestamp: "2022-06-07T08:27:15Z"
name: configmap-env
namespace: default
resourceVersion: "633268"
selfLink: /api/v1/namespaces/default/configmaps/configmap-env
uid: f4a9e6dd-00a0-44d6-8b0a-8945e17432db

1.4 通过yaml文件创建

事先写好标准的yaml文件,通过kubectl apply -f 命令创建。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# 查看yaml文件
[root@k8s-master geocms]# cat configmap_nginx.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: nginx-default
namespace: geocms
data:
default.conf: |
server {
listen 80;
listen [::]:80;
server_name localhost;

#access_log /var/log/nginx/host.access.log main;

location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
# mb服务api代理
location /api/mb {
proxy_pass http://zuul-service.geocms.svc.cluster.local:18900/api/mb;
}

# smb服务api代理
location /api/smb {
proxy_pass http://zuul-service.geocms.svc.cluster.local:18900/api/smb;
}

# dmb服务api代理
location /api/dmb {
proxy_pass http://zuul-service.geocms.svc.cluster.local:18900/api/dmb;
}

# scene服务api代理
location /api/scene {
proxy_pass http://zuul-service.geocms.svc.cluster.local:18900/api/scene;
}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# 创建configMap
[root@k8s-master geocms]# kubectl apply -f configmap_nginx.yaml

# 查看configMap。 key为default.conf, 键值为配置内容。
[root@k8s-master geocms]# kubectl describe configmaps -n geocms nginx-default
Name: nginx-default
Namespace: geocms
Labels: <none>
Annotations: <none>

Data
====
default.conf:
----
server {
listen 80;
listen [::]:80;
server_name localhost;

#access_log /var/log/nginx/host.access.log main;

location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
# mb服务api代理
location /api/mb {
proxy_pass http://zuul-service.geocms.svc.cluster.local:18900/api/mb;
}

# smb服务api代理
location /api/smb {
proxy_pass http://zuul-service.geocms.svc.cluster.local:18900/api/smb;
}

# dmb服务api代理
location /api/dmb {
proxy_pass http://zuul-service.geocms.svc.cluster.local:18900/api/dmb;
}

# scene服务api代理
location /api/scene {
proxy_pass http://zuul-service.geocms.svc.cluster.local:18900/api/scene;
}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}


BinaryData
====

Events: <none>

2 configmap结合pod使用的二种方式

2.1 通过环境变量的方式,直接传递给pod

参考1.1 中的pod应用示例。

2.2 作为volume的方式挂载到pod内

参考1.2.1和1.2.2中的pod应用示例。

参考文档

-------------本文结束感谢您的阅读-------------
坚持原创技术分享,您的支持将鼓励我继续创作!