在大规模分布式系统中有一个分布式配置管理中心的组件,将分布式应用所需的配置信息与程序进行分离,极大简化配置管理的工作。如360的QConf,百度的Disconf。 在大规模容器集群环境中,容器内应用程序的配置管理十分重要。Kubernetes提供了一种统一的管理方案,即ConfigMap。 Kubernetes使用ConfigMap实现容器应用的配置管理。
创建ConfigMap
下面介绍如何创建ConfigMap:
使用yaml创建ConfigMap
ConfigMap yaml的详细定义参考这里resources-reference ConfigMap v1。
下面是一个简单的例子:captcha-cfg.yaml,其中配置信息即可以是captchaRunMode: dev这种键值对,又可以保存一个完成的配置文件内容如app.properites
apiVersion: v1
kind: ConfigMap
metadata:
name: captcha-cfg
data:
captchaRunMode: dev
imageDir: /home/captcha
app.properties: |
property.1=value-1
property.2=value-2
property.3=value-3使用kubectl create创建此ConfigMap:
kubectl create -f captcha-cfg.yaml查看创建的ConfigMap
kubectl get configmap
NAME DATA AGE
captcha-cfg 2 42s
kubectl describe configmap captcha-cfg
Name: captcha-cfg
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
app.properties: 57 bytes
captchaRunMode: 3 bytes
imageDir: 13 bytes
kubectl get configmap captcha-cfg -o yaml
apiVersion: v1
data:
app.properties: |
property.1=value-1
property.2=value-2
property.3=value-3
captchaRunMode: dev
imageDir: /home/captcha
kind: ConfigMap
metadata:
creationTimestamp: 2017-02-17T11:53:28Z
name: captcha-cfg
namespace: default
resourceVersion: "71238"
selfLink: /api/v1/namespaces/default/configmaps/captcha-cfg
uid: b1eda86a-f507-11e6-98ca-0800279704c8###直接使用kubectl命令创建
从目录创建
kubectl create configmap game-config --from-file=docs/user-guide/configmap/kubectl从文件创建
kubectl create configmap game-config-2 --from-file=docs/user-guide/configmap/kubectl/game.properties --from-file=docs/user-guide/configmap/kubectl/ui.properties从字面值创建
kubectl create configmap special-config --from-literal=special.how=very --from-literal=special.type=charm
使用ConfigMap
环境变量方式
这种方式将作为容器内的环境变量。
apiVersion: v1
kind: ConfigMap
metadata:
name: special-config
namespace: default
data:
special.how: very
special.type: charm
apiVersion: v1
kind: Pod
metadata:
name: dapi-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
restartPolicy: Never
apiVersion: v1
kind: Pod
metadata:
name: dapi-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: a-config
key: akey
optional: true
restartPolicy: Never命令行参数方式
这种方式需要先设置为环境变量,之后可以通过$(VAR_NAME)设置容器启动命令的启动参数。
apiVersion: v1
kind: ConfigMap
metadata:
name: special-config
namespace: default
data:
special.how: very
special.type: charm
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: Nevervolume plugin方式
这种方式将以volume的形式挂载为容器内部的文件或目录。
apiVersion: v1
kind: ConfigMap
metadata:
name: special-config
namespace: default
data:
special.how: very
special.type: charm
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/special.how" ]
volumeMounts:
- name: config-volume
mountPath: /etc/config
volumes:
- name: config-volume
configMap:
name: special-config
restartPolicy: Never
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/path/to/special-key" ]
volumeMounts:
- name: config-volume
mountPath: /etc/config
volumes:
- name: config-volume
configMap:
name: special-config
items:
- key: special.how
path: path/to/special-key
restartPolicy: Never
apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
spec:
containers:
- name: test-container
image: gcr.io/google_containers/busybox
command: [ "/bin/sh", "-c", "ls /etc/config" ]
volumeMounts:
- name: config-volume
mountPath: /etc/config
volumes:
- name: config-volume
configMap:
name: no-config
optional: true
restartPolicy: Never使用ConfigMap的一些限制
- ConfigMap必须在Pod之前被创建
- 如果ConfigMap定义了Namespace, 则只有相同的Namespace的Pod才可以使用。
- 目前ConfigMap的限额管理还未实现。