您现在的位置是:首页 > 文章详情

kubernetes ConfigMap

日期:2018-07-05点击:401

ConfigMap概述

configmap用于将应用所需的配置信息与程序进行分离, 使应用程序更好的复用, 在大规模容器集群环境中, 对应用进行统一配置管理.

一般应用场景:

  • 生成为容器内的环境变量;
  • 以Volume的形式挂载为容器内部的文件或目录.

configmap以一个或多个key:value的形式保存在kubernetes系统中供应用使用, 既可以用于表示一个变量的值(例如log-level:info), 也可以用于表示一个完整的配置文件内容(例如xml或properties配置文件的全部内容).

声明方式:

  • 使用yaml配置文件;
  • 使用命令行命令(kubectl create configmap).

Pod中使用方式:

  • 环境变量方式
  • Volume挂载

创建ConfigMap

1. 使用yaml文件方式创建

1) 内容为变量

创建yaml文件cm-log-test.yaml:

apiVersion: v1 kind: ConfigMap metadata: name: cm-log data: apploglevel: info appdatadir: /var/data

生成configmap:

[root@localhost pod_dir]# kubectl create -f cm-log-test.yaml configmap "cm-log" created

2) 内容为配置文件内容

创建yaml文件cm-file-test.yaml:

apiVersion: v1 kind: ConfigMap metadata: name: cm-file data: key-serverxml: | <?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" http://www.springframework.org/schema/task/spring-task.xsd"> <context:annotation-config /> <context:component-scan base-package="com.ysbd" /> <aop:aspectj-autoproxy proxy-target-class="true" /> <bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close"> <constructor-arg> <bean class="com.zaxxer.hikari.HikariConfig"> <property name="poolName" value="HikariDataSource" /> <property name="driverClassName" value="${jdbc.driverClassName}" /> </bean> </constructor-arg> </bean> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property> </bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <tx:advice id="transactionAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="*" propagation="REQUIRED" /> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut expression="execution(* com.ysbd.service..*.*(..))" id="allManagerMethod" /> <aop:advisor advice-ref="transactionAdvice" pointcut-ref="allManagerMethod" /> </aop:config> </beans> key-loggingproperties: | jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/ku8manage?useUnicode=true&useLegacyDatetimeCode=false&serverTimezone=Asia/Shanghai jdbc.username=root jdbc.password=****** https=true web.uri=http://localhost:8080 web.session.expireTime=1200

生成configmap:

[root@localhost pod_dir]# kubectl create -f cm-file-test.yaml configmap "cm-file" created

2. 使用命令行方式创建

1) 字面值创建

使用--from-literal按字面值创建, 每个--from-literal后跟一个键值对.

# 语法 kubectl create configmap <cm-name> --from-literal=key1=value1 --from-literal=key2=value2
[root@localhost pod_dir]# kubectl create configmap cm-literal --from-literal=loglevel=info --from-literal=datadir=/var/data configmap "cm-literal" created

验证

[root@localhost pod_dir]# kubectl get configmap cm-literal -o yaml apiVersion: v1 data: datadir: /var/data loglevel: info kind: ConfigMap metadata: creationTimestamp: 2018-07-05T11:29:55Z name: cm-literal namespace: default resourceVersion: "29156" selfLink: /api/v1/namespaces/default/configmaps/cm-literal uid: bdfe61f9-8046-11e8-b599-000c2964ecfc

2) 文件创建

使用--from-file按文件创建, 每个--from-file后跟一个文件路径, 也可以指定key名称, 可选.

# 语法 kubectl create configmap <cm-name> --from-file=[key=]file-path-1 --from-file=[key=]file-path-2
[root@localhost pod_dir]# kubectl create configmap cm-file --from-file=./server.xml --from-file=./log.properties configmap "cm-file" created

验证

[root@localhost pod_dir]# kubectl describe configmaps cm-file Name: cm-file Namespace: default Labels: <none> Annotations: <none> Data ==== log.properties: ---- verClassName=com.mysql.jdbc.Driver... server.xml: ---- ns xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance...

3) 目录创建

使用--from-literal按字面值创建, --from-file后跟一个目录, 该目录下的每个配置文件名都会被设置为key, 文件的内容被设置为value.

# 语法 kubectl create configmap <cm-name> --from-file=config-file-dir
[root@localhost pod_dir]# kubectl create configmap cm-dir --from-file=./config-dir/ configmap "cm-dir" created

验证

[root@localhost pod_dir]# kubectl describe configmap cm-dir Name: cm-dir Namespace: default Labels: <none> Annotations: <none> Data ==== log.properties: ---- verClassName=com.mysql.jdbc.Driver... server.xml: ---- ns xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance...

在Pod中使用ConfigMap

1. 环境变量方式

以前面创建的创cm-log为例:

apiVersion: v1 kind: ConfigMap metadata: name: cm-log data: apploglevel: info appdatadir: /var/data

定义pod的yaml文件pod-cm-test.yaml:

apiVersion: v1 kind: Pod metadata: name: cm-test-pod labels: name: cm-pod-label spec: containers: - name: cm-test image: busybox command: ["/bin/sh", "-c", "env | grep APP"] env: - name: APPLOGLEVEL #定义环境变量名称 valueFrom: configMapKeyRef: name: cm-log #环境变量的值取自cm-log key: apploglevel #取key apploglevel对应的值 - name: APPDATADIR valueFrom: configMapKeyRef: name: cm-log key: appdatadir restartPolicy: Never

创建Pod:

[root@localhost pod_dir]# kubectl create -f pod-cm-test.yaml pod "cm-test-pod" created

验证:

[root@localhost pod_dir]# kubectl logs cm-test-pod APPDATADIR=/var/data APPLOGLEVEL=info

上面用到了valueFrom, 用于指定单个环境变量情况, 还有envFrom, 可以将指定ConfigMap中的所有键值自动生成环境变量, 环境变量名称就是key, 环境变量的值就是value. 如下:

apiVersion: v1 kind: Pod metadata: name: cm-test-pod labels: name: cm-pod-label spec: containers: - name: cm-test image: busybox command: ["/bin/sh", "-c", "env"] envFrom: - configMapRef: name: cm-log restartPolicy: Never

创建Pod:

[root@localhost pod_dir]# kubectl create -f pod-test-envFrom.yaml pod "cm-test-pod" created

验证:

[root@localhost pod_dir]# kubectl logs cm-test-pod apploglevel=info KUBERNETES_PORT=tcp://10.68.0.1:443 KUBERNETES_PORT_443_TCP_PROTO=tcp appdatadir=/var/data KUBERNETES_PORT_443_TCP=tcp://10.68.0.1:443 PWD=/

2. Volume挂载

使用上边以cm-file-test.yaml创建的cm-file为例.

创建Pod:

apiVersion: v1 kind: Pod metadata: name: cm-test-volume spec: containers: - name: cm-test-volume image: kubeguide/tomcat-app:v1 ports: - containerPort: 8080 volumeMounts: - name: server mountPath: /configfiles volumes: - name: server configMap: name: cm-file items: - key: key-serverxml path: server.xml - key: key-properties path: logging.properties

创建Pod:

[root@localhost pod_dir]# kubectl create -f pod-volume-test.yaml pod "cm-test-volume" created

验证:
登录容器, 查看configfiles目录下存在server.xml和logging.properties文件:

[root@localhost pod_dir]# kubectl exec -ti cm-test-volume -- bash root@cm-test-volume:/usr/local/tomcat# cd /configfiles/ root@cm-test-volume:/configfiles# ls -a . .. ..2018_07_05_13_14_04.531049961 ..data logging.properties server.xml

查看文件内容:

root@cm-test-volume:/configfiles# cat logging.properties verClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/ku8manage?useUnicode=true&useLegacyDatetimeCode=false&serverTimezone=Asia/Shanghai ... root@cm-test-volume:/configfiles# cat logging.properties verClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/ku8manage?useUnicode=true&useLegacyDatetimeCode=false&serverTimezone=Asia/Shanghai jdbc.username=root jdbc.password=****** https=true web.uri=http://localhost:8080 web.session.expireTime=1200 root@cm-test-volume:/configfiles# cat server.xml ns xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" http://www.springframework.org/schema/task/spring-task.xsd"> ...

如果在引用ConfigMap时不指定items, 则使用volumeMount方式在容器内的目录中为每个item生成一个文件名为key的文件, 如下:
创建Pod:

apiVersion: v1 kind: Pod metadata: name: cm-test-volume spec: containers: - name: cm-test-volume image: kubeguide/tomcat-app:v1 ports: - containerPort: 8080 volumeMounts: - name: server mountPath: /configfiles volumes: - name: server configMap: name: cm-file

应用: 使用ConfigMap配置redis

创建ConfigMap:

[root@localhost pod_dir]# kubectl create configmap example-redis-config --from-file=./redis-config configmap "example-redis-config" created

redis-config文件内容如下:

maxmemory 2mb maxmemory-policy allkeys-lru

查看已创建的配置ConfigMap:

[root@localhost pod_dir]# kubectl get configmap example-redis-config -o yaml apiVersion: v1 data: redis-config: | maxmemory 2mb maxmemory-policy allkeys-lru kind: ConfigMap metadata: creationTimestamp: 2018-07-06T02:03:56Z name: example-redis-config namespace: default resourceVersion: "41583" selfLink: /api/v1/namespaces/default/configmaps/example-redis-config uid: d6fa0d84-80c0-11e8-b599-000c2964ecfc

创建pod yaml文件:

apiVersion: v1 kind: Pod metadata: name: redis spec: containers: - name: redis image: kubernetes/redis:v1 env: - name: MASTER value: "true" ports: - containerPort: 6379 resources: limits: cpu: "0.1" volumeMounts: - mountPath: /redis-master-data name: data - mountPath: /redis-master name: config volumes: - name: data emptyDir: {} - name: config configMap: name: example-redis-config items: - key: redis-config path: redis.conf

创建pod:

[root@localhost pod_dir]# kubectl create -f pod-cm-redis-test.yaml pod "redis" created

验证:
进入容器查看

[root@localhost pod_dir]# kubectl exec -it redis redis-cli 127.0.0.1:6379> CONFIG GET maxmemory 1) "maxmemory" 2) "2097152" 127.0.0.1:6379> config get maxmemory-policy 1) "maxmemory-policy" 2) "allkeys-lru"

使用ConfigMap须知:

  • ConfigMap必须在pod创建之前创建;
  • ConfigMap受Namespace限制, 处于同一Namespace的pod才能引用到它;
  • kubelet只支持被API server管理的pod使用ConfigMap, 也就是说静态pod不能使用ConfigMap.
原文链接:https://yq.aliyun.com/articles/607019
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章