配置 Pod 以使用 ConfigMap
许多应用程序依赖于配置,这些配置在应用程序初始化或运行时使用。大多数时候,都需要调整分配给配置参数的值。ConfigMap 是 Kubernetes 的一种机制,允许您将配置数据注入到应用程序的 Pod 中。
ConfigMap 的概念允许您将配置工件与镜像内容分离,以保持容器化应用程序的可移植性。例如,您可以下载并运行相同的 容器镜像,以启动用于本地开发、系统测试或运行实时最终用户工作负载的容器。
本页提供了一系列使用示例,演示如何创建 ConfigMap 以及如何使用 ConfigMap 中存储的数据配置 Pod。
开始之前
您需要有一个 Kubernetes 集群,并且必须配置 kubectl 命令行工具以与您的集群通信。建议在至少有两个不充当控制平面主机的节点的集群上运行本教程。如果您还没有集群,可以使用 minikube 创建一个集群,或者您可以使用以下 Kubernetes 游乐场之一
您需要安装 wget 工具。如果您有其他工具(如 curl),并且没有 wget,则需要调整下载示例数据的步骤。
创建 ConfigMap
您可以使用 kubectl create configmap 或 kustomization.yaml 中的 ConfigMap 生成器来创建 ConfigMap。
使用 kubectl create configmap 创建 ConfigMap
使用 kubectl create configmap 命令从目录、文件或文字值创建 ConfigMap
kubectl create configmap <map-name> <data-source>
其中 <map-name> 是您要分配给 ConfigMap 的名称,而 <data-source> 是要从中提取数据的目录、文件或文字值。ConfigMap 对象的名称必须是有效的 DNS 子域名。
当您基于文件创建 ConfigMap 时,<data-source> 中的键默认为该文件的基本名称,而值默认为文件内容。
您可以使用 kubectl describe 或 kubectl get 来检索有关 ConfigMap 的信息。
从目录创建 ConfigMap
您可以使用 kubectl create configmap 从同一目录中的多个文件创建 ConfigMap。当您基于目录创建 ConfigMap 时,kubectl 会识别文件名是该目录中有效键的文件,并将每个文件打包到新的 ConfigMap 中。除常规文件外的任何目录条目都将被忽略(例如:子目录、符号链接、设备、管道等)。
注意
用于创建 ConfigMap 的每个文件名必须仅包含可接受的字符,这些字符是:字母(A 到 Z 和 a 到 z)、数字(0 到 9)、'-'、'_' 或 '.'。如果您使用 kubectl create configmap 处理目录,而目录中的任何文件名包含不可接受的字符,则 kubectl 命令可能会失败。
当 kubectl 命令遇到无效的文件名时,它不会打印错误。
创建本地目录
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.properties 和 ui.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.properties 和 ui.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 从 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=charm 和 special.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
将环境变量定义为 ConfigMap 中的键值对
kubectl create configmap special-config --from-literal=special.how=very将 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 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 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=very和LOG_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 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=very和SPECIAL_TYPE=charm。当您准备好继续时,删除该 Pod
kubectl delete pod dapi-test-pod --now
在 Pod 命令中使用 ConfigMap 定义的环境变量
您可以使用 Kubernetes 替换语法 $(VAR_NAME) 在容器的 command 和 args 中使用 ConfigMap 定义的环境变量。
例如,以下 Pod 清单
apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
spec:
containers:
- name: test-container
image: registry.k8s.io/busybox
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
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 对象)。
注意
如果该容器映像的/etc/config 目录中有任何文件,则卷挂载将使映像中的这些文件无法访问。当您准备好继续时,删除该 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
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
注意
和以前一样,/etc/config/ 目录中的所有以前的文件都将被删除。删除该 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 作为subPath卷的容器将不会收到 ConfigMap 更新。了解 ConfigMap 和 Pod
ConfigMap API 资源将配置数据存储为键值对。该数据可以在 pod 中使用,或者为控制器等系统组件提供配置。ConfigMap 类似于Secrets,但它提供了一种处理不包含敏感信息的字符串的方法。用户和系统组件都可以将配置数据存储在 ConfigMap 中。
注意
ConfigMap 应该引用属性文件,而不是替换它们。将 ConfigMap 视为类似于 Linux/etc 目录及其内容。例如,如果您从 ConfigMap 创建Kubernetes 卷,则 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 可以从使用 data 或 binaryData 的 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 提供的卷和文件标记为可选。Kubernetes 始终为卷创建挂载路径,即使引用的 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", "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
限制
您必须先创建
ConfigMap对象,然后在 Pod 规范中引用它。或者,在 Pod 规范中将 ConfigMap 引用标记为optional(请参阅可选 ConfigMap)。如果您引用不存在的 ConfigMap,并且您没有将引用标记为optional,则 Pod 将不会启动。同样,除非您将键引用标记为optional,否则引用 ConfigMap 中不存在的键也会阻止 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
下一步是什么
- 遵循使用 ConfigMap 配置 Redis的真实世界示例。
- 遵循通过 ConfigMap 更新配置的示例。