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:


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


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:


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


Example of the first pod:


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


Example of the second pod:


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





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:


ApiVersion:v1kind:ConfigMapmetadata:  name:special-config  namespace:defaultdata:  special.how: Very  Special.type:charm


Pod Example:


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





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:


ApiVersion:v1kind:ConfigMapmetadata:  name:special-config  namespace:defaultdata:  special.how: Very  Special.type:charm


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


ApiVersion:v1kind:Podmetadata:  name:dapi-test-podspec:  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 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:


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





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.






Transferred from: http://www.cnblogs.com/breezey/p/6582082.html



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.