通过 ConfigMap 更新配置
此页面提供了一个逐步更新 Pod 内配置的示例,该示例通过 ConfigMap 更新配置,并基于 配置 Pod 以使用 ConfigMap 任务。
在本教程结束时,您将了解如何更改正在运行的应用程序的配置。
本教程使用 alpine
和 nginx
镜像作为示例。
开始之前
您需要有一个 Kubernetes 集群,并且 kubectl 命令行工具必须配置为与您的集群通信。建议在至少有两个节点的集群上运行本教程,这些节点不充当控制平面主机。如果您还没有集群,可以使用 minikube 创建一个集群,或者可以使用以下 Kubernetes 游乐场之一
您需要使用 curl 命令行工具从终端或命令提示符发出 HTTP 请求。如果您没有 curl
可用,可以安装它。查看您本地操作系统的文档。
目标
- 通过作为卷挂载的 ConfigMap 更新配置
- 通过 ConfigMap 更新 Pod 的环境变量
- 通过 ConfigMap 更新多容器 Pod 中的配置
- 通过 ConfigMap 更新具有 Sidecar 容器的 Pod 中的配置
通过作为卷挂载的 ConfigMap 更新配置
使用 kubectl create configmap
命令从 字面值 创建 ConfigMap
kubectl create configmap sport --from-literal=sport=football
以下是一个 Deployment 清单示例,其中 ConfigMap sport
作为 卷 挂载到 Pod 的唯一容器中。
apiVersion: apps/v1
kind: Deployment
metadata:
name: configmap-volume
labels:
app.kubernetes.io/name: configmap-volume
spec:
replicas: 3
selector:
matchLabels:
app.kubernetes.io/name: configmap-volume
template:
metadata:
labels:
app.kubernetes.io/name: configmap-volume
spec:
containers:
- name: alpine
image: alpine:3
command:
- /bin/sh
- -c
- while true; do echo "$(date) My preferred sport is $(cat /etc/config/sport)";
sleep 10; done;
ports:
- containerPort: 80
volumeMounts:
- name: config-volume
mountPath: /etc/config
volumes:
- name: config-volume
configMap:
name: sport
创建 Deployment
kubectl apply -f https://k8s.io/examples/deployments/deployment-with-configmap-as-volume.yaml
检查此 Deployment 的 Pod,以确保它们已准备就绪(通过 选择器 匹配)
kubectl get pods --selector=app.kubernetes.io/name=configmap-volume
您应该看到类似以下内容的输出
NAME READY STATUS RESTARTS AGE
configmap-volume-6b976dfdcf-qxvbm 1/1 Running 0 72s
configmap-volume-6b976dfdcf-skpvm 1/1 Running 0 72s
configmap-volume-6b976dfdcf-tbc6r 1/1 Running 0 72s
在运行这些 Pod 之一的每个节点上,kubelet 会获取该 ConfigMap 的数据,并将其转换为本地卷中的文件。然后,kubelet 会根据 Pod 模板中指定的配置将该卷挂载到容器中。在该容器中运行的代码从文件中加载信息,并使用它将报告打印到标准输出。您可以通过查看该 Deployment 中的某个 Pod 的日志来检查此报告
# Pick one Pod that belongs to the Deployment, and view its logs
kubectl logs deployments/configmap-volume
您应该看到类似以下内容的输出
Found 3 pods, using pod/configmap-volume-76d9c5678f-x5rgj
Thu Jan 4 14:06:46 UTC 2024 My preferred sport is football
Thu Jan 4 14:06:56 UTC 2024 My preferred sport is football
Thu Jan 4 14:07:06 UTC 2024 My preferred sport is football
Thu Jan 4 14:07:16 UTC 2024 My preferred sport is football
Thu Jan 4 14:07:26 UTC 2024 My preferred sport is football
编辑 ConfigMap
kubectl edit configmap sport
在出现的编辑器中,将键 sport
的值从 football
更改为 cricket
。保存您的更改。kubectl 工具会相应地更新 ConfigMap(如果您看到错误,请重试)。
以下是如何在您编辑它之后该清单可能看起来的示例
apiVersion: v1
data:
sport: cricket
kind: ConfigMap
# You can leave the existing metadata as they are.
# The values you'll see won't exactly match these.
metadata:
creationTimestamp: "2024-01-04T14:05:06Z"
name: sport
namespace: default
resourceVersion: "1743935"
uid: 024ee001-fe72-487e-872e-34d6464a8a23
您应该看到以下输出
configmap/sport edited
尾部(跟踪此 Deployment 所属的某个 Pod 中的最新条目)日志
kubectl logs deployments/configmap-volume --follow
几秒钟后,您应该看到日志输出更改如下
Thu Jan 4 14:11:36 UTC 2024 My preferred sport is football
Thu Jan 4 14:11:46 UTC 2024 My preferred sport is football
Thu Jan 4 14:11:56 UTC 2024 My preferred sport is football
Thu Jan 4 14:12:06 UTC 2024 My preferred sport is cricket
Thu Jan 4 14:12:16 UTC 2024 My preferred sport is cricket
当您有一个 ConfigMap 通过 configMap
卷或 projected
卷映射到正在运行的 Pod 中,并且您更新了该 ConfigMap 时,正在运行的 Pod 几乎会立即看到更新。
但是,您的应用程序只有在编写为轮询更改或监视文件更新时才会看到更改。
在启动时仅加载一次配置的应用程序不会注意到更改。
通过 ConfigMap 更新 Pod 的环境变量
使用 kubectl create configmap
命令从 字面值 创建 ConfigMap
kubectl create configmap fruits --from-literal=fruits=apples
以下是一个 Deployment 清单示例,其中通过 ConfigMap fruits
配置了环境变量。
apiVersion: apps/v1
kind: Deployment
metadata:
name: configmap-env-var
labels:
app.kubernetes.io/name: configmap-env-var
spec:
replicas: 3
selector:
matchLabels:
app.kubernetes.io/name: configmap-env-var
template:
metadata:
labels:
app.kubernetes.io/name: configmap-env-var
spec:
containers:
- name: alpine
image: alpine:3
env:
- name: FRUITS
valueFrom:
configMapKeyRef:
key: fruits
name: fruits
command:
- /bin/sh
- -c
- while true; do echo "$(date) The basket is full of $FRUITS";
sleep 10; done;
ports:
- containerPort: 80
创建 Deployment
kubectl apply -f https://k8s.io/examples/deployments/deployment-with-configmap-as-envvar.yaml
检查此 Deployment 的 Pod,以确保它们已准备就绪(通过 选择器 匹配)
kubectl get pods --selector=app.kubernetes.io/name=configmap-env-var
您应该看到类似以下内容的输出
NAME READY STATUS RESTARTS AGE
configmap-env-var-59cfc64f7d-74d7z 1/1 Running 0 46s
configmap-env-var-59cfc64f7d-c4wmj 1/1 Running 0 46s
configmap-env-var-59cfc64f7d-dpr98 1/1 Running 0 46s
ConfigMap 中的键值对配置为 Pod 容器中的环境变量。通过查看属于 Deployment 的某个 Pod 的日志来检查这一点。
kubectl logs deployment/configmap-env-var
您应该看到类似以下内容的输出
Found 3 pods, using pod/configmap-env-var-7c994f7769-l74nq
Thu Jan 4 16:07:06 UTC 2024 The basket is full of apples
Thu Jan 4 16:07:16 UTC 2024 The basket is full of apples
Thu Jan 4 16:07:26 UTC 2024 The basket is full of apples
编辑 ConfigMap
kubectl edit configmap fruits
在出现的编辑器中,将键 fruits
的值从 apples
更改为 mangoes
。保存您的更改。kubectl 工具会相应地更新 ConfigMap(如果您看到错误,请重试)。
以下是如何在您编辑它之后该清单可能看起来的示例
apiVersion: v1
data:
fruits: mangoes
kind: ConfigMap
# You can leave the existing metadata as they are.
# The values you'll see won't exactly match these.
metadata:
creationTimestamp: "2024-01-04T16:04:19Z"
name: fruits
namespace: default
resourceVersion: "1749472"
您应该看到以下输出
configmap/fruits edited
尾部跟踪 Deployment 的日志,并观察几秒钟的输出
# As the text explains, the output does NOT change
kubectl logs deployments/configmap-env-var --follow
请注意,即使您编辑了 ConfigMap,输出也保持不变
Thu Jan 4 16:12:56 UTC 2024 The basket is full of apples
Thu Jan 4 16:13:06 UTC 2024 The basket is full of apples
Thu Jan 4 16:13:16 UTC 2024 The basket is full of apples
Thu Jan 4 16:13:26 UTC 2024 The basket is full of apples
注意
尽管 ConfigMap 中的键值已更改,但 Pod 中的环境变量仍显示之前的价值。这是因为 Pod 内运行的进程的环境变量在源数据更改时不会更新;如果您想强制更新,则需要让 Kubernetes 替换您现有的 Pod。然后,新的 Pod 将使用更新的信息运行。您可以触发该替换。使用 kubectl rollout
为 Deployment 执行推出
# Trigger the rollout
kubectl rollout restart deployment configmap-env-var
# Wait for the rollout to complete
kubectl rollout status deployment configmap-env-var --watch=true
接下来,检查 Deployment
kubectl get deployment configmap-env-var
您应该看到类似以下内容的输出
NAME READY UP-TO-DATE AVAILABLE AGE
configmap-env-var 3/3 3 3 12m
检查 Pod
kubectl get pods --selector=app.kubernetes.io/name=configmap-env-var
推出会导致 Kubernetes 为 Deployment 创建一个新的 ReplicaSet;这意味着现有的 Pod 最终会终止,并且会创建新的 Pod。几秒钟后,您应该看到类似以下内容的输出
NAME READY STATUS RESTARTS AGE
configmap-env-var-6d94d89bf5-2ph2l 1/1 Running 0 13s
configmap-env-var-6d94d89bf5-74twx 1/1 Running 0 8s
configmap-env-var-6d94d89bf5-d5vx8 1/1 Running 0 11s
注意
请等待较旧的 Pod 完全终止,然后再继续执行下一步。查看此 Deployment 中的某个 Pod 的日志
# Pick one Pod that belongs to the Deployment, and view its logs
kubectl logs deployment/configmap-env-var
您应该看到类似以下内容的输出
Found 3 pods, using pod/configmap-env-var-6d9ff89fb6-bzcf6
Thu Jan 4 16:30:35 UTC 2024 The basket is full of mangoes
Thu Jan 4 16:30:45 UTC 2024 The basket is full of mangoes
Thu Jan 4 16:30:55 UTC 2024 The basket is full of mangoes
这演示了更新从 ConfigMap 派生的 Pod 中的环境变量的场景。ConfigMap 值的更改将在后续推出期间应用于 Pod。如果由于其他原因(例如扩展 Deployment)而创建了 Pod,则新的 Pod 也将使用最新的配置值;如果您没有触发推出,那么您可能会发现您的应用程序正在使用旧的和新的环境变量值的混合。
通过 ConfigMap 更新多容器 Pod 中的配置
使用 kubectl create configmap
命令从 字面值 创建 ConfigMap
kubectl create configmap color --from-literal=color=red
以下是管理一组 Pod 的 Deployment 的示例清单,每个 Pod 包含两个容器。这两个容器共享一个 `emptyDir` 卷,用于它们之间的通信。第一个容器运行一个 Web 服务器(`nginx`)。Web 服务器容器中共享卷的挂载路径为 `/usr/share/nginx/html`。第二个辅助容器基于 `alpine`,对于此容器,`emptyDir` 卷挂载在 `/pod-data` 上。辅助容器会写入一个 HTML 文件,其内容基于 ConfigMap。Web 服务器容器通过 HTTP 提供 HTML 服务。
apiVersion: apps/v1
kind: Deployment
metadata:
name: configmap-two-containers
labels:
app.kubernetes.io/name: configmap-two-containers
spec:
replicas: 3
selector:
matchLabels:
app.kubernetes.io/name: configmap-two-containers
template:
metadata:
labels:
app.kubernetes.io/name: configmap-two-containers
spec:
volumes:
- name: shared-data
emptyDir: {}
- name: config-volume
configMap:
name: color
containers:
- name: nginx
image: nginx
volumeMounts:
- name: shared-data
mountPath: /usr/share/nginx/html
- name: alpine
image: alpine:3
volumeMounts:
- name: shared-data
mountPath: /pod-data
- name: config-volume
mountPath: /etc/config
command:
- /bin/sh
- -c
- while true; do echo "$(date) My preferred color is $(cat /etc/config/color)" > /pod-data/index.html;
sleep 10; done;
创建 Deployment
kubectl apply -f https://k8s.io/examples/deployments/deployment-with-configmap-two-containers.yaml
检查此 Deployment 的 Pod,以确保它们已准备就绪(通过 选择器 匹配)
kubectl get pods --selector=app.kubernetes.io/name=configmap-two-containers
您应该看到类似以下内容的输出
NAME READY STATUS RESTARTS AGE
configmap-two-containers-565fb6d4f4-2xhxf 2/2 Running 0 20s
configmap-two-containers-565fb6d4f4-g5v4j 2/2 Running 0 20s
configmap-two-containers-565fb6d4f4-mzsmf 2/2 Running 0 20s
公开 Deployment(`kubectl` 工具会为您创建一个 服务)
kubectl expose deployment configmap-two-containers --name=configmap-service --port=8080 --target-port=80
使用 `kubectl` 转发端口
kubectl port-forward service/configmap-service 8080:8080 & # this stays running in the background
访问服务。
curl http://localhost:8080
您应该看到类似以下内容的输出
Fri Jan 5 08:08:22 UTC 2024 My preferred color is red
编辑 ConfigMap
kubectl edit configmap color
在出现的编辑器中,将键 `color` 的值从 `red` 更改为 `blue`。保存更改。`kubectl` 工具会相应地更新 ConfigMap(如果出现错误,请重试)。
以下是如何在您编辑它之后该清单可能看起来的示例
apiVersion: v1
data:
color: blue
kind: ConfigMap
# You can leave the existing metadata as they are.
# The values you'll see won't exactly match these.
metadata:
creationTimestamp: "2024-01-05T08:12:05Z"
name: color
namespace: configmap
resourceVersion: "1801272"
uid: 80d33e4a-cbb4-4bc9-ba8c-544c68e425d6
循环访问服务 URL 几秒钟。
# Cancel this when you're happy with it (Ctrl-C)
while true; do curl --connect-timeout 7.5 http://localhost:8080; sleep 10; done
您应该看到输出更改如下
Fri Jan 5 08:14:00 UTC 2024 My preferred color is red
Fri Jan 5 08:14:02 UTC 2024 My preferred color is red
Fri Jan 5 08:14:20 UTC 2024 My preferred color is red
Fri Jan 5 08:14:22 UTC 2024 My preferred color is red
Fri Jan 5 08:14:32 UTC 2024 My preferred color is blue
Fri Jan 5 08:14:43 UTC 2024 My preferred color is blue
Fri Jan 5 08:15:00 UTC 2024 My preferred color is blue
通过拥有辅助容器的 Pod 中的 ConfigMap 更新配置
上述场景可以通过使用 辅助容器 作为辅助容器来写入 HTML 文件来复制。
由于辅助容器在概念上是一个 Init 容器,因此它保证在主 Web 服务器容器之前启动。
这确保了 HTML 文件在 Web 服务器准备好提供服务时始终可用。
请参阅 启用辅助容器 以利用此功能。
如果您从上一个场景继续,则可以重复使用名为 `color` 的 ConfigMap 来进行此场景。
如果您独立执行此场景,请使用 `kubectl create configmap` 命令从 字面值 创建 ConfigMap
kubectl create configmap color --from-literal=color=blue
以下是管理一组 Pod 的 Deployment 的示例清单,每个 Pod 包含一个主容器和一个辅助容器。这两个容器共享一个 `emptyDir` 卷,用于它们之间的通信。主容器运行一个 Web 服务器(NGINX)。Web 服务器容器中共享卷的挂载路径为 `/usr/share/nginx/html`。第二个容器是一个基于 Alpine Linux 的辅助容器,充当辅助容器。对于此容器,`emptyDir` 卷挂载在 `/pod-data` 上。辅助容器会写入一个 HTML 文件,其内容基于 ConfigMap。Web 服务器容器通过 HTTP 提供 HTML 服务。
apiVersion: apps/v1
kind: Deployment
metadata:
name: configmap-sidecar-container
labels:
app.kubernetes.io/name: configmap-sidecar-container
spec:
replicas: 3
selector:
matchLabels:
app.kubernetes.io/name: configmap-sidecar-container
template:
metadata:
labels:
app.kubernetes.io/name: configmap-sidecar-container
spec:
volumes:
- name: shared-data
emptyDir: {}
- name: config-volume
configMap:
name: color
containers:
- name: nginx
image: nginx
volumeMounts:
- name: shared-data
mountPath: /usr/share/nginx/html
initContainers:
- name: alpine
image: alpine:3
restartPolicy: Always
volumeMounts:
- name: shared-data
mountPath: /pod-data
- name: config-volume
mountPath: /etc/config
command:
- /bin/sh
- -c
- while true; do echo "$(date) My preferred color is $(cat /etc/config/color)" > /pod-data/index.html;
sleep 10; done;
创建 Deployment
kubectl apply -f https://k8s.io/examples/deployments/deployment-with-configmap-and-sidecar-container.yaml
检查此 Deployment 的 Pod,以确保它们已准备就绪(通过 选择器 匹配)
kubectl get pods --selector=app.kubernetes.io/name=configmap-sidecar-container
您应该看到类似以下内容的输出
NAME READY STATUS RESTARTS AGE
configmap-sidecar-container-5fb59f558b-87rp7 2/2 Running 0 94s
configmap-sidecar-container-5fb59f558b-ccs7s 2/2 Running 0 94s
configmap-sidecar-container-5fb59f558b-wnmgk 2/2 Running 0 94s
公开 Deployment(`kubectl` 工具会为您创建一个 服务)
kubectl expose deployment configmap-sidecar-container --name=configmap-sidecar-service --port=8081 --target-port=80
使用 `kubectl` 转发端口
kubectl port-forward service/configmap-sidecar-service 8081:8081 & # this stays running in the background
访问服务。
curl http://localhost:8081
您应该看到类似以下内容的输出
Sat Feb 17 13:09:05 UTC 2024 My preferred color is blue
编辑 ConfigMap
kubectl edit configmap color
在出现的编辑器中,将键 `color` 的值从 `blue` 更改为 `green`。保存更改。`kubectl` 工具会相应地更新 ConfigMap(如果出现错误,请重试)。
以下是如何在您编辑它之后该清单可能看起来的示例
apiVersion: v1
data:
color: green
kind: ConfigMap
# You can leave the existing metadata as they are.
# The values you'll see won't exactly match these.
metadata:
creationTimestamp: "2024-02-17T12:20:30Z"
name: color
namespace: default
resourceVersion: "1054"
uid: e40bb34c-58df-4280-8bea-6ed16edccfaa
循环访问服务 URL 几秒钟。
# Cancel this when you're happy with it (Ctrl-C)
while true; do curl --connect-timeout 7.5 http://localhost:8081; sleep 10; done
您应该看到输出更改如下
Sat Feb 17 13:12:35 UTC 2024 My preferred color is blue
Sat Feb 17 13:12:45 UTC 2024 My preferred color is blue
Sat Feb 17 13:12:55 UTC 2024 My preferred color is blue
Sat Feb 17 13:13:05 UTC 2024 My preferred color is blue
Sat Feb 17 13:13:15 UTC 2024 My preferred color is green
Sat Feb 17 13:13:25 UTC 2024 My preferred color is green
Sat Feb 17 13:13:35 UTC 2024 My preferred color is green
通过作为卷挂载的不可变 ConfigMap 更新配置
注意
不可变 ConfigMap 特别适用于始终保持不变且 **不** 预计会随时间变化的配置。将 ConfigMap 标记为不可变可以提高性能,因为 kubelet 不会监视更改。
如果您确实需要进行更改,则应计划:
- 更改 ConfigMap 的名称,并切换到运行引用新名称的 Pod
- 替换集群中所有以前运行过使用旧值的 Pod 的节点
- 重新启动 kubelet 在任何以前加载过旧 ConfigMap 的节点上
以下是 不可变 ConfigMap 的示例清单。
apiVersion: v1
data:
company_name: "ACME, Inc." # existing fictional company name
kind: ConfigMap
immutable: true
metadata:
name: company-name-20150801
创建不可变 ConfigMap
kubectl apply -f https://k8s.io/examples/configmap/immutable-configmap.yaml
以下是一个 Deployment 清单的示例,其中不可变 ConfigMap `company-name-20150801` 作为 卷 挂载到 Pod 的唯一容器中。
apiVersion: apps/v1
kind: Deployment
metadata:
name: immutable-configmap-volume
labels:
app.kubernetes.io/name: immutable-configmap-volume
spec:
replicas: 3
selector:
matchLabels:
app.kubernetes.io/name: immutable-configmap-volume
template:
metadata:
labels:
app.kubernetes.io/name: immutable-configmap-volume
spec:
containers:
- name: alpine
image: alpine:3
command:
- /bin/sh
- -c
- while true; do echo "$(date) The name of the company is $(cat /etc/config/company_name)";
sleep 10; done;
ports:
- containerPort: 80
volumeMounts:
- name: config-volume
mountPath: /etc/config
volumes:
- name: config-volume
configMap:
name: company-name-20150801
创建 Deployment
kubectl apply -f https://k8s.io/examples/deployments/deployment-with-immutable-configmap-as-volume.yaml
检查此 Deployment 的 Pod,以确保它们已准备就绪(通过 选择器 匹配)
kubectl get pods --selector=app.kubernetes.io/name=immutable-configmap-volume
您应该看到类似以下内容的输出
NAME READY STATUS RESTARTS AGE
immutable-configmap-volume-78b6fbff95-5gsfh 1/1 Running 0 62s
immutable-configmap-volume-78b6fbff95-7vcj4 1/1 Running 0 62s
immutable-configmap-volume-78b6fbff95-vdslm 1/1 Running 0 62s
Pod 的容器引用 ConfigMap 中定义的数据,并使用它将报告打印到标准输出。您可以通过查看该 Deployment 中的某个 Pod 的日志来查看此报告
# Pick one Pod that belongs to the Deployment, and view its logs
kubectl logs deployments/immutable-configmap-volume
您应该看到类似以下内容的输出
Found 3 pods, using pod/immutable-configmap-volume-78b6fbff95-5gsfh
Wed Mar 20 03:52:34 UTC 2024 The name of the company is ACME, Inc.
Wed Mar 20 03:52:44 UTC 2024 The name of the company is ACME, Inc.
Wed Mar 20 03:52:54 UTC 2024 The name of the company is ACME, Inc.
注意
一旦 ConfigMap 被标记为不可变,就不可能撤消此更改,也不能修改 `data` 或 `binaryData` 字段的内容。为了修改使用此配置的 Pod 的行为,您将创建一个新的不可变 ConfigMap,并编辑 Deployment 以定义一个略微不同的 Pod 模板,引用新的 ConfigMap。
使用下面显示的清单创建一个新的不可变 ConfigMap
apiVersion: v1
data:
company_name: "Fiktivesunternehmen GmbH" # new fictional company name
kind: ConfigMap
immutable: true
metadata:
name: company-name-20240312
kubectl apply -f https://k8s.io/examples/configmap/new-immutable-configmap.yaml
您应该看到类似以下内容的输出
configmap/company-name-20240312 created
检查新创建的 ConfigMap
kubectl get configmap
您应该看到一个显示旧和新 ConfigMap 的输出
NAME DATA AGE
company-name-20150801 1 22m
company-name-20240312 1 24s
修改 Deployment 以引用新的 ConfigMap。
编辑 Deployment
kubectl edit deployment immutable-configmap-volume
在出现的编辑器中,更新现有的卷定义以使用新的 ConfigMap。
volumes:
- configMap:
defaultMode: 420
name: company-name-20240312 # Update this field
name: config-volume
您应该看到以下输出
deployment.apps/immutable-configmap-volume edited
这将触发滚动更新。等待所有以前的 Pod 终止,新的 Pod 进入就绪状态。
监视 Pod 的状态
kubectl get pods --selector=app.kubernetes.io/name=immutable-configmap-volume
NAME READY STATUS RESTARTS AGE
immutable-configmap-volume-5fdb88fcc8-29v8n 1/1 Running 0 13s
immutable-configmap-volume-5fdb88fcc8-52ddd 1/1 Running 0 14s
immutable-configmap-volume-5fdb88fcc8-n5jx4 1/1 Running 0 15s
immutable-configmap-volume-78b6fbff95-5gsfh 1/1 Terminating 0 32m
immutable-configmap-volume-78b6fbff95-7vcj4 1/1 Terminating 0 32m
immutable-configmap-volume-78b6fbff95-vdslm 1/1 Terminating 0 32m
您最终应该看到类似于以下内容的输出
NAME READY STATUS RESTARTS AGE
immutable-configmap-volume-5fdb88fcc8-29v8n 1/1 Running 0 43s
immutable-configmap-volume-5fdb88fcc8-52ddd 1/1 Running 0 44s
immutable-configmap-volume-5fdb88fcc8-n5jx4 1/1 Running 0 45s
查看此 Deployment 中的某个 Pod 的日志
# Pick one Pod that belongs to the Deployment, and view its logs
kubectl logs deployment/immutable-configmap-volume
您应该看到类似以下内容的输出
Found 3 pods, using pod/immutable-configmap-volume-5fdb88fcc8-n5jx4
Wed Mar 20 04:24:17 UTC 2024 The name of the company is Fiktivesunternehmen GmbH
Wed Mar 20 04:24:27 UTC 2024 The name of the company is Fiktivesunternehmen GmbH
Wed Mar 20 04:24:37 UTC 2024 The name of the company is Fiktivesunternehmen GmbH
一旦所有部署都迁移到使用新的不可变 ConfigMap,建议删除旧的 ConfigMap。
kubectl delete configmap company-name-20150801
总结
对作为 Pod 上的卷挂载的 ConfigMap 的更改将在随后的 kubelet 同步后无缝可用。
对配置 Pod 环境变量的 ConfigMap 的更改将在 Pod 的后续滚动更新后可用。
一旦 ConfigMap 被标记为不可变,就不可能撤消此更改(您无法将不可变 ConfigMap 设为可变),并且您也不能对 `data` 或 `binaryData` 字段的内容进行任何更改。您可以删除并重新创建 ConfigMap,或者您可以创建一个新的不同的 ConfigMap。当您删除 ConfigMap 时,正在运行的容器及其 Pod 会保留对引用该现有 ConfigMap 的任何卷的挂载点。
清理
终止 `kubectl port-forward` 命令(如果它们正在运行)。
删除本教程期间创建的资源
kubectl delete deployment configmap-volume configmap-env-var configmap-two-containers configmap-sidecar-container immutable-configmap-volume
kubectl delete service configmap-service configmap-sidecar-service
kubectl delete configmap sport fruits color company-name-20240312
kubectl delete configmap company-name-20150801 # In case it was not handled during the task execution