1
1
---
2
- title : ConfigMaps
2
+ title : ConfigMap
3
3
api_metadata :
4
4
- apiVersion : " v1"
5
5
kind : " ConfigMap"
6
6
content_type : concept
7
7
weight : 20
8
8
---
9
+ <!--
10
+ title: ConfigMaps
11
+ api_metadata:
12
+ - apiVersion: "v1"
13
+ kind: "ConfigMap"
14
+ content_type: concept
15
+ weight: 20
16
+ -->
9
17
10
18
<!-- overview -->
11
19
@@ -108,17 +116,17 @@ You can write a Pod `spec` that refers to a ConfigMap and configures the contain
108
116
in that Pod based on the data in the ConfigMap. The Pod and the ConfigMap must be in
109
117
the same {{< glossary_tooltip text="namespace" term_id="namespace" >}}.
110
118
-->
111
- ## ConfigMaps 和 Pods
119
+ ## ConfigMap 和 Pod {#configmaps-and-pods}
112
120
113
121
你可以写一个引用 ConfigMap 的 Pod 的 ` spec ` ,并根据 ConfigMap 中的数据在该
114
122
Pod 中配置容器。这个 Pod 和 ConfigMap 必须要在同一个
115
123
{{< glossary_tooltip text="名字空间" term_id="namespace" >}} 中。
116
124
125
+ {{< note >}}
117
126
<!--
118
127
The `spec` of a {{< glossary_tooltip text="static Pod" term_id="static-pod" >}} cannot refer to a ConfigMap
119
128
or any other API objects.
120
129
-->
121
- {{< note >}}
122
130
{{< glossary_tooltip text="静态 Pod" term_id="static-pod" >}} 中的 ` spec `
123
131
字段不能引用 ConfigMap 或任何其他 API 对象。
124
132
{{< /note >}}
@@ -150,6 +158,7 @@ data:
150
158
color.bad=yellow
151
159
allow.textmode=true
152
160
` ` `
161
+
153
162
<!--
154
163
There are four different ways that you can use a ConfigMap to configure
155
164
a container inside a Pod:
@@ -356,11 +365,12 @@ ConfigMaps consumed as environment variables are not updated automatically and r
356
365
以环境变量方式使用的 ConfigMap 数据不会被自动更新。
357
366
更新这些数据需要重新启动 Pod。
358
367
368
+ {{< note >}}
359
369
<!--
360
370
A container using a ConfigMap as a [subPath](/docs/concepts/storage/volumes#using-subpath) volume mount will not receive ConfigMap updates.
361
371
-->
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 的更新。
364
374
{{< /note >}}
365
375
366
376
<!--
@@ -386,9 +396,82 @@ in a Pod:
386
396
387
397
<!--
388
398
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:
389
401
-->
390
402
下面是一个将 ConfigMap 定义为 Pod 环境变量的示例:
391
403
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
+
392
475
` ` ` yaml
393
476
apiVersion: v1
394
477
kind: Pod
@@ -404,9 +487,16 @@ spec:
404
487
configMapKeyRef:
405
488
name: myconfigmap
406
489
key: username
407
-
408
490
` ` `
409
491
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
+
410
500
<!--
411
501
It's important to note that the range of characters allowed for environment
412
502
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
429
519
(at least tens of thousands of unique ConfigMap to Pod mounts), preventing changes to their
430
520
data has the following advantages :
431
521
-->
432
- Kubernetes 特性 _Immutable Secret 和 ConfigMaps_ 提供了一种将各个
522
+ Kubernetes 特性 **Immutable Secret 和 ConfigMap** 提供了一种将各个
433
523
Secret 和 ConfigMap 设置为不可变更的选项。对于大量使用 ConfigMap 的集群
434
524
(至少有数万个各不相同的 ConfigMap 给 Pod 挂载)而言,禁止更改
435
525
ConfigMap 的数据有以下好处:
0 commit comments