在执行应用程式或是生产环境等等, 会有许多的情况需要做变更, 而我们不希望因应每一种需求就要准备一个镜像档, 这时就可以透过 ConfigMap 来帮我们做一个配置档或是命令参数的映射, 更加弹性化使用我们的服务或是应用程式。
ConfigMap 用于保存配置数据的键值对,可以用来保存单个属性,也可以用来保存配置文件。ConfigMap 跟 secret 很类似,但它可以更方便地处理不包含敏感信息的字符串。
一.API 版本对照表
二.ConfigMap 创建
ConfigMap是用来存储配置文件的kubernetes资源对象,所有的配置内容都存储在etcd中。
创建ConfigMap的方式有4种:
通过直接在命令行中指定configmap参数创建,即--from-literal
;
通过指定文件创建,即将一个配置文件创建为一个ConfigMap,--from-file=<文件>
;
通过一个文件内多个键值对,--from-env-file=<文件>
;
事先写好标准的configmap的yaml文件,然后kubectl create -f
创建
可以使用 kubectl create configmap
从文件、目录或者 key-value 字符串创建等创建 ConfigMap。也可以通过 kubectl create -f file
创建。
1.通过 --from-literal 从 key-value 字符串创建
$ kubectl create configmap special-config --from-literal=special.how=very
configmap "special-config" created
$ kubectl get configmap special-config -o go-template='{{.data}}'
map[special.how:very]
或者
kubectl create configmap test-config1 --from-literal=db.host=172.18.8.200 --from-literal=db.port='3306'
2.通过--from-env-file 从 env 文件创建
$ echo -e "a=b\nc=d" | tee config.env
a=b
c=d
$ kubectl create configmap special-config --from-env-file=config.env
configmap "special-config" created
$ kubectl get configmap special-config -o go-template='{{.data}}'
map[a:b c:d]
3.通过 --from-file 从目录创建
$ mkdir config
$ echo a>config/a
$ echo b>config/b
$ kubectl create configmap special-config --from-file=config/
configmap "special-config" created
$ kubectl get configmap special-config -o go-template='{{.data}}'
map[a:a
b:b
]
4.YAML 配置文件 从文件 Yaml/Json 文件创建
apiVersion: v1
kind: ConfigMap
metadata:
name: special-config
namespace: default
data:
special.how: very
special.type: charm
$ kubectl create -f config.yaml
configmap "special-config" created
三,ConfigMap 使用
ConfigMap 可以通过三种方式在 Pod 中使用,三种分别方式为:设置环境变量、设置容器命令行参数以及在 Volume 中直接挂载文件或目录。
注意
ConfigMap 必须在 Pod 引用它之前创建
Pod 只能使用同一个命名空间内的 ConfigMap
首先创建 ConfigMap:
$ kubectl create configmap special-config --from-literal=special.how=very --from-literal=special.type=charm
$ kubectl create configmap env-config --from-literal=log_level=INFO
用作环境变量
apiVersion: v1
kind: Pod
metadata:
name: test-pod
spec:
containers:
- name: test-container
image: gcr.io/google_containers/busybox
command: ["/bin/sh", "-c", "env"]
env:
- name: SPECIAL_LEVEL_KEY
valueFrom:
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
当 Pod 结束后会输出
SPECIAL_LEVEL_KEY=very
SPECIAL_TYPE_KEY=charm
log_level=INFO
用作命令行参数
将 ConfigMap 用作命令行参数时,需要先把 ConfigMap 的数据保存在环境变量中,然后通过 $(VAR_NAME)
的方式引用环境变量.
apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
spec:
containers:
- name: test-container
image: gcr.io/google_containers/busybox
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
valueFrom:
configMapKeyRef:
name: special-config
key: special.type
restartPolicy: Never
当 Pod 结束后会输出
使用 volume 将 ConfigMap 作为文件或目录直接挂载
将创建的 ConfigMap 直接挂载至 Pod 的 / etc/config 目录下,其中每一个 key-value 键值对都会生成一个文件,key 为文件名,value 为内容
apiVersion: v1
kind: Pod
metadata:
name: vol-test-pod
spec:
containers:
- name: test-container
image: gcr.io/google_containers/busybox
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
当 Pod 结束后会输出
将创建的 ConfigMap 中 special.how 这个 key 挂载到 / etc/config 目录下的一个相对路径 / keys/special.level。如果存在同名文件,直接覆盖。其他的 key 不挂载
apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
spec:
containers:
- name: test-container
image: gcr.io/google_containers/busybox
command: ["/bin/sh","-c","cat /etc/config/keys/special.level"]
volumeMounts:
- name: config-volume
mountPath: /etc/config
volumes:
- name: config-volume
configMap:
name: special-config
items:
- key: special.how
path: keys/special.level
restartPolicy: Never
当 Pod 结束后会输出
ConfigMap 支持同一个目录下挂载多个 key 和多个目录。例如下面将 special.how 和 special.type 通过挂载到 / etc/config 下。并且还将 special.how 同时挂载到 / etc/config2 下。
apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
spec:
containers:
- name: test-container
image: gcr.io/google_containers/busybox
command: ["/bin/sh","-c","sleep 36000"]
volumeMounts:
- name: config-volume
mountPath: /etc/config
- name: config-volume2
mountPath: /etc/config2
volumes:
- name: config-volume
configMap:
name: special-config
items:
- key: special.how
path: keys/special.level
- key: special.type
path: keys/special.type
- name: config-volume2
configMap:
name: special-config
items:
- key: special.how
path: keys/special.level
restartPolicy: Never
# ls /etc/config/keys/
special.level special.type
# ls /etc/config2/keys/
special.level
# cat /etc/config/keys/special.level
very
# cat /etc/config/keys/special.type
charm
使用 subpath 将 ConfigMap 作为单独的文件挂载到目录
在一般情况下 configmap 挂载文件时,会先覆盖掉挂载目录,然后再将 congfigmap 中的内容作为文件挂载进行。如果想不对原来的文件夹下的文件造成覆盖,只是将 configmap 中的每个 key,按照文件的方式挂载到目录下,可以使用 subpath 参数。
apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
spec:
containers:
- name: test-container
image: nginx
command: ["/bin/sh","-c","sleep 36000"]
volumeMounts:
- name: config-volume
mountPath: /etc/nginx/special.how
subPath: special.how
volumes:
- name: config-volume
configMap:
name: special-config
items:
- key: special.how
path: special.how
restartPolicy: Never
root@dapi-test-pod:/# ls /etc/nginx/
conf.d fastcgi_params koi-utf koi-win mime.types modules nginx.conf scgi_params special.how uwsgi_params win-utf
root@dapi-test-pod:/# cat /etc/nginx/special.how
very
root@dapi-test-pod:/#
ConfigMap的热更新
使用该 ConfigMap 挂载的 Env 不会同步更新;
使用该 ConfigMap 挂载的 Volume 中的数据需要一段时间(实测大概10秒)才能同步更新
最佳使用方法
大多数情况下,配置信息都以文件形式提供,所以在创建 ConfigMap 时通常采用 --from-file 或 YAML 方式,读取 ConfigMap 时通常采用 Volume 方式。
比如我们的 MySQL 配置文件/etc/my.cnf
。
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
symbolic-links=0
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
!includedir /etc/my.cnf.d
创建CongifMap。
kubectl create cm mysql-cm --from-file=/etc/my.cnf
查看创建好的cm。
[root@master ~]# kubectl get cm mysql-cm -o yaml
apiVersion: v1
data:
my.cnf: |
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
symbolic-links=0
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
!includedir /etc/my.cnf.d
kind: ConfigMap
metadata:
creationTimestamp: "2018-12-16T05:38:29Z"
name: mysql-cm
namespace: default
resourceVersion: "9273"
selfLink: /api/v1/namespaces/default/configmaps/mysql-cm
uid: d1201233-00f4-11e9-9fa7-000c291fb1b3
在 Pod 中使用此 ConfigMap,配置文件为:
apiVersion: v1
kind: Pod
metadata:
name: mypod
spec:
containers:
- name: mypod
image: busybox
args: [ "/bin/sh", "-c", "sleep 3000" ]
volumeMounts:
- name: mysql
mountPath: "/tmp"
volumes:
- name: mysql
configMap:
name: mysql-cm
items:
- key: my.cnf
path: mysql/my.cnf
创建 Pod 并读取配置信息:
[root@master ~]# kubectl exec -it mypod sh
/ # cat /tmp/mysql/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
symbolic-links=0
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
!includedir /etc/my.cnf.d
/ # exit
关于挂在路径大家可以自行进行修改。
参考文档: