Kubernetes(k8s)ConfigMap詳解及應用

大數據老司機 發佈 2022-07-08T17:08:19.730956+00:00

可以使用 kubectl create configmap 從文件、目錄或者 key-value 字符串創建等創建 ConfigMap。

一、ConfigMap概述

ConfigMap是k8s的一個配置管理組件,可以將配置以key-value的形式傳遞,通常用來保存不需要加密的配置信息,加密信息則需用到Secret,主要用來應對以下場景:

  • 使用k8s部署應用,當你將應用配置寫進代碼中,就會存在一個問題,更新配置時也需要打包鏡像,ConfigMap可以將配置信息和docker鏡像解耦
  • 使用微服務架構的話,存在多個服務共用配置的情況,如果每個服務中單獨一份配置的話,那麼更新配置就很麻煩,使用ConfigMap可以友好的進行配置共享

二、ConfigMap創建

可以使用 kubectl create configmap 從文件、目錄或者 key-value 字符串創建等創建 ConfigMap。

1)通過命令行創建configmap(key-value鍵值對)

$ kubectl create configmap configmapname --from-literal=key=value
# 獲取整個configmap 數據
$ kubectl get configmap configmapname -o go-template='{{.data}}'
# 查看詳情
$ kubectl describe configmap configmapname
# 獲取具體某個key值
$ kubectl get configmap configmapname -o go-template='{{.data.key}}'
# 刪除
$ kubectl delete configmap configmapname
# 再查看
$ kubectl get configmap configmapname

2)通過文件創建configmap

$ echo hello > test1.txt
$ ehco world > test2.txt
$ kubectl create configmap my-config --from-file=key1=test1.txt  --from-file=key2=test2.txt
$ kubectl describe configmap my-config

看到該configmap中有兩個鍵值對,key1:hello 和 key2:world

3)通過文件夾創建configmap

$ mkdir config
$ echo hello > config/test1
$ echo world > config/test2
$ kubectl create configmap dir-config --from-file=config/
$ kubectl describe configmap dir-config

4)通過yaml文件創建

$ cat << EOF > config.yaml
apiVersion: v1
kind: ConfigMap
metadata:
 name: my-config2
data:
 key1: hello
 key2: world
EOF

執行

$ kubectl create -f config.yaml
$ kubectl describe configmap my-config2

三、ConfigMap簡單使用

Pod可以通過三種方式來使用ConfigMap,分別為:

  • 將ConfigMap中的數據設置為環境變量
  • 將ConfigMap中的數據設置為命令行參數
  • 使用Volume將ConfigMap作為文件或目錄掛載

【注意】

  • ConfigMap必須在Pod使用它之前創建
  • 使用envFrom時,將會自動忽略無效的鍵
  • Pod只能使用同一個命名空間的ConfigMap

1)用作環境變量

首先創建兩個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
apiVersion: v1
kind: Pod
metadata:
  name: test-pod
spec:
  containers:
    - name: test-container
      image: 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

執行

$ kubectl apply -f test-pod.yaml
$ kubectl get pod test-pod
# 查看pod日誌輸出env
$ kubectl logs test-pod

當pod運行結束後,環境變量中會多輸出如下:

SPECIAL_LEVEL_KEY=very
SPECIAL_TYPE_KEY=charm
log_level=INFO

2)用作命令行參數

將ConfigMap用作命令行參數時,需要先把ConfigMap的數據保存在環境變量中,然後通過$(VAR_NAME)的方式引用環境變量。

#test-pod-command-args.yaml
apiVersion: v1
kind: Pod
metadata:
  name: test-pod-command-args
spec:
  containers:
    - name: test-container
      image: 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

執行

$ kubectl apply -f test-pod-command-args.yaml
$ kubectl get pod test-pod-command-args -o wide
$ kubectl get pod test-pod-command-args -o wide

另開窗口執行,容器沒起來,執行下面命令會報錯

$ kubectl logs -f test-pod-command-arg

當pod運行結束後,它的輸出如下:

very charm

3)使用volume將ConfigMap作為文件或目錄直接掛載

【示例1】將創建的ConfigMap直接掛載至Pod的/etc/config目錄下,其中每一個key-value鍵值對都會生成一個文件,key為文件名,value為內容

$ cat << EOF >test-pod-mount-volume.yaml
apiVersion: v1
kind: Pod
metadata:
  name: test-pod-mount-volume
spec:
  containers:
    - name: test-container
      image: 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
EOF

執行

$ kubectl apply -f test-pod-mount-volume.yaml
$ kubectl get pod test-pod-mount-volume -o wide

當Pod結束後會輸出:

$ kubectl logs test-pod-mount-volume

【示例2】將創建的ConfigMap中special.how這個key掛載到/etc/config目錄下的一個相對路徑/keys/special.level。如果存在同名文件,直接覆蓋

$ cat << EOF >test-pod-mount-volume-path.yaml
apiVersion: v1
kind: Pod
metadata:
  name: test-pod-mount-volume-path
spec:
  containers:
    - name: test-container
      image: 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
EOF

執行

$ kubectl apply -f test-pod-mount-volume-path.yaml
$ kubectl get pod test-pod-mount-volume-path -o wide

當Pod結束後會輸出:

$ kubectl logs test-pod-mount-volume-path

【示例3】在一般情況下 configmap 掛載文件時,會先覆蓋掉掛載目錄,然後再將 congfigmap 中的內容作為文件掛載進行。如果想不對原來的文件夾下的文件造成覆蓋,只是將 configmap 中的每個 key,按照文件的方式掛載到目錄下,可以使用 subpath 參數

$ cat << EOF >test-pod-mount-volume-subpath.yaml
apiVersion: v1
kind: Pod
metadata:
  name: test-pod-mount-volume-subpath
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
EOF

執行

$ kubectl apply -f test-pod-mount-volume-subpath.yaml
$ kubectl get pod test-pod-mount-volume-subpath -o wide

當Pod正在運行中進入pod中查看

$ kubectl exec -ti test-pod-mount-volume-subpath -- /bin/sh
# ls /etc/nginx/
# cat /etc/nginx/special.how

非交互式查看

$ kubectl exec -it test-pod-mount-volume-subpath -- ls /etc/nginx/
$ kubectl exec -it test-pod-mount-volume-subpath -- cat /etc/nginx/special.how
關鍵字: