[TOC]html
不少状况下咱们为某一应用作好镜像,当咱们想修改其中的一些参数的时候,就变得比较麻烦,又要从新制做镜像,咱们是否是有一种方式,让镜像根据不一样的场景调用咱们不一样的配置文件呢,那咱们就须要用到 k8s 的另一种资源,那就是 ConfigMap。nginx
咱们知道,在几乎全部的应用开发中,都会涉及到配置文件的变动,好比说在web的程序中,须要链接数据库,缓存甚至是队列等等。而咱们的一个应用程序从写第一行代码开始,要经历开发环境、测试环境、预发布环境只到最终的线上环境。而每个环境都要定义其独立的各类配置。若是咱们不能很好的管理这些配置文件,你的运维工做将顿时变的无比的繁琐。为此业内的一些大公司专门开发了本身的一套配置管理中心,如360的Qcon,百度的disconf等。kubernetes也提供了本身的一套方案,即ConfigMap。kubernetes经过ConfigMap来实现对容器中应用的配置管理。web
ConfigMap是用来存储配置文件的kubernetes资源对象,全部的配置内容都存储在etcd中。shell
建立ConfigMap的方式有4种:数据库
kubectl create configmap nginx-config --from-literal=nginx_port=80 --from-literal=server_name=myapp.magedu.com #查看configmap [root@master ~]# kubectl get cm NAME DATA AGE nginx-config 2 4s #查看configmap的具体信息 [root@master ~]# kubectl describe configmaps nginx-config Name: nginx-config Namespace: default Labels: <none> Annotations: <none> Data ==== nginx_port: ---- 80 server_name: ---- myapp.magedu.com Events: <none>
#文件内容 cat manifests/configmap/www.conf server { server_name myapp.magedu.com; listen 80; root /data/web/html } #经过文件建立configmap kubectl create configmap nginx-www --from-file=./manifests/configmap/www.conf #查看configmap [root@master configmap]# kubectl describe configmaps nginx-www Name: nginx-www Namespace: default Labels: <none> Annotations: <none> Data ==== www.conf: ---- server { server_name myapp.magedu.com; listen 80; root /data/web/html } Events: <none>
cat << EOF > env.txt db.host=10.0.0.50 db.port=3306 EOF kubectl create cm env-cm --from-env-file=env.txt
若是有多个env文件, 只有最后一个env文件会生效 |
[root@master configmap_test]# 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@master configmap_test]# cat ui.properties color.good=purple color.bad=yellow allow.textmode=true how.nice.to.look=fairlyNice
#执行命令建立configmap kubectl create configmap configmap-env --from-env-file=./game.properties --from-env-file=./ui.properties #能够看到, 只有ui.properties生效了 [root@master configmap_test]# kubectl get configmaps configmap-env -o yaml apiVersion: v1 data: allow.textmode: "true" color.bad: yellow color.good: purple how.nice.to.look: fairlyNice kind: ConfigMap metadata: creationTimestamp: "2019-09-11T01:58:17Z" name: configmap-env namespace: default resourceVersion: "186936" selfLink: /api/v1/namespaces/default/configmaps/configmap-env uid: 4e36009f-267c-4713-8a7a-99d8f6dd3039
kubectl apply -f
建立[root@master configmap]# cat test.yaml apiVersion: v1 kind: ConfigMap metadata: name: cm-4 data: db.host: 10.0.0.50 db.port: "3306" [root@master configmap]# kubectl apply -f test.yaml [root@master configmap]# kubectl describe cm cm-4 Name: cm-4 Namespace: default Labels: <none> Annotations: kubectl.kubernetes.io/last-applied-configuration: {"apiVersion":"v1","data":{"db.host":"10.0.0.50","db.port":"3306"},"kind":"ConfigMap","metadata":{"annotations":{},"name":"cm-4","... Data ==== db.host: ---- 10.0.0.50 db.port: ---- 3306 Events: <none>
使用ConfigMap有二种方式:api
apiVersion: v1 kind: Pod metadata: name: pod-cm-1 #name必须小写 namespace: default labels: app: myapp tier: frontend annotations: create-by: tianpei.wang spec: containers: - name: myapp image: ikubernetes/myapp:v1 ports: - name: http containerPort: 80 env: - name: NGINX_SERVER_PORT valueFrom: configMapKeyRef: name: nginx-config key: nginx_port - name: NGINX_SERVER_NAME valueFrom: configMapKeyRef: name: nginx-config key: server_name
#建立configmap kubectl create configmap nginx-config --from-literal=nginx_port=80 --from-literal=server_name=myapp.magedu.com #建立pod经过configmap注入到pod内 [root@master configmap]# kubectl apply -f pod-cm-1.yaml pod/pod-cm-1 created 能够看到已经成功注入到pod中了 [root@master configmap]# kubectl exec -it pod-cm-1 -- /bin/sh / # printenv |grep NGINX NGINX_SERVER_PORT=80 NGINX_SERVER_NAME=myapp.magedu.com
当以环境变量的方式注入pod时, 只在pod启动时加载, 后续更改configmap不会同步到pod内 |
apiVersion: v1 kind: Pod metadata: name: pod-cm-2 #name必须小写 namespace: default labels: app: myapp tier: frontend annotations: create-by: tianpei.wang spec: containers: - name: myapp image: ikubernetes/myapp:v1 ports: - name: http containerPort: 80 volumeMounts: - name: nginxconf mountPath: /etc/nginx/config.d readOnly: true volumes: - name: nginxconf configMap: name: nginx-config
#经过上边的yaml文件建立pod, 并将configmap以volumes的形式挂载到pod内 kubectl apply -f pod-cm-2.yaml #能够看到已经生效了 [root@master configmap]# kubectl exec -it pod-cm-2 -- /bin/sh / # cd /etc/nginx/config.d/ /etc/nginx/config.d # ls nginx_port server_name /etc/nginx/config.d # cat nginx_port 80 #将configmap的端口更改成8080 [root@master configmap]# kubectl edit cm nginx-config configmap/nginx-config edited [root@master configmap]# kubectl describe cm nginx-config Name: nginx-config Namespace: default Labels: <none> Annotations: <none> Data ==== nginx_port: ---- 8080 server_name: ---- myapp.magedu.com Events: <none> #等待一段时间后生效了 [root@master configmap]# kubectl exec -it pod-cm-2 -- /bin/sh / # cd /etc/nginx/config.d/ /etc/nginx/config.d # ls nginx_port server_name /etc/nginx/config.d # cat nginx_port /etc/nginx/config.d # cat nginx_port /etc/nginx/config.d # cat nginx_port /etc/nginx/config.d # cat nginx_port 8080 ``````shell wget https://k8s.io/examples/configmap/ui-env-file.properties -O configure-pod-container/configmap/ui-env-file.properties
建立configmap缓存
apiVersion: v1 kind: ConfigMap metadata: name: special-config namespace: default data: SPECIAL_LEVEL: very SPECIAL_TYPE: charm
将configmap中的SPECIAL_LEVEL
挂载到pod
中/etc/config/keys
app
apiVersion: v1 kind: Pod metadata: name: test-pod spec: containers: - name: test-container image: busybox command: [ "/bin/sh","-c","sleep 3600"] volumeMounts: - name: config-volume mountPath: /etc/config volumes: - name: config-volume configMap: name: special-config items: - key: SPECIAL_LEVEL path: keys
能够看到已经生效了运维
[root@master configmap_test]# kubectl exec -ti test-pod -- /bin/sh / # cat /etc/config/keys very