前言
ConfigMap 物件可以用來管理普通的、非機密的配置資訊,以明文形式存放,
Secret 物件用來管理重要的、機密的、不能泄露的類似秘鑰、密碼等資訊,
ConfigMap 物件可以實作程式的配置和程式本身的解耦,從而使程式更具移植性,
更新歷史
- 20200705 - 初稿 - 左程立
- 原文地址 - https://blog.zuolinux.com/2020/07/05/about-configmap.html
通過目錄/檔案創建 ConfigMap
從目錄創建
mkdir configmap
wget https://kubernetes.io/examples/configmap/game.properties -O configmap/game.properties
wget https://kubernetes.io/examples/configmap/ui.properties -O configmap/ui.properties
kubectl create configmap game-config --from-file=configmap/
可以看到兩個檔案內容合并存盤到 data 中,檔案名轉換為 key
# kubectl get configmap game-config -o yaml
apiVersion: v1
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
kind: ConfigMap
,,,,,,
通過檔案創建
kubectl create configmap game-config-2 --from-file=configmap/game.properties
通過多個檔案創建
效果和通過目錄創建一樣
kubectl create configmap game-config-2 --from-file=configmap/game.properties --from-file=configmap/ui.properties
從環境檔案創建 ConfigMap
使用 --from-env-file 選項
# 將樣本檔案下載到 `configmap/` 目錄
wget https://kubernetes.io/examples/configmap/game-env-file.properties -O configmap/game-env-file.properties
# kubectl create configmap game-config-env-file --from-env-file=configmap/game-env-file.properties
configmap/game-config-env-file created
查看
# kubectl get configmap game-config-env-file -o yaml
apiVersion: v1
data:
allowed: '"true"'
enemies: aliens
lives: "3"
kind: ConfigMap
可以看到檔案內容直接存盤到configmap data中作為鍵值對,沒有使用檔案名的key 對應多行值的形式
env檔案特點:
- env 檔案中的每一行必須為 VAR = VAL 格式,
- 以#開頭的行(即注釋)將被忽略,
- 空行將被忽略,
- 引號沒有特殊處理(即它們將成為 ConfigMap 值的一部分),
- 當使用多個 --from-env-file 時,僅僅最后一個 env 檔案有效,
- 作為卷形式在 Pod 中使用時,會出現多個檔案,檔案名為 data 中的 key,
定義從檔案創建 ConfigMap 時要使用的鍵
默認檔案名是鍵名
指定鍵名為
kubectl create configmap game-config-3 --from-file=<my-key-name>=<path-to-file>
命令列中指定key/value值
不從檔案獲取,直接命令列中指定
kubectl create configmap special-config --from-literal=special.how=very --from-literal=special.type=charm
在 Pod 中掛載 ConfigMap
使用卷掛載的方式來使用 ConfigMap
創建 ConfigMap,名為 special-config
cat configmap-multikeys.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: special-config
namespace: default
data:
SPECIAL_LEVEL: very
SPECIAL_TYPE: charm
創建 Pod
cat pod-configmap-volume.yaml
apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
spec:
containers:
- name: test-container
image: busybox
command: [ "/bin/sh", "-c", "ls /etc/config/" ]
volumeMounts:
- name: config-volume
mountPath: /etc/config
volumes:
- name: config-volume
configMap:
name: special-config
restartPolicy: Never
在 Pod 組態檔中,創建一個卷,名為 config-volume,該卷掛載了名為 special-config 的 configMap
在容器中,將名為 config-volume 的卷掛載到系統路徑 /etc/config 中,
查看結果
該容器執行了命令 ls /etc/config/,我們可以看下容器日志
# kubectl logs dapi-test-pod
SPECIAL_LEVEL
SPECIAL_TYPE
可以看到 /etc/config 目錄下出現了以 ConfigMap key 為名稱的檔案,內容為ConfigMap key對應的value
結束語
- 使用卷模式掛載的 ConfigMap 可以自動更新,
- 使用環境變數模式的 ConfigMap 無法自動更新,
- 可以把 ConfigMap 理解為 Linux 中的 /etc 目錄,
- ConfigMap 必須先于參考的 Pod 存在,否則 Pod 無法啟動,
- 每個 ConfigMap 只能被同一命名空間中的 Pod 參考,
聯系我
微信公眾號:zuolinux_com

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/78501.html
標籤:其他
下一篇:K8S實戰(十五)| 存盤卷概念
