Skip to content

Commit 95b49ef

Browse files
committed
[zh] Sync a concept: configuation/configmap.md
1 parent 2bdd12a commit 95b49ef

File tree

2 files changed

+109
-7
lines changed

2 files changed

+109
-7
lines changed

content/zh-cn/docs/concepts/configuration/configmap.md

Lines changed: 97 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
---
2-
title: ConfigMaps
2+
title: ConfigMap
33
api_metadata:
44
- apiVersion: "v1"
55
kind: "ConfigMap"
66
content_type: concept
77
weight: 20
88
---
9+
<!--
10+
title: ConfigMaps
11+
api_metadata:
12+
- apiVersion: "v1"
13+
kind: "ConfigMap"
14+
content_type: concept
15+
weight: 20
16+
-->
917

1018
<!-- overview -->
1119

@@ -108,17 +116,17 @@ You can write a Pod `spec` that refers to a ConfigMap and configures the contain
108116
in that Pod based on the data in the ConfigMap. The Pod and the ConfigMap must be in
109117
the same {{< glossary_tooltip text="namespace" term_id="namespace" >}}.
110118
-->
111-
## ConfigMapsPods
119+
## ConfigMapPod {#configmaps-and-pods}
112120

113121
你可以写一个引用 ConfigMap 的 Pod 的 `spec`,并根据 ConfigMap 中的数据在该
114122
Pod 中配置容器。这个 Pod 和 ConfigMap 必须要在同一个
115123
{{< glossary_tooltip text="名字空间" term_id="namespace" >}} 中。
116124

125+
{{< note >}}
117126
<!--
118127
The `spec` of a {{< glossary_tooltip text="static Pod" term_id="static-pod" >}} cannot refer to a ConfigMap
119128
or any other API objects.
120129
-->
121-
{{< note >}}
122130
{{< glossary_tooltip text="静态 Pod" term_id="static-pod" >}} 中的 `spec`
123131
字段不能引用 ConfigMap 或任何其他 API 对象。
124132
{{< /note >}}
@@ -150,6 +158,7 @@ data:
150158
color.bad=yellow
151159
allow.textmode=true
152160
```
161+
153162
<!--
154163
There are four different ways that you can use a ConfigMap to configure
155164
a container inside a Pod:
@@ -356,11 +365,12 @@ ConfigMaps consumed as environment variables are not updated automatically and r
356365
以环境变量方式使用的 ConfigMap 数据不会被自动更新。
357366
更新这些数据需要重新启动 Pod。
358367

368+
{{< note >}}
359369
<!--
360370
A container using a ConfigMap as a [subPath](/docs/concepts/storage/volumes#using-subpath) volume mount will not receive ConfigMap updates.
361371
-->
362-
{{< note >}}
363-
使用 ConfigMap 作为 [subPath](/zh-cn/docs/concepts/storage/volumes#using-subpath) 卷挂载的容器将不会收到 ConfigMap 的更新。
372+
使用 ConfigMap 作为 [subPath](/zh-cn/docs/concepts/storage/volumes#using-subpath)
373+
卷挂载的容器将不会收到 ConfigMap 的更新。
364374
{{< /note >}}
365375

366376
<!--
@@ -386,9 +396,82 @@ in a Pod:
386396

387397
<!--
388398
This is an example of defining a ConfigMap as a pod environment variable:
399+
400+
The following ConfigMap (myconfigmap.yaml) stores two properties: username and access_level:
389401
-->
390402
下面是一个将 ConfigMap 定义为 Pod 环境变量的示例:
391403

404+
以下 ConfigMap (myconfigmap.yaml) 存储两个属性:username 和 access_level:
405+
406+
```yaml
407+
apiVersion: v1
408+
kind: ConfigMap
409+
metadata:
410+
name: myconfigmap
411+
data:
412+
username: k8s-admin
413+
access_level: "1"
414+
```
415+
416+
<!--
417+
The following command will create the ConfigMap object:
418+
-->
419+
以下命令将创建 ConfigMap 对象:
420+
421+
```shell
422+
kubectl apply -f myconfigmap.yaml
423+
```
424+
425+
<!--
426+
The following Pod consumes the content of the ConfigMap as environment variables:
427+
-->
428+
以下 Pod 将 ConfigMap 的内容用作环境变量:
429+
430+
{{% code_sample file="configmap/env-configmap.yaml" %}}
431+
432+
<!--
433+
The `envFrom` field instructs Kubernetes to create environment variables from the sources nested within it.
434+
The inner `configMapRef` refers to a ConfigMap by its name and selects all its key-value pairs.
435+
Add the Pod to your cluster, then retrieve its logs to see the output from the printenv command.
436+
This should confirm that the two key-value pairs from the ConfigMap have been set as environment variables:
437+
-->
438+
`envFrom` 字段指示 Kubernetes 使用其中嵌套的源创建环境变量。
439+
内部的 `configMapRef` 通过 ConfigMap 的名称引用之,并选择其所有键值对。
440+
将 Pod 添加到你的集群中,然后检索其日志以查看 printenv 命令的输出。
441+
此操作可确认来自 ConfigMap 的两个键值对已被设置为环境变量:
442+
443+
```shell
444+
kubectl apply -f env-configmap.yaml
445+
```
446+
447+
```shell
448+
kubectl logs pod/ env-configmap
449+
```
450+
451+
<!--
452+
The output is similar to this:
453+
-->
454+
输出类似于:
455+
456+
```console
457+
...
458+
username: "k8s-admin"
459+
access_level: "1"
460+
...
461+
```
462+
463+
<!--
464+
Sometimes a Pod won't require access to all the values in a ConfigMap.
465+
For example, you could have another Pod which only uses the username value from the ConfigMap.
466+
For this use case, you can use the `env.valueFrom` syntax instead, which lets you select individual keys in
467+
a ConfigMap. The name of the environment variable can also be different from the key within the ConfigMap.
468+
For example:
469+
-->
470+
有时 Pod 不需要访问 ConfigMap 中的所有值。
471+
例如,你可以有另一个 Pod 只使用 ConfigMap 中的 username 值。
472+
在这种使用场景中,你可以转为使用 `env.valueFrom` 语法,这样可以让你选择 ConfigMap 中的单个键。
473+
环境变量的名称也可以不同于 ConfigMap 中的键。例如:
474+
392475
```yaml
393476
apiVersion: v1
394477
kind: Pod
@@ -404,9 +487,16 @@ spec:
404487
configMapKeyRef:
405488
name: myconfigmap
406489
key: username
407-
408490
```
409491

492+
<!--
493+
In the Pod created from this manifest, you will see that the environment variable
494+
`CONFIGMAP_USERNAME` is set to the value of the `username` value from the ConfigMap.
495+
Other keys from the ConfigMap data are not copied into the environment.
496+
-->
497+
在从此清单创建的 Pod 中,你将看到环境变量 `CONFIGMAP_USERNAME` 被设置为 ConfigMap 中 `username` 的取值。
498+
来自 ConfigMap 数据中的其他键不会被复制到环境中。
499+
410500
<!--
411501
It's important to note that the range of characters allowed for environment
412502
variable names in pods is [restricted](/docs/tasks/inject-data-application/define-environment-variable-container/#using-environment-variables-inside-of-your-config).
@@ -429,7 +519,7 @@ individual Secrets and ConfigMaps as immutable. For clusters that extensively us
429519
(at least tens of thousands of unique ConfigMap to Pod mounts), preventing changes to their
430520
data has the following advantages:
431521
-->
432-
Kubernetes 特性 _Immutable Secret 和 ConfigMaps_ 提供了一种将各个
522+
Kubernetes 特性 **Immutable Secret 和 ConfigMap** 提供了一种将各个
433523
Secret 和 ConfigMap 设置为不可变更的选项。对于大量使用 ConfigMap 的集群
434524
(至少有数万个各不相同的 ConfigMap 给 Pod 挂载)而言,禁止更改
435525
ConfigMap 的数据有以下好处:
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
apiVersion: v1
2+
kind: Pod
3+
metadata:
4+
name: env-configmap
5+
spec:
6+
containers:
7+
- name: app
8+
command: ["/bin/sh", "-c", "printenv"]
9+
image: busybox:latest
10+
envFrom:
11+
- configMapRef:
12+
name: myconfigmap

0 commit comments

Comments
 (0)