Skip to content

Commit 3eced5d

Browse files
authored
Merge pull request #35411 from yanrongshi/update-configure-pod-configmap.md
[zh-cn]update configure-pod-configmap.md
2 parents dc6a6a2 + 6954524 commit 3eced5d

File tree

1 file changed

+117
-78
lines changed

1 file changed

+117
-78
lines changed

content/zh-cn/docs/tasks/configure-pod-container/configure-pod-configmap.md

Lines changed: 117 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ card:
1919
<!--
2020
Many applications rely on configuration which is used during either application initialization or runtime.
2121
Most of the times there is a requirement to adjust values assigned to configuration parameters.
22-
ConfigMaps is the kubernetes way to inject application pods with configuration data.
22+
ConfigMaps are the Kubernetes way to inject application pods with configuration data.
2323
ConfigMaps allow you to decouple configuration artifacts from image content to keep containerized applications portable. This page provides a series of usage examples demonstrating how to create ConfigMaps and configure Pods using data stored in ConfigMaps.
2424
-->
2525
很多应用在其初始化或运行期间要依赖一些配置信息。大多数时候,
@@ -85,19 +85,13 @@ You can use [`kubectl describe`](/docs/reference/generated/kubectl/kubectl-comma
8585
[`kubectl get`](/docs/reference/generated/kubectl/kubectl-commands/#get) to retrieve information
8686
about a ConfigMap.
8787
-->
88-
你可以使用[`kubectl describe`](/docs/reference/generated/kubectl/kubectl-commands/#describe) 或者
88+
你可以使用 [`kubectl describe`](/docs/reference/generated/kubectl/kubectl-commands/#describe) 或者
8989
[`kubectl get`](/docs/reference/generated/kubectl/kubectl-commands/#get) 获取有关 ConfigMap 的信息。
9090

9191
<!--
9292
#### Create ConfigMaps from directories
9393
94-
You can use `kubectl create configmap` to create a ConfigMap from multiple
95-
files in the same directory.
96-
When you are creating a ConfigMap based on a directory, kubectl identifies
97-
files whose basename is a valid key in the directory and packages each of
98-
those files into the new ConfigMap. Any directory entries except regular files
99-
are ignored (e.g. subdirectories, symlinks, devices, pipes, etc).
100-
94+
You can use `kubectl create configmap` to create a ConfigMap from multiple files in the same directory. When you are creating a ConfigMap based on a directory, kubectl identifies files whose basename is a valid key in the directory and packages each of those files into the new ConfigMap. Any directory entries except regular files are ignored (e.g. subdirectories, symlinks, devices, pipes, etc).
10195
For example:
10296
-->
10397
#### 基于目录创建 ConfigMap {#create-configmaps-from-directories}
@@ -135,10 +129,7 @@ kubectl create configmap game-config --from-file=configure-pod-container/configm
135129
```
136130

137131
<!--
138-
The above command packages each file, in this case, `game.properties` and
139-
`ui.properties` in the `configure-pod-container/configmap/` directory into the
140-
game-config ConfigMap. You can display details of the ConfigMap using the
141-
following command:
132+
The above command packages each file, in this case, `game.properties` and `ui.properties` in the `configure-pod-container/configmap/` directory into the game-config ConfigMap. You can display details of the ConfigMap using the following command:
142133
-->
143134
以上命令将 `configure-pod-container/configmap` 目录下的所有文件,也就是
144135
`game.properties``ui.properties` 打包到 game-config ConfigMap
@@ -1056,49 +1047,7 @@ basis. The [Secrets](/docs/concepts/configuration/secret/#using-secrets-as-files
10561047
[Secret 用户指南](/zh-cn/docs/concepts/configuration/secret/#using-secrets-as-files-from-a-pod)
10571048
中为这一语法提供了解释。
10581049

1059-
<!--
1060-
### Optional References
1061-
1062-
A ConfigMap reference may be marked "optional". If the ConfigMap is non-existent, the mounted volume will be empty. If the ConfigMap exists, but the referenced
1063-
key is non-existent the path will be absent beneath the mount point.
1064-
-->
1065-
### 可选的引用 {#optional-references}
1066-
1067-
ConfigMap 引用可以被标记为 “optional(可选的)”。如果所引用的 ConfigMap 不存在,
1068-
则所挂载的卷将会是空的。如果所引用的 ConfigMap 确实存在,但是所引用的主键不存在,
1069-
则在挂载点下对应的路径也会不存在。
1070-
1071-
<!--
1072-
### Mounted ConfigMaps are updated automatically
1073-
-->
1074-
### 挂载的 ConfigMap 将自动更新 {#mounted-configmaps-are-updated-automatically}
1075-
1076-
<!--
1077-
When a mounted ConfigMap is updated, the projected content is eventually updated too. This applies in the case where an optionally referenced ConfigMap comes into
1078-
existence after a pod has started.
1079-
-->
1080-
当某个已被挂载的 ConfigMap 被更新,所投射的内容最终也会被更新。
1081-
对于 Pod 已经启动之后所引用的、可选的 ConfigMap 才出现的情形,
1082-
这一动态更新现象也是适用的。
10831050

1084-
<!--
1085-
Kubelet checks whether the mounted ConfigMap is fresh on every periodic sync. However, it uses its local TTL-based cache for getting the current value of the
1086-
ConfigMap. As a result, the total delay from the moment when the ConfigMap is updated to the moment when new keys are projected to the pod can be as long as
1087-
kubelet sync period (1 minute by default) + TTL of ConfigMaps cache (1 minute by default) in kubelet.
1088-
-->
1089-
`kubelet` 在每次周期性同步时都会检查已挂载的 ConfigMap 是否是最新的。
1090-
但是,它使用其本地的基于 TTL 的缓存来获取 ConfigMap 的当前值。
1091-
因此,从更新 ConfigMap 到将新键映射到 Pod 的总延迟可能与
1092-
kubelet 同步周期(默认 1 分钟) + ConfigMap 在 kubelet 中缓存的 TTL
1093-
(默认 1 分钟)一样长。
1094-
1095-
{{< note >}}
1096-
<!--
1097-
A container using a ConfigMap as a [subPath](/docs/concepts/storage/volumes/#using-subpath) volume will not receive ConfigMap updates.
1098-
-->
1099-
使用 ConfigMap 作为 [subPath](/zh-cn/docs/concepts/storage/volumes/#using-subpath)
1100-
的数据卷将不会收到 ConfigMap 更新。
1101-
{{< /note >}}
11021051

11031052
<!-- discussion -->
11041053

@@ -1132,25 +1081,7 @@ ConfigMap 的 `data` 字段包含配置数据。如下例所示,它可以简
11321081
(如用 `--from-literal` 的单个属性定义)或复杂
11331082
(如用 `--from-file` 的配置文件或 JSON blob定义)。
11341083

1135-
<!--
1136-
```yaml
1137-
apiVersion: v1
1138-
kind: ConfigMap
1139-
metadata:
1140-
creationTimestamp: 2016-02-18T19:14:38Z
1141-
name: example-config
1142-
namespace: default
1143-
data:
1144-
# example of a simple property defined using --from-literal
1145-
example.property.1: hello
1146-
example.property.2: world
1147-
# example of a complex property defined using --from-file
1148-
example.property.file: |-
1149-
property.1=value-1
1150-
property.2=value-2
1151-
property.3=value-3
1152-
```
1153-
-->
1084+
11541085
```yaml
11551086
apiVersion: v1
11561087
kind: ConfigMap
@@ -1172,13 +1103,14 @@ data:
11721103
<!--
11731104
### Restrictions
11741105
1175-
- You must create a ConfigMap before referencing it in a Pod specification (unless you mark the ConfigMap as "optional"). If you reference a ConfigMap that doesn't exist, the Pod won't start. Likewise, references to keys that don't exist in the ConfigMap will prevent the pod from starting.
1106+
- You must create the `ConfigMap` object before you reference it in a Pod specification. Alternatively, mark the ConfigMap reference as `optional` in the Pod spec (see [Optional ConfigMaps](#optional-configmaps)). If you reference a ConfigMap that doesn't exist and you don't mark the reference as `optional`, the Pod won't start. Similarly, references to keys that don't exist in the ConfigMap will also prevent the Pod from starting, unless you mark the key references as `optional`.
11761107
-->
11771108
### 限制 {#restrictions}
11781109

1179-
- 在 Pod 规约中引用某个 ConfigMap 之前,必须先创建它(除非将 ConfigMap 标记为
1180-
“optional(可选)”)。如果引用的 ConfigMap 不存在,则 Pod 将不会启动。
1181-
同样,引用 ConfigMap 中不存在的主键也会令 Pod 无法启动。
1110+
- 在 Pod 规约中引用某个 `ConfigMap` 之前,必须先创建这个对象,
1111+
或者在 Pod 规约中将 ConfigMap 标记为 `optional`(请参阅[可选的 ConfigMaps](#optional-configmaps))。
1112+
如果所引用的 ConfigMap 不存在,并且没有将应用标记为 `optional` 则 Pod 将无法启动。
1113+
同样,引用 ConfigMap 中不存在的主键也会令 Pod 无法启动,除非你将 Configmap 标记为 `optional`。
11821114

11831115
<!--
11841116
- If you use `envFrom` to define environment variables from ConfigMaps, keys that are considered invalid will be skipped. The pod will be allowed to start, but the invalid names will be recorded in the event log (`InvalidVariableNames`). The log message lists each skipped key. For example:
@@ -1213,6 +1145,113 @@ data:
12131145
- 你不能将 ConfigMap 用于{{< glossary_tooltip text="静态 Pod" term_id="static-pod" >}},
12141146
因为 Kubernetes 不支持这种用法。
12151147
1148+
<!--
1149+
### Optional ConfigMaps
1150+
-->
1151+
### 可选的 ConfigMap {#optional-configmaps}
1152+
1153+
<!--
1154+
You can mark a reference to a ConfigMap as _optional_ in a Pod specification.
1155+
If the ConfigMap doesn't exist, the configuration for which it provides data in the Pod (e.g. environment variable, mounted volume) will be empty.
1156+
If the ConfigMap exists, but the referenced key is non-existent the data is also empty.
1157+
-->
1158+
你可以在 Pod 规约中将对 ConfigMap 的引用标记为 **可选(optional)**。
1159+
如果 ConfigMap 不存在,那么它在 Pod 中为其提供数据的配置(例如环境变量、挂载的卷)将为空。
1160+
如果 ConfigMap 存在,但引用的键不存在,那么数据也是空的。
1161+
1162+
<!--
1163+
For example, the following Pod specification marks an environment variable from a ConfigMap as optional:
1164+
-->
1165+
例如,以下 Pod 规约将 ConfigMap 中的环境变量标记为可选:
1166+
1167+
```yaml
1168+
apiVersion: v1
1169+
kind: Pod
1170+
metadata:
1171+
name: dapi-test-pod
1172+
spec:
1173+
containers:
1174+
- name: test-container
1175+
image: gcr.io/google_containers/busybox
1176+
command: [ "/bin/sh", "-c", "env" ]
1177+
env:
1178+
- name: SPECIAL_LEVEL_KEY
1179+
valueFrom:
1180+
configMapKeyRef:
1181+
name: a-config
1182+
key: akey
1183+
optional: true # 将环境变量标记为可选
1184+
restartPolicy: Never
1185+
```
1186+
<!--
1187+
If you run this pod, and there is no ConfigMap named `a-config`, the output is empty.
1188+
If you run this pod, and there is a ConfigMap named `a-config` but that ConfigMap doesn't have
1189+
a key named `akey`, the output is also empty. If you do set a value for `akey` in the `a-config`
1190+
ConfigMap, this pod prints that value and then terminates.
1191+
-->
1192+
当你运行这个 Pod 并且名称为 `a-config` 的 ConfigMap 不存在时,输出空值。
1193+
当你运行这个 Pod 并且名称为 `a-config` 的 ConfigMap 存在,
1194+
但是在 ConfigMap 中没有名称为 `akey` 的键时,控制台输出也会为空。
1195+
如果你确实在名为 `a-config` 的 ConfigMap 中为 `akey` 设置了键值,
1196+
那么这个 Pod 会打印该值,然后终止。
1197+
1198+
<!--
1199+
You can also mark the volumes and files provided by a ConfigMap as optional. Kubernetes always creates the mount paths for the volume, even if the referenced ConfigMap or key doesn't exist. For example, the following
1200+
Pod specification marks a volume that references a ConfigMap as optional:
1201+
-->
1202+
你也可以在 Pod 规约中将 ConfigMap 提供的卷和文件标记为可选。
1203+
此时 Kubernetes 将总是为卷创建挂载路径,即使引用的 ConfigMap 或键不存在。
1204+
例如,以下 Pod 规约将所引用得 ConfigMap 的卷标记为可选:
1205+
1206+
1207+
```yaml
1208+
apiVersion: v1
1209+
kind: Pod
1210+
metadata:
1211+
name: dapi-test-pod
1212+
spec:
1213+
containers:
1214+
- name: test-container
1215+
image: gcr.io/google_containers/busybox
1216+
command: [ "/bin/sh", "-c", "ls /etc/config" ]
1217+
volumeMounts:
1218+
- name: config-volume
1219+
mountPath: /etc/config
1220+
volumes:
1221+
- name: config-volume
1222+
configMap:
1223+
name: no-config
1224+
optional: true # 将引用的 ConfigMap 的卷标记为可选
1225+
restartPolicy: Never
1226+
```
1227+
1228+
### 挂载的 ConfigMap 将被自动更新 {#mounted-configmaps-are-updated-automatically}
1229+
1230+
<!--
1231+
When a mounted ConfigMap is updated, the projected content is eventually updated too. This applies in the case where an optionally referenced ConfigMap comes into
1232+
existence after a pod has started.
1233+
-->
1234+
当某个已被挂载的 ConfigMap 被更新,所投射的内容最终也会被更新。
1235+
对于 Pod 已经启动之后所引用的、可选的 ConfigMap 才出现的情形,
1236+
这一动态更新现象也是适用的。
1237+
1238+
<!--
1239+
The kubelet checks whether the mounted ConfigMap is fresh on every periodic sync. However, it uses its local TTL-based cache for getting the current value of the
1240+
ConfigMap. As a result, the total delay from the moment when the ConfigMap is updated to the moment when new keys are projected to the pod can be as long as
1241+
kubelet sync period (1 minute by default) + TTL of ConfigMaps cache (1 minute by default) in kubelet.
1242+
-->
1243+
kubelet 在每次周期性同步时都会检查已挂载的 ConfigMap 是否是最新的。
1244+
但是,它使用其本地的基于 TTL 的缓存来获取 ConfigMap 的当前值。
1245+
因此,从更新 ConfigMap 到将新键映射到 Pod 的总延迟可能等于 kubelet 同步周期
1246+
(默认 1 分钟) + ConfigMap 在 kubelet 中缓存的 TTL(默认 1 分钟)。
1247+
1248+
{{< note >}}
1249+
<!--
1250+
A container using a ConfigMap as a [subPath](/docs/concepts/storage/volumes/#using-subpath) volume will not receive ConfigMap updates.
1251+
-->
1252+
使用 ConfigMap 作为 [subPath](/zh-cn/docs/concepts/storage/volumes/#using-subpath)
1253+
的数据卷将不会收到 ConfigMap 更新。
1254+
{{< /note >}}
12161255
## {{% heading "whatsnext" %}}
12171256
12181257
<!--

0 commit comments

Comments
 (0)