Fight again k8s (8): ConfigMap

1 Introduction to ConfigMap

1.1 Overview

When deploying an application, we all involve the configuration of the application. In a container, such as a Docker container, if the configuration file is entered into the container image, this behavior is equivalent to hard-writing the configuration. Every time the configuration is modified, the image has to be rebuild. Of course, we can also perform configuration management and modification by mounting the volume containing the file. In k8s, we want to talk about a better way, namely ConfigMap. The emergence of this resource object greatly facilitates the configuration management of applications.
  ConfigMap is stored in k8s in the form of one or more key/value, which can manage variables or complete configuration file content internally.

1.2 Usage

生成容器内的环境变量,在pod中可以通过spec.env或者spec.envFrom进行引用。
设置容器启动命令的启动参数,前提是设置为环境变量。
以卷volume的方式挂载到容器内部的文件或目录,通过spec.volumes引用。

2 ConfigMap usage

在使用命令的时候注意单词: configmap等价于cm,cm算是简写,类似于deployment可以使用命令时写成deploy,service可以写成svc,namespace可以写成ns,pod可以写成po。

2.1 Create

1) Create a yaml file
Example:

apiVersion: v1
kind: ConfigMap
metadata:
name: cm-test01
data:
appconf01: value01
appconf02: value02

命令:
$ kubectl create -f configmap-test01.yaml

2) Create by command line

读取文件方式(也可以是目录)通过--from-file参数从文件中读取。可以指定key的名称,若不指定,则默认使用文件名为key。
如当前目录有一个配置文件为test.properties

key01:value01
key02:value02
conf01: value03

$ kubectl create cm cm-test-file --from-file=test.properties

指定参数方式,通过--from-literal指定keyxx=valuexx创建confimap中的data内配置属性。
$ kubectl create configmap cm-test-literal --from-literal=key01=value01 --from-literal=key02=value02

2.2 Query

1) View configmap list
$ kubectl get cm

[root@k8s /cm/test]# kubectl get cm
NAME DATA AGE
cm-test-file 1 1m
cm-test-literal 2 2s
cm-test01 2 1h

2) View configmap details
$ kubectl describe cm cm-test01

[root@k8s /cm/test]# kubectl describe cm cm-test01
Name: cm-test01
Namespace: system-pro
Labels:
Annotations:

Data

===

appconf01:

---

value01

appconf02:

value02

$ kubectl describe configmap cm-test-file

[root@k8s /cm/test]# kubectl describe configmap cm-test-file
Name: cm-test-file
Namespace: system-pro
Labels:
Annotations:

Data

test.properties:

key01:value01
key02:value02
conf01: value03

$ kubectl describe cm cm-test-literal

[root@k8s /cm/test]# kubectl describe cm cm-test-literal
Name: cm-test-literal
Namespace: system-pro
Labels:
Annotations:

Data

===

key01:

---

value01

key02:

---

value02

3) View yaml output
$ kubectl get cm cm-test01 -o yaml

[root@k8s /cm/test]# kubectl get cm cm-test01 -o yaml
apiVersion: v1
data:
appconf01: value01
appconf02: value02
kind: ConfigMap
metadata:
creationTimestamp: 2020-03-13T13:06:21Z
name: cm-test01
namespace: system-pro
resourceVersion: “594861”
selfLink: /api/v1/namespaces/system-pro/configmaps/cm-test01
uid: 6f5e7efb-652b-11ea-adf9-fa163e4464a5

$ kubectl get configmap cm-test-file -o yaml

[root@k8s /cm/test]# kubectl get configmap cm-test-file -o yaml
apiVersion: v1
data:
test.properties: |
key01:value01
key02:value02
conf01: value03
kind: ConfigMap
metadata:
creationTimestamp: 2020-03-13T14:29:30Z
name: cm-test-file
namespace: system-pro
resourceVersion: “598548”
selfLink: /api/v1/namespaces/system-pro/configmaps/cm-test-file
uid: 0d226ad2-6537-11ea-adf9-fa163e4464a5

$ kubectl get cm cm-test-literal -o yaml

[root@k8s /cm/test]# kubectl get cm cm-test-literal -o yaml
apiVersion: v1
data:
key01: value01
key02: value02
kind: ConfigMap
metadata:
creationTimestamp: 2020-03-13T14:30:57Z
name: cm-test-literal
namespace: system-pro
resourceVersion: “598613”
selfLink: /api/v1/namespaces/system-pro/configmaps/cm-test-literal
uid: 412affd4-6537-11ea-adf9-fa163e4464a5

2.3 Update

1)edit
$ kubectl edit cm cm-test01
insert image description here

Check whether the update takes effect through kubectl describe cm cm-test01
insert image description here

2) apply
directly change the value in the yaml file, and republish it through kubectl apply -f configmap-test01.yaml to update it.

2.4 Delete

1) Delete
$ kubectl delete -f configmap-test01.yaml by way of yaml file

2) Directly delete resources
$ kubectl delete cm cm-test01
3 Use of ConfigMap and Pod

容器应用对ConfigMap的使用主要是两种:
1)通过环境变量获取ConfigMap的内容:spec.env和spec.envFrom
2)通过卷volume挂载的方式将ConfigMap的内容挂载到容器内部的文件或目录:spec.volumes

The following content is based on the above ConfigMap resource cm-test01 as an example

3.1 Environment variable method

spec.env method

1) Create a pod

[root@k8s /cm/test]# vim pod-test01.yaml
apiVersion: v1
kind: Pod
metadata:
name: cm-pod-test001
spec:
containers:

  • name: cm-test
    image: tomcat:8
    command: [ “/bin/sh”, “-c”, “env | grep APP”]
    env:

    • name: APPCONF01 # Define the name of the environment variable
      valueFrom: # The value of key "appconf01" gets
      configMapKeyRef:
      name: cm-test01 # The value of the environment variable comes from configmap cm-test01
      key: appconf01 # The configuration key in configmap is appconf01
    • name: APPCONF02 # Define the name of the environment variable
      valueFrom: # The value of key "appconf02" gets
      configMapKeyRef:
      name: cm-test01 # The value of the environment variable comes from configmap cm-test01
      key: appconf02 # The configuration key in configmap is appconf02
      restartPolicy: Never # restart strategy: never.

Execute to create a pod:
$ kubectl create -f pod-test01.yaml

2) View pods
$ kubectl get pods

[root@k8s /cm/test]# kubectl get pods
NAME READY STATUS RESTARTS AGE
cm-pod-test001 0/1 Completed 0 1h

3) View pod logs
$ kubectl logs cm-pod-test001

[root@k8s /cm/test]# kubectl logs cm-pod-test001
APPCONF01=value01
APPCONF02=value02

Indicates that the environment variables inside the container are read using ConfigMap.

spec.envFrom method

1) Create pod
yaml file

[root@k8s /cm/test]# vim pod-test02.yaml
apiVersion: v1
kind: Pod
metadata:
name: cm-pod-test002
spec:
containers:

  • name: cm-test2
    image: tomcat:8
    command: [ “/bin/sh”, “-c”, “env”]
    envFrom:

    • configMapRef:
      name: cm-test01 # Automatically generate environment variables based on ConfigMap cm-test01 resources
      restartPolicy: Never

Execute to create a pod:
$ kubectl create -f pod-test02.yaml

2) View pod
$ kubectl get po

[root@k8s /cm/test]# kubectl get po
NAME READY STATUS RESTARTS AGE
cm-pod-test001 0/1 Completed 0 2h
cm-pod-test002 0/1 Completed 0 1h

Note:
The name of the environment variable is restricted: [a-zA-Z][a-zA-Z0-9_]*, cannot start with a number or illegal character.

3.2 Volume Mounting Method

Specified items

[root@k8s /cm/test]# vim pod-test03.yaml
apiVersion: v1
kind: Pod
metadata:
name: cm-pod-test003
spec:
containers:

  • name: cm-test3
    image: tomcat:8
    volumeMounts:

    • name: vm-01-1
      mountPath: /conf
      volumes:
  • name: vm-01-1
    configMap:
    name: cm-test-file
    items:

    • key: key-testproperties
      path: test.properties
      restartPolicy: Never

do not specify items

[root@k8s /cm/test]# vim pod-test04.yaml
apiVersion: v1
kind: Pod
metadata:
name: cm-pod-test004
spec:
containers:

  • name: cm-test4
    image: tomcat:8
    volumeMounts:

    • name: vm-02-2
      mountPath: /conf
      volumes:
  • name: vm-02-2
    configMap:
    name: cm-test-file
    restartPolicy: Never

Enter the container to check
$ kubectl exec -it cm-pod-test004 -c cm-test4 –
After bash enters the container, ls /conf to see if there is a test.properties file.

[root@k8s /cm/test]# kubectl exec -it cm-pod-test004 -c cm-test4 – bash
root@cm-pod-test004:/usr/local/tomcat# ls /conf
test.properties

Replenish

The difference between creating a specified key and not specifying a key in the way of --from-file
1) Without specifying the key name
Create:
$ kubectl create cm cm-test-file --from-file=test.properties
Output:
$ kubectl get cm cm -test-file -o yaml
insert image description here
2) Specify key
creation:
$ kubectl create cm cm-test-file02 --from-file=tp=test.properties
Output:
$ kubectl get cm cm-test-file - o yaml
insert image description here
If you specify the name of the key, the specified name will be used in the configmap; if not specified, the file name will be used by default as the key.

Guess you like

Origin blog.csdn.net/qq_43762191/article/details/123295025