在部署应用程序时,我们都会涉及到应用的配置,在容器中,如Docker容器中,如果将配置文件打入容器镜像,这种行为等同于写死配置,每次修改完配置,镜像就得重新构建。当然,我们也可以通过挂载包含该文件的卷进行配置管理和修改。而在k8s中,我们要讲一种更好的方式,即ConfigMap,这种资源对象的出现,更是极大的方便了应用程序的配置管理。
ConfigMap是一个或多个key/value的形式保存在k8s中,内部可以管理变量也可以管理完整的配置文件内容。
注:在使用命令的时候注意单词: configmap等价于cm,cm算是简写,类似于deployment可以使用命令时写成deploy,service可以写成svc,namespace可以写成ns,pod可以写成po。
configmap配置信息和镜像解耦,实现方式是把配置信息放到configmap对象中,然后在pod中作为volume挂载到pod中,从而实现导入配置的目的
适用场景:
注意事项:
示例:
apiVersion: v1
kind: ConfigMap
metadata:
name: cm-test01
data:
appconf01: value01
appconf02: value02
命令:
[root@k8s-master k8s]# kubectl create -f cm-test01.yaml
configmap/cm-test01 created
读取文件方式(也可以是目录)通过--from-file参数从文件中读取。可以指定key的名称,若不指定,则默认使用文件名为key。
案例:如当前目录有一个配置文件为test.properties
[root@k8s-master k8s]# cat test.properties
key01:value01
key02:value02
conf01: value03
命令:
[root@k8s-master k8s]# kubectl create cm cm-test-file --from-file=test.properties
configmap/cm-test-file created
//指定参数方式,通过--from-literal指定keyxx=valuexx创建confimap中的data内配置属性。
[root@k8s-master k8s]# kubectl create configmap cm-test-literal --from-literal=key01=value01 --from-literal=key02=value02
configmap/cm-test-literal created
[root@k8s-master k8s]# kubectl get cm
NAME DATA AGE
cm-test-file 1 3m51s
cm-test-literal 2 3m30s
cm-test01 2 9m2s
kube-root-ca.crt 1 4d16h
[root@k8s-master k8s]# kubectl describe cm cm-test01
Name: cm-test01
Namespace: default
Labels:
Annotations:
Data
====
appconf01:
----
value01
appconf02:
----
value02
Events:
[root@k8s-master k8s]# kubectl describe cm cm-test-file
Name: cm-test-file
Namespace: default
Labels:
Annotations:
Data
====
test.properties:
----
key01:value01
key02:value02
conf01: value03
Events:
[root@k8s-master k8s]# kubectl describe cm cm-test-literal
Name: cm-test-literal
Namespace: default
Labels:
Annotations:
Data
====
key01:
----
value01
key02:
----
value02
Events:
[root@k8s-master k8s]# kubectl get cm cm-test01 -o yaml
apiVersion: v1
data:
appconf01: value01
appconf02: value02
kind: ConfigMap
metadata:
creationTimestamp: "2022-08-07T00:18:47Z"
name: cm-test01
namespace: default
resourceVersion: "44876"
uid: 7492a928-b163-4b86-a3dd-54a7fbe59a10
[root@k8s-master k8s]# kubectl get configmap cm-test-file -o yaml
apiVersion: v1
data:
test.properties: |+
key01:value01
key02:value02
conf01: value03
kind: ConfigMap
metadata:
creationTimestamp: "2022-08-07T00:23:58Z"
name: cm-test-file
namespace: default
resourceVersion: "45341"
uid: 818a39b4-cbaf-4643-9541-1119b4d61982
[root@k8s-master k8s]# kubectl get cm cm-test-literal -o yaml
apiVersion: v1
data:
key01: value01
key02: value02
kind: ConfigMap
metadata:
creationTimestamp: "2022-08-07T00:24:19Z"
name: cm-test-literal
namespace: default
resourceVersion: "45372"
uid: 051849e0-bf0a-4926-a549-2405dc4963cc
[root@k8s-master k8s]# kubectl edit cm cm-test01
configmap/cm-test01 edited
通过kubectl describe cm cm-test01
查看更新是否生效。
[root@k8s-master k8s]# kubectl describe cm cm-test01
Name: cm-test01
Namespace: default
Labels:
Annotations:
Data
====
appconf02:
----
value02
appconf01:
----
value001
Events:
直接更新yaml文件里的值,通过 kubectl apply -f configmap-test01.yaml重新发布一遍进行更新。
$ kubectl delete -f configmap-test01.yaml
kubectl delet cm cm-test01
容器应用对ConfigMap的使用主要是两种:
1)通过环境变量获取ConfigMap的内容:spec.env
和spec.envFrom
2)通过卷volume挂载的方式将ConfigMap的内容挂载到容器内部的文件或目录:spec.volumes。
[root@k8s-master k8s]# vim pod-test01.yaml
apiVersion: v1
kind: Pod
metadata:
name: cm-pod-test001
spec:
containers:
- name: cm-test
image: tomcat:8
command: [ "/bin/sh", "-c", "env | grep APP"]
env:
- name: APPCONF01 # 定义环境变量的名称
valueFrom: # key “appconf01”的值获取
configMapKeyRef:
name: cm-test01 # 环境变量的值来自于configmap cm-test01
key: appconf01 # configmap中的配置key为appconf01
- name: APPCONF02 # 定义环境变量的名称
valueFrom: # key “appconf02”的值获取
configMapKeyRef:
name: cm-test01 # 环境变量的值来自于configmap cm-test01
key: appconf02 # configmap中的配置key为appconf02
restartPolicy: Never # 重启策略:从不。
执行创建pod:
[root@k8s-master k8s]# kubectl create -f pod-test01.yaml
pod/cm-test001 created
[root@k8s-master k8s]# kubectl get pods
[root@k8s /cm/test]# kubectl get pods
NAME READY STATUS RESTARTS AGE
cm-pod-test001 0/1 Completed 0 1h
[root@k8s-master k8s]# kubectl logs cm-pod-test001
APPCONF01=value01
APPCONF02=value02
说明容器内部的环境变量使用ConfigMap中进行读取的
[root@k8s-master k8s]# vim pod-test02.yaml
piVersion: v1
kind: Pod
metadata:
name: cm-pod-test002
spec:
containers:
- name: cm-test2
image: tomcat:8
command: [ "/bin/sh", "-c", "env"]
envFrom:
- configMapRef:
name: cm-test01 # 根据ConfigMap cm-test01资源自动生成环境变量
restartPolicy: Never
执行创建pod:
[root@k8s-master k8s]# kubectl create -f pod-test02.yaml
[root@k8s-master k8s]# kubectl get po
NAME READY STATUS RESTARTS AGE
cm-pod-test001 0/1 Completed 0 2h
cm-pod-test002 0/1 Completed 0 1h
注意:
环境变量的名称受限制:[a-zA-Z][a-zA-Z0-9_]*,不能以数字或非法字符开头。
[root@k8s-master k8s]# vim pod-test03.yaml
apiVersion: v1
kind: Pod
metadata:
name: cm-pod-test003
spec:
containers:
- name: cm-test3
image: tomcat:8
volumeMounts:
- name: vm-01-1
mountPath: /conf
volumes:
- name: vm-01-1
configMap:
name: cm-test-file
items:
- key: key-testproperties
path: test.properties
restartPolicy: Never
[root@k8s-master k8s]# vim pod-test04.yaml
apiVersion: v1
kind: Pod
metadata:
name: cm-pod-test004
spec:
containers:
- name: cm-test4
image: tomcat:8
volumeMounts:
- name: vm-02-2
mountPath: /conf
volumes:
- name: vm-02-2
configMap:
name: cm-test-file
restartPolicy: Never
进入容器中查看
[root@k8s-master k8s]# kubectl exec -it cm-pod-test004 -c cm-test4 -- bash
进入容器后,ls /conf查看是否有test.properties文件。
[root@k8s-master k8s]# kubectl exec -it cm-pod-test004 -c cm-test4 -- bash
root@cm-pod-test004:/usr/local/tomcat# ls /conf
test.properties
关于--from-file的方式的创建指定key和不指定key的区别
1)不指定key名
创建:
[root@k8s-master k8s]# kubectl create cm cm-test-file --from-file=test.properties
输出:
[root@k8s-master k8s]# kubectl get cm cm-test-file -o yaml
2)指定key
创建:
[root@k8s-master k8s]# kubectl create cm cm-test-file02 --from-file=tp=test.properties
输出:
[root@k8s-master k8s]# kubectl get cm cm-test-file -o yaml
若指定key的名称,configmap中将会使用指定名称;若不指定,则默认使用文件名为key。