ConfigMap
ConfigMap function Kubernetes1.2 version was introduced, many of the application from the configuration file, the command-line parameters or environment variables read with
configuration information. ConfigMap API provides us injection mechanism configuration information to the vessel, The ConfigMap may be used to hold a single attribute, it can also
be used to save the entire configuration file or JSON blob
Creating ConfigMap
You can use kubectl create configmap create ConfigMap from file, directory or key-value string to create and so on.
Use directory created using files created using a literal creation
[root@k8s-master dir]# 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 [root@k8s-master dir]# cat ui.properties color.good=purple color.bad=yellow allow.textmode=true how.nice.to.look=fairlyNice [root@k8s-master dir]#
root@k8s-master dir]# kubectl create configmap game-config --from-file=../dir/ configmap/game-config created [root@k8s-master dir]# kubectl get cm NAME DATA AGE game-config 2 21s [root@k8s-master dir]# kubectl get cm game-config -o yaml apiVersion: v1 data: 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: | color.good=purple color.bad=yellow allow.textmode=true how.nice.to.look=fairlyNice kind: ConfigMap metadata: creationTimestamp: "2019-12-25T13:51:28Z" name: game-config namespace: default resourceVersion: "96998" selfLink: /api/v1/namespaces/default/configmaps/game-config uid: 7fa2195e-08b9-4ab2-927b-21420493e28f
[root@k8s-master dir]# kubectl create configmap game-config2 --from-file=game.properties configmap/game-config2 created [root@k8s-master dir]# kubectl get cm NAME DATA AGE game-config 2 4m11s game-config2 1 28s [root@k8s-master dir]# kubectl create configmap special-config --from-literal=special.how=very --from-literal=special.type=charm configmap/special-config created [root@k8s-master dir]# kubectl get cm NAME DATA AGE game-config 2 9m29s game-config2 1 5m46s special-config 2 3s [root@k8s-master dir]# kubectl describe cm special-config Name: special-config Namespace: default Labels: <none> Annotations: <none> Data ==== special.how: ---- very special.type: ---- charm Events: <none> [root@k8s-master dir]# vim env.yaml [root@k8s-master dir]# kubectl apply -f env.yaml configmap/env-config created [root@k8s-master dir]# kubectl get cm NAME DATA AGE env-config 1 6s game-config 2 14m game-config2 1 11m special-config 2 5m32s
Pod used ConfigMap
Ⅰ, using ConfigMap instead environment variables
[root@k8s-master dir]# cat pod.yaml apiVersion: v1 kind: Pod metadata: name: dapi-test-pod spec: containers: - name: test-container image: wangyanglinux/myapp:v1 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 [root@k8s-master dir]# ll 总用量 28 -rw-r--r-- 1 root root 376 12月 25 22:48 111.yaml -rw-r--r-- 1 root root 105 12月 25 22:06 env.yaml -rw-r--r-- 1 root root 158 12月 25 21:50 game.properties -rw-r--r-- 1 root root 616 12月 25 23:09 log-config.yaml -rw-r--r-- 1 root root 560 12月 25 22:34 pod1.yaml -rw-r--r-- 1 root root 584 12月 25 22:26 pod.yaml -rw-r--r-- 1 root root 83 12月 25 21:50 ui.properties [root@k8s-master dir]# cat env.yaml apiVersion: v1 kind: ConfigMap metadata: name: env-config namespace: default data: log_level: INFO [root@k8s-master dir]#
result:
[root@k8s-master dir]# vim pod.yaml [root@k8s-master dir]# kubectl create -f pod.yaml pod/dapi-test-pod created [root@k8s-master dir]# kubectl get pod NAME READY STATUS RESTARTS AGE dapi-test-pod 0/1 Completed 0 3s [root@k8s-master dir]# kubectl logs pod dapi-test-pod Error from server (NotFound): pods "pod" not found [root@k8s-master dir]# kubectl logs dapi-test-pod MYAPP_SVC_PORT_80_TCP_ADDR=10.98.57.156 KUBERNETES_PORT=tcp://10.96.0.1:443 KUBERNETES_SERVICE_PORT=443 MYAPP_SVC_PORT_80_TCP_PORT=80 HOSTNAME=dapi-test-pod SHLVL=1 MYAPP_SVC_PORT_80_TCP_PROTO=tcp HOME=/root SPECIAL_TYPE_KEY=charm MYAPP_SVC_PORT_80_TCP=tcp://10.98.57.156:80 NGINX_VERSION=1.12.2 KUBERNETES_PORT_443_TCP_ADDR=10.96.0.1 PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin KUBERNETES_PORT_443_TCP_PORT=443 KUBERNETES_PORT_443_TCP_PROTO=tcp MYAPP_SVC_SERVICE_HOST=10.98.57.156 SPECIAL_LEVEL_KEY=very log_level=INFO KUBERNETES_SERVICE_PORT_HTTPS=443 KUBERNETES_PORT_443_TCP=tcp://10.96.0.1:443 PWD=/ KUBERNETES_SERVICE_HOST=10.96.0.1 MYAPP_SVC_SERVICE_PORT=80 MYAPP_SVC_PORT=tcp://10.98.57.156:80
Ⅱ, with ConfigMap command line arguments to
[root@k8s-master dir]# cat pod1.yaml apiVersion: v1 kind: Pod metadata: name: dapi-test-pod66 spec: containers: - name: test-container image: wangyanglinux/myapp:v1 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 restartPolicy: Never [root@k8s-master dir]#
result:
[root@k8s-master dir]# vim pod1.yaml [root@k8s-master dir]# kubectl create -f pod1.yaml pod/dapi-test-pod66 created [root@k8s-master dir]# kubectl get pod NAME READY STATUS RESTARTS AGE dapi-test-pod 0/1 Completed 0 7m45s dapi-test-pod66 0/1 Completed 0 5s [root@k8s-master dir]# kubectl logs dapi-test-pod66 very charm
Ⅲ, data volume by using plug ConfigMap
[root@k8s-master dir]# cat 111.yaml apiVersion: v1 kind: Pod metadata: name: dapi-test-pod11 spec: containers: - name: test-container image: wangyanglinux/myapp:v1 command: [ "/bin/sh", "-c", "sleep 600s" ] volumeMounts: - name: config-volume mountPath: /etc/config volumes: - name: config-volume configMap: name: special-config restartPolicy: Never [root@k8s-master dir]#
result:
[root@k8s-master dir]# vim 111.yaml [root@k8s-master dir]# kubectl create -f 111.yaml pod/dapi-test-pod11 created [root@k8s-master dir]# kubectl get pod NAME READY STATUS RESTARTS AGE dapi-test-pod 0/1 Completed 0 22m dapi-test-pod11 1/1 Running 0 5s dapi-test-pod66 0/1 Completed 0 14m [root@k8s-master dir]# kubectl exec dapi-test-pod11 -it -- /bin/sh / # cd /etc/config /etc/config # ls special.how special.type /etc/config # cat special.how very/etc/config # cat special.type charm/etc/config # exit
ConfigMap hot update
[root@k8s-master dir]# cat log-config.yaml 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: wangyanglinux/myapp:v1 ports: - containerPort: 80 volumeMounts: - name: config-volume mountPath: /etc/config volumes: - name: config-volume configMap: name: log-config [root@k8s-master dir]#
result:
[root@k8s-master dir]# vim log-config.yaml [root@k8s-master dir]# kubectl apply -f log-config.yaml configmap/log-config unchanged deployment.apps/my-nginx created [root@k8s-master dir]# kubectl get pod NAME READY STATUS RESTARTS AGE my-nginx-5d57c6897b-fm2ql 1/1 Running 0 8s [root@k8s-master dir]# kubectl exec my-nginx-5d57c6897b-fm2ql -it -- cat /tec/config/log_level cat: can't open '/tec/config/log_level': No such file or directory command terminated with exit code 1 [root@k8s-master dir]# kubectl exec my-nginx-5d57c6897b-fm2ql -it -- cat /etc/config/log_level INFO[root@k8s-master dir]# kubectl edit configmap log-config configmap/log-config edited [root@k8s-master dir]# kubectl exec my-nginx-5d57c6897b-fm2ql -it -- cat /etc/config/log_level INFO[root@k8s-master dir]# kubectl exec my-nginx-5d57c6897b-fm2ql -it -- cat /etc/config/log_level [root@k8s-master dir]# kubectl exec my-nginx-5d57c6897b-fm2ql -it -- cat /etc/config/log_level DEBUG[root@k8s-master dir]#
Update ConfigMap after:
Use this ConfigMap mounted Env not updated simultaneously
using the ConfigMap mounted Volume data will take some time (measured about 10 seconds) to synchronize updates