k8s-ConfigMap

create

ConfigMapResource object using key-valuethe form of key-value pairs to the configuration data that can Poduse it, ConfigMapand we want to address the back of Secretsrelatively similar, is a relatively large difference ConfigMapcan be more convenient to deal with some non-sensitive data, such as passwords still need Secretsto be managed. Let's take the next example illustrates the ConfigMapuse of:

kind: ConfigMap
apiVersion: v1
metadata:
  name: cm-demo
  namespace: default
data:
  data.1: hello
  data.2: world
  config: |
    property.1=value-1
    property.2=value-2
    property.3=value-3

 

Wherein the configuration data datafor the following configuration attributes, the first two are used to hold a single attribute, a back is used to hold a profile.

Of course, the same we can use kubectl create -f xx.yamlto create the above ConfigMapobjects, but if we do not know how to create ConfigMapit, do not forget kubectlis our best teacher, can be used kubectl create configmap -hto see about creating ConfigMaphelp 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

We can see that can create a catalog from a given ConfigMapsubject, such as we have a testcmdirectory, the directory contains configuration files, redisand mysqlconnection information, as follows:

$ ls testcm
redis.conf
mysql.conf

$ cat testcm/redis.conf
host=127.0.0.1
port=6379

$ cat testcm/mysql.conf host=127.0.0.1 port=3306 

Then we can use the from-filekeyword to create a directory that contains the configuration file so ConfigMap:

$ kubectl create configmap cm-demo1 --from-file=testcm
configmap "cm-demo1" created

Where from-fileall the files in the following parameter specifies the directory will be used on ConfigMapthe inside to create a key-value pair, the key is the name of the file name, value is the contents of the file.

Once created, the same we can use the following command to view ConfigMapa list:

$ kubectl get configmap
NAME       DATA      AGE
cm-demo1   2         17s

You can see already created a cm-demo1of the ConfigMapobject, and then you can use the describecommand to check detailed information:

kubectl describe configmap cm-demo1
Name:         cm-demo1
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
==== mysql.conf: ---- host=127.0.0.1 port=3306 redis.conf: ---- host=127.0.0.1 port=6379 Events: <none> 

We can see the two keyis testcmthe following list of file names, the corresponding valuevalue is the contents of the file, then, here is worth noting that if a large file inside the configuration information, then describethe time may not display the corresponding value, to view the key you can use the following command:

$ kubectl get configmap cm-demo1 -o yaml
apiVersion: v1
data:
  mysql.conf: |
    host=127.0.0.1
    port=3306
  redis.conf: |
    host=127.0.0.1 port=6379 kind: ConfigMap metadata: creationTimestamp: 2018-06-14T16:24:36Z name: cm-demo1 namespace: default resourceVersion: "3109975" selfLink: /api/v1/namespaces/default/configmaps/cm-demo1 uid: 6e0f4d82-6fef-11e8-a101-525400db4df7 

In addition to the file directory created by, we can also use the specified file created ConfigMap, similar to the above configuration file, for example, we create a redisa separate configuration of ConfigMapobjects:

$ kubectl create configmap cm-demo2 --from-file=testcm/redis.conf
configmap "cm-demo2" created
$ kubectl get configmap cm-demo2 -o yaml
apiVersion: v1
data:
  redis.conf: |
    host=127.0.0.1
    port=6379 kind: ConfigMap metadata: creationTimestamp: 2018-06-14T16:34:29Z name: cm-demo2 namespace: default resourceVersion: "3110758" selfLink: /api/v1/namespaces/default/configmaps/cm-demo2 uid: cf59675d-6ff0-11e8-a101-525400db4df7 

We can see an associated redis.conffile configuration information of ConfigMapthe object is successfully created, the other is worth noting that --from-filethis parameter can be used multiple times, such as we use here two were designated redis.confand mysql.conffile, you specify an entire directory and direct the same effects.

In addition, through the help documentation we can see that we can also use direct string created by --from-literalpassing configuration parameters, the same, this parameter can be used multiple times in the following format:

$ kubectl create configmap cm-demo3 --from-literal=db.host=localhost --from-literal=db.port=3306
configmap "cm-demo3" created
$ kubectl get configmap cm-demo3 -o yaml
apiVersion: v1
data:
  db.host: localhost
  db.port: "3306"
kind: ConfigMap
metadata:
  creationTimestamp: 2018-06-14T16:43:12Z
  name: cm-demo3
  namespace: default
  resourceVersion: "3111447"
  selfLink: /api/v1/namespaces/default/configmaps/cm-demo3
  uid: 06eeec7e-6ff2-11e8-a101-525400db4df7

use

ConfigMapCreating successful, then we should be how in Podthe use of it? We say that ConfigMapthese data can be configured in a variety of ways by Podusing the years, mainly in the following ways:

  • The value of the environment variable settings
  • Set command-line arguments in containers
  • Create a config file in the data inside the volume

First, we use ConfigMapto fill our environment variables:

apiVersion: v1
kind: Pod
metadata:
  name: testcm1-pod
spec:
  containers:
    - name: testcm1
      image: busybox
      command: [ "/bin/sh", "-c", "env" ]
      env:
        - name: DB_HOST
          valueFrom:
            configMapKeyRef:
              name: cm-demo3
              key: db.host
        - name: DB_PORT
          valueFrom:
            configMapKeyRef:
              name: cm-demo3
              key: db.port
      envFrom:
        - configMapRef:
            name: cm-demo1

 

After running the Pod will output the following lines:

$ kubectl logs testcm1-pod
......
DB_HOST=localhost
DB_PORT=3306 mysql.conf=host=127.0.0.1 port=3306 redis.conf=host=127.0.0.1 port=6379 ...... 

We can see DB_HOSTand DB_PORThave normal output, the other environment variables directly because we are here cm-demo1to inject it came in, so they put out the whole key to the output, and this is in line with expectations.

In addition, we can use ConfigMapto set the command line parameters ConfigMapmay be used to set a parameter value or a command vessel, the following Pod:

apiVersion: v1
kind: Pod
metadata:
  name: testcm2-pod
spec:
  containers:
    - name: testcm2
      image: busybox
      command: [ "/bin/sh", "-c", "echo $(DB_HOST) $(DB_PORT)" ]
      env:
        - name: DB_HOST
          valueFrom:
            configMapKeyRef:
              name: cm-demo3
              key: db.host
        - name: DB_PORT
          valueFrom:
            configMapKeyRef:
              name: cm-demo3
              key: db.port

 

Run Podafter outputs the following information:

$ kubectl logs testcm2-pod
localhost 3306

Another use is very common ConfigMapways: by using the data volume, in which the use of data volume ConfigMap, the file is filled in data volumes, in this file, the file name is the key, the key is the contents of the file:

apiVersion: v1
kind: Pod
metadata:
  name: testcm3-pod
spec:
  containers:
    - name: testcm3
      image: busybox
      command: [ "/bin/sh", "-c", "cat /etc/config/redis.conf" ]
      volumeMounts:
      - name: config-volume
        mountPath: /etc/config
  volumes:
    - name: config-volume
      configMap:
        name: cm-demo2

 

Run Pod, and view the log:

$ kubectl logs testcm3-pod
host=127.0.0.1
port=6379

Of course, we may also be in ConfigMapthe control path of the data volume values are mapped to go, following Poddefinition:

apiVersion: v1
kind: Pod
metadata:
  name: testcm4-pod
spec:
  containers:
    - name: testcm4
      image: busybox
      command: [ "/bin/sh","-c","cat /etc/config/path/to/msyql.conf" ]
      volumeMounts:
      - name: config-volume
        mountPath: /etc/config
  volumes:
    - name: config-volume
      configMap:
        name: cm-demo1
        items:
        - key: mysql.conf
          path: path/to/msyql.conf

 

Run Pod, and view the log:

$ kubectl logs testcm4-pod
host=127.0.0.1
port=3306

Also note that, when the ConfigMaploading of data into the form of a roll Podwhen, then updates ConfigMap(or deleted reconstruction ConfigMap), Podthe thermal loading configuration information is updated. Then you can increase the number of monitoring scripts configuration file is changed, then reloadthe corresponding service.

Guess you like

Origin www.cnblogs.com/fuyuteng/p/10936309.html