很多应用程序需要一些配置通过组合的配置文件,命令行参数和环境变量。这些配置应该与镜像内容分离以保持容器化应用程序的可移植性。ConfigMap API资源提供了将配置数据注入容器的机制,同时保持容器不受kubernetes的影响。ConfigMap可用于存储细粒度信息,如单个属性,或粗粒度信息如整个配置文件或JSON对象

ConfigMap概述

ConfigMap 资源对象使用key-value形式的键值对来配置数据,这些数据可以在pod里面使用,configmap和secrets比较类似,一个比较大的区别是configmap可以比较方便的处理一些非敏感的数据,比如密码之类的还是需要使用secrets来进行管理。

示例:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
kind: ConfigMap
apiVersion: v1
metadata:
  name: example-config
  namespace: default
data:
  example.property.1: hello
  example.property.2: world
  example.property.file: |-
    property.1=value-1
    property.2=value-2
    property.3=value-3

data一栏包括了配置数据,ConfigMap可以被用来保存单个属性,也可以用来保存一个配置文件。配置数据可以通过很多方式在pods里被使用。ConfigMap可以被用来:

1、设置环境变量的值

2、在容器里设置命令行参数

3、在数据卷里面创建config文件

用户和系统组件两者都可以在ConfigMap里面存储配置数据

COnfigMap创建

可以使用 kubectl create configmap 从文件、目录或者key-value字符串创建等创建ConfigMap。也可以通过 kubectl create -f file创建

使用key-value字符串创建

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
$ kubectl create configmap special-config --from-literal=special.how=very
$ kubectl get configmaps special-config -o yaml
apiVersion: v1
data:
  special.how: very
kind: ConfigMap
metadata:
  creationTimestamp: "2020-03-30T01:53:56Z"
  name: special-config
  namespace: default
  resourceVersion: "1557984"
  selfLink: /api/v1/namespaces/default/configmaps/special-config
  uid: 50aea7e1-7229-11ea-9fde-005056b24815

使用conf文件创建

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
$ cat test.conf
a=1
b=2
$ kubectl create configmap special-config --from-env-file=test.conf
$ kubectl get configmaps special-config -o yaml
apiVersion: v1
data:
  a: "1"
  b: "2"
kind: ConfigMap
metadata:
  creationTimestamp: "2020-03-30T01:59:12Z"
  name: special-config
  namespace: default
  resourceVersion: "1558796"
  selfLink: /api/v1/namespaces/default/configmaps/special-config
  uid: 0d404dae-722a-11ea-9fde-005056b24815

使用目录创建

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
$ ls dosc/
game.properties  ui.properties
$ cat dosc/game.properties
c=3
b=4
$ cat dosc/ui.properties
e=5
g=6
$ kubectl create configmap special-config --from-file=dosc/
$ kubectl get configmaps special-config -o yaml
apiVersion: v1
data:
  game.properties: |
    c=3
    b=4
  ui.properties: |
    e=5
    g=6
kind: ConfigMap
metadata:
  creationTimestamp: "2020-03-30T02:06:58Z"
  name: special-config
  namespace: default
  resourceVersion: "1559993"
  selfLink: /api/v1/namespaces/default/configmaps/special-config
  uid: 22c3dd6a-722b-11ea-9fde-005056b24815

使用yaml文件创建

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
$ cat conf.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: special-config
  namespace: default
data:
  special.how: very
  special.type: charm
$ kubectl apply -f conf.yaml
$ kubectl get configmaps special-config -o yaml
apiVersion: v1
data:
  special.how: very
  special.type: charm
kind: ConfigMap
metadata:
  annotations:
    kubectl.kubernetes.io/last-applied-configuration: |
      {"apiVersion":"v1","data":{"special.how":"very","special.type":"charm"},"kind":"ConfigMap","metadata":{"annotations":{},"name":"special-config","namespace":"default"}}
  creationTimestamp: "2020-03-30T02:12:22Z"
  name: special-config
  namespace: default
  resourceVersion: "1560828"
  selfLink: /api/v1/namespaces/default/configmaps/special-config
  uid: e429af2e-722b-11ea-9fde-005056b24815

使用ConfigMap

使用ConfigMap来替代环境变量

ConfigMap:

1
2
3
4
5
6
7
8
apiVersion: v1
kind: ConfigMap
metadata:
  name: special-config
  namespace: default
data:
  special.how: very
  special.type: charm

Pod:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  containers:
    - name: test-container
      image: 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

pod运行后会输出这几行:

1
2
SPECIAL_TYPE_KEY=charm
SPECIAL_LEVEL_KEY=very

使用ConfigMap设置命令行参数

ConfigMap:

1
2
3
4
5
6
7
8
apiVersion: v1
kind: ConfigMap
metadata:
  name: special-config
  namespace: default
data:
  special.how: very
  special.type: charm

pod:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  containers:
    - name: test-container
      image: 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运行后会输出以下内容:

1
very charm

通过数据卷插件使用ConfMap

ConfigMap:

1
2
3
4
5
6
7
8
apiVersion: v1
kind: ConfigMap
metadata:
  name: special-config
  namespace: default
data:
  special.how: very
  special.type: charm

Pod-1:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  containers:
    - name: test-container
      image: 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运行后会输出以下内容:

1
very

警告:如果/etc/config/目录中有一些文件,它们将被删除

将ConfigMap数据添加到卷中的特定路径

Pod:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  containers:
    - name: test-container
      image: busybox
      command: [ "/bin/sh","-c","cat /etc/config/keys/test" ]
      volumeMounts:
      - name: config-volume
        mountPath: /etc/config
  volumes:
    - name: config-volume
      configMap:
        name: special-config
        items:
        - key: special.how
          path: keys/test
  restartPolicy: Never

pod运行后会输出以下内容:

1
very

警告:如果/etc/config/目录中有一些文件,它们将被删除

参考链接

官方文档