@@ -12,16 +12,17 @@ weight: 20
12
12
<!-- overview -->
13
13
<!--
14
14
This page provides a step-by-step example of updating configuration within a Pod via a ConfigMap
15
- and builds upon the [Configure a Pod to Use a ConfigMap](/docs/tasks/configure-pod-container/configure-pod-configmap/) task.
16
- At the end of this tutorial, you will understand how to change the configuration for a running application.
15
+ and builds upon the [Configure a Pod to Use a ConfigMap](/docs/tasks/configure-pod-container/configure-pod-configmap/) task.
16
+ At the end of this tutorial, you will understand how to change the configuration for a running application.
17
17
This tutorial uses the `alpine` and `nginx` images as examples.
18
18
-->
19
19
本页提供了通过 ConfigMap 更新 Pod 中配置信息的分步示例,
20
- 本教程的前置任务是[ 配置 Pod 以使用 ConfigMap] ( /zh-cn/docs/tasks/configure-pod-container/configure-pod-configmap/ ) 。
20
+ 本教程的前置任务是[ 配置 Pod 以使用 ConfigMap] ( /zh-cn/docs/tasks/configure-pod-container/configure-pod-configmap/ ) 。
21
21
在本教程结束时,你将了解如何变更运行中应用的配置。
22
22
本教程以 ` alpine ` 和 ` nginx ` 镜像为例。
23
23
24
24
## {{% heading "prerequisites" %}}
25
+
25
26
{{< include "task-tutorial-prereqs.md" >}}
26
27
27
28
<!--
@@ -85,7 +86,8 @@ kubectl apply -f https://k8s.io/examples/deployments/deployment-with-configmap-a
85
86
Check the pods for this Deployment to ensure they are ready (matching by
86
87
{{< glossary_tooltip text="selector" term_id="selector" >}}):
87
88
-->
88
- 检查此 Deployment 的 Pod 以确保它们已就绪(通过{{< glossary_tooltip text="选择算符" term_id="selector" >}}进行匹配):
89
+ 检查此 Deployment 的 Pod 以确保它们已就绪
90
+ (通过{{< glossary_tooltip text="选择算符" term_id="selector" >}}进行匹配):
89
91
90
92
``` shell
91
93
kubectl get pods --selector=app.kubernetes.io/name=configmap-volume
@@ -161,8 +163,20 @@ Here's an example of how that manifest could look after you edit it:
161
163
以下是你编辑后该清单可能的样子:
162
164
163
165
<!--
166
+ ```yaml
167
+ apiVersion: v1
168
+ data:
169
+ sport: cricket
170
+ kind: ConfigMap
164
171
# You can leave the existing metadata as they are.
165
172
# The values you'll see won't exactly match these.
173
+ metadata:
174
+ creationTimestamp: "2024-01-04T14:05:06Z"
175
+ name: sport
176
+ namespace: default
177
+ resourceVersion: "1743935"
178
+ uid: 024ee001-fe72-487e-872e-34d6464a8a23
179
+ ```
166
180
-->
167
181
``` yaml
168
182
apiVersion : v1
@@ -213,23 +227,23 @@ Thu Jan 4 14:12:16 UTC 2024 My preferred sport is cricket
213
227
<!--
214
228
When you have a ConfigMap that is mapped into a running Pod using either a
215
229
`configMap` volume or a `projected` volume, and you update that ConfigMap,
216
- the running Pod sees the update almost immediately.
230
+ the running Pod sees the update almost immediately.
217
231
However, your application only sees the change if it is written to either poll for changes,
218
- or watch for file updates.
232
+ or watch for file updates.
219
233
An application that loads its configuration once at startup will not notice a change.
220
234
-->
221
235
当你有一个 ConfigMap 通过 ` configMap ` 卷或 ` projected ` 卷映射到运行中的 Pod,
222
- 并且你更新了该 ConfigMap 时,运行中的 Pod 几乎会立即更新。
223
- 但是,你的应用只有在编写为轮询变更或监视文件更新时才能看到变更。
236
+ 并且你更新了该 ConfigMap 时,运行中的 Pod 几乎会立即更新。
237
+ 但是,你的应用只有在编写为轮询变更或监视文件更新时才能看到变更。
224
238
启动时一次性加载其配置的应用将不会注意到变更。
225
239
226
240
{{< note >}}
227
241
<!--
228
242
The total delay from the moment when the ConfigMap is updated to the moment when
229
- new keys are projected to the Pod can be as long as kubelet sync period.
243
+ new keys are projected to the Pod can be as long as kubelet sync period.
230
244
Also check [Mounted ConfigMaps are updated automatically](/docs/tasks/configure-pod-container/configure-pod-configmap/#mounted-configmaps-are-updated-automatically).
231
245
-->
232
- 从更新 ConfigMap 的那一刻到将新的键投射到 Pod 的那一刻,整个延迟可能与 kubelet 同步周期相同。
246
+ 从更新 ConfigMap 的那一刻到将新的键投射到 Pod 的那一刻,整个延迟可能与 kubelet 同步周期相同。
233
247
另请参阅[ 挂载的 ConfigMap 会被自动更新] ( /zh-cn/docs/tasks/configure-pod-container/configure-pod-configmap/#mounted-configmaps-are-updated-automatically ) 。
234
248
{{< /note >}}
235
249
@@ -331,8 +345,19 @@ Here's an example of how that manifest could look after you edit it:
331
345
以下是你编辑后该清单可能的样子:
332
346
333
347
<!--
348
+ ```yaml
349
+ apiVersion: v1
350
+ data:
351
+ fruits: mangoes
352
+ kind: ConfigMap
334
353
# You can leave the existing metadata as they are.
335
354
# The values you'll see won't exactly match these.
355
+ metadata:
356
+ creationTimestamp: "2024-01-04T16:04:19Z"
357
+ name: fruits
358
+ namespace: default
359
+ resourceVersion: "1749472"
360
+ ```
336
361
-->
337
362
``` yaml
338
363
apiVersion : v1
@@ -632,8 +657,20 @@ Here's an example of how that manifest could look after you edit it:
632
657
以下是你编辑后该清单可能的样子:
633
658
634
659
<!--
660
+ ```yaml
661
+ apiVersion: v1
662
+ data:
663
+ color: blue
664
+ kind: ConfigMap
635
665
# You can leave the existing metadata as they are.
636
666
# The values you'll see won't exactly match these.
667
+ metadata:
668
+ creationTimestamp: "2024-01-05T08:12:05Z"
669
+ name: color
670
+ namespace: configmap
671
+ resourceVersion: "1801272"
672
+ uid: 80d33e4a-cbb4-4bc9-ba8c-544c68e425d6
673
+ ```
637
674
-->
638
675
``` yaml
639
676
apiVersion : v1
@@ -663,7 +700,7 @@ You should see the output change as follows:
663
700
循环访问服务 URL 几秒钟。
664
701
665
702
``` shell
666
- # 当你满意时可以取消此操作 ( Ctrl-C)
703
+ # 当你满意时可以取消此操作( Ctrl-C)
667
704
while true ; do curl --connect-timeout 7.5 http://localhost:8080; sleep 10; done
668
705
```
669
706
@@ -683,24 +720,22 @@ Fri Jan 5 08:15:00 UTC 2024 My preferred color is blue
683
720
## Update configuration via a ConfigMap in a Pod possessing a sidecar container {#rollout-configmap-sidecar}
684
721
685
722
The above scenario can be replicated by using a [Sidecar Container](/docs/concepts/workloads/pods/sidecar-containers/)
686
- as a helper container to write the HTML file.
687
- As a Sidecar Container is conceptually an Init Container, it is guaranteed to start before the main web server container.
688
- This ensures that the HTML file is always available when the web server is ready to serve it.
689
- Please see [Enabling sidecar containers](/docs/concepts/workloads/pods/sidecar-containers/#enabling-sidecar-containers) to utilize this feature.
723
+ as a helper container to write the HTML file.
724
+ As a Sidecar Container is conceptually an Init Container, it is guaranteed to start before the main web server container.
725
+ This ensures that the HTML file is always available when the web server is ready to serve it.
690
726
-->
691
727
## 在包含边车容器的 Pod 中通过 ConfigMap 更新配置 {#rollout-configmap-sidecar}
692
728
693
- 要重现上述场景,可以使用[ 边车容器] ( /zh-cn/docs/concepts/workloads/pods/sidecar-containers/ ) 作为辅助容器来写入 HTML 文件。
694
- 由于边车容器在概念上是一个 Init 容器,因此保证会在主要 Web 服务器容器启动之前启动。
695
- 这确保了当 Web 服务器准备好提供服务时,HTML 文件始终可用。
696
- 请参阅[ 启用边车容器] ( /zh-cn/docs/concepts/workloads/pods/sidecar-containers/#enabling-sidecar-containers ) 以使用此特性。
729
+ 要重现上述场景,可以使用[ 边车容器] ( /zh-cn/docs/concepts/workloads/pods/sidecar-containers/ ) 作为辅助容器来写入
730
+ HTML 文件。由于边车容器在概念上是一个 Init 容器,因此保证会在主要 Web 服务器容器启动之前启动。
731
+ 这确保了当 Web 服务器准备好提供服务时,HTML 文件始终可用。
697
732
698
733
<!--
699
- If you are continuing from the previous scenario, you can reuse the ConfigMap named `color` for this scenario.
734
+ If you are continuing from the previous scenario, you can reuse the ConfigMap named `color` for this scenario.
700
735
If you are executing this scenario independently, use the `kubectl create configmap` command to create a ConfigMap
701
736
from [literal values](/docs/tasks/configure-pod-container/configure-pod-configmap/#create-configmaps-from-literal-values):
702
737
-->
703
- 如果你从前一个场景继续操作,你可以在此场景中重用名为 ` color ` 的 ConfigMap。
738
+ 如果你从前一个场景继续操作,你可以在此场景中重用名为 ` color ` 的 ConfigMap。
704
739
如果你是独立执行此场景,请使用 ` kubectl create configmap `
705
740
命令基于[ 字面值] ( /zh-cn/docs/tasks/configure-pod-container/configure-pod-configmap/#create-configmaps-from-literal-values ) 创建一个
706
741
ConfigMap:
@@ -819,8 +854,20 @@ Here's an example of how that manifest could look after you edit it:
819
854
以下是你编辑后该清单可能的样子:
820
855
821
856
<!--
857
+ ```yaml
858
+ apiVersion: v1
859
+ data:
860
+ color: green
861
+ kind: ConfigMap
822
862
# You can leave the existing metadata as they are.
823
863
# The values you'll see won't exactly match these.
864
+ metadata:
865
+ creationTimestamp: "2024-02-17T12:20:30Z"
866
+ name: color
867
+ namespace: default
868
+ resourceVersion: "1054"
869
+ uid: e40bb34c-58df-4280-8bea-6ed16edccfaa
870
+ ```
824
871
-->
825
872
``` yaml
826
873
apiVersion : v1
@@ -979,12 +1026,12 @@ Wed Mar 20 03:52:54 UTC 2024 The name of the company is ACME, Inc.
979
1026
{{< note >}}
980
1027
<!--
981
1028
Once a ConfigMap is marked as immutable, it is not possible to revert this change
982
- nor to mutate the contents of the data or the binaryData field.
1029
+ nor to mutate the contents of the data or the binaryData field.
983
1030
In order to modify the behavior of the Pods that use this configuration,
984
1031
you will create a new immutable ConfigMap and edit the Deployment
985
1032
to define a slightly different pod template, referencing the new ConfigMap.
986
1033
-->
987
- 一旦 ConfigMap 被标记为不可变,就无法撤销此变更,也无法修改 ` data ` 或 ` binaryData ` 字段的内容。
1034
+ 一旦 ConfigMap 被标记为不可变,就无法撤销此变更,也无法修改 ` data ` 或 ` binaryData ` 字段的内容。
988
1035
为了修改使用此配置的 Pod 的行为,你需要创建一个新的不可变 ConfigMap,并编辑 Deployment
989
1036
以定义一个稍有不同的 Pod 模板,引用新的 ConfigMap。
990
1037
{{< /note >}}
@@ -1180,7 +1227,13 @@ Delete the resources created during the tutorial:
1180
1227
删除以上教程中所创建的资源:
1181
1228
1182
1229
<!--
1183
- # In case it was not handled during the task execution
1230
+ ```shell
1231
+ kubectl delete deployment configmap-volume configmap-env-var configmap-two-containers configmap-sidecar-container immutable-configmap-volume
1232
+ kubectl delete service configmap-service configmap-sidecar-service
1233
+ kubectl delete configmap sport fruits color company-name-20240312
1234
+
1235
+ kubectl delete configmap company-name-20150801 # In case it was not handled during the task execution
1236
+ ```
1184
1237
-->
1185
1238
``` shell
1186
1239
kubectl delete deployment configmap-volume configmap-env-var configmap-two-containers configmap-sidecar-container immutable-configmap-volume
0 commit comments