ConigMap
什麼是ConfigMap
ConfigMap 採用 key-value 格式進行保存數據,一般用來保存非敏感數據,Pods可以將configmap作爲環境變量、命令行參數或卷中的配置文件使用。ConfigMap 將特定環境的配置從容器中解耦。
創建ConfigMap
-
從目錄創建
-
從文件創建
-
從envfile創建
-
從 literal values 創建
-
...
使用ConfigMap
-
以key-value爲例
創建 ConfigMap
kubectl create configmap special-config --from-literal=special.how=very
[root@master01 ~]# kubectl create configmap special-config --from-literal=special.how=very
configmap/special-config created
[root@master01 ~]# kubectl get configmap
NAME DATA AGE
kube-root-ca.crt 1 42d
special-config 1 9s
[root@master01 ~]# kubectl describe configmap special-config
Name: special-config
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
special.how:
----
very
Events: <none>
創建Pod
[root@master01 configmap]# cat configmap.yaml
apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
spec:
containers:
- name: test-container
image: busybox
command: [ "/bin/sh", "-c", "env" ]
env:
# Define the environment variable
- name: SPECIAL_LEVEL_KEY
valueFrom:
configMapKeyRef:
# The ConfigMap containing the value you want to assign to SPECIAL_LEVEL_KEY
name: special-config
# Specify the key associated with the value
key: special.how
restartPolicy: Never
查看pod信息
[root@master01 configmap]# kubectl describe po dapi-test-pod
Name: dapi-test-pod
Namespace: default
Priority: 0
Node: node01/192.168.44.13
Start Time: Tue, 06 Dec 2022 22:06:41 +0800
Labels: <none>
Annotations: <none>
Status: Succeeded
IP: 172.29.55.34
IPs:
IP: 172.29.55.34
Containers:
test-container:
Container ID: docker://341fdf9b58e1254265de902d6fd5e23be205fb66353e400174b7abd869afc2e7
Image: busybox
Image ID: docker-pullable://busybox@sha256:59f225fdf34f28a07d22343ee415ee417f6b8365cf4a0d3a2933cbd8fd7cf8c1
Port: <none>
Host Port: <none>
Command:
/bin/sh
-c
env
State: Terminated
Reason: Completed
Exit Code: 0
Started: Tue, 06 Dec 2022 22:07:01 +0800
Finished: Tue, 06 Dec 2022 22:07:01 +0800
Ready: False
Restart Count: 0
Environment:
SPECIAL_LEVEL_KEY: <set to the key 'special.how' of config map 'special-config'> Optional: false
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-c7jnm (ro)
Conditions:
Type Status
Initialized True
Ready False
ContainersReady False
PodScheduled True
Volumes:
default-token-c7jnm:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-c7jnm
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 6m32s default-scheduler Successfully assigned default/dapi-test-pod to node01
Normal Pulling 6m31s kubelet Pulling image "busybox"
Normal Pulled 6m12s kubelet Successfully pulled image "busybox" in 18.272935062s
Normal Created 6m12s kubelet Created container test-container
Normal Started 6m12s kubelet Started container test-container
- 使用 yaml 創建 configmap
[root@master01 configmap]# cat config-mutikeys.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: special-config
namespace: default
data:
SPECIAL_LEVEL: very
SPECIAL_TYPE: charm
[root@master01 configmap]# kubectl create -f config-mutikeys.yaml
[root@master01 configmap]# kubectl get configmap
NAME DATA AGE
kube-root-ca.crt 1 42d
special-config 3 19m
[root@master01 configmap]# kubectl describe configmap special-config
Name: special-config
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
SPECIAL_LEVEL:
----
very
SPECIAL_TYPE:
----
charm
Events: <none>
創建Pod
[root@master01 configmap]# cat muti-keys-demo.yaml
apiVersion: v1
kind: Pod
metadata:
name: config-map-demo
spec:
containers:
- name: test-config-map-1
image: busybox
command:
- sleep
- "3600"
envFrom:
- configMapRef:
name:
[root@master01 configmap]# cat muti-keys-demo.yaml
apiVersion: v1
kind: Pod
metadata:
name: config-map-demo
spec:
containers:
- name: test-config-map-1
image: busybox
command:
- sleep
- "3600"
envFrom:
- configMapRef:
name: special-config
[root@master01 configmap]# kubectl create -f muti-keys-demo.yaml
pod/config-map-demo created
[root@master01 configmap]# kubectl get po
NAME READY STATUS RESTARTS AGE
busybox 1/1 Terminating 8 33d
config-map-demo 1/1 Running 0 4s
nginx-deployment-5787596d54-42qfx 1/1 Running 0 50m
nginx-deployment-5787596d54-6ffh4 1/1 Terminating 3 28d
nginx-deployment-5787596d54-7m47n 1/1 Running 4 28d
nginx-deployment-5787596d54-cnjb8 1/1 Terminating 3 28d
nginx-deployment-5787596d54-d4lkw 1/1 Running 0 50m
[root@master01 configmap]# kubectl describe po config-map-demo
Name: config-map-demo
Namespace: default
Priority: 0
Node: node01/192.168.44.13
Start Time: Tue, 06 Dec 2022 22:38:37 +0800
Labels: <none>
Annotations: <none>
Status: Running
IP: 172.29.55.39
IPs:
IP: 172.29.55.39
Containers:
test-config-map-1:
Container ID: docker://d6c068ee4c3d771c0ce73f3be41fcb8abffe17f56b968974ed579af5b007edfc
Image: busybox
Image ID: docker-pullable://busybox@sha256:59f225fdf34f28a07d22343ee415ee417f6b8365cf4a0d3a2933cbd8fd7cf8c1
Port: <none>
Host Port: <none>
Command:
sleep
3600
State: Running
Started: Tue, 06 Dec 2022 22:38:40 +0800
Ready: True
Restart Count: 0
Environment Variables from:
special-config ConfigMap Optional: false
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-c7jnm (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
default-token-c7jnm:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-c7jnm
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 16s default-scheduler Successfully assigned default/config-map-demo to node01
Normal Pulling 15s kubelet Pulling image "busybox"
Normal Pulled 13s kubelet Successfully pulled image "busybox" in 1.951543384s
Normal Created 13s kubelet Created container test-config-map-1
Normal Started 13s kubelet Started container test-config-map-1
[root@master01 configmap]# kubectl exec -ti config-map-demo -- sh
/ # echo $SPECIAL_LEVEL
very
/ # echo $very
/ # echo $SPECIAL_TYPE
charm
用存儲在ConfigMap中的數據填充卷
[root@master01 configmap]# cat configmap-volume.yaml
apiVersion: v1
kind: Pod
metadata:
name: test-container-pod
spec:
containers:
- name: test-container-1
image: busybox
command:
- sleep
- "3600"
volumeMounts:
- name: config-volume
mountPath: /etc/config #掛載到 /etc/config
volumes:
- name: config-volume
configMap:
name: special-config
[root@master01 configmap]# kubectl create -f configmap-volume.yaml
pod/test-container-pod created
[root@master01 configmap]# kubectl get po
NAME READY STATUS RESTARTS AGE
busybox 1/1 Terminating 8 33d
nginx-deployment-5787596d54-42qfx 1/1 Running 0 63m
nginx-deployment-5787596d54-6ffh4 1/1 Terminating 3 28d
nginx-deployment-5787596d54-7m47n 1/1 Running 4 28d
nginx-deployment-5787596d54-cnjb8 1/1 Terminating 3 28d
nginx-deployment-5787596d54-d4lkw 1/1 Running 0 63m
test-container-pod 1/1 Running 0 4s
[root@master01 configmap]# kubectl exec -ti test-container-pod -- sh
/ # ls
bin dev etc home proc root sys tmp usr var
/ # ls /etc/
config/ group hostname hosts localtime mtab network/ passwd resolv.conf shadow
/ # ls /etc/config/
SPECIAL_LEVEL SPECIAL_TYPE special.how
/ # ll /etc/config/
sh: ll: not found
/ # ls -al /etc/config/
total 0
drwxrwxrwx 3 root root 119 Dec 6 14:51 .
drwxr-xr-x 1 root root 20 Dec 6 14:51 ..
drwxr-xr-x 2 root root 66 Dec 6 14:51 ..2022_12_06_14_51_09.874549422
lrwxrwxrwx 1 root root 31 Dec 6 14:51 ..data -> ..2022_12_06_14_51_09.874549422
lrwxrwxrwx 1 root root 20 Dec 6 14:51 SPECIAL_LEVEL -> ..data/SPECIAL_LEVEL
lrwxrwxrwx 1 root root 19 Dec 6 14:51 SPECIAL_TYPE -> ..data/SPECIAL_TYPE
lrwxrwxrwx 1 root root 18 Dec 6 14:51 special.how -> ..data/special.how
If there are some files in the /etc/config/
directory, they will be deleted.
注意事項
-
在 Pod 規約中引用某個
ConfigMap
之前,必須先創建這個對象, 或者在 Pod 規約中將 ConfigMap 標記爲optional
如果所引用的 ConfigMap 不存在,並且沒有將應用標記爲optional
則 Pod 將無法啓動。 -
如果你使用
envFrom
來基於 ConfigMap 定義環境變量,那麼無效的鍵將被忽略。 Pod 可以被啓動,但無效名稱將被記錄在事件日誌中(InvalidVariableNames
)kubectl get events
-
在 Pod 規約中將對 ConfigMap 的引用標記爲 可選(optional)。 如果 ConfigMap 不存在,那麼它在 Pod 中爲其提供數據的配置(例如環境變量、掛載的卷)將爲空。 如果 ConfigMap 存在,但引用的鍵不存在,那麼數據也是空的
-
當某個已被掛載的 ConfigMap 被更新,所對應得內容跟最終也會被更新。但是使用 ConfigMap 作爲 subPath 的數據卷不會更新