透過 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