Kubernetes配置文件管理ConfigMap

1、ConfigMap的介绍

作用:

1
将配置文件和Pod解偶,方便管理配置文件

ConfigMap里的配置文件如何储存的

1
2
3
4
5
6
#方法一
键值对的方式:
key:value

#方法二:
文件名:配置文件的内容

ConfigMap支持的配置类型

1
2
1、直接定义键值对
2、基于文件创建的键值对

ConfigMap创建方式

1
2
1、命令行
2、资源清单

ConfigMap的配置文件如何传递到pod里

1
2
1、变量传递    (只是环境变量,又不会改变配置文件,所以毫无意义)
2、数据卷挂载

使用Configmap的限制条件

1
2
1、ConfigMap必须在pod之前创建,pod才能引用
2、ConfigMap受限于名称空间的限制,只有处于同一个名称空间的pod才可以被引用

2、命令行创建ConfigMap

方式一:命令行创建键值对Key:value的储存

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
1、查看帮助
[root@master01 kubernetes]# kubectl create configmap --help

2、创建configmap
[root@master01 kubernetes]# kubectl create configmap nginx-config --from-literal=nginx_port=80 --from-literal=server_name=blog.test.com

--from-literal:创建键值对的key=value

3、查看
[root@master01 kubernetes]# kubectl get cm
NAME DATA AGE
nginx-config 2 67s

ConfigMap的名字:nginx-config,里面有2条数据

4、查看详细信息
[root@master01 kubernetes]# kubectl describe cm nginx-config
Name: nginx-config
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
nginx_port:
----
80
server_name:
----
blog.test.com
Events: <none>

#创建这种键值对的没有意义,因为配置文件很少是键值对的


5、引用ConfigMap,如果pod想要引用上面的ConfigMap,只能通过环境变量的形式

通过变量传递到pod

image-20240928205743737

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
[root@master01 kubernetes]# vim nginx-cm.yml 
apiVersion: v1
kind: Pod
metadata:
name: nginx-cm
spec:
containers:
- name: nginx-pod
image: nginx:alpine
env:
- name: NGINX_PORT
valueFrom:
configMapKeyRef:
name: nginx-config
key: nginx_port

- name: SERVER_NAME
valueFrom:
configMapKeyRef:
name: nginx-config
key: server_name

6、启动pod
[root@master01 kubernetes]# kubectl apply -f nginx-cm.yml
[root@master01 kubernetes]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-cm 1/1 Running 0 9s

7、进入pod,查看pod已经引入了变量
[root@master01 kubernetes]# kubectl exec -it nginx-cm -- /bin/sh
/ # echo $NGINX_PORT
80
/ # echo $SERVER_NAME
blog.test.com

#虽然这两个变量传进来了,但是配置文件并没有改变,因为他不知道会传到哪个配置文件,没那么智能,只是传递变量

方式二:基于文件形式创建ConfigMap

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
1、创建配置文件
[root@master01 kubernetes]# cat > www.conf <<EOF
server {
listen 80;
server_name blog.test.com;
location / {
root /usr/share/nginx/html/www;
index index.html index.htm;
}
}
EOF

2、命令行创建ConfigMap资源
kubectl create configmap [自定义的ConfigMap名字] --from-file=[自定义的key名字]=[配置文件路径]

[root@master01 kubernetes]# kubectl create configmap nginx-www --from-file=test-www=./www.conf

3、查看cm资源
[root@master01 kubernetes]# kubectl get cm
NAME DATA AGE
nginx-www 1 117s

image-20240928214603450

  • 通过挂载卷传递到pod
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[root@master01 kubernetes]# vim nginx-cm.yml
apiVersion: v1
kind: Pod
metadata:
name: nginx-cm-v1
spec:
volumes:
- name: nginx-data-configmap
configMap:
name: nginx-www
items:
- key: test-www
path: wordpress.conf
containers:
- name: nginx-pod
image: nginx:alpine
volumeMounts:
- name: nginx-data-configmap
mountPath: /etc/nginx/conf.d/

image-20240928221503694

  • 一次性创建ConfigMap的2个key
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
1、准备配置文件
[root@master01 kubernetes]# cat > wp.conf <<EOF
server {
listen 80;
server_name www.wp.com;
location / {
root /usr/share/nginx/html/www;
index index.html index.htm;
}
}
EOF


[root@master01 kubernetes]# cat > wc.conf <<EOF
server {
listen 90;
server_name www.wc.com;
location / {
root /usr/share/nginx/html/www;
index index.html index.htm;
}
}
EOF

2、命令行创建ConfigMap
[root@master01 kubernetes]# kubectl create configmap wp-wc-conf --from-file=wp-conf=./wp.conf --from-file=wc-conf=./wc.conf


3、查看
[root@master01 ~]# kubectl get cm
NAME DATA AGE
nginx-config 2 126m
nginx-www 1 45m
wp-wc-conf 2 112s

image-20240928223040756

一次性挂载2个文件进去

image-20240928225407162

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
1、编写资源清单
[root@master01 kubernetes]# vim wp-wc-configmap.yml
apiVersion: v1
kind: Pod
metadata:
name: wp-wc-cm
spec:
volumes:
- name: wp-wc-configmap
configMap:
name: wp-wc-conf
items:
- key: wp-conf
path: wordpress.conf
- key: wc-conf
path: wecenter.conf

containers:
- name: nginx-pod
image: nginx:alpine
volumeMounts:
- name: wp-wc-configmap
mountPath: /etc/nginx/conf.d/

2、运行,并查看
[root@master01 kubernetes]# kubectl apply -f wp-wc-configmap.yml
[root@master01 kubernetes]# kubectl get pod
NAME READY STATUS RESTARTS AGE
wp-wc-cm 1/1 Running 0 5s

3、进入pod查看挂载成功
[root@master01 kubernetes]# kubectl exec -it wp-wc-cm -- /bin/sh
/ # ls -l /etc/nginx/conf.d/
lrwxrwxrwx 1 root root 20 Sep 28 14:41 wecenter.conf -> ..data/wecenter.conf
lrwxrwxrwx 1 root root 21 Sep 28 14:41 wordpress.conf -> ..data/wordpress.conf
/ # cat /etc/nginx/conf.d/wecenter.conf && cat /etc/nginx/conf.d/wordpress.conf
server {
listen 90;
server_name www.wc.com;
location / {
root /usr/share/nginx/html/www;
index index.html index.htm;
}
}
server {
listen 80;
server_name www.wp.com;
location / {
root /usr/share/nginx/html/www;
index index.html index.htm;
}
}

测试动态修改

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
[root@master01 kubernetes]# kubectl get cm
NAME DATA AGE
wp-wc-conf 2 30m

1、修改
[root@master01 kubernetes]# kubectl edit cm wp-wc-conf
listen 91;
......
listen 81;

2、再次进入容器内观察也会自动更新
[root@master01 kubernetes]# kubectl exec -it wp-wc-cm -- /bin/sh
/ # cat /etc/nginx/conf.d/wecenter.conf && cat /etc/nginx/conf.d/wordpress.conf

3、但是端口没有变
/ # netstat -lntup
tcp 0 0 0.0.0.0:80
tcp 0 0 0.0.0.0:90

4、需要重启服务才可以生效,需要删除资源再重启,如果是pod资源就不能删除重启,pod资源就删除,重新应用一下
[root@master01 kubernetes]# kubectl delete -f wp-wc-configmap.yml
[root@master01 kubernetes]# kubectl apply -f wp-wc-configmap.yml
[root@master01 kubernetes]# kubectl exec -it wp-wc-cm -- /bin/sh
/ # netstat -lntup
tcp 0 0 0.0.0.0:81 0.0.0.0:*
tcp 0 0 0.0.0.0:91 0.0.0.0:*
# 动态修改配置文件,但是无法重新加载服务 (有一些企业用Nacos服务:配置管理中心,改完了就帮你重新加载服务)
1
2
以上操作把配置文件挂到pod里面了,需要先准备配置文件,但是还有缺陷,需要提前准备配置文件
但有的资源清单,比如kube-flannel,没有准备配置文件,使用资源清单创建ConfigMap就不需要提前准备

3、使用资源清单创建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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
为避免冲突,删除上次实验创建的cm和pod

1、创建资源清单
root@master01 kubernetes]# vim wp-wc-configmap.yml
apiVersion: v1
kind: ConfigMap
metadata:
name: wp-wc-conf
namespace: default
data:
wp-key: |
server {
listen 88;
server_name www.wwpp.com;
location / {
root /usr/share/nginx/html/www;
index index.html index.htm;
}
}
wc-key: |
server {
listen 99;
server_name www.wwcc.com;
location / {
root /usr/share/nginx/html/www;
index index.html index.htm;
}
}
---
apiVersion: v1
kind: Pod
metadata:
name: wp-wc-cm2
spec:
volumes:
- name: wp-wc-configmap
configMap:
name: wp-wc-conf
items:
- key: wp-key
path: wordpress.conf
- key: wc-key
path: wecenter.conf

containers:
- name: nginx-pod
image: nginx:alpine
volumeMounts:
- name: wp-wc-configmap
mountPath: /etc/nginx/conf.d/

2、运行,并且查看详细信息
root@master01 kubernetes]# kubectl apply -f wp-wc-configmap.yml
configmap/wp-wc-conf created
pod/wp-wc-cm2 created
[root@master01 kubernetes]# kubectl get pod
NAME READY STATUS RESTARTS AGE
wp-wc-cm2 1/1 Running 0 2m42s
[root@master01 kubernetes]# kubectl get cm
NAME DATA AGE
wp-wc-conf 2 21s
[root@master01 kubernetes]# kubectl describe cm wp-wc-conf
Name: wp-wc-conf
Namespace: default
Labels: <none>
Annotations: <none>

Data
====
wc-key:
----
server {
listen 99;
server_name www.wwcc.com;
location / {
root /usr/share/nginx/html/www;
index index.html index.htm;
}
}

wp-key:
----
server {
listen 88;
server_name www.wwpp.com;
location / {
root /usr/share/nginx/html/www;
index index.html index.htm;
}
}


3、进入容器并查看配置文件
[root@master01 ~]# kubectl exec -it wp-wc-cm2 -- /bin/sh
/# cat /etc/nginx/conf.d/wecenter.conf && cat /etc/nginx/conf.d/wordpress.conf
/# netstat -lntup
tcp 0 0 0.0.0.0:88
tcp 0 0 0.0.0.0:99

4、动态修改能够修改,但是不生效

4、wordpress综合练习

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
## wordpress 要运行在k8s中
## MySQL
1.名称空间: blog
2.镜像mysql:5.7
3.环境变量
- root密码:123
- 数据库:wordpress
- 用户:wordpress
- 参数:字符集
4.数据持久化:在宿主机的/data/mysql/data

## mysql-svc需求
名称空间:blog

## wordpress需求
副本数为:2
镜像:wordpress:latest
数据库地址:cluster ip
数据库名称:wordpress
用户:wordpress

******** 使用NFS持久化数据,宿主机:/data/wordpress/data 10.0.0.31

mysql deployment
mysql clusterip
wordpress deployment
wordpress clusterip
wordpress ingress
wordpress hpa

资源清单如下

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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
---
# 名称空间
apiVersion: v1
kind: Namespace
metadata:
name: blog
---
# mysql的PV资源
apiVersion: v1
kind: PersistentVolume
metadata:
name: mysql-pv
namespace: blog
spec:
storageClassName: mysql-hostpath
persistentVolumeReclaimPolicy: Retain
accessModes:
- ReadWriteOnce
capacity:
storage: 5Gi
hostPath:
path: /data/wp-db

---
# mysql的pvc资源
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mysql-pvc
namespace: blog
spec:
storageClassName: mysql-pvc
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 5Gi

---
# mysql的deploy资源
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql-deploy
namespace: blog
spec:
replicas: 1
selector:
matchLabels:
run: mysql-deploy
template:
metadata:
name: mysql-pod
namespace: blog
labels:
run: mysql-deploy
spec:
volumes:
- name: db-pvc
persistentVolumeClaim:
claimName: mysql-pvc
containers:
- name: mysql-container
image: mysql:5.7
imagePullPolicy: IfNotPresent
livenessProbe:
tcpSocket:
port: 3306
failureThreshold: 3
initialDelaySeconds: 5
periodSeconds: 1
timeoutSeconds: 10
env:
- name: MYSQL_ROOT_PASSWORD
value: '123'
- name: MYSQL_DATABASE
value: 'wordpress'
- name: MYSQL_USER
value: 'wp_user'
- name: MYSQL_PASSWORD
value: '123'
volumeMounts:
- name: db-pvc
mountPath: /var/lib/mysql

---
# mysql的service资源
apiVersion: v1
kind: Service
metadata:
name: mysql-service
namespace: blog
spec:
selector:
run: mysql-deploy
ports:
- name: mysql
protocol: TCP
port: 3306
targetPort: 3306
type: ClusterIP

---
# wordpress的PV资源
apiVersion: v1
kind: PersistentVolume
metadata:
name: wordpress-pv
namespace: blog
spec:
storageClassName: wordpress-nfs
persistentVolumeReclaimPolicy: Retain
accessModes:
- ReadWriteOnce
capacity:
storage: 5Gi
nfs:
path: /data/wp
server: 172.16.1.31

---
#wordpress的PVC
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: wordpress-pvc
namespace: blog
spec:
accessModes:
- ReadWriteOnce
storageClassName: wordpress-nfs
resources:
requests:
storage: 5Gi

---
# wordpress的deployment的资源
apiVersion: apps/v1
kind: Deployment
metadata:
name: wordpress-deploy
namespace: blog
spec:
replicas: 2
selector:
matchLabels:
run: wordpress-deploy
template:
metadata:
labels:
run: wordpress-deploy
name: wordpress-pod
namespace: blog
spec:
volumes:
- name: web-pvc
persistentVolumeClaim:
claimName: wordpress-pvc
containers:
- name: wordpress-container
image: wordpress
imagePullPolicy: IfNotPresent
#livenessProbe:
# httpGet:
# path: /
# port: 80
# failureThreshold: 3
# initialDelaySeconds: 5
# periodSeconds: 1
# timeoutSeconds: 10
readinessProbe:
tcpSocket:
port: 3306
failureThreshold: 3
initialDelaySeconds: 3
periodSeconds: 1
successThreshold: 3
timeoutSeconds: 10
env:
- name: WORDPRESS_DB_HOST
value: 'mysql-service'
- name: WORDPRESS_DB_USR
value: 'wp_user'
- name: WORDPRESS_DB_DATABASE
value: 'wordpress'
- name: WORDPRESS_DB_PASSWORD
value: '123'
volumeMounts:
- name: web-pvc
mountPath: /var/www/html
---
# wordpress的service资源
apiVersion: v1
kind: Service
metadata:
name: wordpress-service
namespace: blog
spec:
selector:
run: wordpress-deploy
ports:
- name: http
port: 80
targetPort: 80
protocol: TCP
type: ClusterIP

---
# wordpress的HPA资源
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
name: wordpress-hpa
namespace: blog
spec:
maxReplicas: 10
minReplicas: 2
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: wordpress-deploy
targetCPUUtilizationPercentage: 50

---
# wordpress的ingress资源
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: wordpress-ingress
namespace: blog
spec:
rules:
- host: wp.web.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: wordpress-service
port:
number: 80