配置 Pod 以使用 ConfigMap

许多应用依赖于在应用初始化或运行时使用的配置。大多数时候,需要调整分配给配置参数的值。ConfigMaps 是 Kubernetes 中的一种机制,允许你将配置数据注入到应用的 Pod 中。

ConfigMap 概念允许你将配置制品与镜像内容解耦,以保持容器化应用的可移植性。例如,你可以下载并运行相同的 容器镜像,从而启动容器用于本地开发、系统测试或运行实际终端用户工作负载。

本页面提供了一系列使用示例,演示如何创建 ConfigMaps 并使用存储在 ConfigMaps 中的数据来配置 Pod。

开始之前

你需要拥有一个 Kubernetes 集群,并且 kubectl 命令行工具已配置为与你的集群通信。建议在至少有两个非控制平面主机节点的集群上运行本教程。如果你还没有集群,可以使用 minikube 创建一个,或者使用以下 Kubernetes 演练场之一

你需要安装 wget 工具。如果你有其他工具(例如 curl)但没有 wget,则需要调整下载示例数据的步骤。

创建 ConfigMap

你可以使用 kubectl create configmap 或在 kustomization.yaml 中使用 ConfigMap 生成器来创建 ConfigMap。

使用 kubectl create configmap 创建 ConfigMap

使用 kubectl create configmap 命令从目录文件字面值创建 ConfigMaps

kubectl create configmap <map-name> <data-source>

其中 <map-name> 是你想要赋予 ConfigMap 的名称,<data-source> 是从中获取数据的目录、文件或字面值。ConfigMap 对象的名称必须是有效的DNS 子域名

当你基于文件创建 ConfigMap 时,<data-source> 中的键默认为文件的基本名称,值默认为文件内容。

你可以使用kubectl describekubectl get 获取 ConfigMap 的信息。

从目录创建 ConfigMap

你可以使用 kubectl create configmap 从同一目录中的多个文件创建 ConfigMap。当你基于目录创建 ConfigMap 时,kubectl 会识别目录中文件名为有效键的文件,并将每个文件打包到新的 ConfigMap 中。除了常规文件之外的任何目录条目都将被忽略(例如:子目录、符号链接、设备、管道等)。

创建本地目录

mkdir -p configure-pod-container/configmap/

现在,下载示例配置并创建 ConfigMap

# Download the sample files into `configure-pod-container/configmap/` directory
wget https://kubernetes.ac.cn/examples/configmap/game.properties -O configure-pod-container/configmap/game.properties
wget https://kubernetes.ac.cn/examples/configmap/ui.properties -O configure-pod-container/configmap/ui.properties

# Create the ConfigMap
kubectl create configmap game-config --from-file=configure-pod-container/configmap/

上述命令将 configure-pod-container/configmap/ 目录中的每个文件(在此例中为 game.propertiesui.properties)打包到 game-config ConfigMap 中。你可以使用以下命令显示 ConfigMap 的详细信息

kubectl describe configmaps game-config

输出类似于这样

Name:         game-config
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
game.properties:
----
enemies=aliens
lives=3
enemies.cheat=true
enemies.cheat.level=noGoodRotten
secret.code.passphrase=UUDDLRLRBABAS
secret.code.allowed=true
secret.code.lives=30
ui.properties:
----
color.good=purple
color.bad=yellow
allow.textmode=true
how.nice.to.look=fairlyNice

configure-pod-container/configmap/ 目录中的 game.propertiesui.properties 文件被表示在 ConfigMap 的 data 部分。

kubectl get configmaps game-config -o yaml

输出类似于这样

apiVersion: v1
kind: ConfigMap
metadata:
  creationTimestamp: 2022-02-18T18:52:05Z
  name: game-config
  namespace: default
  resourceVersion: "516"
  uid: b4952dc3-d670-11e5-8cd0-68f728db1985
data:
  game.properties: |
    enemies=aliens
    lives=3
    enemies.cheat=true
    enemies.cheat.level=noGoodRotten
    secret.code.passphrase=UUDDLRLRBABAS
    secret.code.allowed=true
    secret.code.lives=30    
  ui.properties: |
    color.good=purple
    color.bad=yellow
    allow.textmode=true
    how.nice.to.look=fairlyNice    

从文件创建 ConfigMaps

你可以使用 kubectl create configmap 从单个文件或多个文件创建 ConfigMap。

例如,

kubectl create configmap game-config-2 --from-file=configure-pod-container/configmap/game.properties

会生成以下 ConfigMap

kubectl describe configmaps game-config-2

其中输出类似于这样

Name:         game-config-2
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
game.properties:
----
enemies=aliens
lives=3
enemies.cheat=true
enemies.cheat.level=noGoodRotten
secret.code.passphrase=UUDDLRLRBABAS
secret.code.allowed=true
secret.code.lives=30

你可以多次传入 --from-file 参数,以便从多个数据源创建 ConfigMap。

kubectl create configmap game-config-2 --from-file=configure-pod-container/configmap/game.properties --from-file=configure-pod-container/configmap/ui.properties

你可以使用以下命令显示 game-config-2 ConfigMap 的详细信息

kubectl describe configmaps game-config-2

输出类似于这样

Name:         game-config-2
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
game.properties:
----
enemies=aliens
lives=3
enemies.cheat=true
enemies.cheat.level=noGoodRotten
secret.code.passphrase=UUDDLRLRBABAS
secret.code.allowed=true
secret.code.lives=30
ui.properties:
----
color.good=purple
color.bad=yellow
allow.textmode=true
how.nice.to.look=fairlyNice

使用 --from-env-file 选项从 env-file 创建 ConfigMap,例如

# Env-files contain a list of environment variables.
# These syntax rules apply:
#   Each line in an env file has to be in VAR=VAL format.
#   Lines beginning with # (i.e. comments) are ignored.
#   Blank lines are ignored.
#   There is no special handling of quotation marks (i.e. they will be part of the ConfigMap value)).

# Download the sample files into `configure-pod-container/configmap/` directory
wget https://kubernetes.ac.cn/examples/configmap/game-env-file.properties -O configure-pod-container/configmap/game-env-file.properties
wget https://kubernetes.ac.cn/examples/configmap/ui-env-file.properties -O configure-pod-container/configmap/ui-env-file.properties

# The env-file `game-env-file.properties` looks like below
cat configure-pod-container/configmap/game-env-file.properties
enemies=aliens
lives=3
allowed="true"

# This comment and the empty line above it are ignored
kubectl create configmap game-config-env-file \
       --from-env-file=configure-pod-container/configmap/game-env-file.properties

将生成一个 ConfigMap。查看该 ConfigMap

kubectl get configmap game-config-env-file -o yaml

输出类似于

apiVersion: v1
kind: ConfigMap
metadata:
  creationTimestamp: 2019-12-27T18:36:28Z
  name: game-config-env-file
  namespace: default
  resourceVersion: "809965"
  uid: d9d1ca5b-eb34-11e7-887b-42010a8002b8
data:
  allowed: '"true"'
  enemies: aliens
  lives: "3"

从 Kubernetes v1.23 开始,kubectl 支持多次指定 --from-env-file 参数,以便从多个数据源创建 ConfigMap。

kubectl create configmap config-multi-env-files \
        --from-env-file=configure-pod-container/configmap/game-env-file.properties \
        --from-env-file=configure-pod-container/configmap/ui-env-file.properties

会生成以下 ConfigMap

kubectl get configmap config-multi-env-files -o yaml

其中输出类似于这样

apiVersion: v1
kind: ConfigMap
metadata:
  creationTimestamp: 2019-12-27T18:38:34Z
  name: config-multi-env-files
  namespace: default
  resourceVersion: "810136"
  uid: 252c4572-eb35-11e7-887b-42010a8002b8
data:
  allowed: '"true"'
  color: purple
  enemies: aliens
  how: fairlyNice
  lives: "3"
  textmode: "true"

定义从文件创建 ConfigMap 时使用的键

使用 --from-file 参数时,你可以定义一个与文件名不同的键用于 ConfigMap 的 data 部分

kubectl create configmap game-config-3 --from-file=<my-key-name>=<path-to-file>

其中 <my-key-name> 是你想要在 ConfigMap 中使用的键,<path-to-file> 是你想要该键代表的数据源文件的位置。

例如

kubectl create configmap game-config-3 --from-file=game-special-key=configure-pod-container/configmap/game.properties

会生成以下 ConfigMap

kubectl get configmaps game-config-3 -o yaml

其中输出类似于这样

apiVersion: v1
kind: ConfigMap
metadata:
  creationTimestamp: 2022-02-18T18:54:22Z
  name: game-config-3
  namespace: default
  resourceVersion: "530"
  uid: 05f8da22-d671-11e5-8cd0-68f728db1985
data:
  game-special-key: |
    enemies=aliens
    lives=3
    enemies.cheat=true
    enemies.cheat.level=noGoodRotten
    secret.code.passphrase=UUDDLRLRBABAS
    secret.code.allowed=true
    secret.code.lives=30    

从字面值创建 ConfigMaps

你可以使用带 --from-literal 参数的 kubectl create configmap 从命令行定义字面值

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

你可以传入多个键值对。命令行上提供的每一对键值都会作为 ConfigMap 的 data 部分中的一个独立条目。

kubectl get configmaps special-config -o yaml

输出类似于这样

apiVersion: v1
kind: ConfigMap
metadata:
  creationTimestamp: 2022-02-18T19:14:38Z
  name: special-config
  namespace: default
  resourceVersion: "651"
  uid: dadce046-d673-11e5-8cd0-68f728db1985
data:
  special.how: very
  special.type: charm

从生成器创建 ConfigMap

你也可以从生成器创建 ConfigMap,然后应用它在集群的 API 服务器中创建对象。你应该在目录内的 kustomization.yaml 文件中指定生成器。

从文件生成 ConfigMaps

例如,从文件 configure-pod-container/configmap/game.properties 生成 ConfigMap

# Create a kustomization.yaml file with ConfigMapGenerator
cat <<EOF >./kustomization.yaml
configMapGenerator:
- name: game-config-4
  options:
    labels:
      game-config: config-4
  files:
  - configure-pod-container/configmap/game.properties
EOF

应用 kustomization 目录创建 ConfigMap 对象

kubectl apply -k .
configmap/game-config-4-m9dm2f92bt created

你可以像这样检查 ConfigMap 是否已创建

kubectl get configmap
NAME                       DATA   AGE
game-config-4-m9dm2f92bt   1      37s

还可以

kubectl describe configmaps/game-config-4-m9dm2f92bt
Name:         game-config-4-m9dm2f92bt
Namespace:    default
Labels:       game-config=config-4
Annotations:  kubectl.kubernetes.io/last-applied-configuration:
                {"apiVersion":"v1","data":{"game.properties":"enemies=aliens\nlives=3\nenemies.cheat=true\nenemies.cheat.level=noGoodRotten\nsecret.code.p...

Data
====
game.properties:
----
enemies=aliens
lives=3
enemies.cheat=true
enemies.cheat.level=noGoodRotten
secret.code.passphrase=UUDDLRLRBABAS
secret.code.allowed=true
secret.code.lives=30
Events:  <none>

请注意,生成的 ConfigMap 名称附加了一个通过内容哈希计算得出的后缀。这确保了每次修改内容时都会生成一个新的 ConfigMap。

定义从文件生成 ConfigMap 时使用的键

你可以定义一个与文件名不同的键用于 ConfigMap 生成器。例如,从文件 configure-pod-container/configmap/game.properties 生成 ConfigMap,并使用键 game-special-key

# Create a kustomization.yaml file with ConfigMapGenerator
cat <<EOF >./kustomization.yaml
configMapGenerator:
- name: game-config-5
  options:
    labels:
      game-config: config-5
  files:
  - game-special-key=configure-pod-container/configmap/game.properties
EOF

应用 kustomization 目录创建 ConfigMap 对象。

kubectl apply -k .
configmap/game-config-5-m67dt67794 created

从字面值生成 ConfigMaps

本例展示了如何使用 Kustomize 和 kubectl 从两个字面键值对:special.type=charmspecial.how=very 创建一个 ConfigMap。为此,你可以指定 ConfigMap 生成器。创建(或替换)kustomization.yaml 文件,使其包含以下内容

---
# kustomization.yaml contents for creating a ConfigMap from literals
configMapGenerator:
- name: special-config-2
  literals:
  - special.how=very
  - special.type=charm

应用 kustomization 目录创建 ConfigMap 对象

kubectl apply -k .
configmap/special-config-2-c92b5mmcf2 created

中期清理

在继续之前,清理一些你创建的 ConfigMaps

kubectl delete configmap special-config
kubectl delete configmap env-config
kubectl delete configmap -l 'game-config in (config-4,config-5)'

现在你已经学会了如何定义 ConfigMaps,你可以继续下一节,学习如何在 Pod 中使用这些对象。


使用 ConfigMap 数据定义容器环境变量

使用来自单个 ConfigMap 的数据定义容器环境变量

  1. 在 ConfigMap 中将环境变量定义为键值对

    kubectl create configmap special-config --from-literal=special.how=very
    
  2. 将 ConfigMap 中定义的 special.how 值赋给 Pod Spec 中的 SPECIAL_LEVEL_KEY 环境变量。

    apiVersion: v1
    kind: Pod
    metadata:
      name: dapi-test-pod
    spec:
      containers:
        - name: test-container
          image: registry.k8s.io/busybox:1.27.2
          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

    kubectl create -f https://kubernetes.ac.cn/examples/pods/pod-single-configmap-env-variable.yaml
    

    现在,Pod 的输出包含了环境变量 SPECIAL_LEVEL_KEY=very

使用来自多个 ConfigMaps 的数据定义容器环境变量

与之前的示例一样,首先创建 ConfigMaps。下面是你将使用的清单

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
  • 创建 ConfigMap

    kubectl create -f https://kubernetes.ac.cn/examples/configmap/configmaps.yaml
    
  • 在 Pod Spec 中定义环境变量。

    apiVersion: v1
    kind: Pod
    metadata:
      name: dapi-test-pod
    spec:
      containers:
        - name: test-container
          image: registry.k8s.io/busybox:1.27.2
          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: Never
    

    创建 Pod

    kubectl create -f https://kubernetes.ac.cn/examples/pods/pod-multiple-configmap-env-variable.yaml
    

    现在,Pod 的输出包含了环境变量 SPECIAL_LEVEL_KEY=veryLOG_LEVEL=INFO

    一旦你准备继续,删除该 Pod 和 ConfigMap

    kubectl delete pod dapi-test-pod --now
    kubectl delete configmap special-config
    kubectl delete configmap env-config
    

将 ConfigMap 中的所有键值对配置为容器环境变量

  • 创建一个包含多个键值对的 ConfigMap。

    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: special-config
      namespace: default
    data:
      SPECIAL_LEVEL: very
      SPECIAL_TYPE: charm
    

    创建 ConfigMap

    kubectl create -f https://kubernetes.ac.cn/examples/configmap/configmap-multikeys.yaml
    
  • 使用 envFrom 将 ConfigMap 的所有数据定义为容器环境变量。ConfigMap 中的键成为 Pod 中的环境变量名称。

    apiVersion: v1
    kind: Pod
    metadata:
      name: dapi-test-pod
    spec:
      containers:
        - name: test-container
          image: registry.k8s.io/busybox:1.27.2
          command: [ "/bin/sh", "-c", "env" ]
          envFrom:
          - configMapRef:
              name: special-config
      restartPolicy: Never
    

    创建 Pod

    kubectl create -f https://kubernetes.ac.cn/examples/pods/pod-configmap-envFrom.yaml
    

    现在,Pod 的输出包含了环境变量 SPECIAL_LEVEL=verySPECIAL_TYPE=charm

    一旦你准备继续,删除该 Pod

    kubectl delete pod dapi-test-pod --now
    

在 Pod 命令中使用 ConfigMap 定义的环境变量

你可以使用 $(VAR_NAME) Kubernetes 替换语法在容器的 commandargs 中使用 ConfigMap 定义的环境变量。

例如,以下 Pod 清单

apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  containers:
    - name: test-container
      image: registry.k8s.io/busybox:1.27.2
      command: [ "/bin/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

通过运行以下命令创建该 Pod

kubectl create -f https://kubernetes.ac.cn/examples/pods/pod-configmap-env-var-valueFrom.yaml

该 Pod 会从 test-container 容器产生以下输出

kubectl logs dapi-test-pod
very charm

一旦你准备继续,删除该 Pod

kubectl delete pod dapi-test-pod --now

将 ConfigMap 数据添加到 Volume

从文件创建 ConfigMaps 中所述,当你使用 --from-file 创建 ConfigMap 时,文件名成为存储在 ConfigMap 的 data 部分中的键。文件内容成为键的值。

本节中的示例引用了一个名为 special-config 的 ConfigMap

apiVersion: v1
kind: ConfigMap
metadata:
  name: special-config
  namespace: default
data:
  SPECIAL_LEVEL: very
  SPECIAL_TYPE: charm

创建 ConfigMap

kubectl create -f https://kubernetes.ac.cn/examples/configmap/configmap-multikeys.yaml

使用存储在 ConfigMap 中的数据填充 Volume

在 Pod Spec 的 volumes 部分下添加 ConfigMap 名称。这将把 ConfigMap 数据添加到指定为 volumeMounts.mountPath 的目录(在本例中为 /etc/config)。command 部分列出了与 ConfigMap 中的键同名的目录文件。

apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  containers:
    - name: test-container
      image: registry.k8s.io/busybox:1.27.2
      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

kubectl create -f https://kubernetes.ac.cn/examples/pods/pod-configmap-volume.yaml

Pod 运行时,命令 ls /etc/config/ 将产生以下输出

SPECIAL_LEVEL
SPECIAL_TYPE

文本数据使用 UTF-8 字符编码作为文件暴露。要使用其他字符编码,请使用 binaryData(有关更多详细信息,请参阅ConfigMap 对象)。

一旦你准备继续,删除该 Pod

kubectl delete pod dapi-test-pod --now

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

使用 path 字段为特定的 ConfigMap 条目指定所需的文件路径。在本例中,SPECIAL_LEVEL 条目将被挂载到 config-volume 卷中的 /etc/config/keys

apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  containers:
    - name: test-container
      image: registry.k8s.io/busybox:1.27.2
      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

kubectl create -f https://kubernetes.ac.cn/examples/pods/pod-configmap-volume-specific-key.yaml

Pod 运行时,命令 cat /etc/config/keys 将产生以下输出

very

删除该 Pod

kubectl delete pod dapi-test-pod --now

将键投射到特定路径和文件权限

你可以将键投射到特定路径。有关语法,请参考Secret 指南中的相应章节。
你可以为键设置 POSIX 权限。有关语法,请参考Secret 指南中的相应章节。

可选引用

ConfigMap 引用可以标记为 optional(可选)。如果 ConfigMap 不存在,挂载的卷将为空。如果 ConfigMap 存在,但引用的键不存在,则挂载点下将没有该路径。更多详细信息请参阅可选 ConfigMaps

挂载的 ConfigMaps 会自动更新

当挂载的 ConfigMap 被更新时,投射的内容最终也会更新。这适用于 Pod 启动后可选引用的 ConfigMap 才存在的情况。

Kubelet 在每次周期性同步时都会检查挂载的 ConfigMap 是否最新。但是,它使用其本地基于 TTL 的缓存来获取 ConfigMap 的当前值。因此,从 ConfigMap 更新到新的键被投射到 Pod 的总延迟可能长达 Kubelet 同步周期(默认为 1 分钟)+ Kubelet 中 ConfigMaps 缓存的 TTL(默认为 1 分钟)。你可以通过更新 Pod 的注解之一来触发立即刷新。

理解 ConfigMaps 和 Pods

ConfigMap API 资源以键值对的形式存储配置数据。这些数据可以在 Pod 中使用,或为控制器等系统组件提供配置。ConfigMap 与 Secret 类似,但它提供了一种处理不包含敏感信息的字符串的方式。用户和系统组件都可以将配置数据存储在 ConfigMap 中。

ConfigMap 的 data 字段包含配置数据。如下例所示,这可以是简单的(如使用 --from-literal 定义的单个属性)或复杂的(如使用 --from-file 定义的配置文件或 JSON Blob)。

apiVersion: v1
kind: ConfigMap
metadata:
  creationTimestamp: 2016-02-18T19:14:38Z
  name: example-config
  namespace: default
data:
  # example of a simple property defined using --from-literal
  example.property.1: hello
  example.property.2: world
  # example of a complex property defined using --from-file
  example.property.file: |-
    property.1=value-1
    property.2=value-2
    property.3=value-3    

kubectl 从非 ASCII 或非 UTF-8 的输入创建 ConfigMap 时,该工具会将这些数据放入 ConfigMap 的 binaryData 字段,而不是 data 字段。文本和二进制数据源可以组合在一个 ConfigMap 中。

如果你想查看 ConfigMap 中的 binaryData 键(及其值),可以运行 kubectl get configmap -o jsonpath='{.binaryData}' <name>

Pod 可以从使用 databinaryData 的 ConfigMap 加载数据。

可选 ConfigMaps

你可以在 Pod Spec 中将对 ConfigMap 的引用标记为 optional(可选)。如果 ConfigMap 不存在,则它在 Pod 中提供的配置数据(例如:环境变量、挂载卷)将为空。如果 ConfigMap 存在,但引用的键不存在,则数据也为空。

例如,以下 Pod Spec 将来自 ConfigMap 的环境变量标记为可选

apiVersion: v1
kind: Pod
metadata:
  name: dapi-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: a-config
              key: akey
              optional: true # mark the variable as optional
  restartPolicy: Never

如果你运行此 Pod,并且没有名为 a-config 的 ConfigMap,则输出为空。如果你运行此 Pod,存在名为 a-config 的 ConfigMap,但该 ConfigMap 没有名为 akey 的键,则输出也为空。如果你在 a-config ConfigMap 中设置了 akey 的值,则该 Pod 将打印该值然后终止。

你还可以将 ConfigMap 提供的卷和文件标记为可选。即使引用的 ConfigMap 或键不存在,Kubernetes 也会为该卷创建挂载路径。例如,以下 Pod Spec 将引用 ConfigMap 的卷标记为可选

apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  containers:
    - name: test-container
      image: gcr.io/google_containers/busybox
      command: ["/bin/sh", "-c", "ls /etc/config"]
      volumeMounts:
      - name: config-volume
        mountPath: /etc/config
  volumes:
    - name: config-volume
      configMap:
        name: no-config
        optional: true # mark the source ConfigMap as optional
  restartPolicy: Never

限制

  • 在 Pod Spec 中引用 ConfigMap 对象之前,你必须先创建它。或者,在 Pod Spec 中将 ConfigMap 引用标记为 optional(可选)(参阅可选 ConfigMaps)。如果你引用了一个不存在的 ConfigMap 并且没有将其标记为 optional,Pod 将不会启动。同样,引用 ConfigMap 中不存在的键也会阻止 Pod 启动,除非你将键引用标记为 optional

  • 如果你使用 envFrom 从 ConfigMaps 定义环境变量,被视为无效的键将被跳过。Pod 将允许启动,但无效名称将被记录在事件日志中(InvalidVariableNames)。日志消息将列出每个被跳过的键。例如

    kubectl get events
    

    输出类似于这样

    LASTSEEN FIRSTSEEN COUNT NAME          KIND  SUBOBJECT  TYPE      REASON                            SOURCE                MESSAGE
    0s       0s        1     dapi-test-pod Pod              Warning   InvalidEnvironmentVariableNames   {kubelet, 127.0.0.1}  Keys [1badkey, 2alsobad] from the EnvFrom configMap default/myconfig were skipped since they are considered invalid environment variable names.
    
  • ConfigMaps 驻留在特定的命名空间中。Pod 只能引用与 Pod 位于同一命名空间中的 ConfigMaps。

  • 你不能将 ConfigMaps 用于静态 Pod,因为 kubelet 不支持这一点。

清理

删除你创建的 ConfigMaps 和 Pods

kubectl delete configmaps/game-config configmaps/game-config-2 configmaps/game-config-3 \
               configmaps/game-config-env-file
kubectl delete pod dapi-test-pod --now

# You might already have removed the next set
kubectl delete configmaps/special-config configmaps/env-config
kubectl delete configmap -l 'game-config in (config-4,config-5)'

删除用于生成 ConfigMap 的 kustomization.yaml 文件

rm kustomization.yaml

如果你创建了 configure-pod-container 目录并且不再需要它,也应该将其删除,或者将其移动到回收站/已删除文件位置。

rm -r configure-pod-container

下一步

最后修改于 太平洋标准时间 2023 年 11 月 27 日 1:38 AM: [en] config-pod-configmap add cleanup step. (2d13865044)