🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
ConfigMap 可以通过三种方式在 Pod 中使用,三种方式分别为:设置环境变量、设置容器命令行参数以及在 Volume 中直接挂载文件或目录。 >[info]&nbsp;1. ConfigMap 必须在 Pod 引用它之前创建。<br/>&nbsp;2. 使用属性 `envFrom` 时,将会自动忽略无效的键。<br/>&nbsp;3. Pod 只能使用同一个命名空间内的 ConfigMap。 首先创建 ConfigMap: ```shell $ kubectl create configmap special-config --from-literal=special.how=very --from-literal=special.type=charm $ kubectl create configmap env-config --from-literal=log_level=INFO ``` <br/> **1. 使用 ConfigMap 设置环境变量** ```yaml apiVersion: v1 kind: Pod metadata: name: 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 envFrom: - configMapRef: name: env-config restartPolicy: Never ``` 当 Pod 结束后会输出: ```shell SPECIAL_LEVEL_KEY=very SPECIAL_TYPE_KEY=charm log_level=INFO ``` <br/> **2. ConfigMap 用作命令行参数** 将 ConfigMap 用作命令行参数时,需要先把 ConfigMap 的数据保存在环境变量中,然后通过 `$(VAR_NAME)` 的方式引用环境变量. ```shell 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: Never ``` 当 Pod 结束后会输出: ```shell very charm ``` <br/> **3. 使用 volume 将 ConfigMap 作为文件或目录直接挂载** (1)将创建的 ConfigMap 直接挂载至 Pod 的 /etc/config 目录下,其中每一个 key-value 键值对都会生成一个文件,key 为文件名,value 为内容。 ```yaml apiVersion: v1 kind: Pod metadata: name: vol-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 ``` 当 Pod 结束后会输出: ```shell very ``` (2)将创建的 ConfigMap 中 special.how 这个 key 挂载到 /etc/config 目录下的一个相对路径 keys/special.level 。如果存在同名文件,直接覆盖。其他的 key 不挂载。 ```shell 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/keys/special.level"] volumeMounts: - name: config-volume mountPath: /etc/config volumes: - name: config-volume configMap: name: special-config items: - key: special.how path: keys/special.level restartPolicy: Never ``` 当 Pod 结束后会输出: ```shell very ``` (3)ConfigMap 支持同一个目录下挂载多个 key 和多个目录。例如下面将 special.how 和 special.type 挂载到 /etc/config 下。并且还将 special.how 同时挂载到 /etc/config2 下。 ```shell apiVersion: v1 kind: Pod metadata: name: dapi-test-pod spec: containers: - name: test-container image: gcr.io/google_containers/busybox command: ["/bin/sh","-c","sleep 36000"] volumeMounts: - name: config-volume mountPath: /etc/config - name: config-volume2 mountPath: /etc/config2 volumes: - name: config-volume configMap: name: special-config items: - key: special.how path: keys/special.level - key: special.type path: keys/special.type - name: config-volume2 configMap: name: special-config items: - key: special.how path: keys/special.level restartPolicy: Never ``` 进入相关容器中,可以看到设置的ConfigMap信息。 ```shell # ls /etc/config/keys/ special.level special.type # ls /etc/config2/keys/ special.level # cat /etc/config/keys/special.level very # cat /etc/config/keys/special.type charm ``` <br/> **4. 使用 `subpath`属性将 ConfigMap 作为单独的文件挂载到目录** 在一般情况下 configmap 挂载文件时,会先覆盖掉挂载目录,然后再将 congfigmap 中的内容作为文件挂载进行。如果想不对原来的文件夹下的文件造成覆盖,只是将 configmap 中的每个 key,按照文件的方式挂载到目录下,可以使用 subpath 参数。 ```shell apiVersion: v1 kind: Pod metadata: name: dapi-test-pod spec: containers: - name: test-container image: nginx command: ["/bin/sh","-c","sleep 36000"] volumeMounts: - name: config-volume mountPath: /etc/nginx/special.how subPath: special.how volumes: - name: config-volume configMap: name: special-config items: - key: special.how path: special.how restartPolicy: Never ``` ```shell root@dapi-test-pod:/# ls /etc/nginx/ conf.d fastcgi_params koi-utf koi-win mime.types modules nginx.conf scgi_params special.how uwsgi_params win-utf root@dapi-test-pod:/# cat /etc/nginx/special.how very root@dapi-test-pod:/# ``` **** 参考文档:https://feisky.xyz/kubernetes-handbook/concepts/configmap.html