ConfigMap性能在Kubernetes1.2版本中引入,许多应用程序会从配置文件,命令行参数或环境变量中读取配置信息,ConfigMap API给咱们提供了向容器中注入配置信息的机制,ConfigMap能够被用来保留单个属性,也能够用来保留整个配置文件或者JSON二进制对象
ConfigMap的创立
1.应用目录创立
$ ls docs/user-guide/configmap/kubectl/game.propertiesui.properties$ cat docs/user-guide/configmap/kubectl/game.propertiesenemies=alienslives=3enemies.cheat=trueenemies.cheat.level=noGoodRottenenemies.code.passphrase=UUDDLRLRBABASsecret.code.allowed=truesecret.code.lives=30$ cat docs/user-guide/configmap/kubectl/ui.propertiescolor.good=purplecolor.bad=yellowaoolw.textmode=truehow.nice.to.look=fairlyNice$ kubectl create configmap game-config --from-file=docs/user-guide/configmap/kubectl
--from-file
指定在目录下的所有文件都会被用在ConfigMap外面创立一个键值对,键的名字就是文件名,值就是文件的内容
2.应用文件创建
$ kubectl create configmap game-config --from-file=docs/user-guide/configmap/kubectl/game.properties$ kubectl get configmaps game-config -o yaml
3.应用字面值创立
应用文字值创立,利用--from-literal
参数传递配置信息,该参数能够应用屡次,格局如下:
$ kubectl create configmap special-config --from-literal=special.how --from-literal=special.type=charm$ kubectl get configmaps special-config -o yaml
Pod中应用ConfigMap
- 应用ConfigMap来代替环境变量
apiVersion: v1kind: ConfigMapmetadata: name :special-config namespace: defaultdata: special.how: very special.type.charm
apiVersion: v1kind: ConfigMapmetadata: name: env-config namespace: defaultdata: log_level: INFO
apiVersion: v1kind: Podmetadata: name: dapi-test-podspec: containers: - name: test-container image: myapp:v1 command: ["/bin/sh","-c","env"] env: - name: SPECIAL_LEVEL_KEY calueFrom: 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
- 用ConfigMap设置命令行参数
apiVersion: v1kind: ConfigMapmetadata: name: special-config namespace: defaultdata: special.how: very special.type: charm
apiVersion: v1kind: Podmetadata: name: dapi-test-podspec: containers: - name: test-container image: 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 calueFrom: configMapKeyRef: name: special-config key: special.type restartPolicy: Never
- 通过数据卷插件应用ConfigMap
apiVersion: v1kind: ConfigMapmetadata: name: special-config namespace: dafaultdata: special.how: very special.type: charm
在数据卷外面应用这个ConfigMap,有不同的选项,最根本的就是将文件填入数据卷,在这个文件中,键就是文件名,值就是文件内容
apiVersion: v1kind: Podmetadata: name: dapi-test-podspec: containers: - name: test-container image: myapp:v1 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
ConfigMap的热更新
apiVersion: v1kind: ConfigMapmetadata: name: log-config namespace: defaultdata: log_level: INFO---apiVersion: extensions/v1beta1kind: Deploymentmetadata: name: my-nginxspec: replicas: 1 template: metadata: labels: run: my-nginx spec: containers: - name: my-nginx image: myapp:v1 ports: - containerPort: 80 volumeMounts: - name: config-volume mountPath: /etc/config volumes: - name: config-volume configMap: name: log-config
$ kubectl exec 'kubectl get pods -l run-my-nginx -o=name | cut -d "/" -f2' --it -- cat /etc/config/log_levelINFO
批改ConfigMap
$ kubectl edit configmap log-config
批改log_level
的值为DEBUG
期待大概10秒,再次查看环境变量的值
$ kubectl exec 'kubectl get pods -l run-my-nginx -o=name | cut -d "/" -f2' --it -- cat /etc/config/log_levelDEBUG
留神: ConfigMap如果以ENV的形式挂载至容器,批改ConfigMap并不会实现热更新
ConfigMap更新后滚动更新Pod
更新ConfigMap目前并不会触发相干Pod的滚动更新,能够通过批改pod annotations的形式强制触发滚动更新
$ kubectl patch deployment my-nginx --patch '{"spec": {"template": {"metadata": {"annotations":{"version/config": "20210516"}}}}}'
更新ConfigMap后:
- 应用该ConfigMap挂载的ENV不会同步更新
- 应用该ConfigMap挂载的Volume中的数据须要一段时间(10s)能力同步更新