學(xué)習(xí)本節(jié)內(nèi)容前扮休,希望你已經(jīng)對(duì)Kubernetes中Volume的概念有了初步的了解,具體請(qǐng)參考這篇文章:
ConfigMap顧名思義墩弯,是用于保存配置數(shù)據(jù)的鍵值對(duì)吩跋,可以用來(lái)保存單個(gè)屬性,也可以保存配置文件渔工。
ConfigMap的創(chuàng)建
可以使用 kubectl create configmap
從文件锌钮、目錄或者 key-value 字符串創(chuàng)建等創(chuàng)建 ConfigMap。也可以通過(guò) kubectl create -f
從描述文件創(chuàng)建引矩。
從key-value字符串創(chuàng)建
$ kubectl create configmap special-config --from-literal=special.how=very
configmap "special-config" created
$ kubectl get configmap special-config -o go-template='{{.data}}'
map[special.how:very]
上面的命令創(chuàng)建了一個(gè)名為special-config梁丘,擁有一條key為special.how
,value為very
的鍵值對(duì)數(shù)據(jù)旺韭。
從env文件創(chuàng)建
$ echo -e "a=b\nc=d" | tee config.env
a=b
c=d
$ kubectl create configmap special-config --from-env-file=config.env
configmap "special-config" created
$ kubectl get configmap special-config -o go-template='{{.data}}'
map[a:b c:d]
上面的命令從一個(gè)env文件讀取鍵值對(duì)氛谜,然后存入一個(gè)名為special-config的ConfigMap中。
從目錄創(chuàng)建
$ mkdir config
$ echo a>config/a
$ echo b>config/b
$ kubectl create configmap special-config --from-file=config/
configmap "special-config" created
$ kubectl get configmap special-config -o go-template='{{.data}}'
map[a:a
b:b
]
上面的命令讀取config目錄下的所有文件区端,以文件名為key值漫,文件內(nèi)容為value,存入名為special-config的ConfigMap中织盼。
根據(jù)yaml描述文件創(chuàng)建
apiVersion: v1
kind: ConfigMap
metadata:
name: special-config
data:
special.how: very
special.type: charm
$ kubectl create -f config.yaml
configmap "special-config" created
ConfigMap的使用
Pod可以通過(guò)三種方式來(lái)使用ConfigMap杨何,分別為:
- 將ConfigMap中的數(shù)據(jù)設(shè)置為環(huán)境變量
- 將ConfigMap中的數(shù)據(jù)設(shè)置為命令行參數(shù)
- 使用Volume將ConfigMap作為文件或目錄掛載
注意!沥邻!
- ConfigMap必須在Pod使用它之前創(chuàng)建
- 使用envFrom時(shí)危虱,將會(huì)自動(dòng)忽略無(wú)效的鍵
- Pod只能使用同一個(gè)命名空間的ConfigMap
用作環(huán)境變量
首先創(chuàng)建兩個(gè)ConfigMap,分別名為special-config
和env-config
:
$ kubectl create configmap special-config --from-literal=special.how=very --from-literal=special.type=charm
$ kubectl create configmap env-config --from-literal=log_level=INFO
然后以環(huán)境變量方式引用:
apiVersion: v1
kind: Pod
metadata:
name: 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: special-config
key: special.how
- name: SPECIAL_TYPE_KEY
valueFrom:
configMapKeyRef:
name: special-config
key: special.type
envFrom:
- configMapRef:
name: env-config
restartPolicy: Never
當(dāng)pod運(yùn)行結(jié)束后谋国,它的輸出如下:
SPECIAL_LEVEL_KEY=very
SPECIAL_TYPE_KEY=charm
log_level=INFO
用作命令行參數(shù)
將ConfigMap用作命令行參數(shù)時(shí),需要先把ConfigMap的數(shù)據(jù)保存在環(huán)境變量中迁沫,然后通過(guò)$(VAR_NAME)的方式引用環(huán)境變量芦瘾。
apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
spec:
containers:
- name: test-container
image: gcr.io/google_containers/busybox
command: [ "/bin/sh", "-c", "echo $(SPECIAL_LEVEL_KEY) $(SPECIAL_TYPE_KEY)" ]
env:
- name: SPECIAL_LEVEL_KEY
valueFrom:
configMapKeyRef:
name: special-config
key: special.how
- name: SPECIAL_TYPE_KEY
valueFrom:
configMapKeyRef:
name: special-config
key: special.type
restartPolicy: Never
當(dāng)pod運(yùn)行結(jié)束后,它的輸出如下:
very charm
使用volume將ConfigMap作為文件或目錄直接掛載
將創(chuàng)建的ConfigMap直接掛載至Pod的/etc/config目錄下集畅,其中每一個(gè)key-value鍵值對(duì)都會(huì)生成一個(gè)文件近弟,key為文件名,value為內(nèi)容挺智。
apiVersion: v1
kind: Pod
metadata:
name: vol-test-pod
spec:
containers:
- name: test-container
image: gcr.io/google_containers/busybox
command: [ "/bin/sh", "-c", "cat /etc/config/special.how" ]
volumeMounts:
- name: config-volume
mountPath: /etc/config
volumes:
- name: config-volume
configMap:
name: special-config
restartPolicy: Never
當(dāng)Pod結(jié)束后會(huì)輸出:
very
將創(chuàng)建的ConfigMap中special.how這個(gè)key掛載到/etc/config目錄下的一個(gè)相對(duì)路徑/keys/special.level祷愉。如果存在同名文件,直接覆蓋赦颇。其他的key不掛載二鳄。
apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
spec:
containers:
- name: test-container
image: gcr.io/google_containers/busybox
command: [ "/bin/sh","-c","cat /etc/config/keys/special.level" ]
volumeMounts:
- name: config-volume
mountPath: /etc/config
volumes:
- name: config-volume
configMap:
name: special-config
items:
- key: special.how
path: keys/special.level
restartPolicy: Never
當(dāng)Pod結(jié)束后會(huì)輸出:
very
在一般情況下 configmap 掛載文件時(shí),會(huì)先覆蓋掉掛載目錄媒怯,然后再將 congfigmap 中的內(nèi)容作為文件掛載進(jìn)行订讼。如果想不對(duì)原來(lái)的文件夾下的文件造成覆蓋,只是將 configmap 中的每個(gè) key扇苞,按照文件的方式掛載到目錄下欺殿,可以使用 subpath 參數(shù)寄纵。
apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
spec:
containers:
- name: test-container
image: nginx
command: ["/bin/sh","-c","sleep 36000"]
volumeMounts:
- name: config-volume
mountPath: /etc/nginx/special.how
subPath: special.how
volumes:
- name: config-volume
configMap:
name: special-config
items:
- key: special.how
path: special.how
restartPolicy: Never
root@dapi-test-pod:/# ls /etc/nginx/
conf.d fastcgi_params koi-utf koi-win mime.types modules nginx.conf scgi_params special.how uwsgi_params win-utf
root@dapi-test-pod:/# cat /etc/nginx/special.how
very