配置 Pod 使用 ConfigMap

許多應用依賴於在應用初始化或執行時使用的配置。大多數情況下,需要調整分配給配置引數的值。ConfigMap 是 Kubernetes 的一種機制,允許你將配置資料注入到應用程式的 Pod 中。

ConfigMap 概念允許你將配置工件與映象內容解耦,以保持容器化應用程式的可移植性。例如,你可以下載並執行相同的容器映象,以用於本地開發、系統測試或執行即時終端使用者工作負載。

本頁面提供了一系列使用示例,演示如何建立 ConfigMap 以及如何使用 ConfigMap 中儲存的資料配置 Pod。

準備工作

你需要擁有一個 Kubernetes 叢集,並且 kubectl 命令列工具必須配置為與你的叢集通訊。建議在至少有兩個不作為控制平面主機的節點上執行本教程。如果你還沒有叢集,可以使用 minikube 建立一個,或者使用這些 Kubernetes 遊樂場之一

你需要安裝 wget 工具。如果你有其他工具(例如 curl)但沒有 wget,則需要調整下載示例資料的步驟。

建立 ConfigMap

你可以使用 kubectl create configmapkustomization.yaml 中的 ConfigMap 生成器來建立 ConfigMap。

使用 kubectl create configmap 建立 ConfigMap

使用 kubectl create configmap 命令從目錄檔案字面值建立 ConfigMap。

kubectl create configmap <map-name> <data-source>

其中 是你想要分配給 ConfigMap 的名稱, 是用於獲取資料的目錄、檔案或字面值。ConfigMap 物件的名稱必須是有效的 DNS 子域名

當你基於檔案建立 ConfigMap 時, 中的鍵預設為檔案的基本名稱,值預設為檔案內容。

你可以使用 kubectl describekubectl get 來檢索有關 ConfigMap 的資訊。

從目錄建立 ConfigMap

你可以使用 kubectl create configmap 從同一目錄中的多個檔案建立 ConfigMap。當你基於目錄建立 ConfigMap 時,kubectl 會識別目錄中檔名是有效鍵的檔案,並將這些檔案中的每一個打包到新的 ConfigMap 中。除了常規檔案之外的任何目錄條目都將被忽略(例如:子目錄、符號連結、裝置、管道等)。

建立本地目錄

mkdir -p configure-pod-container/configmap/

現在,下載示例配置並建立 ConfigMap

# Download the sample files into `configure-pod-container/configmap/` directory
wget https://kubernetes.club.tw/examples/configmap/game.properties -O configure-pod-container/configmap/game.properties
wget https://kubernetes.club.tw/examples/configmap/ui.properties -O configure-pod-container/configmap/ui.properties

# Create the ConfigMap
kubectl create configmap game-config --from-file=configure-pod-container/configmap/

上述命令將 configure-pod-container/configmap/ 目錄中的每個檔案(本例中為 game.propertiesui.properties)打包到 game-config ConfigMap 中。你可以使用以下命令顯示 ConfigMap 的詳細資訊

kubectl describe configmaps game-config

輸出類似於:

Name:         game-config
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
game.properties:
----
enemies=aliens
lives=3
enemies.cheat=true
enemies.cheat.level=noGoodRotten
secret.code.passphrase=UUDDLRLRBABAS
secret.code.allowed=true
secret.code.lives=30
ui.properties:
----
color.good=purple
color.bad=yellow
allow.textmode=true
how.nice.to.look=fairlyNice

configure-pod-container/configmap/ 目錄中的 game.propertiesui.properties 檔案在 ConfigMap 的 data 部分中表示。

kubectl get configmaps game-config -o yaml

輸出類似於:

apiVersion: v1
kind: ConfigMap
metadata:
  creationTimestamp: 2022-02-18T18:52:05Z
  name: game-config
  namespace: default
  resourceVersion: "516"
  uid: b4952dc3-d670-11e5-8cd0-68f728db1985
data:
  game.properties: |
    enemies=aliens
    lives=3
    enemies.cheat=true
    enemies.cheat.level=noGoodRotten
    secret.code.passphrase=UUDDLRLRBABAS
    secret.code.allowed=true
    secret.code.lives=30    
  ui.properties: |
    color.good=purple
    color.bad=yellow
    allow.textmode=true
    how.nice.to.look=fairlyNice    

從檔案建立 ConfigMap

你可以使用 kubectl create configmap 從單個檔案或多個檔案建立 ConfigMap。

例如,

kubectl create configmap game-config-2 --from-file=configure-pod-container/configmap/game.properties

將生成以下 ConfigMap

kubectl describe configmaps game-config-2

其中輸出類似於

Name:         game-config-2
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
game.properties:
----
enemies=aliens
lives=3
enemies.cheat=true
enemies.cheat.level=noGoodRotten
secret.code.passphrase=UUDDLRLRBABAS
secret.code.allowed=true
secret.code.lives=30

你可以多次傳遞 --from-file 引數,以從多個數據源建立 ConfigMap。

kubectl create configmap game-config-2 --from-file=configure-pod-container/configmap/game.properties --from-file=configure-pod-container/configmap/ui.properties

你可以使用以下命令顯示 game-config-2 ConfigMap 的詳細資訊

kubectl describe configmaps game-config-2

輸出類似於:

Name:         game-config-2
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
game.properties:
----
enemies=aliens
lives=3
enemies.cheat=true
enemies.cheat.level=noGoodRotten
secret.code.passphrase=UUDDLRLRBABAS
secret.code.allowed=true
secret.code.lives=30
ui.properties:
----
color.good=purple
color.bad=yellow
allow.textmode=true
how.nice.to.look=fairlyNice

使用選項 --from-env-file 從環境變數檔案建立 ConfigMap,例如

# Env-files contain a list of environment variables.
# These syntax rules apply:
#   Each line in an env file has to be in VAR=VAL format.
#   Lines beginning with # (i.e. comments) are ignored.
#   Blank lines are ignored.
#   There is no special handling of quotation marks (i.e. they will be part of the ConfigMap value)).

# Download the sample files into `configure-pod-container/configmap/` directory
wget https://kubernetes.club.tw/examples/configmap/game-env-file.properties -O configure-pod-container/configmap/game-env-file.properties
wget https://kubernetes.club.tw/examples/configmap/ui-env-file.properties -O configure-pod-container/configmap/ui-env-file.properties

# The env-file `game-env-file.properties` looks like below
cat configure-pod-container/configmap/game-env-file.properties
enemies=aliens
lives=3
allowed="true"

# This comment and the empty line above it are ignored
kubectl create configmap game-config-env-file \
       --from-env-file=configure-pod-container/configmap/game-env-file.properties

將生成一個 ConfigMap。檢視 ConfigMap

kubectl get configmap game-config-env-file -o yaml

輸出類似於

apiVersion: v1
kind: ConfigMap
metadata:
  creationTimestamp: 2019-12-27T18:36:28Z
  name: game-config-env-file
  namespace: default
  resourceVersion: "809965"
  uid: d9d1ca5b-eb34-11e7-887b-42010a8002b8
data:
  allowed: '"true"'
  enemies: aliens
  lives: "3"

從 Kubernetes v1.23 開始,kubectl 支援多次指定 --from-env-file 引數,以從多個數據源建立 ConfigMap。

kubectl create configmap config-multi-env-files \
        --from-env-file=configure-pod-container/configmap/game-env-file.properties \
        --from-env-file=configure-pod-container/configmap/ui-env-file.properties

將生成以下 ConfigMap

kubectl get configmap config-multi-env-files -o yaml

其中輸出類似於

apiVersion: v1
kind: ConfigMap
metadata:
  creationTimestamp: 2019-12-27T18:38:34Z
  name: config-multi-env-files
  namespace: default
  resourceVersion: "810136"
  uid: 252c4572-eb35-11e7-887b-42010a8002b8
data:
  allowed: '"true"'
  color: purple
  enemies: aliens
  how: fairlyNice
  lives: "3"
  textmode: "true"

定義從檔案建立 ConfigMap 時使用的鍵

當使用 --from-file 引數時,你可以在 ConfigMap 的 data 部分中使用除檔名之外的其他鍵。

kubectl create configmap game-config-3 --from-file=<my-key-name>=<path-to-file>

其中 <my-key-name> 是你希望在 ConfigMap 中使用的鍵,<path-to-file> 是你希望該鍵表示的資料來源檔案的位置。

例如

kubectl create configmap game-config-3 --from-file=game-special-key=configure-pod-container/configmap/game.properties

將生成以下 ConfigMap

kubectl get configmaps game-config-3 -o yaml

其中輸出類似於

apiVersion: v1
kind: ConfigMap
metadata:
  creationTimestamp: 2022-02-18T18:54:22Z
  name: game-config-3
  namespace: default
  resourceVersion: "530"
  uid: 05f8da22-d671-11e5-8cd0-68f728db1985
data:
  game-special-key: |
    enemies=aliens
    lives=3
    enemies.cheat=true
    enemies.cheat.level=noGoodRotten
    secret.code.passphrase=UUDDLRLRBABAS
    secret.code.allowed=true
    secret.code.lives=30    

從字面值建立 ConfigMap

你可以使用 kubectl create configmap--from-literal 引數從命令列定義一個字面值

kubectl create configmap special-config --from-literal=special.how=very --from-literal=special.type=charm

你可以傳入多個鍵值對。命令列上提供的每個鍵值對在 ConfigMap 的 data 部分中都表示為一個單獨的條目。

kubectl get configmaps special-config -o yaml

輸出類似於:

apiVersion: v1
kind: ConfigMap
metadata:
  creationTimestamp: 2022-02-18T19:14:38Z
  name: special-config
  namespace: default
  resourceVersion: "651"
  uid: dadce046-d673-11e5-8cd0-68f728db1985
data:
  special.how: very
  special.type: charm

從生成器建立 ConfigMap

你還可以從生成器建立 ConfigMap,然後將其應用於在叢集的 API 伺服器中建立物件。你應在目錄中的 kustomization.yaml 檔案中指定生成器。

從檔案生成 ConfigMap

例如,要從檔案 configure-pod-container/configmap/game.properties 生成 ConfigMap

# Create a kustomization.yaml file with ConfigMapGenerator
cat <<EOF >./kustomization.yaml
configMapGenerator:
- name: game-config-4
  options:
    labels:
      game-config: config-4
  files:
  - configure-pod-container/configmap/game.properties
EOF

應用 Kustomization 目錄以建立 ConfigMap 物件

kubectl apply -k .
configmap/game-config-4-m9dm2f92bt created

你可以這樣檢查 ConfigMap 是否已建立

kubectl get configmap
NAME                       DATA   AGE
game-config-4-m9dm2f92bt   1      37s

並且

kubectl describe configmaps/game-config-4-m9dm2f92bt
Name:         game-config-4-m9dm2f92bt
Namespace:    default
Labels:       game-config=config-4
Annotations:  kubectl.kubernetes.io/last-applied-configuration:
                {"apiVersion":"v1","data":{"game.properties":"enemies=aliens\nlives=3\nenemies.cheat=true\nenemies.cheat.level=noGoodRotten\nsecret.code.p...

Data
====
game.properties:
----
enemies=aliens
lives=3
enemies.cheat=true
enemies.cheat.level=noGoodRotten
secret.code.passphrase=UUDDLRLRBABAS
secret.code.allowed=true
secret.code.lives=30
Events:  <none>

請注意,生成的 ConfigMap 名稱會附加一個透過內容雜湊計算的字尾。這確保了每次修改內容時都會生成一個新的 ConfigMap。

定義從檔案生成 ConfigMap 時使用的鍵

你可以在 ConfigMap 生成器中使用除檔名之外的其他鍵。例如,要從檔案 configure-pod-container/configmap/game.properties 生成 ConfigMap,並使用鍵 game-special-key

# Create a kustomization.yaml file with ConfigMapGenerator
cat <<EOF >./kustomization.yaml
configMapGenerator:
- name: game-config-5
  options:
    labels:
      game-config: config-5
  files:
  - game-special-key=configure-pod-container/configmap/game.properties
EOF

應用 Kustomization 目錄以建立 ConfigMap 物件。

kubectl apply -k .
configmap/game-config-5-m67dt67794 created

從字面量生成 ConfigMap

本例向你展示如何使用 Kustomize 和 kubectl 從兩個字面量鍵值對(special.type=charmspecial.how=very)建立 ConfigMap。為此,你可以指定 ConfigMap 生成器。建立(或替換)kustomization.yaml,使其具有以下內容

---
# kustomization.yaml contents for creating a ConfigMap from literals
configMapGenerator:
- name: special-config-2
  literals:
  - special.how=very
  - special.type=charm

應用 Kustomization 目錄以建立 ConfigMap 物件

kubectl apply -k .
configmap/special-config-2-c92b5mmcf2 created

臨時清理

在繼續之前,清理你製作的一些 ConfigMap

kubectl delete configmap special-config
kubectl delete configmap env-config
kubectl delete configmap -l 'game-config in (config-4,config-5)'

既然你已經學會了如何定義 ConfigMap,你可以繼續下一節,學習如何在 Pod 中使用這些物件。


使用 ConfigMap 資料定義容器環境變數

使用單個 ConfigMap 中的資料定義容器環境變數

  1. 在 ConfigMap 中將環境變數定義為鍵值對

    kubectl create configmap special-config --from-literal=special.how=very
    
  2. 將 ConfigMap 中定義的 special.how 值賦給 Pod 規約中的 SPECIAL_LEVEL_KEY 環境變數。

    apiVersion: v1
    kind: Pod
    metadata:
      name: dapi-test-pod
    spec:
      containers:
        - name: test-container
          image: registry.k8s.io/busybox:1.27.2
          command: [ "/bin/sh", "-c", "env" ]
          env:
            # Define the environment variable
            - name: SPECIAL_LEVEL_KEY
              valueFrom:
                configMapKeyRef:
                  # The ConfigMap containing the value you want to assign to SPECIAL_LEVEL_KEY
                  name: special-config
                  # Specify the key associated with the value
                  key: special.how
      restartPolicy: Never
    

    建立 Pod

    kubectl create -f https://kubernetes.club.tw/examples/pods/pod-single-configmap-env-variable.yaml
    

    現在,Pod 的輸出包含環境變數 SPECIAL_LEVEL_KEY=very

使用多個 ConfigMap 中的資料定義容器環境變數

與上一個示例一樣,首先建立 ConfigMap。這是你將使用的清單

apiVersion: v1
kind: ConfigMap
metadata:
  name: special-config
  namespace: default
data:
  special.how: very
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: env-config
  namespace: default
data:
  log_level: INFO
  • 建立 ConfigMap

    kubectl create -f https://kubernetes.club.tw/examples/configmap/configmaps.yaml
    
  • 在 Pod 規約中定義環境變數。

    apiVersion: v1
    kind: Pod
    metadata:
      name: dapi-test-pod
    spec:
      containers:
        - name: test-container
          image: registry.k8s.io/busybox:1.27.2
          command: [ "/bin/sh", "-c", "env" ]
          env:
            - name: SPECIAL_LEVEL_KEY
              valueFrom:
                configMapKeyRef:
                  name: special-config
                  key: special.how
            - name: LOG_LEVEL
              valueFrom:
                configMapKeyRef:
                  name: env-config
                  key: log_level
      restartPolicy: Never
    

    建立 Pod

    kubectl create -f https://kubernetes.club.tw/examples/pods/pod-multiple-configmap-env-variable.yaml
    

    現在,Pod 的輸出包含環境變數 SPECIAL_LEVEL_KEY=veryLOG_LEVEL=INFO

    當你準備好繼續時,刪除該 Pod 和 ConfigMap

    kubectl delete pod dapi-test-pod --now
    kubectl delete configmap special-config
    kubectl delete configmap env-config
    

將 ConfigMap 中的所有鍵值對配置為容器環境變數

  • 建立一個包含多個鍵值對的 ConfigMap。

    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: special-config
      namespace: default
    data:
      SPECIAL_LEVEL: very
      SPECIAL_TYPE: charm
    

    建立 ConfigMap

    kubectl create -f https://kubernetes.club.tw/examples/configmap/configmap-multikeys.yaml
    
  • 使用 envFrom 將 ConfigMap 的所有資料定義為容器環境變數。ConfigMap 中的鍵成為 Pod 中的環境變數名。

    apiVersion: v1
    kind: Pod
    metadata:
      name: dapi-test-pod
    spec:
      containers:
        - name: test-container
          image: registry.k8s.io/busybox:1.27.2
          command: [ "/bin/sh", "-c", "env" ]
          envFrom:
          - configMapRef:
              name: special-config
      restartPolicy: Never
    

    建立 Pod

    kubectl create -f https://kubernetes.club.tw/examples/pods/pod-configmap-envFrom.yaml
    

    現在,Pod 的輸出包含環境變數 SPECIAL_LEVEL=verySPECIAL_TYPE=charm

    當你準備好繼續時,刪除該 Pod

    kubectl delete pod dapi-test-pod --now
    

在 Pod 命令中使用 ConfigMap 定義的環境變數

你可以使用 $(VAR_NAME) Kubernetes 替換語法在容器的 commandargs 中使用 ConfigMap 定義的環境變數。

例如,以下 Pod 清單

apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  containers:
    - name: test-container
      image: registry.k8s.io/busybox:1.27.2
      command: [ "/bin/echo", "$(SPECIAL_LEVEL_KEY) $(SPECIAL_TYPE_KEY)" ]
      env:
        - name: SPECIAL_LEVEL_KEY
          valueFrom:
            configMapKeyRef:
              name: special-config
              key: SPECIAL_LEVEL
        - name: SPECIAL_TYPE_KEY
          valueFrom:
            configMapKeyRef:
              name: special-config
              key: SPECIAL_TYPE
  restartPolicy: Never

透過執行以下命令建立該 Pod

kubectl create -f https://kubernetes.club.tw/examples/pods/pod-configmap-env-var-valueFrom.yaml

該 Pod 從 test-container 容器生成以下輸出

kubectl logs dapi-test-pod
very charm

當你準備好繼續時,刪除該 Pod

kubectl delete pod dapi-test-pod --now

將 ConfigMap 資料新增到卷

從檔案建立 ConfigMap 中所述,當你使用 --from-file 建立 ConfigMap 時,檔名會成為儲存在 ConfigMap 的 data 部分中的鍵。檔案內容成為鍵的值。

本節中的示例引用名為 special-config 的 ConfigMap

apiVersion: v1
kind: ConfigMap
metadata:
  name: special-config
  namespace: default
data:
  SPECIAL_LEVEL: very
  SPECIAL_TYPE: charm

建立 ConfigMap

kubectl create -f https://kubernetes.club.tw/examples/configmap/configmap-multikeys.yaml

用儲存在 ConfigMap 中的資料填充卷

在 Pod 規約的 volumes 部分下新增 ConfigMap 名稱。這將 ConfigMap 資料新增到指定為 volumeMounts.mountPath 的目錄(本例中為 /etc/config)。command 部分列出了與 ConfigMap 中的鍵匹配的目錄檔案。

apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  containers:
    - name: test-container
      image: registry.k8s.io/busybox:1.27.2
      command: [ "/bin/sh", "-c", "ls /etc/config/" ]
      volumeMounts:
      - name: config-volume
        mountPath: /etc/config
  volumes:
    - name: config-volume
      configMap:
        # Provide the name of the ConfigMap containing the files you want
        # to add to the container
        name: special-config
  restartPolicy: Never

建立 Pod

kubectl create -f https://kubernetes.club.tw/examples/pods/pod-configmap-volume.yaml

當 Pod 執行時,命令 ls /etc/config/ 將生成以下輸出

SPECIAL_LEVEL
SPECIAL_TYPE

文字資料以 UTF-8 字元編碼的檔案形式公開。要使用其他字元編碼,請使用 binaryData(有關更多詳細資訊,請參閱ConfigMap 物件)。

當你準備好繼續時,刪除該 Pod

kubectl delete pod dapi-test-pod --now

將 ConfigMap 資料新增到卷中的特定路徑

使用 path 欄位為特定的 ConfigMap 項指定所需的檔案路徑。在這種情況下,SPECIAL_LEVEL 項將被掛載到 config-volume 卷中的 /etc/config/keys 處。

apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  containers:
    - name: test-container
      image: registry.k8s.io/busybox:1.27.2
      command: [ "/bin/sh","-c","cat /etc/config/keys" ]
      volumeMounts:
      - name: config-volume
        mountPath: /etc/config
  volumes:
    - name: config-volume
      configMap:
        name: special-config
        items:
        - key: SPECIAL_LEVEL
          path: keys
  restartPolicy: Never

建立 Pod

kubectl create -f https://kubernetes.club.tw/examples/pods/pod-configmap-volume-specific-key.yaml

當 Pod 執行時,命令 cat /etc/config/keys 將生成以下輸出

very

刪除該 Pod

kubectl delete pod dapi-test-pod --now

將鍵投射到特定路徑和檔案許可權

你可以將鍵投射到特定路徑。有關語法,請參閱 Secrets 指南中的相應部分。
你可以為鍵設定 POSIX 許可權。有關語法,請參閱 Secrets 指南中的相應部分。

可選引用

ConfigMap 引用可以標記為“可選”。如果 ConfigMap 不存在,則掛載的卷將為空。如果 ConfigMap 存在,但引用的鍵不存在,則掛載點下將沒有該路徑。有關更多詳細資訊,請參閱可選 ConfigMap

掛載的 ConfigMap 會自動更新

當掛載的 ConfigMap 更新時,投射的內容最終也會更新。這適用於 Pod 啟動後可選引用的 ConfigMap 出現的情況。

Kubelet 在每次定期同步時檢查掛載的 ConfigMap 是否新鮮。但是,它使用其基於 TTL 的本地快取來獲取 ConfigMap 的當前值。因此,從 ConfigMap 更新到新鍵投射到 Pod 的總延遲可能與 kubelet 同步週期(預設為 1 分鐘)+ kubelet 中 ConfigMap 快取的 TTL(預設為 1 分鐘)一樣長。你可以透過更新 Pod 的一個註解來觸發立即重新整理。

理解 ConfigMap 和 Pod

ConfigMap API 資源以鍵值對的形式儲存配置資料。資料可以在 Pod 中使用,也可以為控制器等系統元件提供配置。ConfigMap 類似於 Secrets,但提供了一種處理不包含敏感資訊的字串的方法。使用者和系統元件都可以將配置資料儲存在 ConfigMap 中。

ConfigMap 的 data 欄位包含配置資料。如下例所示,這可以是簡單的(例如使用 --from-literal 定義的單個屬性)或複雜的(例如使用 --from-file 定義的配置檔案或 JSON blob)。

apiVersion: v1
kind: ConfigMap
metadata:
  creationTimestamp: 2016-02-18T19:14:38Z
  name: example-config
  namespace: default
data:
  # example of a simple property defined using --from-literal
  example.property.1: hello
  example.property.2: world
  # example of a complex property defined using --from-file
  example.property.file: |-
    property.1=value-1
    property.2=value-2
    property.3=value-3    

kubectl 從非 ASCII 或 UTF-8 輸入建立 ConfigMap 時,該工具會將這些輸入放入 ConfigMap 的 binaryData 欄位,而不是 data 欄位。文字和二進位制資料來源可以合併在一個 ConfigMap 中。

如果你想檢視 ConfigMap 中的 binaryData 鍵(及其值),可以執行 kubectl get configmap -o jsonpath='{.binaryData}' <name>

Pod 可以從使用 databinaryData 的 ConfigMap 載入資料。

可選 ConfigMap

你可以在 Pod 規約中將對 ConfigMap 的引用標記為“可選”。如果 ConfigMap 不存在,則它在 Pod 中提供資料的配置(例如:環境變數、掛載卷)將為空。如果 ConfigMap 存在,但引用的鍵不存在,則資料也為空。

例如,以下 Pod 規約將 ConfigMap 中的環境變數標記為可選

apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  containers:
    - name: test-container
      image: gcr.io/google_containers/busybox
      command: ["/bin/sh", "-c", "env"]
      env:
        - name: SPECIAL_LEVEL_KEY
          valueFrom:
            configMapKeyRef:
              name: a-config
              key: akey
              optional: true # mark the variable as optional
  restartPolicy: Never

如果你執行這個 Pod,並且沒有名為 a-config 的 ConfigMap,則輸出為空。如果你執行這個 Pod,並且有一個名為 a-config 的 ConfigMap,但該 ConfigMap 沒有名為 akey 的鍵,則輸出也為空。如果你確實在 a-config ConfigMap 中為 akey 設定了一個值,則該 Pod 將列印該值然後終止。

你還可以將 ConfigMap 提供的卷和檔案標記為可選。即使引用的 ConfigMap 或鍵不存在,Kubernetes 也會始終為卷建立掛載路徑。例如,以下 Pod 規約將引用 ConfigMap 的卷標記為可選

apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  containers:
    - name: test-container
      image: gcr.io/google_containers/busybox
      command: ["/bin/sh", "-c", "ls /etc/config"]
      volumeMounts:
      - name: config-volume
        mountPath: /etc/config
  volumes:
    - name: config-volume
      configMap:
        name: no-config
        optional: true # mark the source ConfigMap as optional
  restartPolicy: Never

限制

  • 在 Pod 規約中引用 ConfigMap 物件之前,你必須先建立它。或者,在 Pod 規約中將 ConfigMap 引用標記為 optional(請參閱可選 ConfigMap)。如果你引用一個不存在的 ConfigMap 並且沒有將引用標記為 optional,則 Pod 將無法啟動。同樣,如果 ConfigMap 中不存在的鍵引用未標記為 optional,也會阻止 Pod 啟動。

  • 如果你使用 envFrom 從 ConfigMap 定義環境變數,則被認為是無效的鍵將被跳過。Pod 將被允許啟動,但無效名稱將記錄在事件日誌中 (InvalidVariableNames)。日誌訊息列出了每個跳過的鍵。例如

    kubectl get events
    

    輸出類似於:

    LASTSEEN FIRSTSEEN COUNT NAME          KIND  SUBOBJECT  TYPE      REASON                            SOURCE                MESSAGE
    0s       0s        1     dapi-test-pod Pod              Warning   InvalidEnvironmentVariableNames   {kubelet, 127.0.0.1}  Keys [1badkey, 2alsobad] from the EnvFrom configMap default/myconfig were skipped since they are considered invalid environment variable names.
    
  • ConfigMap 位於特定的 名稱空間 中。Pod 只能引用與 Pod 位於同一名稱空間中的 ConfigMap。

  • 你不能將 ConfigMap 用於靜態 Pod,因為 kubelet 不支援此功能。

清理

刪除你建立的 ConfigMap 和 Pod

kubectl delete configmaps/game-config configmaps/game-config-2 configmaps/game-config-3 \
               configmaps/game-config-env-file
kubectl delete pod dapi-test-pod --now

# You might already have removed the next set
kubectl delete configmaps/special-config configmaps/env-config
kubectl delete configmap -l 'game-config in (config-4,config-5)'

刪除用於生成 ConfigMap 的 kustomization.yaml 檔案

rm kustomization.yaml

如果你建立了 configure-pod-container 目錄並且不再需要它,你也應該刪除它,或者將其移到回收站/已刪除檔案位置。

rm -r configure-pod-container

下一步

上次修改時間:2023 年 11 月 27 日太平洋標準時間凌晨 1:38:[en] config-pod-configmap add cleanup step. (2d13865044)