ConfigMap(用户端)

    示例

    高级模式详情如下:

    1. 填充环境变量

    Configmap(配置集) - 图2

    高级模式使用示例:

    1. apiVersion: v1
    2. kind: Pod
    3. metadata:
    4. name: dapi-test-pod
    5. spec:
    6. containers:
    7. - name: test-container
    8. image: gcr.io/google_containers/busybox
    9. command: [ "/bin/sh", "-c", "env" ]
    10. env:
    11. - name: SPECIAL_LEVEL_KEY
    12. valueFrom:
    13. configMapKeyRef:
    14. name: example-config #需要使用的 ConfigMap 名称,必须已经存在
    15. key: example.property.1 #对应 ConfigMap data 的 key
    16. restartPolicy: Never

    设置结果

    2. 设置容器启动命令行参数

    仅支持高级模式

    1. apiVersion: v1
    2. kind: Pod
    3. metadata:
    4. name: dapi-test-pod
    5. spec:
    6. containers:
    7. - name: test-container
    8. image: gcr.io/google_containers/busybox
    9. command: [ "/bin/sh", "-c", "echo $(SPECIAL_LEVEL_KEY) $(SPECIAL_TYPE_KEY)" ]
    10. env:
    11. - name: SPECIAL_LEVEL_KEY
    12. valueFrom:
    13. configMapKeyRef:
    14. name: example-config
    15. key: example.property.1
    16. - name: SPECIAL_TYPE_KEY
    17. valueFrom:
    18. configMapKeyRef:
    19. name: example-config
    20. key: example.property.2
    1. hello world

    3. 挂载 ConfigMap 到文件

    仅支持高级模式

    创建的文件如下:

    1. etc
    2. └── config
    3. ├── example.property.1
    4. ├── example.property.2
    5. └── example.property.file
    6. #cat example.property.2 world
    7. #cat example.property.file
    8. property.1=value-1
    9. property.2=value-2
    10. property.3=value-3
    • ConfigMap 必须在使用之前被创建,如果引用了一个不存在的 configMap,将会导致 Pod 无法启动
    • ConfigMap 只能被相同 namespace 内的应用使用。
    3.1 挂载 ConfigMap 到单个文件:

    仅支持高级模式

    假设我的项目结构如下 (项目路径为 /usr/local):

    1. .
    2. ├── Dockerfile
    3. ├── Makefile
    4. ├── app.conf
    5. ├── controllers
    6. └── main.go

    我只想把 app.conf 文件从 ConfigMap 挂载进来,其他文件保持不变,现在应该怎么做呢?好,我们开始。

    假设我的 ConfigMap 如下:

    1. apiVersion: v1
    2. kind: ConfigMap
    3. metadata:
    4. name: test-cfgmap
    5. data:
    6. app.conf: file data
    • 第一种方式:
      可以使用下面的定义文件使用 ConfigMap:
    1. apiVersion: v1
    2. kind: Pod
    3. metadata:
    4. name: test-pd-plus-cfgmap
    5. spec:
    6. containers:
    7. - image: ubuntu
    8. name: bash
    9. volumeMounts:
    10. - mountPath: /usr/local/app.conf
    11. name: cfgmap
    12. subPath: app.conf
    13. volumes:
    14. - name: cfgmap
    15. name: test-cfgmap
    • 第二种方式:
    • 其它方式:
      当然,如果你愿意,你也可以挂载 ConfigMap 到一个其它 路径 ,然后通过软连接的方式链接到你需要的文件。
    1. maxmemory 2mb
    2. maxmemory-policy allkeys-lru

    首先,让我们来创建一个 ConfigMap:

    1. apiVersion: v1
    2. data:
    3. redis-config: |
    4. maxmemory 2mb
    5. maxmemory-policy allkeys-lru
    6. kind: ConfigMap
    7. metadata:
    8. name: example-redis-config
    9. namespace: default

    下面我们来创建一个 Pod 来使用它:

    1. apiVersion: v1
    2. kind: Pod
    3. metadata:
    4. name: redis
    5. spec:
    6. containers:
    7. - name: redis
    8. image: kubernetes/redis:v1
    9. env:
    10. - name: MASTER
    11. value: "true"
    12. ports:
    13. - containerPort: 6379
    14. volumeMounts:
    15. - mountPath: /redis-master
    16. name: config
    17. volumes:
    18. - name: config
    19. configMap:
    20. name: example-redis-config
    21. items:
    22. - key: redis-config
    23. path: redis.conf #指定生成的配置文件名称

    当我们创建完 Pod 后,进入它:生成的配置文件如下:

    1. redis-master
    2. `-- redis.conf -> ..data/redis.conf

    我们发现在 redis-master 目录下生成了一个文件 redis.conf ,对应我们上面 path 定义的文件名。输出一下 redis.conf 内容:

    下面我们看一下 redis 的配置:

    1. $ kubectl exec -it redis redis-cli
    2. 127.0.0.1:6379> CONFIG GET maxmemory
    3. 1) "maxmemory"
    4. 2) "2097152"
    5. 127.0.0.1:6379> CONFIG GET maxmemory-policy
    6. 2) "allkeys-lru"

    符合我们的预期。