Configmap Analysis of Kubernetes

Source: Internet
Author: User
Tags docker run

The Configmap feature is available in the Kubernetes1.2 version, and many applications read configuration information from configuration files, command-line parameters, or environment variables. These configuration information needs to be decoupled from the Docker image, and you can't always redo an image with every configuration modification. The Configmap API gives us a mechanism to inject configuration information into a container, configmap can be used to hold a single attribute, or it can be used to hold an entire configuration file or JSON binary large object.

Configmap Overview

The configmap API resource is used to hold key-value pair configuration data, which can be used in pods or used as a controller The same system components store configuration data. Although Configmap is similar to secrets, Configmap is more convenient for handling strings that do not contain sensitive information. Note:configmaps is not a substitute for the attribute configuration file. Configmaps is just a reference to multiple properties files. You can think of it as a directory in a Linux system /etc , a directory dedicated to storing configuration files. For example, using the Configmap configuration to create a kuberntes volumes,configmap each data item in the file will become a new one.

 kind:configmapapiversion:v1metadata:creationtimestamp: 2016-02-18t19:14:< Span class= "Hljs-number" >38z name:example-config namespace: defaultdata: Example.property.1:hello example.property.2:world Example.property.file: |-property.1=value-1 property.2=value- 2 Property.3=value-3   

dataA column includes the configuration data, which can be used to save a single property or to save a configmap file. Configuration data can be used in a variety of ways in pods. Configmaps can be used to:

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

Both the user and the system components can store configuration data inside the Configmap.

In fact, do not look at the following article, directly from kubectl create configmap -h the Help information can be configmap exactly how to create knows.

Examples: #Create a new configmap named My-config basedOn Folder Bar KubectlCreate 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 with a given value, file, or directory.

create configmap
Using catalog creation

For example, we already have a configuration file that contains the value of the configmap we want to set:

$ ls docs/user-guide/configmap/kubectl/game.propertiesui.properties$ cat docs/user-guide/configmap/kubectl/game.propertiesenemies=alienslives=3enemies.cheat=trueenemies.cheat.level=noGoodRottensecret.code.passphrase=UUDDLRLRBABASsecret.code.allowed=truesecret.code.lives=30$ cat docs/user-guide/configmap/kubectl/ui.propertiescolor.good=purplecolor.bad=yellowallow.textmode=truehow.nice.to.look=fairlyNice

Use the following command to create a configmap that contains all the files in the directory.

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

—from-fileAll files specified in the directory are used to create a key-value pair inside the Configmap, the name of the key is the file name, and the value is the content of the document.

Let's take a look at the Configmap created by this command:

$ kubectl describe configmaps game-configName:           game-configNamespace:      defaultLabels:         <none>Annotations:    <none>Data====game.properties:        158 bytesui.properties:          83 bytes

We can see that the two keys are the file names in the directory specified from Kubectl. The contents of these keys can be very large, so in the output of Kubectl describe, only the name of the key and their size can be seen. If you want to see the value of the key, you can use kubectl get :

get configmaps game-config -o yaml

We output the configuration in a yaml format.

ApiVersion:v1data:game.properties: | Enemies=aliens lives=3 enemies.cheat=True Enemies.cheat.level=nogoodrotten Secret.code.passphrase=uuddlrlrbabas secret.code.allowed=true secret.code.lives=ui.properties: | Color.good=purple color.bad=yellow allow.textmode=true How.nice.to.look=fairlynicekind:configmapmetadata:crea Tiontimestamp: 2016-02-18t18:05Z name:game-config namespace: default Resourceversion : "407" Selflink:/api/v1/namespaces/default/configmaps/game-config uid: 30944725-d66e-11e5- 8cd0-68f728db1985               
Using File creation

When we created a directory just now —from-file , we specified a directory where we could create a configmap from a single file as long as it was specified as a file.

create configmap game-config-2 --from-file=docs/user-guide/configmap/kubectl/game.properties $ kubectl get configmaps game-config-2 -o yaml
Apiversion:v1data:game-special-key: | Enemies=aliens lives=3 enemies.cheat=true enemies.cheat.level=nogoodrotten Secret.code.passphrase=uuddlrlrbabas Secret.code  . Allowed=true secret.code.lives=30kind:configmapmetadata: 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-fileThis parameter can be used multiple times, you can use two times to specify the last instance of the two configuration files, the effect is the same as specifying the entire directory.

Using the literal value to create

Using literal value creation, using —from-literal parameters to pass configuration information, this parameter can be used more than once, the format is as follows;

create configmap special-config --from-literal=special.how=very --from-literal=special.type=charm$ kubectl get configmaps special-config -o yaml
apiVersion: v1data:  special.how: very  special.type: charmkind: ConfigMapmetadata:  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 Pods

Use CONFIGMAP to replace environment variables

Configmap can be used to fill in environment variables. Look at the configmap below.

apiVersion: v1kind: ConfigMapmetadata:  name: special-config  namespace: defaultdata:  special.how: very  special.type: charm
apiVersion: v1kind: ConfigMapmetadata:  name: env-config  namespace: defaultdata:  log_level: INFO

We can use Configmap in pods like this:

apiVersion: v1kind: Podmetadata:  name: dapi-test-podspec:  containers:    - name: test-container      image: gcr.io/google_containers/busybox      "/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 this pod runs, it will output the following lines:

SPECIAL_LEVEL_KEY=verySPECIAL_TYPE_KEY=charmlog_level=INFO

Setting command line parameters with Configmap

Configmap can also be used to set commands or parameter values in a container. It uses the Kubernetes $ (var_name) substitution syntax. Let's take a look at the configmap below.

apiVersion: v1kind: ConfigMapmetadata:  name: special-config  namespace: defaultdata:  special.how: very  special.type: charm

To inject the values in the Configmap into the command-line arguments, we will use the environment variable substitution syntax as in the previous example ${VAR_NAME) . (This is actually to set the environment variable for the Docker container, which I used to play when I created the image, to specify the-e parameter in the Docker run to modify the environment variables in the image, and then the Docker cmd command to reuse the $ (var_name) Use SED to modify the configuration file or as a command line startup parameter. )

apiVersion: v1kind: Podmetadata:  name: dapi-test-podspec:  containers:    - name: test-container      image: gcr.io/google_containers/busybox      "/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

Running this pod will output:

very charm

Using CONFIGMAP with the data volume plugin

Configmap can also be used in data volumes. Or is this configmap.

apiVersion: v1kind: ConfigMapmetadata:  name: special-config  namespace: defaultdata:  special.how: very  special.type: charm

There are different options for using this configmap in the data volume. The most basic is to fill the file into the data volume, in this file, the key is the filename, the key value is the file content:

apiVersion: v1kind: Podmetadata:  name: dapi-test-podspec:  containers:    - name: test-container      image: gcr.io/google_containers/busybox      "/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 where the CONFIGMAP value is mapped.

apiVersion: v1kind: Podmetadata:  name: dapi-test-podspec:  containers:    - name: test-container      image: gcr.io/google_containers/busybox      "/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 of running this pod is very .

http://rootsongjc.github.io/blogs/kubernetes-configmap-introduction/

Configmap Analysis of Kubernetes

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.