【云原生Kubernetes】11-ConfigMap解析
簡介
ConfigMap 是一種 API 對象,用來將非機(jī)密性的數(shù)據(jù)保存到鍵值對中。使用時(shí), Pods 可以將其用作環(huán)境變量、命令行參數(shù)或者存儲卷中的配置文件。
ConfigMap 將你的環(huán)境配置信息和 容器鏡像 解耦,便于應(yīng)用配置的修改。
ConfigMap 并不提供保密或者加密功能。 如果你想存儲的數(shù)據(jù)是機(jī)密的,請使用 Secret, 或者使用其他第三方工具來保證你的數(shù)據(jù)的私密性,而不是用 ConfigMap。
ConfigMap的作用
使用 ConfigMap 來將你的配置數(shù)據(jù)和應(yīng)用程序代碼分開。
比如,假設(shè)你正在開發(fā)一個(gè)應(yīng)用,它可以在你自己的電腦上(用于開發(fā))和在云上 (用于實(shí)際流量)運(yùn)行。 你的代碼里有一段是用于查看環(huán)境變量
DATABASE_HOST
,在本地運(yùn)行時(shí), 你將這個(gè)變量設(shè)置為localhost
,在云上,你將其設(shè)置為引用 Kubernetes 集群中的 公開數(shù)據(jù)庫組件的 服務(wù)。這讓你可以獲取在云中運(yùn)行的容器鏡像,并且如果有需要的話,在本地調(diào)試完全相同的代碼。
ConfigMap 在設(shè)計(jì)上不是用來保存大量數(shù)據(jù)的。在 ConfigMap 中保存的數(shù)據(jù)不可超過 1 MiB。如果你需要保存超出此尺寸限制的數(shù)據(jù),你可能希望考慮掛載存儲卷 或者使用獨(dú)立的數(shù)據(jù)庫或者文件服務(wù)。
了解ConfigMap和Pod
-
ConfigMap 是一個(gè) API 對象, 讓你可以存儲其他對象所需要使用的配置。 和其他 Kubernetes 對象都有一個(gè)
spec
不同的是,ConfigMap 使用data
和binaryData
字段。這些字段能夠接收鍵-值對作為其取值。data
和binaryData
字段都是可選的。data
字段設(shè)計(jì)用來保存 UTF-8 字符串,而binaryData
則被設(shè)計(jì)用來保存二進(jìn)制數(shù)據(jù)作為 base64 編碼的字串。 -
ConfigMap 的名字必須是一個(gè)合法的 DNS 子域名。
-
data
或binaryData
字段下面的每個(gè)鍵的名稱都必須由字母數(shù)字字符或者-
、_
或.
組成。在data
下保存的鍵名不可以與在binaryData
下出現(xiàn)的鍵名有重疊。
說明:
ConfigMap 應(yīng)該引用屬性文件,而不是替換它們。可以將 ConfigMap 理解為類似于 Linux
/etc
目錄及其內(nèi)容的東西。例如,如果你基于 ConfigMap 創(chuàng)建 Kubernetes 卷,則 ConfigMap 中的每個(gè)數(shù)據(jù)項(xiàng)都由該數(shù)據(jù)卷中的某個(gè)獨(dú)立的文件表示。
ConfigMap 的 data
字段包含配置數(shù)據(jù)。如下例所示,它可以簡單 (如用 --from-literal
的單個(gè)屬性定義)或復(fù)雜 (如用 --from-file
的配置文件或 JSON blob 定義)
apiVersion: v1
kind: ConfigMap
metadata:
creationTimestamp: 2016-02-18T19:14:38Z
name: example-config
namespace: default
data:
# 使用 --from-literal 定義的簡單屬性
example.property.1: hello
example.property.2: world
# 使用 --from-file 定義復(fù)雜屬性的例子
example.property.file: |-
property.1=value-1
property.2=value-2
property.3=value-3
創(chuàng)建ConfigMap
- 可以使用
kubectl create configmap
創(chuàng)建; - 可以在
xxx.yaml
中使用kind:configmap
資源來創(chuàng)建
1. 使用kubectl create configmap
創(chuàng)建
- 格式語法
kubectl create configmap <映射名稱> <數(shù)據(jù)源>
<映射名稱>
是為 ConfigMap 指定的名稱,<數(shù)據(jù)源>
是要從中提取數(shù)據(jù)的目錄、 文件或者字面值。ConfigMap 對象的名稱必須是合法的DNS 子域名
2. 使用configmap資源創(chuàng)建ConfigMap
- 定義configmap資源文件
apiVersion: v1
kind: ConfigMap
metadata:
name: my-config-map
data:
key1: value1
key2: value2
my-config-map
是要?jiǎng)?chuàng)建的 ConfigMap 的名稱,data
字段包含 ConfigMap 的鍵值對數(shù)據(jù)。
使用–from-file基于一個(gè)目錄來創(chuàng)建 ConfigMap
? 可以使用 kubectl create configmap
基于同一目錄中的多個(gè)文件創(chuàng)建 ConfigMap。 當(dāng)你基于目錄來創(chuàng)建 ConfigMap 時(shí),kubectl 識別目錄下文件名可以作為合法鍵名的文件, 并將這些文件打包到新的 ConfigMap 中。普通文件之外的所有目錄項(xiàng)都會被忽略 (例如:子目錄、符號鏈接、設(shè)備、管道等等)。
說明:
用于創(chuàng)建 ConfigMap 的每個(gè)文件名必須由可接受的字符組成,即:字母(
A
到Z
和a
到z
)、數(shù)字(0
到9
)、‘-’、‘_‘或’.’。 如果在一個(gè)目錄中使用kubectl create configmap
,而其中任一文件名包含不可接受的字符, 則kubectl
命令可能會失敗。
kubectl
命令在遇到不合法的文件名時(shí)不會打印錯(cuò)誤。
- 創(chuàng)建本地目錄:
mkdir -p configure-pod-container/configmap/
- 下載示例的配置
# 將示例文件下載到 `configure-pod-container/configmap/` 目錄
wget https://kubernetes.io/examples/configmap/game.properties -O configure-pod-container/configmap/game.properties
wget https://kubernetes.io/examples/configmap/ui.properties -O configure-pod-container/configmap/ui.properties
[root@master configmap]# ls -l
total 8
-rw-r--r-- 1 root root 157 May 29 02:44 game.properties
lrwxrwxrwx 1 root root 8 May 29 02:46 log -> /var/log
-rw-r--r-- 1 root root 83 May 29 02:44 ui.properties
- 創(chuàng)建configmap
[root@master ~]# kubectl create configmap game-config --from-file=configure-pod-container/configmap/
configmap/game-config created
[root@master ~]#
以上命令將
configure-pod-container/configmap
目錄下的所有文件,也就是game.properties
和ui.properties
打包到 game-config ConfigMap 中
- 查看configmap詳細(xì)信息
[root@master ~]# 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
Events: <none>
[root@master ~]#
configure-pod-container/configmap/
目錄中的game.properties
和ui.properties
文件出現(xiàn)在 ConfigMap 的data
部分;此時(shí)目 目錄中的log軟連接沒有創(chuàng)建到configmap中,應(yīng)證了:普通文件之外的所有目錄項(xiàng)都會被忽略 (例如:子目錄、符號鏈接、設(shè)備、管道等等)
- 查看configmap的yaml格式
[root@master ~]# kubectl get configmaps 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
metadata:
creationTimestamp: "2023-05-28T18:47:57Z"
name: game-config
namespace: default
resourceVersion: "316804"
uid: b99b03cf-d7d8-4f41-9eca-60edcf77369c
[root@master ~]#
使用–from-file基于文件創(chuàng)建 ConfigMap
? 可以使用 kubectl create configmap
基于單個(gè)文件或多個(gè)文件創(chuàng)建 ConfigMap
- 基于
單個(gè)文件
創(chuàng)建configmap
[root@master ~]# kubectl create configmap game-config-2 --from-file=configure-pod-container/configmap/game.properties
configmap/game-config-2 created
[root@master ~]#
- 查看詳細(xì)信息
[root@master ~]# kubectl get configmap game-config-2
NAME DATA AGE
game-config-2 1 26s
[root@master ~]#
[root@master ~]# kubectl describe configmap 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
Events: <none>
[root@master ~]#
可以多次使用 --from-file
參數(shù),從多個(gè)數(shù)據(jù)源創(chuàng)建 ConfigMap。
- 基于
多個(gè)文件
創(chuàng)建configmap
[root@master ~]# kubectl create configmap game-config-2 --from-file=configure-pod-container/configmap/game.properties --from-file=configure-pod-container/configmap/ui.properties
configmap/game-config-2 created
[root@master ~]#
- 查看詳細(xì)信息
[root@master ~]# kubectl get configmap game-config-2 -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
metadata:
creationTimestamp: "2023-05-28T19:00:24Z"
name: game-config-2
namespace: default
resourceVersion: "317879"
uid: 4423fe0b-fb74-4d2d-8b37-e435bbc173d2
[root@master ~]#
定義從文件創(chuàng)建 ConfigMap 時(shí)要使用的鍵
? 在使用 --from-file
參數(shù)時(shí),你可以定義在 ConfigMap 的 data
部分出現(xiàn)鍵名, 而不是按默認(rèn)行為使用文件名。
kubectl create configmap configmap-name --from-file=<我的鍵名>=<文件路徑>
<我的鍵名>
是你要在 ConfigMap 中使用的鍵名,<文件路徑>
是你想要鍵所表示的數(shù)據(jù)源文件的位置。
- 創(chuàng)建示例
[root@master configmap]# kubectl create configmap test --from-file=text11=game.properties
configmap/test created
[root@master configmap]#
- 查看詳細(xì)信息
[root@master configmap]# kubectl describe configmap test
Name: test
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
text11:
----
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>
[root@master configmap]#
使用–from-env-file選項(xiàng)基于 env 文件創(chuàng)建 ConfigMap
Env 文件包含環(huán)境變量列表。其中適用以下語法規(guī)則:
- Env 文件中的每一行必須為 VAR=VAL 格式;
- 以#開頭的行(即注釋)將被忽略;
- 空行將被忽略;
- 引號不會被特殊處理(即它們將成為 ConfigMap 值的一部分)。
- 將示例文件下載
# 將示例文件下載到 `configure-pod-container/configmap/` 目錄
wget https://kubernetes.io/examples/configmap/game-env-file.properties -O configure-pod-container/configmap/game-env-file.properties
wget https://kubernetes.io/examples/configmap/ui-env-file.properties -O configure-pod-container/configmap/ui-env-file.properties
- Env 文件
game-env-file.properties
如下所示
[root@master ~]# 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
[root@master ~]#
- 創(chuàng)建configmap
[root@master ~]# kubectl create configmap game-config-env-file --from-env-file=configure-pod-container/configmap/game-env-file.properties
configmap/game-config-env-file created
[root@master ~]#
- 查看詳細(xì)信息
[root@master ~]# kubectl describe configmap game-config-env-file
Name: game-config-env-file
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
lives:
----
3
allowed:
----
"true"
enemies:
----
aliens
Events: <none>
[root@master ~]#
- 查看yaml信息
[root@master ~]# kubectl get configmap game-config-env-file -o yaml
apiVersion: v1
data:
allowed: '"true"'
enemies: aliens
lives: "3"
kind: ConfigMap
metadata:
creationTimestamp: "2023-05-28T19:26:22Z"
name: game-config-env-file
namespace: default
resourceVersion: "320111"
uid: b7651222-2a42-4701-8a48-201a8492b0f5
[root@master ~]#
從 Kubernetes 1.23 版本開始,
kubectl
支持多次指定--from-env-file
參數(shù)來從多個(gè)數(shù)據(jù)源創(chuàng)建 ConfigMap。
使用–from-literal根據(jù)字面值創(chuàng)建 ConfigMap
可以將 kubectl create configmap
與 --from-literal
參數(shù)一起使用, 通過命令行定義文字值。
[root@master configmap]# kubectl create configmap special-config --from-literal=special.how=very --from-literal=special.type=charm
configmap/special-config created
[root@master configmap]#
可以傳入多個(gè)鍵值對。命令行中提供的每對鍵值在 ConfigMap 的 data
部分中均表示為單獨(dú)的條目。
[root@master configmap]# kubectl get configmaps special-config -o yaml
apiVersion: v1
data:
special.how: very
special.type: charm
kind: ConfigMap
metadata:
creationTimestamp: "2023-05-28T19:40:30Z"
name: special-config
namespace: default
resourceVersion: "321325"
uid: 431d078c-80c0-4b75-a49a-29cb66dbe859
[root@master configmap]#
基于生成器創(chuàng)建 ConfigMap
- 還可以基于生成器(Generators)創(chuàng)建 ConfigMap,然后將其應(yīng)用于集群的 API 服務(wù)器上創(chuàng)建對象。 生成器應(yīng)在目錄內(nèi)的
kustomization.yaml
中指定。 - 個(gè)人任務(wù)使用生成器(Generators)創(chuàng)建 ConfigMap沒有太大必要,直接使用上面的方法即可,如果想了解請查閱官方文檔
使用ConfigMap
使用 ConfigMap 數(shù)據(jù)定義容器環(huán)境變量
在Pod中通過env來設(shè)置
1. 在 ConfigMap 中將環(huán)境變量定義為鍵值對:
- 創(chuàng)建configmap
[root@master configmap]# kubectl create configmap special-config --from-literal=special.how=very --from-literal=special.type=charm
configmap/special-config created
[root@master configmap]#
- 查看configmap
[root@master ~]# kubectl describe configmap special-config
Name: special-config
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
special.how:
----
very
special.type:
----
charm
Events: <none>
[root@master ~]#
- 創(chuàng)建Pod使用configmap定義容器的環(huán)境變量
apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
spec:
containers:
- name: test-container
image: busybox
command: [ "/bin/sh", "-c", "env" ]
env:
# 定義環(huán)境變
- name: xhz
valueFrom:
configMapKeyRef:
# ConfigMap 包含你要賦給 xhz 的值
name: special-config
# 指定與取值相關(guān)的鍵名
key: special.how
- name: flf
valueFrom:
configMapKeyRef:
# ConfigMap 包含你要賦給 flf 的值
name: special-config
# 指定與取值相關(guān)的鍵名
key: special.type
restartPolicy: Never
- 創(chuàng)建pod,并查看pod日志
[root@master configmap]# kubectl apply -f pod1.yml
pod/dapi-test-pod created
[root@master configmap]#
[root@master configmap]# kubectl logs dapi-test-pod |grep xhz
xhz=very
[root@master configmap]# kubectl logs dapi-test-pod |grep flf
flf=charm
[root@master configmap]#
在Pod中使用envFrom來設(shè)置
- 查看configmap
[root@master configmap]# kubectl describe configmap game-config-env-file
Name: game-config-env-file
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
allowed:
----
"true"
enemies:
----
aliens
lives:
----
3
Events: <none>
[root@master configmap]#
- 創(chuàng)建Pod使用configmap定義容器的環(huán)境變量
apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod-2
spec:
containers:
- name: test-container-2
image: busybox
command: [ "/bin/sh", "-c", "env" ]
envFrom:
- configMapRef:
name: game-config-env-file
restartPolicy: Never
- 創(chuàng)建pod。查看信息
[root@master configmap]#
[root@master configmap]# kubectl apply -f pod2.yaml
pod/dapi-test-pod-2 created
[root@master configmap]# kubectl logs dapi-test-pod-2 |grep allowed
allowed="true"
[root@master configmap]# kubectl logs dapi-test-pod-2 |grep enemies
enemies=aliens
[root@master configmap]#
[root@master configmap]# kubectl logs dapi-test-pod-2 |grep lives
lives=3
[root@master configmap]#
總結(jié)
在Pod中通過
env
來導(dǎo)入configmap中的數(shù)據(jù),作為容器變量:
- 可以指定環(huán)境變量的key,然后將configmap中的value作為環(huán)境變量的value;
- 需要使用
valueFrom. configMapKeyRef
屬性將configmap中的值一個(gè)個(gè)導(dǎo)入為環(huán)境變量;- 可以多次使用
valueFrom
在Pod中通過
envFrom
來導(dǎo)入configmap中的數(shù)據(jù),作為容器變量:
- 使用
envFrom.configMapRef
,可以一次性將config中的所有值設(shè)置為容器的環(huán)境變量;- 環(huán)境變量的key為configmap中的key,value為configmap中 的value
將 ConfigMap 數(shù)據(jù)添加到容器的文件
- 上面所述的,將configmap數(shù)據(jù)添加到容器的環(huán)境變量,而這些configmap都是通過–from-env-file,字面鍵值對來創(chuàng)建的;然后那些通過
--from-file
創(chuàng)建 ConfigMap 時(shí),文件名成為存儲在 ConfigMap 的data
部分中的鍵, 文件內(nèi)容成為鍵對應(yīng)的值,這個(gè)時(shí)候就需要通過volume來使用configmap中的數(shù)據(jù)。(當(dāng)然通過–from-env-file,字面鍵值對來創(chuàng)建的configmap,也可以通過volume來導(dǎo)入到容器的文件中) - 數(shù)據(jù)會展現(xiàn)為 UTF-8 字符編碼的文件。如果使用其他字符編碼, 可以使用
binaryData
.(詳情參閱 ConfigMap 對象)。
通過volumes來使用configmap中的數(shù)據(jù)
1. --from-env-file來創(chuàng)建的configmap
- 創(chuàng)建configmap
[root@master ~]# kubectl create configmap game-config-env-file --from-env-file=configure-pod-container/configmap/game-env-file.properties
configmap/game-config-env-file created
[root@master ~]#
[root@master ~]# kubectl describe configmap game-config-env-file
Name: game-config-env-file
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
allowed:
----
"true"
enemies:
----
aliens
lives:
----
3
Events: <none>
[root@master ~]#
- 通過valume將configmap中的數(shù)據(jù)導(dǎo)入
apiVersion: v1
kind: Pod
metadata:
name: test-pod-3
spec:
containers:
- name: test-container-3
image: busybox
imagePullPolicy: IfNotPresent
command: [ "/bin/sh", "-c", "sleep 3600"]
volumeMounts:
- name: configmap-volume
mountPath: /etc/config
volumes:
- name: configmap-volume
configMap:
name: game-config-env-file
restartPolicy: Never
- 進(jìn)入容器查看數(shù)據(jù)
[root@master configmap]# kubectl exec -it test-pod-3 /bin/sh
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
/ # cd /etc/config/
/etc/config # ls
allowed enemies lives
/etc/config # ls -l
total 0
lrwxrwxrwx 1 root root 14 May 28 21:27 allowed -> ..data/allowed
lrwxrwxrwx 1 root root 14 May 28 21:27 enemies -> ..data/enemies
lrwxrwxrwx 1 root root 12 May 28 21:27 lives -> ..data/lives
/etc/config # cat allowed
/etc/config # cat enemies
aliens/etc/config #
/etc/config # cat lives
3/etc/config #
此時(shí)configmap中的key作為了容器中的文件名,configmap中的valume稱為了容器中文件的數(shù)據(jù)
2. --from-literal來創(chuàng)建的configmap
- 創(chuàng)建的configmap
[root@master configmap]# kubectl create configmap special-config --from-literal=special.how=very --from-literal=special.type=charm
configmap/special-config created
[root@master configmap]#
[root@master configmap]# kubectl describe configmap special-config
Name: special-config
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
special.how:
----
very
special.type:
----
charm
Events: <none>
[root@master configmap]#
- 通過valume將configmap中的數(shù)據(jù)導(dǎo)入
apiVersion: v1
kind: Pod
metadata:
name: test-pod-4
spec:
containers:
- name: test-container-4
image: busybox
imagePullPolicy: IfNotPresent
command: [ "/bin/sh", "-c", "sleep 3600"]
volumeMounts:
- name: configmap-volume
mountPath: /etc/config
volumes:
- name: configmap-volume
configMap:
name: special-config
restartPolicy: Never
- 創(chuàng)建并進(jìn)入容器查看數(shù)據(jù)
[root@master configmap]# kubectl apply -f pod4.yaml
pod/test-pod-4 created
[root@master configmap]#
[root@master configmap]# kubectl exec -it test-pod-4 /bin/sh
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
/ # cd /etc/config/
/etc/config # ls -l
total 0
lrwxrwxrwx 1 root root 18 May 28 21:44 special.how -> ..data/special.how
lrwxrwxrwx 1 root root 19 May 28 21:44 special.type -> ..data/special.type
/etc/config # cat special.how
very
/etc/config #
/etc/config # cat special.type
charm
etc/config #
/etc/config #
此時(shí)configmap中的key作為了容器中的文件名,configmap中的valume稱為了容器中文件的數(shù)據(jù)
3. --from-file來創(chuàng)建的configmap
- 查看confimap
[root@master configmap]# kubectl describe configmap 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
Events: <none>
[root@master configmap]#
- 通過valume將configmap中的數(shù)據(jù)導(dǎo)入
apiVersion: v1
kind: Pod
metadata:
name: test-pod-5
spec:
containers:
- name: test-container-5
image: busybox
imagePullPolicy: IfNotPresent
command: [ "/bin/sh", "-c", "sleep 3600"]
volumeMounts:
- name: configmap-volume
mountPath: /etc/config
volumes:
- name: configmap-volume
configMap:
name: game-config
restartPolicy: Never
- 創(chuàng)建并進(jìn)入容器查看數(shù)據(jù)
[root@master configmap]# kubectl exec -it test-pod-5 /bin/sh
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
/ # cd /etc/config/
/etc/config # ls -l
total 0
lrwxrwxrwx 1 root root 22 May 28 21:49 game.properties -> ..data/game.properties
lrwxrwxrwx 1 root root 20 May 28 21:49 ui.properties -> ..data/ui.properties
/etc/config # cat 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/etc/config #
/etc/config # cat ui.properties
color.good=purple
color.bad=yellow
allow.textmode=true
how.nice.to.look=fairlyNice
/etc/config #
/etc/config #
此時(shí)configmap中的key作為了容器中的文件名,configmap中的valume稱為了容器中文件的數(shù)據(jù)
將 ConfigMap 數(shù)據(jù)添加到卷中的特定文件
? 使用 path
字段為特定的 ConfigMap 項(xiàng)目指定預(yù)期的文件路徑。 在這里,ConfigMap 中鍵 SPECIAL_LEVEL
的內(nèi)容將掛載在 config-volume
卷中 /etc/config/keys
文件中。
apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
spec:
containers:
- name: test-container
image: registry.k8s.io/busybox
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
注意:
如前,
/etc/config/
目錄中所有先前的文件都將被刪除。
ConfigMap其他說明
可選的 ConfigMap
你可以在 Pod 規(guī)約中將對 ConfigMap 的引用標(biāo)記為 可選(optional)。 如果 ConfigMap 不存在,那么它在 Pod 中為其提供數(shù)據(jù)的配置(例如:環(huán)境變量、掛載的卷)將為空。 如果 ConfigMap 存在,但引用的鍵不存在,那么數(shù)據(jù)也是空的。
以下 Pod 規(guī)約將 ConfigMap 中的環(huán)境變量標(biāo)記為可選
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 # 將環(huán)境變量標(biāo)記為可選
restartPolicy: Never
當(dāng)你運(yùn)行這個(gè) Pod 并且名稱為
a-config
的 ConfigMap 不存在時(shí),輸出空值。 當(dāng)你運(yùn)行這個(gè) Pod 并且名稱為a-config
的 ConfigMap 存在, 但是在 ConfigMap 中沒有名稱為akey
的鍵時(shí),控制臺輸出也會為空。 如果你確實(shí)在名為a-config
的 ConfigMap 中為akey
設(shè)置了鍵值, 那么這個(gè) Pod 會打印該值,然后終止。
你也可以在 Pod 規(guī)約中將 ConfigMap 提供的卷和文件標(biāo)記為可選。 此時(shí) Kubernetes 將總是為卷創(chuàng)建掛載路徑,即使引用的 ConfigMap 或鍵不存在。
例如,以下 Pod 規(guī)約將所引用得 ConfigMap 的卷標(biāo)記為可選:
ConfigMap限制
-
在 Pod 規(guī)約中引用某個(gè)
ConfigMap
之前,必須先創(chuàng)建這個(gè)對象, 或者在 Pod 規(guī)約中將 ConfigMap 標(biāo)記為optional
(請參閱可選的 ConfigMaps)。 如果所引用的 ConfigMap 不存在,并且沒有將應(yīng)用標(biāo)記為optional
則 Pod 將無法啟動(dòng)。 同樣,引用 ConfigMap 中不存在的主鍵也會令 Pod 無法啟動(dòng),除非你將 Configmap 標(biāo)記為optional
。 -
如果你使用
envFrom
來基于 ConfigMap 定義環(huán)境變量,那么無效的鍵將被忽略。 Pod 可以被啟動(dòng),但無效名稱將被記錄在事件日志中(InvalidVariableNames
)。 日志消息列出了每個(gè)被跳過的鍵。例如:
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 位于確定的命名空間中。 每個(gè) ConfigMap 只能被同一名字空間中的 Pod 引用.
-
你不能將 ConfigMap 用于靜態(tài) Pod, 因?yàn)?Kubernetes 不支持這種用法。
ConfigMap操作實(shí)例
使用 ConfigMap 來配置mysql
該示例完成一下幾個(gè)功能:
- 給mysql傳遞
MYSQL_ROOT_PASSWORD
參數(shù),設(shè)置root密碼;- 傳遞
MYSQL_DATABASE
參數(shù),新建一個(gè)庫;- 傳遞
MYSQL_USER
和MYSQL_PASSWORD
,新建用戶和密碼。無實(shí)際作用,只是展示用法
- 創(chuàng)建mysql的configmap文件
[root@master configmap]# kubectl delete -f mysql-pod.yml
pod "mysql-pod" deleted
[root@master configmap]# cat mysql-map
MYSQL_ROOT_PASSWORD=root123
MYSQL_DATABASE=test
MYSQL_USER=uos
MYSQL_PASSWORD=uos123
[root@master configmap]#
[root@master configmap]# kubectl create configmap mysql-map --from-env-file=mysql-map
configmap/mysql-map created
[root@master configmap]#
[root@master configmap]#
[root@master configmap]# kubectl describe configmap mysql-map
Name: mysql-map
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
MYSQL_PASSWORD:
----
uos123
MYSQL_ROOT_PASSWORD:
----
root123
MYSQL_USER:
----
uos
MYSQL_DATABASE:
----
test
Events: <none>
[root@master configmap]#
- 創(chuàng)建mysql pod的資源文件
apiVersion: v1
kind: Pod
metadata:
name: mysql-pod
spec:
containers:
- name: mysql-container
image: mysql:5.7
imagePullPolicy: IfNotPresent
envFrom:
- configMapRef:
name: mysql-map
ports:
- containerPort: 3306
##當(dāng)然也可以使用env逐個(gè)導(dǎo)入
env:
- name: MYSQL_ROOT_PASSWORD
valumeFrom:
configMapKeyRef:
name: mysql-map
key: MYSQL_ROOT_PASSWORD
- name: MYSQL_DATABASE
valumeFrom:
configMapKeyRef:
name: mysql-map
key: MYSQL_DATABASE
.....
- 創(chuàng)建Pod,查看Pod信息
[root@master configmap]# kubectl apply -f mysql-pod.yml
pod/mysql-pod created
[root@master configmap]#
[root@master configmap]# kubectl get pods mysql-pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
mysql-pod 1/1 Running 0 26s 10.244.2.102 192.168.194.131 <none> <none>
[root@master configmap]#
- 登錄測試
[root@master configmap]# mysql -uroot -proot123 -h10.244.2.102
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.36 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test |
+--------------------+
5 rows in set (0.00 sec)
MySQL [(none)]> exit
Bye
[root@master configmap]# mysql -uuos -puos123 -h10.244.2.102
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.36 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(none)]> Ctrl-C -- exit!
Aborted
[root@master configmap]#
為了完善一點(diǎn),我們還可以將my.cnf的配置文件通過–from-file的形式創(chuàng)建為configmap,然后再Pod中通過volume的方式掛載到容器里面。
注意:
當(dāng)我們創(chuàng)建Pod使用的mysql的鏡像為8.x版本的時(shí)候,但是我們本地的mysql-client客戶端又是5.x的時(shí)候,本地登錄mysql時(shí)可能會出現(xiàn)如下報(bào)錯(cuò):
[root@master configmap]# mysql -uroot -h 10.244.2.99 -proot123 ERROR 2059 (HY000): Authentication plugin 'caching_sha2_password' cannot be loaded: /usr/lib64/mysql/plugin/caching_sha2_password.so: cannot open shared object file: No such file or directory
報(bào)錯(cuò)原因:
這個(gè)錯(cuò)誤是因?yàn)?MySQL 8.0 引入了一個(gè)新的身份驗(yàn)證插件 caching_sha2_password,而舊版本的 MySQL 客戶端可能無法加載該插件。
使用 ConfigMap 來配置 Redis
實(shí)現(xiàn)功能:
- 使用 ConfigMap 中的數(shù)據(jù)來配置 Redis 緩存
- 創(chuàng)建ConfigMap
[root@master configmap]# cat example-redis-config.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: example-redis-config
data:
redis-config: ""
[root@master configmap]#
[root@master configmap]# kubectl apply -f example-redis-config.yaml
configmap/example-redis-config created
[root@master configmap]#
[root@master configmap]#
[root@master configmap]# kubectl describe configmap example-redis-config
Name: example-redis-config
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
redis-config:
----
Events: <none>
[root@master configmap]#
### 數(shù)據(jù)redis-config為空
- 創(chuàng)建redis Pod資源文件
[root@master configmap]# cat redis-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: redis
spec:
containers:
- name: redis
image: redis:5.0.4
command:
- redis-server
- "/redis-master/redis.conf"
env:
- name: MASTER
value: "true"
ports:
- containerPort: 6379
resources:
limits:
cpu: "0.1"
volumeMounts:
- name: data
mountPath: /redis-master-data
- name: config
mountPath: /redis-master
volumes:
- name: data
emptyDir: {}
- name: config
configMap:
name: example-redis-config
items:
- key: redis-config
path: redis.conf
[root@master configmap]#
[root@master configmap]#
- 由
spec.volumes[1]
創(chuàng)建一個(gè)名為config
的卷。spec.volumes[1].items[0]
下的key
和path
會將來自example-redis-config
ConfigMap 中的redis-config
密鑰公開在config
卷上一個(gè)名為redis.conf
的文件中。- 然后
config
卷被spec.containers[0].volumeMounts[1]
掛載在/redis-master
。這樣做的最終效果是將上面
example-redis-config
配置中data.redis-config
的數(shù)據(jù)作為 Pod 中的/redis-master/redis.conf
公開。
- 查看Pod信息
[root@master configmap]# kubectl apply -f redis-pod.yaml
pod/redis created
[root@master configmap]#
[root@master configmap]# kubectl get pods redis -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
redis 1/1 Running 0 3m41s 10.244.1.46 192.168.194.130 <none> <none>
[root@master configmap]#
- 使用
kubectl exec
進(jìn)入 pod,運(yùn)行redis-cli
工具檢查當(dāng)前配置:
[root@master configmap]# kubectl exec -it redis -- redis-cli
127.0.0.1:6379> CONFIG GET maxmemory
1) "maxmemory"
2) "0"
127.0.0.1:6379> CONFIG GET maxmemory-policy
1) "maxmemory-policy"
2) "noeviction"
127.0.0.1:6379>
- 它也應(yīng)該顯示默認(rèn)值
noeviction
- maxmemory顯示默認(rèn)值 0
- 現(xiàn)在,向
example-redis-config
ConfigMap 添加一些配置:
apiVersion: v1
kind: ConfigMap
metadata:
name: example-redis-config
data:
redis-config: |
maxmemory 2mb
maxmemory-policy allkeys-lru
- 更新configmap,并查看
[root@master configmap]# kubectl apply -f example-redis-config.yaml
configmap/example-redis-config configured
[root@master configmap]#
[root@master configmap]#
[root@master configmap]# kubectl describe configmap example-redis-config
Name: example-redis-config
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
redis-config:
----
maxmemory 2mb
maxmemory-policy allkeys-lru
Events: <none>
[root@master configmap]#
- 再次驗(yàn)證redis
[root@master configmap]# kubectl exec -it redis -- redis-cli
127.0.0.1:6379> CONFIG GET maxmemory
1) "maxmemory"
2) "2097152"
127.0.0.1:6379> CONFIG GET maxmemory-policy
1) "maxmemory-policy"
2) "allkeys-lru"
127.0.0.1:6379>
? 當(dāng) ConfigMap 發(fā)生變化時(shí),Pod 中使用該 ConfigMap 的容器不會自動(dòng)更新其配置。但是,您可以使用 Kubernetes 提供的一些機(jī)制來讓 Pod 中的容器更新其配置。文章來源:http://www.zghlxwxcb.cn/news/detail-525293.html
? 下面是幾種更新 Pod 配置的方法:文章來源地址http://www.zghlxwxcb.cn/news/detail-525293.html
- 使用
kubectl rollout restart
命令重啟 Pod,這個(gè)命令會將 Pod 中的所有容器停止,然后重新啟動(dòng)它們。當(dāng)容器重新啟動(dòng)時(shí),它們將使用最新的 ConfigMap 配置。例如,要重啟名為my-pod
的 Pod,您可以使用以下命令:kubectl rollout restart deployment my-pod
- 在容器中重新加載配置:許多應(yīng)用程序支持在運(yùn)行時(shí)重新加載其配置。如果您的容器支持這個(gè)功能,您可以在 ConfigMap 發(fā)生變化時(shí),觸發(fā)容器重新加載其配置。例如,您可以使用
kubectl exec
命令在容器中運(yùn)行一個(gè)命令,以重新加載配置。例如,如果您的容器支持SIGHUP
信號來重新加載配置,則可以使用以下命令:kubectl exec my-pod -- kill -HUP當(dāng) ConfigMap 發(fā)生變化時(shí),Pod 中使用該 ConfigMap 的容器不會自動(dòng)更新其配置。但是,您可以使用 Kubernetes 提供的一些機(jī)制來讓 Pod 中的容器更新其配置。
到了這里,關(guān)于【云原生|Kubernetes】11-ConfigMap解析的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!