吾八哥博客

您现在的位置是:首页 > 云原生 > Kubernetes > 正文

Kubernetes

吾八哥学k8s(七):kubernetes里ConfigMap的用法

吾八哥2020-04-26Kubernetes3312

什么是ConfigMap?

kubernetes通过ConfigMap来实现对容器中应用的配置管理。ConfigMap是一个将配置文件、参数变量等配置信息绑定到Pod容器的组件,ConfigMap允许将配置与Pod和组件分开,然后通过挂载的方式进行使用。

ConfigMap的创建

支持从文件、目录、字符串值来创建ConfigMap,可以使用-n指定NameSpace,语法如下:

kubectl create configmap NAME [--from-file=[key=]source] [--from-literal=key1=value1] [--dry-run=server|client|none]

查看帮助信息的方法:kubectl create configmap -h,可以看到一些例子:

Examples:
  # Create a new configmap named my-config based on folder bar
  kubectl create configmap my-config --from-file=path/to/bar

  # Create a new configmap named my-config with specified keys instead of file basenames on disk
  kubectl create configmap my-config --from-file=key1=/path/to/bar/file1.txt --from-file=key2=/path/to/bar/file2.txt

  # Create a new configmap named my-config with key1=config1 and key2=config2
  kubectl create configmap my-config --from-literal=key1=config1 --from-literal=key2=config2

  # Create a new configmap named my-config from the key=value pairs in the file
  kubectl create configmap my-config --from-file=path/to/bar

  # Create a new configmap named my-config from an env file
  kubectl create configmap my-config --from-env-file=path/to/bar.env

从文件创建ConfigMap

支持传递多个文件路径,例子如下:

kubectl create configmap 5bug-config \
  --from-file=/5bug/projects/k8s-demo/config1.yaml \
  --from-file=/5bug/projects/k8s-demo/config2.yaml

默认使用的文件名作为配置文件名,若要指定文件名则可以按如下方法写:

kubectl create configmap NAME --from-file=<filename>=<path-to-file>

例子:

kubectl create configmap 5bug-config-file --from-file=5bug=/5bug/projects/k8s-demo/config1.yaml

从目录创建ConfigMap

若from-file参数指定为目录的话,则目录下所有的文件都会创建为ConfigMap:

kubectl create configmap 5bug-config-path --from-file=/5bug/projects/k8s-demo/config/

从ENV文件创建ConfigMap

定义test.env文件如下:

env=prd
url=www.5bug.wang

创建方法为:

kubectl create configmap 5bug-config-env --from-env-file=/5bug/projects/k8s-demo/test.env

从键值对创建ConfigMap

方法如下:

kubectl create configmap 5bug-config-literal --from-literal=web.name=5bug --from-literal=web.url=http://www.5bug.wang/

ConfigMap的查看

使用kubectl get configmaps命令可以查看ConfigMap列表,如图:

image.png

可以通过如下两种方法来查看ConfigMap的内容:

kubectl get configmap 5bug-config-env -o yaml

kubectl describe configmap 5bug-config-env

ConfigMap的删除

语法如下:

kubectl delete configmap NAME

例如:

kubectl delete configmap 5bug-config-env

ConfigMap的使用

ConfigMap支持如下用法:

  • 支持作为容器内的环境变量

  • 支持设置为容器启动命令的参数

  • 支持挂载为容器内部的文件或目录

这里提供一个yaml写法来说明:

---
    apiVersion: v1  
    kind: Pod  
    metadata: 
      name: k8s-demo
    spec:
      restartPolicy: Always  
      containers:
      - image: www.5bug.wang/docker/k8s-demo:1.1  
        imagePullPolicy: IfNotPresent  
        command: ["/bin/bash","-c","./k8s-demo"]  
        name: k8s-demo
        ports:
        - containerPort: 8080  
          protocol: TCP
        env:
          - name: URL
            valueFrom: #将5bug-config-literal这个configmap里的web.url的值赋值给URL变量
              configMapKeyRef:
                name: 5bug-config-literal
                key: web.url
        envFrom: #将configMap里的所有键值作为容器的环境变量
          - configMapRef:
              name: 5bug-config-literal
        volumeMounts:  #挂载配置
          - name: logs  #挂载设备的名字,与volumes[*].name 需要对应    
            mountPath: /data/logs  #挂载到容器的/data/logs下
            readOnly: True  
          - name: config-path #将名称为5bug-config-path的configMap挂载到容器的/data/config目录下
            mountPath: /data/config 
            readOnly: True 
      volumes:  #定义一组挂载设备  
        - name: logs  #定义一个挂载设备的名字  
          hostPath:
            path: /data/logs  #挂载设备类型为hostPath,路径为宿主机下的/data/logs
        - name: config-path
          configMap:
              name: 5bug-config-path

使用ConfigMap需要注意的一些问题

  • 必须先创建ConfigMap才能使用它

  • 如果Pod引用的ConfigMap不存在则无法启动Pod

  • 如果是volume的形式挂载到容器内部,只能挂载到某个目录下,该目录下原有的文件会被覆盖掉

  • ConfigMap和使用它的Pod必须在同一个NameSpace