Kubernetes explains ConfigMap in detail

ConfigMap


In fact, the ConfigMap function has been available since Kubernetes version 1.2. Many applications will read configuration information from configuration files, command line parameters or environment variables. These configuration information need to be decoupled from the docker image. You can't redo an image every time you modify a configuration, right?

The ConfigMap API provides us with a mechanism to inject configuration information into the container. ConfigMap can be used to save a single attribute, or it can be used to save the entire configuration file or a JSON binary large object.

ConfigMap Overview


The ConfigMap API  resource is used to save  key-value pair  configuration data. This data can be used in  pods  or used to   store configuration data for system components like controllers .

Although ConfigMap is similar to  Secrets  , ConfigMap is more convenient for handling strings that do not contain sensitive information.

Note: ConfigMaps are not a replacement for property configuration files. ConfigMaps simply serve as references to multiple properties files. You can think of it as a directory in the Linux system  /etc , a directory specifically used to store configuration files. As an example below, use ConfigMap configuration to create Kubernetes Volumes. Each data item in ConfigMap will become a new file.

kind: ConfigMap
apiVersion: v1
metadata:
  creationTimestamp: 2016-02-18T19:14:38Z
  name: example-config
  namespace: default
data:
  example.property.1: hello
  example.property.2: world
  example.property.file: |-
    property.1=value-1
    property.2=value-2
    property.3=value-3

data One column contains configuration data. ConfigMap can be used to save a single property or a configuration file. Configuration data can be used in Pods in many ways. ConfigMaps can be used to :

  1. Set the value of environment variables
  2. Set command line parameters in the container
  3. Create a config file in the data volume

Both user and system components can store configuration data in a ConfigMap.

In fact, you don't need to read the following article,  kubectl create configmap -h you can know a little bit about how to create a ConfigMap directly from the help information.

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 ConfigMaps


You can use this command to create a ConfigMap from a given value, file, or directory.

kubectl create configmap

Create using directory

For example, we already have some configuration files that contain the ConfigMap values ​​we want to set:

$ ls docs/user-guide/configmap/kubectl/
game.properties
ui.properties

$ cat docs/user-guide/configmap/kubectl/game.properties
enemies=aliens
lives=3
enemies.cheat=true
enemies.cheat.level=noGoodRotten
secret.code.passphrase=UUDDLRLRBABAS
secret.code.allowed=true
secret.code.lives=30

$ cat docs/user-guide/configmap/kubectl/ui.properties
color.good=purple
color.bad=yellow
allow.textmode=true
how.nice.to.look=fairlyNice

Use the following command to create a ConfigMap containing all files in a directory.

$ kubectl create configmap game-config --from-file=docs/user-guide/configmap/kubectl

 —from-file All files specified in the directory will be used to create a key-value pair in the ConfigMap. The name of the key is the file name and the value is the content of the file.

$ kubectl describe configmaps game-config
Name:           game-config
Namespace:      default
Labels:         <none>
Annotations:    <none>

Data
====
game.properties:        158 bytes
ui.properties:          83 bytes

We can see that the two keys are file names in the directory specified by kubectl . The contents of these keys may be large, so in the output of kubectl describe, only the names of the keys and their sizes can be seen. If you want to see the value of a key, you can use

$ kubectl get configmaps game-config -o yaml

We  yaml output the configuration in the format.

apiVersion: v1
data:
  game.properties: |
    enemies=aliens
    lives=3
    enemies.cheat=true
    enemies.cheat.level=noGoodRotten
    secret.code.passphrase=UUDDLRLRBABAS
    secret.code.allowed=true
    secret.code.lives=30
  ui.properties: |
    color.good=purple
    color.bad=yellow
    allow.textmode=true
    how.nice.to.look=fairlyNice
kind: ConfigMap
metadata:
  creationTimestamp: 2016-02-18T18:34:05Z
  name: game-config
  namespace: default
  resourceVersion: "407"
  selfLink: /api/v1/namespaces/default/configmaps/game-config
  uid: 30944725-d66e-11e5-8cd0-68f728db1985
Create using file

When we used the directory creation just now , we  —from-file specified a directory. As long as it is specified as a file, the ConfigMap can be created from a single file.

$ kubectl create configmap game-config-2 --from-file=docs/user-guide/configmap/kubectl/game.properties 

$ kubectl get configmaps game-config-2 -o yaml
apiVersion: v1
data:
  game.properties: |
    enemies=aliens
    lives=3
    enemies.cheat=true
    enemies.cheat.level=noGoodRotten
    secret.code.passphrase=UUDDLRLRBABAS
    secret.code.allowed=true
    secret.code.lives=30
kind: ConfigMap
metadata:
  creationTimestamp: 2016-02-18T18:54:22Z
  name: game-config-3
  namespace: default
  resourceVersion: "530"
  selfLink: /api/v1/namespaces/default/configmaps/game-config-3
  uid: 05f8da22-d671-11e5-8cd0-68f728db1985

—from-file This parameter can be used multiple times. You can use it twice to specify the two configuration files in the previous instance. The effect is the same as specifying the entire directory.

Create using literal

Create using literal values ​​and use  —from-literal parameters to transfer configuration information. This parameter can be used multiple times and the format is as follows:

$ kubectl create configmap special-config --from-literal=special.how=very --from-literal=special.type=charm

$ kubectl get configmaps special-config -o yaml
apiVersion: v1
data:
  special.how: very
  special.type: charm
kind: ConfigMap
metadata:
  creationTimestamp: 2016-02-18T19:14:38Z
  name: special-config
  namespace: default
  resourceVersion: "651"
  selfLink: /api/v1/namespaces/default/configmaps/special-config
  uid: dadce046-d673-11e5-8cd0-68f728db1985

 

 

 

Using ConfigMap in Pod


Use ConfigMap to replace environment variables

ConfigMap can be used to populate environment variables. Take a look at the ConfigMap below.

apiVersion: v1
kind: ConfigMap
metadata:
  name: special-config
  namespace: default
data:
  special.how: very
  special.type: charm
apiVersion: v1
kind: ConfigMap
metadata:
  name: env-config
  namespace: default
data:
  log_level: INFO

 We can use ConfigMap in Pod like this:

apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  containers:
    - name: test-container
      image: gcr.io/google_containers/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

After running this Pod, the following lines will be output:

SPECIAL_LEVEL_KEY=very
SPECIAL_TYPE_KEY=charm
log_level=INFO

      containers:
        - env:
            - name: PARAMS
              value: -jar  /opt/app.jar --spring.profiles.active=test12 --spring.cloud.consul.host=192.168.12.121 --spring.cloud.consul.port=32000 --spring.cloud.consul.discovery.acl-token=8dc1eb67-1f5f-4e10-ad9d-5e58b047647c 
            - name: JAVA_OPTS
              value: '-Xmx1024m -Xms1024m'
          image: reg.hrfax.cn/zjboc-eshare/5201-datafusion:6bf93625

Set command line parameters using ConfigMap

ConfigMap can also be used to set command or parameter values ​​in the container. It uses Kubernetes' $(VAR_NAME) substitution syntax. Let's take a look at the ConfigMap below.

apiVersion: v1
kind: ConfigMap
metadata:
  name: special-config
  namespace: default
data:
  special.how: very
  special.type: charm

In order to inject the values ​​in the ConfigMap into the command line parameters, we also need to use the environment variable substitution syntax like the previous example  ${VAR_NAME). (In fact, this thing is to set environment variables for the Docker container. I often did this when creating images in the past. When docker run, specify the -e parameter to modify the environment variables in the image, and then use the docker CMD command to use the $(VAR_NAME) Use sed to modify the configuration file or use it as a command line startup parameter.)

apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  containers:
    - name: test-container
      image: gcr.io/google_containers/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

 After running this Pod, the output will be:

very charm
Using ConfigMap via the data volume plugin

ConfigMap can also be used inside data volumes. Or this ConfigMap.

apiVersion: v1
kind: ConfigMap
metadata:
  name: special-config
  namespace: default
data:
  special.how: very
  special.type: charm

There are different options for using this ConfigMap in a data volume. The most basic thing is to fill the file into the data volume. In this file, the key is the file name and the key value is the file content:

apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  containers:
    - name: test-container
      image: gcr.io/google_containers/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

The output of running this Pod is  very.

We can also control the path in the data volume to which the ConfigMap value is mapped.

apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  containers:
    - name: test-container
      image: gcr.io/google_containers/busybox
      command: [ "/bin/sh","-c","cat /etc/config/path/to/special-key" ]
      volumeMounts:
      - name: config-volume
        mountPath: /etc/config
  volumes:
    - name: config-volume
      configMap:
        name: special-config
        items:
        - key: special.how
          path: path/to/special-key
  restartPolicy: Never

The result after running this Pod is  very.

Guess you like

Origin blog.csdn.net/qq_34556414/article/details/133273375