Configmap Description of Kubernetes

Source: Internet
Author: User



In this blog post, let's say a question about customizing the configuration in the pod of kubernetes.



We know that in almost all application development, configuration file changes are involved, such as in the Web program, the need to connect to the database, cache, and even queues. And one of our applications starts with writing the first line of code, going through the development environment, the test environment, the pre-release environment only to the final online environment. Each environment is defined by its own set of configurations. If we do not manage these configuration files well, your operations will suddenly become extremely cumbersome. For this, some of the industry's major companies have developed their own set of configuration management center, such as Qcon, Baidu's disconf and so on. Kubernetes also offers its own set of options, namely Configmap. Kubernetes uses Configmap to implement configuration management for applications in containers.





Create Configmap


There are two ways to create Configmap, one is created from a Yaml file, and the other is created directly from the command line via Kubectl.



Let's first look at the first, in the Yaml file, the configuration file is saved as a Key-value key-value pair, and of course it can be placed directly into a complete configuration file, in the following example, Cache_hst, Cache_port, cache_ Prefix are key-value key-value pairs, and app.properties and my.cnf are configuration files:



650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "/>


Apiversion:v1kind:configmapmetadata:name:test-cfg namespace:defaultdata:cache_host:memcached-gcxt Cache_port: "    11211 "Cache_prefix:gcxt my.cnf: |    [mysqld] Log-bin = Mysql-bin App.properties: | property.1 = value-1 property.2 = value-2 property.3 = value-3


650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "/>



Create Configmap:


Kubectl create-f test-cfg.yml





The second way is to use Kubectl directly under the command line to create






Create a single configmap for all the profiles in a directory directly:


KUBECTL Create Configmap Test-config--from-file=./configs


Create a configuration file directly as a configmap:


KUBECTL Create Configmap test-config2--from-file=./configs/db.conf--from-file=./configs/cache.conf


When created with Kubectl, it is created by passing key-value pairs directly on the command line:


KUBECTL Create Configmap test-config3--from-literal=db.host=10.5.10.116--from-listeral=db.port= ' 3306 '


We can view the created Configmap in the following ways:


Kubectl get Configmapskubectl Get configmap test-config-o yamlkubectl describe Configmap test-config




Using Configmap


There are three ways to use Configmap, one is to pass the pod directly through the environment variable, the other is to run it through the command line of the pod, and the third is to mount it in the pod using volume.



Example of the first approach:



Configmap file:



650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "/>


Apiversion:v1kind:configmapmetadata:name:special-config Namespace:defaultdata:special.how:very Special.type:ch Arm


650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "/>



Example of the first pod:



650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "/>


Apiversion: v1kind: podmetadata:  name: dapi-test-podspec:  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  restartpolicy: never


650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "/>



Example of the second pod:



650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "/>


 apiversion: v1kind: podmetadata:  name: dapi-test-podspec:  containers :    - name: test-container      image:  gcr.io/google_containers/busybox      command: [  "/bin/sh",  "-C",   "ENV"  ]      env:        -  name: CACHE_HOST          valueFrom:             configMapKeyRef:               name: test-cfg               key: cache_host               optional: true  restartpolicy: never 


650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "/>






The second way, when referenced at the command line, is to be set to an environment variable, which can then be set through the $ (var_name) container to start the command's startup parameters, example:



Examples of configmap files:



650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "/>


Apiversion:v1kind:configmapmetadata:name:special-config Namespace:defaultdata:special.how:very Special.type:ch Arm


650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "/>



Pod Example:



650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "/>


Apiversion: v1kind: podmetadata:  name: dapi-test-podspec:  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


650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "/>






The third way, using volume to Mount Configmap as a file or directory directly, where each of the Key-value key value pairs will generate a file, key is the file name, value is the content, here is an example:



Configmap Example:



650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "/>


Apiversion:v1kind:configmapmetadata:name:special-config Namespace:defaultdata:special.how:very Special.type:ch Arm


650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "/>



The first pod example simply mounts the configmap created above directly to the pod's/etc/config directory:



650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "/>


Apiversion:v1kind:podmetadata:name:dapi-test-podspec:containers:-Name:test-container IMAGE:GCR.IO/GOOGL E_containers/busybox command: ["/bin/sh", "-C", "Cat/etc/config/special.how"] volumemounts:-name:conf Ig-volume Mountpath:/etc/config volumes:-Name:config-volume configmap:name:special-config RE Startpolicy:never


650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "/>



The second pod example only mounts the Configmap special.how key to a relative path in the/etc/config directory Path/to/special-key, if there is a file with the same name, overwrite it directly. The other key does not mount:



650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "/>


Apiversion: v1kind: podmetadata:  name: dapi-test-podspec:  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 


650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "/>






Finally, two points need to be explained:



1. Configmap must be created before pod



2, only with the current Configmap in the same namespace pod in order to use this configmap, in other words, configmap can not be called across the namespace.






This article is from the "My Sky" blog, so be sure to keep this source http://sky66.blog.51cto.com/2439074/1934003



Configmap Description of Kubernetes


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.