A Kubernetes ConfigMap is a type of resources that are used to store the configuration information required by applications. A Kubernetes ConfigMap can be used as a set of environmental variables in the runtime environment of a container. This way, you can flexibly modify the configurations of containers after applications are deployed. A Kubernetes ConfigMap can also be used as a configuration file. You can inject configuration information into a container by mounting a configuration file on the container. This topic describes how to create and manage a ConfigMap for a namespace in the SAE console.

Background information

The ConfigMaps in a namespace are used only to store and configure the container runtime environment. For information about how to create a configuration file for your application code, see Manage configurations.

Create a ConfigMap

  1. Log on to the SAE console.
  2. In the left-side navigation pane, click Namespaces. In the top navigation bar, select a region. Then, click the name of a namespace.
  3. In the left-side navigation pane, click ConfigMaps. On the ConfigMaps page, click Create.
  4. In the Create ConfigMap panel, select one of the following methods to create a ConfigMap. Then, click OK.
    • Manual creation: On the Manual tab, configure the parameters. The following table describes the parameters. sc_create_configmap_via_manual
      ParameterDescription
      ConfigMap NameEnter a name for the ConfigMap.
      ConfigMap DescriptionEnter a description for the ConfigMap.
      Configure Mappings
      • A key can contain up to 250 characters in length. A value can contain up to 10,000 characters in length.
      • You can specify up to 20 key-value pairs for a ConfigMap.
    • Use a file: Click the File tab and configure the parameters. The following table describes the parameters. sc_create_configmap_via_file
      ParameterDescription
      ConfigMap NameEnter a name for the ConfigMap.
      Configuration FormatSelect JSON or YAML.
      Configuration Content
      • Sample JSON file:
        {
          "env.shell": "/bin/sh",
          "nginx.conf": "daemon            off;\nworker_processes  2;\nuser              www-data;\n\nevents {\n    use           epoll;\n    worker_connections  128;\n}\n\nerror_log         logs/error.log info;\n\nhttp {\n    server_tokens off;\n    include       mime.types;\n    charset       utf-8;\n\n    access_log    logs/access.log  combined;\n\n    server {\n        server_name   localhost;\n        listen        127.0.0.1:80;\n\n        error_page    500 502 503 504  /50x.html;\n\n        location      / {\n            root      html;\n        }\n\n    }\n\n}"
        }
      • Sample YAML file:
        env.shell: /bin/sh
        nginx.conf: |
           daemon            off;
            worker_processes  2;
            user              www-data;
        
            events {
                use           epoll;
                worker_connections  128;
            }
        
            error_log         logs/error.log info;
        
            http {
                server_tokens off;
                include       mime.types;
                charset       utf-8;
        
                access_log    logs/access.log  combined;
        
                server {
                    server_name   localhost;
                    listen        127.0.0.1:80;
        
                    error_page    500 502 503 504  /50x.html;
        
                    location      / {
                        root      html;
                    }
        
                }
        
            }
      Note
      • The content of the Kubernetes ConfigMap Manifest file. The configuration content can be in the JSON or YAML format.
      • Only the UTF-8 encoding format is supported for Chinese characters.
      • The size of the file cannot exceed 512 KB.
    The created ConfigMap is displayed on the ConfigMaps page. You can click Edit, Copy, or Delete in the Actions column to manage the ConfigMap.
    Note
    • Each time a ConfigMap is modified, a new version is generated for the ConfigMap. The new version takes effect only for new instances that are generated by manual scaling or auto scaling after the modification is performed. The new version does not take effect for existing instances. To ensure that all instances of an application use the same version of a ConfigMap, you must manually restart or redeploy the application after you modify the ConfigMap.
    • If you delete a ConfigMap, the associated applications cannot run as expected. Before you delete the ConfigMap, you must disassociate the ConfigMap from the applications.

Inject configurations

Inject configurations when you create an application

  1. Log on to the SAE console.
  2. In the left-side navigation pane, click Applications. In the top navigation bar, select a region. Then, click Create Application.
  3. In the Basic Information step, configure the parameters and click Next: Application Deployment Configurations.
  4. In the Deployment Configurations step, configure the Technology Stack Programming Language and Application Deployment Method parameters and the corresponding settings.
  5. In the Configuration Management section, select the ConfigMap from the ConfigMap Name drop-down list and a key from the Key drop-down list. Then, specify the path on which the ConfigMap is mounted in the Mount Path field.
    sc_configuration_management
    Note
    • You can create a ConfigMap in the Namespaces module. You can also click Create ConfigMap (K8s configmap) in the Environment Variable Settings section to create a ConfigMap. Then, configure the parameters in the Create ConfigMap panel.
    • You can mount all keys of a ConfigMap. To mount all keys of the ConfigMap on the specified path, select All from the Key drop-down list.
  6. Optional: To reference other ConfigMaps, click Add and repeat the previous step.
  7. Click Next: Confirm Specifications.
  8. In the Specification Confirmation step, view the details of the application and the fee for the selected specifications. Then, click Confirm.
    The Creation Completed step appears. You can click Application Details to go to the Basic Information page of the application.
  9. Check whether the configurations have taken effect.
    • Method 1:

      In the left-side navigation pane of the Basic Information page, click Change Records. On the Change Records page, view the change details. If Executed is displayed in the Change Status column, the application is deployed and the configurations have taken effect.

    • Method 2:

      On the Basic Information page, click the Instance Deployment Information tab to view the Status of the instances. If Running is displayed in the Status column, the application is deployed and the configurations have taken effect.

Inject configurations when you deploy an application

Warning After you redeploy an application, the application is restarted. To prevent unpredictable errors such as business interruptions, we recommend that you deploy applications during off-peak hours.
  1. Log on to the SAE console.
  2. In the left-side navigation pane, click Applications. In the top navigation bar, select a region. Then, click the name of an application.
  3. In the upper-right corner of the Basic Information page, click Deploy Application.
  4. In the Configuration Management section, select the ConfigMap from the ConfigMap Name drop-down list and a key from the Key drop-down list. Then, specify the path on which the ConfigMap is mounted in the Mount Path field.
    sc_configuration_management
    Note
    • You can create a ConfigMap in the Namespaces module. You can also click Create ConfigMap (K8s configmap) in the Environment Variable Settings section to create a ConfigMap. Then, configure the parameters in the Create ConfigMap panel.
    • You can mount all keys of a ConfigMap. To mount all keys of the ConfigMap on the specified path, select All from the Key drop-down list.
  5. After you configure the settings, click Confirm.
  6. Check whether the configurations have taken effect.
    • Method 1:

      In the left-side navigation pane of the Basic Information page, click Change Records. On the Change Records page, view the change details. If Executed is displayed in the Change Status column, the application is deployed and the configurations have taken effect.

    • Method 2:

      On the Basic Information page, click the Instance Deployment Information tab to view the Status of the instances. If Running is displayed in the Status column, the application is deployed and the configurations have taken effect.

Use a ConfigMap as a set of environment variables

You can specify a ConfigMap that includes a set of environment variables for a container in a runtime environment. This way, you can flexibly modify the configuration of the container after applications are deployed. This section describes how to use a ConfigMap that includes a set of environment variables when you create an application.

  1. Log on to the SAE console.
  2. In the left-side navigation pane, click Applications. In the top navigation bar, select a region. Then, click Create Application.
  3. In the Basic Information step, configure the parameters and click Next: Application Deployment Configurations.
  4. In the Deployment Configurations step, configure the required parameters and expand the Environment Variable Settings section.
  5. In the Environment Variable Settings section, select Reference ConfigMap from the Type drop-down list and configure the Variable Name parameter. Then, select the name of the ConfigMap that you created and a key of the ConfigMap from the Variable Value/Variable Reference drop-down list.
    sc_environment_variable_v2.png
    Note You can create a ConfigMap on the Namespace page. You can also click Create ConfigMap (K8s configmap) in the Environment Variable Settings section, and then create a ConfigMap in the Create ConfigMap panel.
  6. Optional: To reference other ConfigMaps, click Add and repeat the previous step.
  7. Click Next: Confirm Specifications.
  8. In the Specification Confirmation step, view the details of the application and the fee for the selected specifications. Then, click Confirm.
    The Creation Completed step appears. You can click Application Details to go to the Basic Information page of the application.

Best practices

Manually configure a php-fpm.conf file for a PHP application

  1. Create a ConfigMap. For more information, see Create a ConfigMap.
    sc_create_configmap_for_php
    ParameterDescription
    MethodSelect Manual.
    ConfigMap NameEnter a name for the ConfigMap. Example: php-fpm.
    ConfigMap DescriptionEnter a description for the ConfigMap. Example: www.conf.
    Configure Mappings
    • Key
      www.conf
    • Value
      [www]
      user = www-data
      group = www-data
      listen = 127.0.0.1:9000
      
      pm = dynamic
      pm.max_children = 5
      pm.start_servers = 2
      pm.min_spare_servers = 1
      pm.max_spare_servers = 3
  2. Inject configurations. For more information, see Inject configurations.
    sc_use_php_configmap
    ParameterDescription
    ConfigMap NameSelect the php-fpm ConfigMap that you created.
    KeySelect the www.conf key that you created.
    Mount PathEnter the following path:
    /usr/local/etc/php-fpm.d/www.conf