配置 Pod 使用 ConfigMap

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

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

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

准备工作

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

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

创建 ConfigMap

你可以使用 kubectl create configmapkustomization.yaml 中的 ConfigMap 生成器来创建 ConfigMap。

使用 kubectl create configmap 创建 ConfigMap

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

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

其中 是你想要分配给 ConfigMap 的名称, 是用于获取数据的目录、文件或字面值。ConfigMap 对象的名称必须是有效的 DNS 子域名

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

你可以使用 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    

从文件创建 ConfigMap

你可以使用 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 从环境变量文件创建 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    

从字面值创建 ConfigMap

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

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 文件中指定生成器。

从文件生成 ConfigMap

例如,要从文件 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

从字面量生成 ConfigMap

本例向你展示如何使用 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

临时清理

在继续之前,清理你制作的一些 ConfigMap

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

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


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

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

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

    kubectl create configmap special-config --from-literal=special.how=very
    
  2. 将 ConfigMap 中定义的 special.how 值赋给 Pod 规约中的 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

使用多个 ConfigMap 中的数据定义容器环境变量

与上一个示例一样,首先创建 ConfigMap。这是你将使用的清单

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 规约中定义环境变量。

    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 数据添加到卷

从文件创建 ConfigMap 中所述,当你使用 --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 中的数据填充卷

在 Pod 规约的 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 数据添加到卷中的特定路径

使用 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

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

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

可选引用

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

挂载的 ConfigMap 会自动更新

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

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

理解 ConfigMap 和 Pod

ConfigMap API 资源以键值对的形式存储配置数据。数据可以在 Pod 中使用,也可以为控制器等系统组件提供配置。ConfigMap 类似于 Secrets,但提供了一种处理不包含敏感信息的字符串的方法。用户和系统组件都可以将配置数据存储在 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 加载数据。

可选 ConfigMap

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

例如,以下 Pod 规约将 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 规约将引用 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 规约中引用 ConfigMap 对象之前,你必须先创建它。或者,在 Pod 规约中将 ConfigMap 引用标记为 optional(请参阅可选 ConfigMap)。如果你引用一个不存在的 ConfigMap 并且没有将引用标记为 optional,则 Pod 将无法启动。同样,如果 ConfigMap 中不存在的键引用未标记为 optional,也会阻止 Pod 启动。

  • 如果你使用 envFrom 从 ConfigMap 定义环境变量,则被认为是无效的键将被跳过。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.
    
  • ConfigMap 位于特定的 命名空间 中。Pod 只能引用与 Pod 位于同一命名空间中的 ConfigMap。

  • 你不能将 ConfigMap 用于静态 Pod,因为 kubelet 不支持此功能。

清理

删除你创建的 ConfigMap 和 Pod

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:[en] config-pod-configmap add cleanup step. (2d13865044)