@@ -30,7 +30,8 @@ ConfigMap 是 Kubernetes 的一种机制,可让你将配置数据注入到应
30
30
<!--
31
31
The ConfigMap concept allow you to decouple configuration artifacts from image content to
32
32
keep containerized applications portable. For example, you can download and run the same
33
- {{< glossary_tooltip text="container image" term_id="image" >}} to spin up containers for the purposes of local development, system test, or running a live end-user workload.
33
+ {{< glossary_tooltip text="container image" term_id="image" >}} to spin up containers for
34
+ the purposes of local development, system test, or running a live end-user workload.
34
35
-->
35
36
ConfigMap 概念允许你将配置清单与镜像内容分离,以保持容器化的应用程序的可移植性。
36
37
例如,你可以下载并运行相同的{{< glossary_tooltip text="容器镜像" term_id="image" >}}来启动容器,
@@ -133,8 +134,10 @@ symlinks, devices, pipes, and more).
133
134
134
135
{{< note >}}
135
136
<!--
136
- Each filename being used for ConfigMap creation must consist of only acceptable characters, which are: letters (`A` to `Z` and `a` to `z`), digits (`0` to `9`), '-', '_', or '.'.
137
- If you use `kubectl create configmap` with a directory where any of the file names contains an unacceptable character, the `kubectl` command may fail.
137
+ Each filename being used for ConfigMap creation must consist of only acceptable characters,
138
+ which are: letters (`A` to `Z` and `a` to `z`), digits (`0` to `9`), '-', '_', or '.'.
139
+ If you use `kubectl create configmap` with a directory where any of the file names contains
140
+ an unacceptable character, the `kubectl` command may fail.
138
141
-->
139
142
用于创建 ConfigMap 的每个文件名必须由可接受的字符组成,即:字母(` A ` 到 ` Z ` 和
140
143
` a ` 到 ` z ` )、数字(` 0 ` 到 ` 9 ` )、'-'、'_ '或'.'。
@@ -162,6 +165,16 @@ Now, download the sample configuration and create the ConfigMap:
162
165
-->
163
166
现在,下载示例的配置并创建 ConfigMap:
164
167
168
+ <!--
169
+ ```shell
170
+ # Download the sample files into `configure-pod-container/configmap/` directory
171
+ wget https://kubernetes.io/examples/configmap/game.properties -O configure-pod-container/configmap/game.properties
172
+ wget https://kubernetes.io/examples/configmap/ui.properties -O configure-pod-container/configmap/ui.properties
173
+
174
+ # Create the ConfigMap
175
+ kubectl create configmap game-config --from-file=configure-pod-container/configmap/
176
+ ```
177
+ -->
165
178
``` shell
166
179
# 将示例文件下载到 `configure-pod-container/configmap/` 目录
167
180
wget https://kubernetes.io/examples/configmap/game.properties -O configure-pod-container/configmap/game.properties
@@ -360,6 +373,28 @@ Use the option `--from-env-file` to create a ConfigMap from an env-file, for exa
360
373
-->
361
374
使用 `--from-env-file` 选项基于 env 文件创建 ConfigMap,例如:
362
375
376
+ <!--
377
+ ` ` ` shell
378
+ # Env-files contain a list of environment variables.
379
+ # These syntax rules apply:
380
+ # Each line in an env file has to be in VAR=VAL format.
381
+ # Lines beginning with # (i.e. comments) are ignored.
382
+ # Blank lines are ignored.
383
+ # There is no special handling of quotation marks (i.e. they will be part of the ConfigMap value)).
384
+
385
+ # Download the sample files into ` configure-pod-container/configmap/` directory
386
+ wget https://kubernetes.io/examples/configmap/game-env-file.properties -O configure-pod-container/configmap/game-env-file.properties
387
+ wget https://kubernetes.io/examples/configmap/ui-env-file.properties -O configure-pod-container/configmap/ui-env-file.properties
388
+
389
+ # The env-file `game-env-file.properties` looks like below
390
+ cat configure-pod-container/configmap/game-env-file.properties
391
+ enemies=aliens
392
+ lives=3
393
+ allowed="true"
394
+
395
+ # This comment and the empty line above it are ignored
396
+ ```
397
+ -->
363
398
``` shell
364
399
# Env 文件包含环境变量列表。其中适用以下语法规则:
365
400
# 这些语法规则适用:
@@ -378,7 +413,7 @@ enemies=aliens
378
413
lives=3
379
414
allowed=" true"
380
415
381
- # This comment and the empty line above it are ignored
416
+ # 此注释和上方的空行将被忽略
382
417
```
383
418
384
419
``` shell
@@ -595,13 +630,17 @@ For example, to generate a ConfigMap from files `configure-pod-container/configm
595
630
例如,要基于 `configure-pod-container/configmap/game.properties`
596
631
文件生成一个 ConfigMap:
597
632
633
+ <!--
634
+ # Create a kustomization.yaml file with ConfigMapGenerator
635
+ -->
598
636
` ` ` shell
599
637
# 创建包含 ConfigMapGenerator 的 kustomization.yaml 文件
600
638
cat <<EOF >./kustomization.yaml
601
639
configMapGenerator:
602
640
- name: game-config-4
603
- labels:
604
- game-config: config-4
641
+ options:
642
+ labels:
643
+ game-config: config-4
605
644
files:
606
645
- configure-pod-container/configmap/game.properties
607
646
EOF
@@ -680,13 +719,28 @@ with the key `game-special-key`
680
719
例如,从 ` configure-pod-container/configmap/game.properties ` 文件生成 ConfigMap,
681
720
但使用 ` game-special-key ` 作为键名:
682
721
722
+ <!--
723
+ ```shell
724
+ # Create a kustomization.yaml file with ConfigMapGenerator
725
+ cat <<EOF >./kustomization.yaml
726
+ configMapGenerator:
727
+ - name: game-config-5
728
+ options:
729
+ labels:
730
+ game-config: config-5
731
+ files:
732
+ - game-special-key=configure-pod-container/configmap/game.properties
733
+ EOF
734
+ ```
735
+ -->
683
736
``` shell
684
737
# 创建包含 ConfigMapGenerator 的 kustomization.yaml 文件
685
738
cat << EOF >./kustomization.yaml
686
739
configMapGenerator:
687
740
- name: game-config-5
688
- labels:
689
- game-config: config-5
741
+ options:
742
+ labels:
743
+ game-config: config-5
690
744
files:
691
745
- game-special-key=configure-pod-container/configmap/game.properties
692
746
EOF
@@ -719,6 +773,9 @@ this, you can specify the `ConfigMap` generator. Create (or replace)
719
773
为了实现这一点,你可以配置 ` ConfigMap ` 生成器。
720
774
创建(或替换)` kustomization.yaml ` ,使其具有以下内容。
721
775
776
+ <!--
777
+ # kustomization.yaml contents for creating a ConfigMap from literals
778
+ -->
722
779
``` yaml
723
780
---
724
781
# 基于字面创建 ConfigMap 的 kustomization.yaml 内容
@@ -784,7 +841,8 @@ section, and learn how to use these objects with Pods.
784
841
```
785
842
786
843
<!--
787
- 2. Assign the `special.how` value defined in the ConfigMap to the `SPECIAL_LEVEL_KEY` environment variable in the Pod specification.
844
+ 2. Assign the `special.how` value defined in the ConfigMap to the `SPECIAL_LEVEL_KEY`
845
+ environment variable in the Pod specification.
788
846
-->
789
847
2. 将 ConfigMap 中定义的 `special.how` 赋值给 Pod 规约中的 `SPECIAL_LEVEL_KEY` 环境变量。
790
848
@@ -964,7 +1022,7 @@ kubectl delete pod dapi-test-pod --now
964
1022
# # Add ConfigMap data to a Volume
965
1023
966
1024
As explained in [Create ConfigMaps from files](# create-configmaps-from-files), when you create
967
- a ConfigMap using ` ` --from-file` ` , the filename becomes a key stored in the ` data` section of
1025
+ a ConfigMap using ` --from-file` , the filename becomes a key stored in the ` data` section of
968
1026
the ConfigMap. The file contents become the key' s value.
969
1027
-->
970
1028
## 将 ConfigMap 数据添加到一个卷中 {#add-configmap-data-to-a-volume}
@@ -1027,7 +1085,8 @@ SPECIAL_TYPE
1027
1085
1028
1086
<!--
1029
1087
Text data is exposed as files using the UTF-8 character encoding. To use some other
1030
- character encoding, use `binaryData` (see [ConfigMap object](/docs/concepts/configuration/configmap/#configmap-object) for more details).
1088
+ character encoding, use `binaryData`
1089
+ (see [ConfigMap object](/docs/concepts/configuration/configmap/#configmap-object) for more details).
1031
1090
-->
1032
1091
文本数据会展现为 UTF-8 字符编码的文件。如果使用其他字符编码,
1033
1092
可以使用 `binaryData`(详情参阅 [ConfigMap 对象](/zh-cn/docs/concepts/configuration/configmap/#configmap-object))。
@@ -1117,7 +1176,7 @@ guide explains the syntax.
1117
1176
< ! --
1118
1177
# ## Optional references
1119
1178
1120
- A ConfigMap reference may be marked _optional_. If the ConfigMap is non-existent, the mounted
1179
+ A ConfigMap reference may be marked _optional_. If the ConfigMap is non-existent, the mounted
1121
1180
volume will be empty. If the ConfigMap exists, but the referenced key is non-existent, the path
1122
1181
will be absent beneath the mount point. See [Optional ConfigMaps](# optional-configmaps) for more
1123
1182
details.
@@ -1157,7 +1216,8 @@ Kubelet 在每次定期同步时都会检查所挂载的 ConfigMap 是否是最
1157
1216
1158
1217
{{< note >}}
1159
1218
<!--
1160
- A container using a ConfigMap as a [subPath](/docs/concepts/storage/volumes/#using-subpath) volume will not receive ConfigMap updates.
1219
+ A container using a ConfigMap as a [subPath](/docs/concepts/storage/volumes/#using-subpath)
1220
+ volume will not receive ConfigMap updates.
1161
1221
-->
1162
1222
使用 ConfigMap 作为 [subPath](/zh-cn/docs/concepts/storage/volumes/#using-subpath)
1163
1223
卷的容器将不会收到 ConfigMap 更新。
@@ -1204,6 +1264,25 @@ ConfigMap 的 `data` 字段包含配置数据。如下例所示,它可以简
1204
1264
(如用 `--from-literal` 的单个属性定义)或复杂
1205
1265
(如用 `--from-file` 的配置文件或 JSON blob 定义)。
1206
1266
1267
+ <!--
1268
+ ```yaml
1269
+ apiVersion: v1
1270
+ kind: ConfigMap
1271
+ metadata:
1272
+ creationTimestamp: 2016-02-18T19:14:38Z
1273
+ name: example-config
1274
+ namespace: default
1275
+ data:
1276
+ # example of a simple property defined using --from-literal
1277
+ example.property.1: hello
1278
+ example.property.2: world
1279
+ # example of a complex property defined using --from-file
1280
+ example.property.file: |-
1281
+ property.1=value-1
1282
+ property.2=value-2
1283
+ property.3=value-3
1284
+ ```
1285
+ -->
1207
1286
```yaml
1208
1287
apiVersion: v1
1209
1288
kind: ConfigMap
@@ -1264,6 +1343,9 @@ as optional:
1264
1343
-->
1265
1344
例如,以下 Pod 规约将 ConfigMap 中的环境变量标记为可选:
1266
1345
1346
+ < ! --
1347
+ optional: true # mark the variable as optional
1348
+ -->
1267
1349
` ` ` yaml
1268
1350
apiVersion: v1
1269
1351
kind: Pod
@@ -1305,6 +1387,9 @@ example, the following Pod specification marks a volume that references a Config
1305
1387
此时 Kubernetes 将总是为卷创建挂载路径,即使引用的 ConfigMap 或键不存在。
1306
1388
例如,以下 Pod 规约将所引用得 ConfigMap 的卷标记为可选:
1307
1389
1390
+ < ! --
1391
+ optional: true # mark the source ConfigMap as optional
1392
+ -->
1308
1393
` ` ` yaml
1309
1394
apiVersion: v1
1310
1395
kind: Pod
@@ -1390,6 +1475,9 @@ Delete the ConfigMaps and Pods that you made:
1390
1475
-->
1391
1476
删除你创建那些的 ConfigMap 和 Pod:
1392
1477
1478
+ < ! --
1479
+ # You might already have removed the next set
1480
+ -->
1393
1481
` ` ` bash
1394
1482
kubectl delete configmaps/game-config configmaps/game-config-2 configmaps/game-config-3 \
1395
1483
configmaps/game-config-env-file
0 commit comments