kubernetes ConfigMap
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.

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
使用kubekit搭建k8s集群
概述 其实有时候安装搭建这个东西是费时费力的事情,如果有脚本为什么不能使用脚本呢?花100分钟在搭建上还不如用100分钟去研究怎么使用 什么是kubekit Kubekit是一个部署工具包,它为kubernetes提供离线安装解决方案。您可以使用它将Kubernetes部署到OFFLINE生产环境。 Kubekit将安装 Docker(1.12.6) Kubernetes及其所有组件 Kubernetes仪表板,默认节点端口:31234 kubkit地址 https://github.com/Orientsoft/kubekit 操作系统 首先官方支持下面两个操作系统,而且都要是最小化安装支持的 CentOS release 7.3.1611 CentOS release 7.4.1708 我使用是1708,k8s版本是V1.9.2,具体的可以看github上的readme wget http://111.1.50.85/files/1128000004BF9EC9/linux.cc.lehigh.edu/centos/7/isos/x86_64/CentOS-7-x86_64-Min...
- 下一篇
基于Docker的Redis高可用集群搭建(redis-sentinel)
前言 之前介绍了用docker来搭建redis主从环境,但这只是对数据添加了从库备份(主从复制),当主库down掉的时候,从库是不会自动升级为主库的,也就是说,该redis主从集群并非是高可用的。 目前来说,高可用(主从复制、主从切换)redis集群有两种方案,一种是redis-sentinel,只有一个master,各实例数据保持一致;一种是redis-cluster,也叫分布式redis集群,可以有多个master,数据分片分布在这些master上。 本文介绍基于docker和redis-sentinel的高可用redis集群搭建,大多数情况下,redis-sentinel也需要做高可用,这里先对redis搭建一主二从环境,另外需要3个redis-sentinel监控redis master。 很显然,只使用单个redis-sentinel进程来监控redis集群是不可靠的,由于redis-sentinel本身也有single-point-of-failure-problem(单点问题),当出现问题时整个redis集群系统将无法按照预期的方式切换主从。官方推荐:一个...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- SpringBoot2编写第一个Controller,响应你的http请求并返回结果
- SpringBoot2更换Tomcat为Jetty,小型站点的福音
- CentOS8,CentOS7,CentOS6编译安装Redis5.0.7
- Jdk安装(Linux,MacOS,Windows),包含三大操作系统的最全安装
- CentOS8编译安装MySQL8.0.19
- Springboot2将连接池hikari替换为druid,体验最强大的数据库连接池
- SpringBoot2整合Thymeleaf,官方推荐html解决方案
- SpringBoot2整合Redis,开启缓存,提高访问速度
- CentOS7编译安装Gcc9.2.0,解决mysql等软件编译问题
- Docker使用Oracle官方镜像安装(12C,18C,19C)