通过 ConfigMap 更新配置
本页面通过一个逐步示例演示如何通过 ConfigMap 更新 Pod 中的配置,并基于配置 Pod 以使用 ConfigMap 任务构建。
本教程结束后,你将了解如何更改正在运行的应用程序的配置。
本教程使用 alpine
和 nginx
镜像作为示例。
准备工作
你需要拥有一个 Kubernetes 集群,并且 kubectl 命令行工具已配置为与你的集群通信。建议在至少有两个不作为控制平面主机的节点的集群上运行本教程。如果你还没有集群,可以使用 minikube 创建一个,或者使用这些 Kubernetes 操场中的一个。
你需要拥有 curl 命令行工具,以便从终端或命令提示符发出 HTTP 请求。如果你没有 curl
,可以安装它。请查阅你本地操作系统的文档。
目标
- 通过卷挂载的 ConfigMap 更新配置
- 通过 ConfigMap 更新 Pod 的环境变量
- 在多容器 Pod 中通过 ConfigMap 更新配置
- 在包含 Sidecar 容器的 Pod 中通过 ConfigMap 更新配置
通过卷挂载的 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 的 Pods 以确保它们已就绪(通过选择器匹配)
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
在运行这些 Pods 的每个节点上,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
卷或 projected
卷映射到正在运行的 Pod 中的 ConfigMap,并且你更新了该 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 的 Pods 以确保它们已就绪(通过选择器匹配)
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 的 rollout。
# 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
检查 Pods
kubectl get pods --selector=app.kubernetes.io/name=configmap-env-var
rollout 会导致 Kubernetes 为 Deployment 创建一个新的ReplicaSet;这意味着现有 Pods 最终会终止,并创建新的 Pods。几秒钟后,你应该看到类似以下的输出:
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
注意
请等待旧的 Pods 完全终止,然后再进行下一步操作。查看此 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 滚动更新期间应用于 Pod。如果 Pods 因其他原因(例如扩缩 Deployment)而创建,则新的 Pods 也会使用最新的配置值;如果你不触发滚动更新,则你可能会发现你的应用程序以新旧环境变量值混合运行。
在多容器 Pod 中通过 ConfigMap 更新配置
使用 kubectl create configmap
命令从字面值创建 ConfigMap
kubectl create configmap color --from-literal=color=red
以下是 Deployment 的示例清单,该 Deployment 管理一组 Pod,每个 Pod 包含两个容器。这两个容器共享一个 emptyDir
卷,用于通信。第一个容器运行一个 Web 服务器 (nginx
)。Web 服务器容器中共享卷的挂载路径是 /usr/share/nginx/html
。第二个辅助容器基于 alpine
,对于此容器,emptyDir
卷挂载在 /pod-data
。辅助容器根据 ConfigMap 写入一个 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 的 Pods 以确保它们已就绪(通过选择器匹配)
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
工具会为你创建一个Service)
kubectl expose deployment configmap-two-containers --name=configmap-service --port=8080 --target-port=80
使用 kubectl
转发端口。
# this stays running in the background
kubectl port-forward service/configmap-service 8080:8080 &
访问服务。
curl https://: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 https://: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
在包含 Sidecar 容器的 Pod 中通过 ConfigMap 更新配置
通过使用Sidecar 容器作为辅助容器来写入 HTML 文件,可以重现上述场景。
由于 Sidecar 容器在概念上是一个 Init 容器,因此它保证在主 Web 服务器容器启动之前启动。
这确保了当 Web 服务器准备好提供 HTML 文件时,HTML 文件始终可用。
如果你从上一个场景继续,你可以重用名为 color
的 ConfigMap。
如果你独立执行此场景,请使用 kubectl create configmap
命令从字面值创建 ConfigMap
kubectl create configmap color --from-literal=color=blue
以下是 Deployment 的示例清单,该 Deployment 管理一组 Pod,每个 Pod 包含一个主容器和一个 Sidecar 容器。这两个容器共享一个 emptyDir
卷,用于通信。主容器运行一个 Web 服务器 (NGINX)。Web 服务器容器中共享卷的挂载路径是 /usr/share/nginx/html
。第二个容器是一个基于 Alpine Linux 的 Sidecar 容器,它充当辅助容器。对于此容器,emptyDir
卷挂载在 /pod-data
。Sidecar 容器根据 ConfigMap 写入一个 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 的 Pods 以确保它们已就绪(通过选择器匹配)
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
工具会为你创建一个Service)
kubectl expose deployment configmap-sidecar-container --name=configmap-sidecar-service --port=8081 --target-port=80
使用 kubectl
转发端口。
# this stays running in the background
kubectl port-forward service/configmap-sidecar-service 8081:8081 &
访问服务。
curl https://: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 https://: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 的名称,并切换到引用新名称的正在运行的 Pods
- 替换集群中所有以前运行过使用旧值的 Pod 的节点。
- 在 kubelet 之前加载过旧 ConfigMap 的任何节点上重新启动 kubelet。
下面显示了一个不可变 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 的 Pods 以确保它们已就绪(通过选择器匹配)
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 被标记为不可变,就无法撤销此更改,也无法修改数据或 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
这将触发一个 rollout。等待所有以前的 Pods 终止,并且新的 Pods 处于就绪状态。
监控 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 随后的 rollout 后可用。
一旦 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