---
title: "ConfigMap 笔记"
date: 2019-12-22T22:04:37+08:00
lastmod: 2019-12-22T22:04:37+08:00
tags: ["kubernetes", "configmap"]
categories: ["container"]
---

## 概述
- ConfigMap 通常用于设置环境变量、设置命令行参数、创建配置文件
- Pod 使用 ConfigMap 前，ConfigMap 必须存在，否则 pod 不能启动
- ConfigMap 只能被在同一一个命名空间中的Pod所引用

## 创建 ConfigMap
- 命令如下
    ```bash
    kubectl create configmap <map-name> <data-source>
    # 或者
    kubectl apply -f <configmap-file.yml>
    ```
- map-name: ConfigMap 名称
- data-source: 目录、文件或具体值

### 通过目录创建 ConfigMaps
- 命令如下
    ```bash
    kubectl create configmap game-config \
        --from-file=https://k8s.io/docs/tasks/configure-pod-container/configmap/kubectl
    ```
- docs/tasks/configure-pod-container/configmap/kubectl/目录下的文件包括
    ```bash
    ls docs/tasks/configure-pod-container/configmap/kubectl/
    ```
    - 输出如下
        ```
        game.properties
        ui.properties
        ```
- 查看 game-config 信息
    ```bash
    kubectl describe configmaps game-config
    # 或者
    kubectl get configmaps game-config -o yaml
    ```

### 通过文件创建 ConfigMaps
- 通过单个文件创建
    ```bash
    kubectl create configmap game-config-2 \
        --from-file=https://k8s.io/docs/tasks/configure-pod-container/configmap/kubectl/game.properties
    ```
- 通过多个文件创建
    ```bash
    kubectl create configmap game-config-3 \
        --from-file=https://k8s.io/docs/tasks/configure-pod-container/configmap/kubectl/game.properties \
        --from-file=https://k8s.io/docs/tasks/configure-pod-container/configmap/kubectl/ui.properties
    ```
- 通过文件创建ConfigMap时可以定义文件的键
    ```bash
    kubectl create configmap game-config-4 \
        --from-file=game-special-key=https://k8s.io/docs/tasks/configure-pod-container/configmap/kubectl/game.properties
    # key 是 "game-special-key"
    # value 是 game.properties 文件的内容
    ```

### 通过具体值创建 ConfigMaps
- 使用 --from-literal 参数定义具体值
    ```bash
    kubectl create configmap special-config \
        --from-literal=special.how=very \
        --from-literal=special.type=charm
    ```

## 使用 ConfigMap
### 定义 pod 环境变量
#### Pod 环境变量的值来自于单一 ConfigMap
- 在ConfigMap中定义一个环境变量作为键值对
    ```bash
    kubectl create configmap special-config --from-literal=special.how=very
    ```
- 指派ConfigMap中定义的special.how的值给Pod中SPECIAL_LEVEL_KEY环境变量
    ```yaml
    apiVersion:v1
    kind:Pod
    metadata:
      name:dapi-test-pod
    spec:
      containers:
      - name:test-container
        image:k8s.gcr.io/busybox
        command:["/bin/sh","-c","env"]
        env:
        # Define the environment variable
        - name:SPECIAL_LEVEL_KEY
          valueFrom:
            configMapKeyRef:
            # The ConfigMap containing the value you want to assign to SPECIAL_LEVEL_KEY
              name:special-config
              # Specify the key associated with the value
              key:special.how
       restartPolicy:Never
    ```
- 保存Pod规格的变化，Pod将输出SPECIAL_LEVEL_KEY=very

#### Pod 环境变量的值来自于多个 ConfigMap
- 创建两个 ConfigMap
    ```yaml
    ---
    apiVersion:v1
    kind:ConfigMap
    metadata:
       name:special-config
       namespace:default
    data:
       special.how:very
    
    ---
    apiVersion:v1
    kind:ConfigMap
    metadata:
       name:env-config
       namespace:default
    data:
       log_level:INFO
   ```
- 在Pod规格中定义环境变量
    ```yaml
    apiVersion:v1
    kind:Pod
    metadata:
      name:dapi-test-pod
    spec:
      containers:
      - name:test-container
        image:k8s.gcr.io/busybox
        command:["/bin/sh","-c","env"]
        env:
        - name:SPECIAL_LEVEL_KEY
          valueFrom:
            configMapKeyRef:
               name:special-config
               key:special.how
         - name:LOG_LEVEL
           valueFrom:
              configMapKeyRef:
                  name:env-config
                  key:log_level
      restartPolicy:Neverv
    ```
- 保存变更后的Pod，Pod将会输出SPECIAL_LEVEL_KEY=very和LOG_LEVEL=info

### 在一个ConfigMap中配置的键值对都作为一个Pod的环境变量
- **Kubernetes v1.6+可用**
- 创建包含多个键-值对的ConfigMap
    ```yaml
    apiVersion:v1
    kind:ConfigMap
    metadata:
      name:special-config
      namespace:default
    data:
      SPECIAL_LEVEL:very
      SPECIAL_TYPE:charm
    ```
- 使用envFrom定义所有的ConfigMap数据作为Pod的环境变量。来自于Config的键成为Pod中环境变量的名
    ```yaml
    apiVersion:v1
    kind:Pod
    metadata:
      name:dapi-test-pod
    spec:
      containers:
      - name:test-container
        image:k8s.gcr.io/busybox
        command:["/bin/sh","-c","env"]
        envFrom:
        - configMapRef:
            name:special-config
       restartPolicy:Never
    ```
- Pod的输出包括: SPECIAL_LEVEL=very 和 SPECIAL_TYPE=charm

### 在Pod命令行中使用ConfigMap定义的环境变量
- 在Pod规范的command 中使用$(VAR_NAME) ，获取ConfigMap定义的环境变量
    ```yaml
    apiVersion:v1
    kind:Pod
    metadata:
      name:dapi-test-pod
    spec:
      containers:
      - name:test-container
        image:k8s.gcr.io/busybox
        command:["/bin/sh","-c","echo $(SPECIAL_LEVEL_KEY) $(SPECIAL_TYPE_KEY)"]
        env:
        - name:SPECIAL_LEVEL_KEY
          valueFrom:
             configMapKeyRef:
                name:special-config
                key:SPECIAL_LEVEL
        - name:SPECIAL_TYPE_KEY
          valueFrom:
              configMapKeyRef:
                name:special-config
                key:SPECIAL_TYPE
      restartPolicy:Never
    ```
- test-container容器的输出: very charm

## 添加ConfigMap数据至存储卷
- 当通过–from-file创建的ConfigMap时，文件将作为一个键保存在ConfigMap中，而此文件的内容将作为值
    ```bash
    apiVersion:v1
    kind:ConfigMap
    metadata:
      name:special-config
      namespace:default
    data:
      special.level:very
      special.type:charm
    ```

### 将ConfigMap中的数据传播到指定目录
- 在Pod的存储卷区域添加ConfigMap的名称
- 这将添加ConfigMap数据到volumeMounts.mountPath指定的目录下(此例为/etc/config)
- command区域将引用保存在ConfigMap中的special.level条目
    ```bash
    apiVersion:v1
    kind:Pod
    metadata:
      name:dapi-test-pod
    spec:
      containers:
      - name:test-container
        image:k8s.gcr.io/busybox
        command:["/bin/sh","-c","ls /etc/config/"]
        volumeMounts:
        - name:config-volume
          mountPath:/etc/config
      volumes:
      - name:config-volume
        configMap:
            # Provide the name of the ConfigMap containing the files you want
            # to add to the container
          name:special-config
      restartPolicy:Never
    ```
- Pod运行时，command (“ls /etc/config/”)将输出: special.level special.type
- **如果在/etc/config/目录下存在文件，将不会删除**

### 添加ConfigMap数据至存储卷指定的目录
- 为ConfigMap条目，使用path指定文件路径
- 此例中，special.level将在config-volume存储卷中被挂接至/etc/config/keys
    ```yaml
    apiVersion:v1
    kind:Pod
    metadata:
      name:dapi-test-pod
    spec:
      containers:
      - name:test-container
        image:k8s.gcr.io/busybox
        command:["/bin/sh","-c","cat /etc/config/keys"]
        volumeMounts:
         - name:config-volume
           mountPath:/etc/config
      volumes:
      - name:config-volume
        configMap:
           name:special-config
           items:
           - key:special.level
             path:keys
      restartPolicy:Never
    ```
- Pod运行时，(“cat /etc/config/keys”) 将输出: very

## 参考
- [Kubernetes-配置字典ConfigMap](https://blog.csdn.net/bbwangj/article/details/81776648)

