ConfigMap描述信息
ConfigMap功能在kubernetes1.2版本中引入,许多应用程序会从配置文件、命令参数或环境变量中读取配置信息。ConfigMap API给我们提供了向容器中注入配置信息的机制。ConfigMap可以被用来保存单个属性,也可以用来保存整个配置文件或者JSON二进制大对象。
ConfigMap的创建
Ⅰ、使用目录创建
[root@k8s-master1 kubectl]# ls -l /home/k8s/confgmap/kubectl/ 总用量 8 -rw-r--r-- 1 root root 164 9月 18 11:43 game.properties -rw-r--r-- 1 root root 82 9月 18 11:44 ui.properties [root@k8s-master1 kubectl]# [root@k8s-master1 kubectl]# cat game.properties enemies=aliens lives=3 enemies.cheat=true enemies.cheat.level=nogcdrotten secret.code.passphrase=UUDDLRLRBABAS secret.code.allowed=true secret.code.lives=30 [root@k8s-master1 kubectl]# cat ui.properties color.good=purple color.bad=yellow allow.textmode=true how.nice.to.look=fairlyice [root@k8s-master1 kubectl]# [root@k8s-master1 kubectl]# kubectl create configmap game-config --from-file=/home/k8s/confgmap/kubectl configmap/game-config created [root@k8s-master1 kubectl]#
--from-file
指定在目录下的所有文件都会被用在ConfigMap里面创建一个键值对,建的名称就是文件名,值就是文件内容。
可以通过kubectl get cm -o yaml
或者 kubectl describe cm game-config
查看。
Ⅱ、使用文件创建
只要指定为一个文件就可以从单个文件中创建ConfigMap.
[root@k8s-master1 kubectl]# kubectl create configmap game-config-2 --from-file=/home/k8s/confgmap/kubectl/game.properties configmap/game-config-2 created [root@k8s-master1 kubectl]# kubectl get configmap game-config-2 -o yaml
--form-file这个参数可以使用多次,你可以使用两次分别指定上个实例中的那两个配置文件,效果就跟指定整个目录一样的。
Ⅲ、使用字面值创建
使用文件值创建,利用--form-literal参数传递配置信息,该参数可以使用多次,格式如下:
[root@k8s-master1 kubectl]# kubectl create configmap special-config --from-literal=special.how=very --from-literal=special.type=charm configmap/special-config created [root@k8s-master1 kubectl]# kubectl get configmap special-config -o yaml apiVersion: v1 data: special.how: very special.type: charm kind: ConfigMap metadata: creationTimestamp: "2020-09-18T04:04:59Z" managedFields: - apiVersion: v1 fieldsType: FieldsV1 fieldsV1: f:data: .: {} f:special.how: {} f:special.type: {} manager: kubectl operation: Update time: "2020-09-18T04:04:59Z" name: special-config namespace: default resourceVersion: "258004" selfLink: /api/v1/namespaces/default/configmaps/special-config uid: d0b947f4-2c03-4647-9195-e0bcb1a7277e
Pod中使用ConfigMap
Ⅰ、使用ConfigMap来替代环境变量
apiVersion: v1 kind: ConfigMap metadata: name: special-conifig namespace: default data: special.how: very special.type: charm
apiVersion: v1 kind: ConfigMap metadata: name: env-config namespace: default data: log_level: INFO
新创建一个pod,调用上面创建的ConfigMap,Pod的yaml文件如下
apiVersion: v1 kind: Pod metadata: name: dapi-test-pod spec: containers: - name: test-container image: azukiapp/dig:latest command: ["/bin/sh",'-c',"env"] env: - name: SPECIAL_LEVEL_KEY valueFrom: configMapKeyRef: name: special-config key: special.how - name: SPECIAL_TYPE_KEY valueFrom: configMapKeyRef: name: special-config key: special.type envFrom: - configMapRef: name: env-config restartPolicy: Never
SPECIAL_LEVEL_KEY环境变量对应special-config的special.how的值very
SPECIAL_TYPE_KEY环境变量对应special-config的special.type的值charm
执行如下命令,创建pod。
[root@k8s-master1 confgmap]# kubectl create -f api_test_pod.yaml
Pod创建完之后,可以通过执行kubectl logs
环境变量
[root@k8s-master1 confgmap]# kubectl logs dapi-test-pod ...省略 SPECIAL_LEVEL_KEY=very log_level=INFO MYWEB_PORT_80_TCP=tcp://10.1.153.198:80 KUBERNETES_PORT_443_TCP=tcp://10.1.0.1:443 KUBERNETES_SERVICE_PORT_HTTPS=443 PWD=/ KUBERNETES_SERVICE_HOST=10.1.0.1 BIND_VERSION=9.10.3
Ⅱ、用ConfigMap设置命令行参数
创建另外一个Pod,Pod的名称为cmd_test_pod,yaml文件如下:
apiVersion: v1 kind: Pod metadata: name: cmd-test-pod spec: containers: - name: test-container image: azukiapp/dig:latest command: ["/bin/sh",'-c',"echo $(SPECIAL_LEVEL_KEY) $(SPECIAL_TYPE_KEY)"] env: - name: SPECIAL_LEVEL_KEY valueFrom: configMapKeyRef: name: special-config key: special.how - name: SPECIAL_TYPE_KEY valueFrom: configMapKeyRef: name: special-config key: special.type envFrom: - configMapRef: name: env-config restartPolicy: Never
执行完之后,通过查看日志文件可以查看输入两个环境变量的值
[root@k8s-master1 confgmap]# kubectl logs cmd-test-pod very charm
Ⅲ、通过数据卷插件使用ConfigMap
在数据卷里面使用这个COnfigMap,有不同的选项。最基本的就是将文件填入数据卷,在这个文件中,键就是文件名,键值就是文件内容
apiVersion: v1 kind: Pod metadata: name: dapi-test-pod spec: containers: - name: test-container image: azukiapp/dig:latest command: ["/bin/sh",'-c',"cat /etc/config/special.how"] volumeMounts: - name: config-volume mountPath: /etc/config volumes: - name: config-volume configMap: name: special-config restartPolicy: Never
执行完之后,通过查看日志文件可以查看输入两个环境变量的值.
[root@k8s-master1 confgmap]# kubectl logs dapi-test-pod very[root@k8s-master1 confgmap]#
Ⅳ、ConfigMap的热更新
apiVersion: v1 kind: ConfigMap metadata: name: log-config namespace: default data: log_level: INFO --- apiVersion: apps/v1 kind: Deployment metadata: name: my-nginx spec: replicas: 1 selector: matchLabels: app: my-nginx template: metadata: labels: app: my-nginx spec: containers: - name: my-nginx image: nginx:1.13 ports: - containerPort: 80 volumeMounts: - name: config-volume mountPath: /etc/config volumes: - name: config-volume configMap: name: log-config
创建完之后,执行如下命令查看
[root@k8s-master1 confgmap]# kubectl exec my-nginx-699f668fbb-dchhr -it -- cat /etc/config/log_level INFO[root@k8s-master1 confgmap]#
修改ConfigMap
root@k8s-master1 confgmap]# kubectl edit configmap log-config configmap/log-config edited
修改log_level的值为DEBUG等待大概10秒钟时间,再次查看环境变量的值。
[root@k8s-master1 confgmap]# kubectl exec my-nginx-699f668fbb-dchhr -it -- cat /etc/config/log_level DEBUG[root@k8s-master1 confgmap]#
特别注意configMap 如果以ENV的方式挂载到容器,修改configMap并不会实现热更新