@@ -12,16 +12,17 @@ weight: 20
1212<!-- overview -->
1313<!--
1414This 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.
1717This tutorial uses the `alpine` and `nginx` images as examples.
1818-->
1919本页提供了通过 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/ ) 。
2121在本教程结束时,你将了解如何变更运行中应用的配置。
2222本教程以 ` alpine ` 和 ` nginx ` 镜像为例。
2323
2424## {{% heading "prerequisites" %}}
25+
2526{{< include "task-tutorial-prereqs.md" >}}
2627
2728<!--
@@ -85,7 +86,8 @@ kubectl apply -f https://k8s.io/examples/deployments/deployment-with-configmap-a
8586Check the pods for this Deployment to ensure they are ready (matching by
8687{{< glossary_tooltip text="selector" term_id="selector" >}}):
8788-->
88- 检查此 Deployment 的 Pod 以确保它们已就绪(通过{{< glossary_tooltip text="选择算符" term_id="selector" >}}进行匹配):
89+ 检查此 Deployment 的 Pod 以确保它们已就绪
90+ (通过{{< glossary_tooltip text="选择算符" term_id="selector" >}}进行匹配):
8991
9092``` shell
9193kubectl 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:
161163以下是你编辑后该清单可能的样子:
162164
163165<!--
166+ ```yaml
167+ apiVersion: v1
168+ data:
169+ sport: cricket
170+ kind: ConfigMap
164171# You can leave the existing metadata as they are.
165172# 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+ ```
166180-->
167181``` yaml
168182apiVersion : v1
@@ -213,23 +227,23 @@ Thu Jan 4 14:12:16 UTC 2024 My preferred sport is cricket
213227<!--
214228When you have a ConfigMap that is mapped into a running Pod using either a
215229`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.
217231However, 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.
219233An application that loads its configuration once at startup will not notice a change.
220234-->
221235当你有一个 ConfigMap 通过 ` configMap ` 卷或 ` projected ` 卷映射到运行中的 Pod,
222- 并且你更新了该 ConfigMap 时,运行中的 Pod 几乎会立即更新。
223- 但是,你的应用只有在编写为轮询变更或监视文件更新时才能看到变更。
236+ 并且你更新了该 ConfigMap 时,运行中的 Pod 几乎会立即更新。
237+ 但是,你的应用只有在编写为轮询变更或监视文件更新时才能看到变更。
224238启动时一次性加载其配置的应用将不会注意到变更。
225239
226240{{< note >}}
227241<!--
228242The 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.
230244Also check [Mounted ConfigMaps are updated automatically](/docs/tasks/configure-pod-container/configure-pod-configmap/#mounted-configmaps-are-updated-automatically).
231245-->
232- 从更新 ConfigMap 的那一刻到将新的键投射到 Pod 的那一刻,整个延迟可能与 kubelet 同步周期相同。
246+ 从更新 ConfigMap 的那一刻到将新的键投射到 Pod 的那一刻,整个延迟可能与 kubelet 同步周期相同。
233247另请参阅[ 挂载的 ConfigMap 会被自动更新] ( /zh-cn/docs/tasks/configure-pod-container/configure-pod-configmap/#mounted-configmaps-are-updated-automatically ) 。
234248{{< /note >}}
235249
@@ -331,8 +345,19 @@ Here's an example of how that manifest could look after you edit it:
331345以下是你编辑后该清单可能的样子:
332346
333347<!--
348+ ```yaml
349+ apiVersion: v1
350+ data:
351+ fruits: mangoes
352+ kind: ConfigMap
334353# You can leave the existing metadata as they are.
335354# 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+ ```
336361-->
337362``` yaml
338363apiVersion : v1
@@ -632,8 +657,20 @@ Here's an example of how that manifest could look after you edit it:
632657以下是你编辑后该清单可能的样子:
633658
634659<!--
660+ ```yaml
661+ apiVersion: v1
662+ data:
663+ color: blue
664+ kind: ConfigMap
635665# You can leave the existing metadata as they are.
636666# 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+ ```
637674-->
638675``` yaml
639676apiVersion : v1
@@ -663,7 +700,7 @@ You should see the output change as follows:
663700循环访问服务 URL 几秒钟。
664701
665702``` shell
666- # 当你满意时可以取消此操作 ( Ctrl-C)
703+ # 当你满意时可以取消此操作( Ctrl-C)
667704while true ; do curl --connect-timeout 7.5 http://localhost:8080; sleep 10; done
668705```
669706
@@ -683,24 +720,22 @@ Fri Jan 5 08:15:00 UTC 2024 My preferred color is blue
683720## Update configuration via a ConfigMap in a Pod possessing a sidecar container {#rollout-configmap-sidecar}
684721
685722The 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.
690726-->
691727## 在包含边车容器的 Pod 中通过 ConfigMap 更新配置 {#rollout-configmap-sidecar}
692728
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 文件始终可用。
697732
698733<!--
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.
700735If you are executing this scenario independently, use the `kubectl create configmap` command to create a ConfigMap
701736from [literal values](/docs/tasks/configure-pod-container/configure-pod-configmap/#create-configmaps-from-literal-values):
702737-->
703- 如果你从前一个场景继续操作,你可以在此场景中重用名为 ` color ` 的 ConfigMap。
738+ 如果你从前一个场景继续操作,你可以在此场景中重用名为 ` color ` 的 ConfigMap。
704739如果你是独立执行此场景,请使用 ` kubectl create configmap `
705740命令基于[ 字面值] ( /zh-cn/docs/tasks/configure-pod-container/configure-pod-configmap/#create-configmaps-from-literal-values ) 创建一个
706741ConfigMap:
@@ -819,8 +854,20 @@ Here's an example of how that manifest could look after you edit it:
819854以下是你编辑后该清单可能的样子:
820855
821856<!--
857+ ```yaml
858+ apiVersion: v1
859+ data:
860+ color: green
861+ kind: ConfigMap
822862# You can leave the existing metadata as they are.
823863# 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+ ```
824871-->
825872``` yaml
826873apiVersion : v1
@@ -979,12 +1026,12 @@ Wed Mar 20 03:52:54 UTC 2024 The name of the company is ACME, Inc.
9791026{{< note >}}
9801027<!--
9811028Once 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.
9831030In order to modify the behavior of the Pods that use this configuration,
9841031you will create a new immutable ConfigMap and edit the Deployment
9851032to define a slightly different pod template, referencing the new ConfigMap.
9861033-->
987- 一旦 ConfigMap 被标记为不可变,就无法撤销此变更,也无法修改 ` data ` 或 ` binaryData ` 字段的内容。
1034+ 一旦 ConfigMap 被标记为不可变,就无法撤销此变更,也无法修改 ` data ` 或 ` binaryData ` 字段的内容。
9881035为了修改使用此配置的 Pod 的行为,你需要创建一个新的不可变 ConfigMap,并编辑 Deployment
9891036以定义一个稍有不同的 Pod 模板,引用新的 ConfigMap。
9901037{{< /note >}}
@@ -1180,7 +1227,13 @@ Delete the resources created during the tutorial:
11801227删除以上教程中所创建的资源:
11811228
11821229<!--
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+ ```
11841237-->
11851238``` shell
11861239kubectl delete deployment configmap-volume configmap-env-var configmap-two-containers configmap-sidecar-container immutable-configmap-volume
0 commit comments