A ConfigMap (Kubernetes ConfigMap) can be used as an environment variable for an application runtime or mounted as a configuration file to an application container's file system. Using a ConfigMap lets you change configurations after application deployment without rebuilding the image.
This topic does not apply to scenarios where you need to dynamically obtain configuration information from a configuration center. For more information, see Use Nacos in MSE or Manage distributed configurations (ACM).
To store sensitive information, such as keys and certificates, see Managing and using K8s Secrets.
Create a ConfigMap
On the SAE ConfigMaps page, select the destination region and namespace at the top of the page, and then click Create. Ensure that you select the namespace where the target application is located.
Specify a custom Configuration Item Name.
Configuration Mapping: Select an edit mode and configure the variable name and value mappings.
Edit With Form: Click Add Variable and enter the variable name and its value. You can add multiple variables. For long configuration content, you can click the
icon next to the variable value to paste and edit the content.Edit In JSON Format: In the code box, enter one or more mappings of variable names and their values. The variable names and values must be enclosed in
"". For example:{ "nv_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 }" }Use YAML Format: In the code box, enter one or more mappings of variable names and their values. The following is a sample configuration:
nv_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; } }
In the ConfigMap list, you can view the created ConfigMaps and perform operations such as Edit, Copy, View History, and Delete.
ImportantAfter you create a ConfigMap, you must use the ConfigMap as an environment variable for the application or mount the ConfigMap as a configuration file to the application's file system for it to take effect.
If you modify a ConfigMap, you must redeploy the application that references it for the changes to take effect.
Deleting a ConfigMap affects the operation of applications that reference it. Before you delete a ConfigMap, ensure that you remove its references from the applications.
Use a ConfigMap as an environment variable for an application
Create an application
Log on to the SAE console. In the left-side navigation pane, choose . On the page that appears, select a region in the top navigation bar and a namespace from the Namespace drop-down list, and then click Create Application.
After the Basic Information is specified on Create Application page, click Next: Advanced Settings.
Modify running applications
WarningAfter 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.
Log on to the SAE console. In the left-side navigation pane, choose . On the page that appears, select a region in the top navigation bar and a namespace from the Namespace drop-down list, and then click the desired application name.
On the Basic Information page of the application, click Deploy Application.
Modify stopped applications
Log on to the SAE console. In the left-side navigation pane, choose . On the page that appears, select a region in the top navigation bar and a namespace from the Namespace drop-down list, and then click the desired application name.
On the Basic Information page of the application, click Modify Application Configurations.
In the Environment Variable section, set Type to Reference Configuration Item and specify a custom Variable Name for the current application. If you reference all keys, you do not need to specify a variable name because the key names are used as the variable names. Then, select an existing Configuration Item Name and Key. A key is a variable in the configuration item. Referencing all keys is also supported. You can add multiple configuration items.
Wait for the application to be deployed. To verify that the configuration is effective, log on to the web shell of the application instance and run the
env | grep <variable name>command. Replace<variable name>with the variable name that you configured. If the command returns the mapping of the variable name and its value, the configuration is effective.
Mount a ConfigMap as a configuration file to the file system of an application
You can mount a ConfigMap to the file system of an application container. The value of the ConfigMap is used as the file content. You can customize the file path and name. If a file with the same name exists in the path, the mounted file overwrites the original file.
A mounted file is often used as the application's configuration file. This lets you change configurations without rebuilding the image. To modify an existing configuration file in the application container, first copy the original file content to your local machine. After you modify the content, save it as the value of a ConfigMap. Then, follow the instructions in this topic to mount the ConfigMap to the application's file system.
Create an application
Log on to the SAE console. In the left-side navigation pane, choose . On the page that appears, select a region in the top navigation bar and a namespace from the Namespace drop-down list, and then click Create Application.
After the Basic Information is specified on Create Application page, click Next: Advanced Settings.
Modify running applications
WarningAfter 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.
Log on to the SAE console. In the left-side navigation pane, choose . On the page that appears, select a region in the top navigation bar and a namespace from the Namespace drop-down list, and then click the desired application name.
On the Basic Information page of the application, click Deploy Application.
Modify stopped applications
Log on to the SAE console. In the left-side navigation pane, choose . On the page that appears, select a region in the top navigation bar and a namespace from the Namespace drop-down list, and then click the desired application name.
On the Basic Information page of the application, click Modify Application Configurations.
In the Configuration Management area, click + Add. Select the Configuration Item Name and Key of an existing configuration item. The key is the variable name in the configuration item. You can mount a single key or all keys. Then, specify a custom Mount Path. To mount a single key, enter an absolute path that includes the filename. To mount all keys, enter an absolute path that does not include a filename. SAE automatically uses the key name of each configuration item as the filename. You can add multiple configuration items.
Wait for the application to be deployed. To verify that the configuration is effective, log on to the web shell of the application instance and view the file content in the mount path.