ConfigMap 此定義檔內容,記載了軟體運行中所需要的:設定、環境變數、指令
等等影響著軟體運作的必載元件。
這樣的設計可以很明確將設定檔案抽離出來,方便用來管理軟體運作。
ConfigMap 很適合用來存放**「非敏感性」**的未加密設定資訊,如有敏感性資料,則需透過 Secret。
某程度上 ConfigMap 也是實現 infrastructure as code 精神上的一環。
K8s 上,軟體就是 pod、設定檔案就是 ConfigMap。
建立 ConfigMap 的指令:
kubectl create configmap [NAME] [DATA]
kubectl apply configmap.yaml
# 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
kubectl create configmap my-config --from-file=my-config.txt \
--from-literal=another-param=config1 \
--from-literal=extra-param=config2
# 11-133-kuard-config.yaml
apiVersion: v1
kind: Pod
metadata:
name: kuard-config
spec:
containers:
- name: test-container
image: gcr.io/kuar-demo/kuard-amd64:1
imagePullPolicy: Always
command:
- "/kuard"
- "$(EXTRA_PARAM)"
env:
- name: ANOTHER_PARAM
valueFrom:
configMapKeyRef:
name: my-config
key: another-param
- name: EXTRA_PARAM
valueFrom:
configMapKeyRef:
name: my-config
key: extra-param
volumeMounts:
- name: config-volume
mountPath: /config
volumes:
- name: config-volume
configMap:
name: my-config
restartPolicy: Never
# 生效 pod
kubectl apply -f 11-133-kuard-config.yaml
# 連線 pod
kubectl port-forward --address 0.0.0.0 kuard-config 30333:8080
# kubectl get cm my-config -o yaml
apiVersion: v1
data:
another-param: config1
extra-param: config2
my-config.txt: |
another-param = value1
extra-param = value2
kind: ConfigMap
metadata:
creationTimestamp: "2019-01-29T03:51:25Z"
name: my-config
namespace: default
resourceVersion: "1426607"
selfLink: /api/v1/namespaces/default/configmaps/my-config
uid: 2644b22a-2379-11e9-8813-08002730aeb3
引用環境變數,定義在env - valueFrom
範圍中,這會參照該my-config (ConfigMap)
裡頭的 key 作為環境變數。
env:
- name: ANOTHER_PARAM
valueFrom:
configMapKeyRef:
name: my-config
key: another-param
- name: EXTRA_PARAM
valueFrom:
configMapKeyRef:
name: my-config
key: extra-param
使用命令列參數,可透過上述valueFrom
環境變數引用,
K8s 將使用$(Environment Variable)
語法作為變數表達與引用。
containers:
- name: test-container
image: gcr.io/kuar-demo/kuard-amd64:1
imagePullPolicy: Always
command:
- "/kuard"
- "$(EXTRA_PARAM)"
檔案系統
Pod 內透過volumeMounts
定義了一個磁碟區命名為_config-volume_,並掛載於 /config 路徑。
實際磁碟來源,是依據ConfigMap
內的 my-config 來建立的檔案系統。
在操作中進入 /config 路徑內,會看見ConfigMap
的每個項目建立了檔案或目錄,
volumeMounts:
- name: config-volume
mountPath: /config
volumes:
- name: config-volume
configMap:
name: my-config