ConfigMap 介绍
在Kubernetes 1.2中新添加了功能ConfigMap,主要功能是为了解决应用程序会从配置文件、环境变量中获取配置信息。但是默认情况下配置信息需要与docker images解耦,ConfigMap API为我们提供了向容器中注入配置信息的机制,ConfigMap可以被用来保存单个属性,也可以用来保存整个配置文件或者JSON二进制对象
ConfigMap API资源用来保存key-vlaue pair配置数据,这个数据可以在pods里使用,或者被用来为contaroller一样的系统组件存储配置数据。ConfigMap 是为了方便的处理不含铭感信息的字符串,你可以将它理解为Linux系统中的/etc目录,专门用来存储配置文件的目录
注意: ConfigMap不是属性配置文件的代替品,ConfigMap只是作为多个properties文件的引用。
ConfigMap 项目演示
configmap主要还是方便处理非敏感的数据,比如注册中心地址、数据库地址、nginx地址等。像密码之类需要加密的还是需要使用secrets来进行管理
下方yaml文件中data一栏包括了配置数据,ConfigMap可以保存单个属性,也可以用来保存一个配置文件。配置数据可以通过很多种方式在Pods里被引用,ConfigMap可以用来:
·设置环境变量的值
·在容器里设置命令行参数
·在数据卷里面创建config文件
用户和系统两者都可以在ConfigMap里存储
1. 使用Yaml文件创建ConfigMap
说明 configmap支持单个key-value键值,也支持config文件的方式
单个环境变量引用
apiVersion: v1kind: ConfigMapmetadata:name: configmap-demodata:data1: hellodata2: test#其中这里data1=hello,类似于环境变量 (如果是数字需要添加双引号)
配置文件变量引用 比如我们需要创建多个环境变量,这里可以直接引用配置文件的方式
apiVersion: v1kind: ConfigMapmetadata:name: configmap-demodata:config: | ###这里是文件名称zuul_host=123456 #变量名称及值zuul_port=123mysql_url=456
下面进行创建文件演示
#编辑配置文件---apiVersion: v1kind: ConfigMapmetadata:name: configmap-demodata:config: |zuul_host=123456zuul_port=123mysql_url=456#创建configmap[root@abcdocker yaml]# kubectl apply -f configmap.yamlconfigmap/configmap-demo created[root@yzsjhl82-135 yaml]# kubectl get configmapNAME DATA AGEconfigmap-demo 1 6s#这里可以看到DATA位1,说明只有一个文件[root@abcdocker yaml]# kubectl describe configmap configmap-demoName: configmap-demoNamespace: defaultLabels: <none>Annotations: kubectl.kubernetes.io/last-applied-configuration:{"apiVersion":"v1","data":{"config":"zuul_host=123456 \nzuul_port=123\nmysql_url=456\n"},"kind":"ConfigMap","metadata":{"annotations":{}...Data====config:----zuul_host=123456zuul_port=123mysql_url=456Events: <none>
2.使用kubectl创建
[root@abcdocker yaml]# kubectl create configmap -hExamples:# Create a new configmap named my-config based on folder barkubectl create configmap my-config --from-file=path/to/bar# Create a new configmap named my-config with specified keys instead of file basenames on diskkubectl create configmap my-config --from-file=key1=/path/to/bar/file1.txt --from-file=key2=/path/to/bar/file2.txt# Create a new configmap named my-config with key1=config1 and key2=config2kubectl create configmap my-config --from-literal=key1=config1 --from-literal=key2=config2# Create a new configmap named my-config from the key=value pairs in the filekubectl create configmap my-config --from-file=path/to/bar# Create a new configmap named my-config from an env filekubectl create configmap my-config --from-env-file=path/to/bar.env
这里我模拟挂载test目录下的2个文件
#当前目录结构[root@abcdocker~]# ll test/总用量 8-rw-r--r-- 1 root root 21 11月 19 20:43 mysql.conf-rw-r--r-- 1 root root 21 11月 19 20:44 nginx.conf#创建configmap[root@abcdocker ~]# kubectl create configmap cm-demo-test --from-file=testconfigmap/cm-demo-test created#后面--from-file接的是目录,也可以是文件#创建完毕后我们进行查看[root@abcdocker ~]# kubectl get configmapNAME DATA AGEcm-demo-test 2 13sconfigmap-demo 3 16m[root@abcdocker ~]# kubectl describe configmap cm-demo-testName: cm-demo-testNamespace: defaultLabels: <none>Annotations: <none>Data====mysql.conf:----mysql_url=10.4.82.11nginx.conf:----nginx_url=10.4.11.11Events: <none>#也可以使用命令行创建单个kubectl create configmap cm-name --from-literal=abc.host=localhost --from-literal=aaa.port=80#--from-literal支持引用多个
温馨提示:使用describe的时候,配置文件如果过大,不会完全显示,如果要查看详细信息需要使用kubectl get configmap [cm-name] -o yaml使用yaml展示(输出格式也可以修改为json)

3.使用环境变量引用configmap
引用单个环境变量
首先创建configmap
apiVersion: v1kind: ConfigMapmetadata:name: configmap-demonamespace: defaultdata:db.host: localhostdb.port: "3306"
创建Pod进行引用
apiVersion: v1kind: Podmetadata:name: test-demo-configmapspec:containers:- name: test-demo-cmimage: busyboxcommand: [ "/bin/sh","-c","env" ]env: #引用环境变量- name: ZUUL_HOST #环境变量值 (这个值可以与下面configmap key中不相同)valueFrom: #环境变量值来源configMapKeyRef: #使用configmap值name: configmap-demo #configmap 名称key: db.host #configmap中的值
检查结果
[]NAME READY STATUS RESTARTS AGEtest-demo-configmap 0/1 Completed 2 48s[]HOSTNAME=test-demo-configmapZUUL_HOST=localhost...
引用文件,不定义key
另外的一种写法,直接引用变量,不自定义Key,主要是用于引用文件,可以使用这种方式
##yaml文件如下apiVersion: v1kind: Podmetadata:name: test-demo-configmap-filespec:containers:- name: test-demo-cmimage: busyboxcommand: [ "/bin/sh","-c","env" ]envFrom: #引用文件作为环境变量- configMapRef: #configmap来源name: configmap-demo #configmap名称####直接创建就可以,这里就是将configmap里面的值全部引用进来## 查看日志[root@yzsjhl82-135 yaml]# kubectl logs test-demo-configmap-file...db.host=localhostdb.port=3306...
4.通过命令引用configmap
在configmap里面支持命令引用变量的方式
apiVersion: v1kind: Podmetadata:name: test-demo-configmap-envspec:containers:- name: test-demo-cmimage: busyboxcommand: [ "/bin/sh","-c","echo $(DB_HOST) $(DB_PORT)" ]env:- name: DB_HOSTvalueFrom:configMapKeyRef:name: configmap-demokey: db.host- name: DB_PORTvalueFrom:configMapKeyRef:name: configmap-demokey: db.port#这里还是使用Key的方式,命令行直接echo下面的环境变量就可以
由于使用的busybox镜像,没有后台执行命令,所以会出现CrashLoopBackOff状态,是属于正常状态
4.通过数据卷挂载引用configmap
配置文件如下,包含configmap文件
apiVersion: v1kind: ConfigMapmetadata:name: configmap-demo-volumenamespace: defaultdata:nginx.conf: |nginx.host: localhostnginx.port: "3306"---apiVersion: v1kind: Podmetadata:name: test-demo-configmap-vloumespec:containers:- name: test-demo-cmimage: busyboxcommand: [ "/bin/sh","-c","cat /etc/config/nginx.conf" ]volumeMounts: #挂载声明- name: config-volume #volume名称mountPath: /etc/config #挂载点volumes:- name: config-volume #和volumeMounts的名称要一致configMap:name: configmap-demo-volume #configmap名称
将上面的yaml保存到文件进行执行,然后我们直接看pod日志就可以了,从pod日志可以看出我们挂载已经完成,因为默认的busybox镜像中没有nginx.conf,通过挂载才获取到的
[]configmap/configmap-demo-volume createdpod/test-demo-configmap-vloume created[]nginx.host: localhostnginx.port: "3306"
如果/etc/没有config目录也是可以挂载的,并不影响
如果我们有2个pod都需要挂载configmap,其中一个是在/etc/config/nginx.conf,还有一个是在/etc/config/path/to目录下,这时候就可以使用下面的案例
apiVersion: v1kind: Podmetadata:name: test-demo-configmap-nginxspec:containers:- name: test-demo-cmimage: busyboxcommand: [ "/bin/sh","-c","cat /etc/config/path/go/nginx.conf" ]volumeMounts: #挂载声明- name: config-volume #volume名称mountPath: /etc/config #挂载点volumes:- name: config-volume #和volumeMounts的名称要一致configMap:name: configmap-demo-volume #configmap名称items:- key: nginx.conf #configmap中的keypath: path/go/nginx.conf #挂载路径
日志如下
[]nginx.host: localhostnginx.port: "3306"
需要注意的是,当configmap以数据卷的形式挂载Pod时,这时更新ConfigMap,Pod内挂载的配置信息会热更新。如果服务也需要更新则需要对应的reload服务

