通过 ConfigMap 更新配置
本页面提供了通过 ConfigMap 更新 Pod 中配置的分步示例,它基于配置 Pod 以使用 ConfigMap 任务。
完成本教程后,你将了解如何更改运行中应用程序的配置。
本教程使用 alpine
和 nginx
镜像作为示例。
开始之前
你需要有一个 Kubernetes 集群,并且 kubectl 命令行工具已配置为与你的集群通信。建议在至少有两个非控制平面主机节点的集群上运行本教程。如果你还没有集群,可以使用 minikube 创建一个,或者使用以下 Kubernetes 演练场之一:
你需要 curl 命令行工具,以便从终端或命令提示符发出 HTTP 请求。如果你没有 curl
,可以安装它。请查阅本地操作系统的文档。
目标
- 通过挂载为 Volume 的 ConfigMap 更新配置
- 通过 ConfigMap 更新 Pod 的环境变量
- 在多容器 Pod 中通过 ConfigMap 更新配置
- 在具有 Sidecar Container 的 Pod 中通过 ConfigMap 更新配置
通过挂载为 Volume 的 ConfigMap 更新配置
使用 kubectl create configmap
命令根据字面值创建 ConfigMap
kubectl create configmap sport --from-literal=sport=football
下面是一个 Deployment manifest 示例,其中 ConfigMap sport
作为 volume 挂载到 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 以确保它们就绪(通过 selector 匹配)
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 的数据并将其转换为本地 volume 中的文件。然后 kubelet 会根据 Pod 模板中的指定将该 volume 挂载到容器中。在该容器中运行的代码会从文件中加载信息并使用它向标准输出打印报告。你可以通过查看该 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(如果看到错误,请重试)。
这是编辑后 manifest 可能的样子示例
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
volume 或 projected
volume 将 ConfigMap 映射到运行中的 Pod 中,并且更新了该 ConfigMap 时,运行中的 Pod 会几乎立即看到更新。
然而,你的应用程序只有在编写成轮询变更或监视文件更新时才能看到变更。
在启动时只加载一次配置的应用程序将不会注意到变更。
通过 ConfigMap 更新 Pod 的环境变量
使用 kubectl create configmap
命令根据字面值创建 ConfigMap
kubectl create configmap fruits --from-literal=fruits=apples
下面是一个 Deployment manifest 示例,其中通过 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 以确保它们就绪(通过 selector 匹配)
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(如果看到错误,请重试)。
这是编辑后 manifest 可能的样子示例
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。如果 Pod 因其他原因被创建,例如扩缩 Deployment,则新的 Pod 也会使用最新的配置值;如果你不触发滚动更新,则可能会发现你的应用程序运行着旧值和新值的混合环境变量。
在多容器 Pod 中通过 ConfigMap 更新配置
使用 kubectl create configmap
命令根据字面值创建 ConfigMap
kubectl create configmap color --from-literal=color=red
下面是一个 Deployment manifest 示例,用于管理一组 Pod,每个 Pod 包含两个容器。这两个容器共享一个 emptyDir
volume,用于相互通信。第一个容器运行一个 Web 服务器(nginx
)。共享 volume 在 Web 服务器容器中的挂载路径是 /usr/share/nginx/html
。第二个辅助容器基于 alpine
,该容器的 emptyDir
volume 挂载在 /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 以确保它们就绪(通过 selector 匹配)
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 &
访问 service。
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(如果看到错误,请重试)。
这是编辑后 manifest 可能的样子示例
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
循环访问 service 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
在具有 sidecar 容器的 Pod 中通过 ConfigMap 更新配置
上述场景可以通过使用 Sidecar Container 作为辅助容器来写入 HTML 文件来实现。
由于 Sidecar Container 在概念上是 Init Container,因此它保证在主 Web 服务器容器之前启动。
这确保了当 Web 服务器准备好提供 HTML 文件时,该文件始终可用。
如果你正在继续上一个场景,可以复用名为 color
的 ConfigMap 来用于此场景。
如果你独立执行此场景,请使用 kubectl create configmap
命令根据字面值创建一个 ConfigMap
kubectl create configmap color --from-literal=color=blue
下面是一个 Deployment manifest 示例,用于管理一组 Pod,每个 Pod 包含一个主容器和一个 sidecar 容器。这两个容器共享一个 emptyDir
volume,用于相互通信。主容器运行一个 Web 服务器(NGINX)。共享 volume 在 Web 服务器容器中的挂载路径是 /usr/share/nginx/html
。第二个容器是基于 Alpine Linux 的 Sidecar Container,作为辅助容器。该容器的 emptyDir
volume 挂载在 /pod-data
。Sidecar Container 写入一个 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 以确保它们就绪(通过 selector 匹配)
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 &
访问 service。
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(如果看到错误,请重试)。
这是编辑后 manifest 可能的样子示例
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
循环访问 service 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
通过作为 volume 挂载的不可变 ConfigMap 更新配置
注意
不可变 ConfigMap 特别适用于那些常量且预期不会随时间变化的配置。将 ConfigMap 标记为不可变可以带来性能提升,因为 kubelet 不需要监视其变更。
如果确实需要进行更改,你应该计划以下任一方式:
- 更改 ConfigMap 的名称,并切换到引用新名称的 Pod
- 替换集群中所有以前运行过使用旧值的 Pod 的节点
- 在 kubelet 之前加载了旧 ConfigMap 的任何节点上重启 kubelet
下面显示了 不可变 ConfigMap 的 manifest 示例。
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 manifest 示例,其中不可变 ConfigMap company-name-20150801
作为 volume 挂载到 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 以确保它们就绪(通过 selector 匹配)
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。
使用下面所示的 manifest 创建一个新的不可变 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 和新 ConfigMap 的输出
NAME DATA AGE
company-name-20150801 1 22m
company-name-20240312 1 24s
修改 Deployment 以引用新的 ConfigMap。
编辑 Deployment
kubectl edit deployment immutable-configmap-volume
在出现的编辑器中,更新现有的 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
一旦所有 Deployment 都迁移到使用新的不可变 ConfigMap,建议删除旧的 ConfigMap。
kubectl delete configmap company-name-20150801
总结
挂载到 Pod 上作为 Volume 的 ConfigMap 的更改会在 kubelet 后续同步后无缝生效。
配置 Pod 环境变量的 ConfigMap 的更改会在该 Pod 后续滚动更新后生效。
一旦 ConfigMap 被标记为不可变,既无法撤销此更改(不能将不可变 ConfigMap 变回可变),也无法对 data
或 binaryData
字段的内容进行任何更改。你可以删除并重新创建 ConfigMap,或者创建一个新的不同的 ConfigMap。当你删除一个 ConfigMap 时,运行中的容器及其 Pod 会保留对引用该现有 ConfigMap 的任何 volume 的挂载点。
清理
如果 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