Kubernetes(k8s)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

posted @ 2022-07-08 23:17  大数据老司机  阅读(2172)  评论(0编辑  收藏  举报