在大规模分布式系统中有一个分布式配置管理中心的组件,将分布式应用所需的配置信息与程序进行分离,极大简化配置管理的工作。如360的QConf,百度的Disconf。 在大规模容器集群环境中,容器内应用程序的配置管理十分重要。Kubernetes提供了一种统一的管理方案,即ConfigMap。 Kubernetes使用ConfigMap实现容器应用的配置管理。

创建ConfigMap

下面介绍如何创建ConfigMap:

使用yaml创建ConfigMap

ConfigMap yaml的详细定义参考这里resources-reference ConfigMap v1

下面是一个简单的例子:captcha-cfg.yaml,其中配置信息即可以是captchaRunMode: dev这种键值对,又可以保存一个完成的配置文件内容如app.properites

 1apiVersion: v1
 2kind: ConfigMap
 3metadata:
 4  name: captcha-cfg
 5data:
 6  captchaRunMode: dev
 7  imageDir: /home/captcha
 8  app.properties: |
 9    property.1=value-1
10    property.2=value-2
11    property.3=value-3

使用kubectl create创建此ConfigMap:

1kubectl create -f captcha-cfg.yaml

查看创建的ConfigMap

 1kubectl get configmap
 2NAME               DATA      AGE
 3captcha-cfg        2         42s
 4
 5
 6kubectl describe configmap captcha-cfg
 7Name:           captcha-cfg
 8Namespace:      default
 9Labels:         <none>
10Annotations:    <none>
11
12Data
13====
14app.properties: 57 bytes
15captchaRunMode: 3 bytes
16imageDir:       13 bytes
17
18
19kubectl get configmap captcha-cfg -o yaml
20apiVersion: v1
21data:
22  app.properties: |
23    property.1=value-1
24    property.2=value-2
25    property.3=value-3
26  captchaRunMode: dev
27  imageDir: /home/captcha
28kind: ConfigMap
29metadata:
30  creationTimestamp: 2017-02-17T11:53:28Z
31  name: captcha-cfg
32  namespace: default
33  resourceVersion: "71238"
34  selfLink: /api/v1/namespaces/default/configmaps/captcha-cfg
35  uid: b1eda86a-f507-11e6-98ca-0800279704c8

###直接使用kubectl命令创建

  • 从目录创建

    1kubectl create configmap game-config --from-file=docs/user-guide/configmap/kubectl
    
  • 从文件创建

    1kubectl create configmap game-config-2 --from-file=docs/user-guide/configmap/kubectl/game.properties --from-file=docs/user-guide/configmap/kubectl/ui.properties
    
  • 从字面值创建

    1kubectl create configmap special-config --from-literal=special.how=very --from-literal=special.type=charm
    

使用ConfigMap

环境变量方式

这种方式将作为容器内的环境变量。

 1apiVersion: v1
 2kind: ConfigMap
 3metadata:
 4  name: special-config
 5  namespace: default
 6data:
 7  special.how: very
 8  special.type: charm
 9  
10
11apiVersion: v1
12kind: Pod
13metadata:
14  name: dapi-test-pod
15spec:
16  containers:
17    - name: test-container
18      image: gcr.io/google_containers/busybox
19      command: [ "/bin/sh", "-c", "env" ]
20      env:
21        - name: SPECIAL_LEVEL_KEY
22          valueFrom:
23            configMapKeyRef:
24              name: special-config
25              key: special.how
26        - name: SPECIAL_TYPE_KEY
27          valueFrom:
28            configMapKeyRef:
29              name: special-config
30              key: special.type
31  restartPolicy: Never
32  
33
34apiVersion: v1
35kind: Pod
36metadata:
37  name: dapi-test-pod
38spec:
39  containers:
40    - name: test-container
41      image: gcr.io/google_containers/busybox
42      command: [ "/bin/sh", "-c", "env" ]
43      env:
44        - name: SPECIAL_LEVEL_KEY
45          valueFrom:
46            configMapKeyRef:
47              name: a-config
48              key: akey
49              optional: true
50  restartPolicy: Never

命令行参数方式

这种方式需要先设置为环境变量,之后可以通过$(VAR_NAME)设置容器启动命令的启动参数。

 1apiVersion: v1
 2kind: ConfigMap
 3metadata:
 4  name: special-config
 5  namespace: default
 6data:
 7  special.how: very
 8  special.type: charm
 9  
10
11apiVersion: v1
12kind: Pod
13metadata:
14  name: dapi-test-pod
15spec:
16  containers:
17    - name: test-container
18      image: gcr.io/google_containers/busybox
19      command: [ "/bin/sh", "-c", "echo $(SPECIAL_LEVEL_KEY) $(SPECIAL_TYPE_KEY)" ]
20      env:
21        - name: SPECIAL_LEVEL_KEY
22          valueFrom:
23            configMapKeyRef:
24              name: special-config
25              key: special.how
26        - name: SPECIAL_TYPE_KEY
27          valueFrom:
28            configMapKeyRef:
29              name: special-config
30              key: special.type
31  restartPolicy: Never

volume plugin方式

这种方式将以volume的形式挂载为容器内部的文件或目录。

 1apiVersion: v1
 2kind: ConfigMap
 3metadata:
 4  name: special-config
 5  namespace: default
 6data:
 7  special.how: very
 8  special.type: charm
 9
10 
11apiVersion: v1
12kind: Pod
13metadata:
14  name: dapi-test-pod
15spec:
16  containers:
17    - name: test-container
18      image: gcr.io/google_containers/busybox
19      command: [ "/bin/sh", "-c", "cat /etc/config/special.how" ]
20      volumeMounts:
21      - name: config-volume
22        mountPath: /etc/config
23  volumes:
24    - name: config-volume
25      configMap:
26        name: special-config
27  restartPolicy: Never
28
29
30apiVersion: v1
31kind: Pod
32metadata:
33  name: dapi-test-pod
34spec:
35  containers:
36    - name: test-container
37      image: gcr.io/google_containers/busybox
38      command: [ "/bin/sh","-c","cat /etc/config/path/to/special-key" ]
39      volumeMounts:
40      - name: config-volume
41        mountPath: /etc/config
42  volumes:
43    - name: config-volume
44      configMap:
45        name: special-config
46        items:
47        - key: special.how
48          path: path/to/special-key
49  restartPolicy: Never
50  
51apiVersion: v1
52kind: Pod
53metadata:
54  name: dapi-test-pod
55spec:
56  containers:
57    - name: test-container
58      image: gcr.io/google_containers/busybox
59      command: [ "/bin/sh", "-c", "ls /etc/config" ]
60      volumeMounts:
61      - name: config-volume
62        mountPath: /etc/config
63  volumes:
64    - name: config-volume
65      configMap:
66        name: no-config
67        optional: true
68  restartPolicy: Never

使用ConfigMap的一些限制

  • ConfigMap必须在Pod之前被创建
  • 如果ConfigMap定义了Namespace, 则只有相同的Namespace的Pod才可以使用。
  • 目前ConfigMap的限额管理还未实现。

参考