認識 ConfigMap
ConfigMap 此定義檔內容,記載了軟體運行中所需要的:設定、環境變數、指令等等影響著軟體運作的必載元件。
這樣的設計可以很明確將設定檔案抽離出來,方便用來管理軟體運作。
ConfigMap 很適合用來存放「非敏感性」的未加密設定資訊,如有敏感性資料,則需透過 Secret。
某程度上 ConfigMap 也是實現 infrastructure as code 精神上的一環。
K8s 上,軟體就是 pod、設定檔案就是 ConfigMap。 建立 ConfigMap 的指令:
kubectl create configmap [NAME] [DATA]
kubectl apply configmap.yamlExamples
# Create a new configmap named my-config based on folder bar
kubectl create configmap my-config --from-file=path/to/bar
# Create a new configmap named my-config with specified keys instead of file basenames on disk
kubectl create configmap my-config --from-file=key1=/path/to/bar/file1.txt --from-file=key2=/path/to/bar/file2.txt
# Create a new configmap named my-config with key1=config1 and key2=config2
kubectl create configmap my-config --from-literal=key1=config1 --from-literal=key2=config2
# Create a new configmap named my-config from the key=value pairs in the file
kubectl create configmap my-config --from-file=path/to/bar
# Create a new configmap named my-config from an env file
kubectl create configmap my-config --from-env-file=path/to/bar.env
# Delete a configmap named my-config
kubectl delete configmap my-config建立 config
建立 pod
生效、連線 pod
查看 ConfigMap
回頭解析 Pod YAML
環境變數
引用環境變數,定義在env - valueFrom範圍中,這會參照該my-config (ConfigMap)裡頭的 key 作為環境變數。
Command - 引用變數
使用命令列參數,可透過上述valueFrom環境變數引用,
K8s 將使用$(Environment Variable)語法作為變數表達與引用。
檔案系統
Pod 內透過volumeMounts定義了一個磁碟區命名為config-volume,並掛載於 /config 路徑。
實際磁碟來源,是依據ConfigMap內的 my-config 來建立的檔案系統。
在操作中進入 /config 路徑內,會看見ConfigMap的每個項目建立了檔案或目錄,
Last updated