ConfigMaps serve as environment variables or mounted configuration files in application containers, enabling you to change configurations after deployment without rebuilding the image.
SAE environment variables take effect only during the instance runtime phase. Environment variable injection is not supported during the build phase. As a deployment attribute of the application, environment variables are injected into the runtime environment when the container starts and are not involved in the image build process.
To dynamically fetch configuration data from a configuration center, see Use Nacos in MSE or Manage distributed configurations (ACM).
To store sensitive information, such as keys and certificates, see Manage and use Secrets (K8s Secret).
Create a ConfigMap
-
On the SAE ConfigMaps page, select the region and namespace for your application, and then click Create.
-
Specify a custom ConfigMap Name.
-
Under Configure Mappings, select an editing mode to define the key-value pairs:
-
Edit using form: Click Add Variable, and then enter a key and its value. You can add multiple key-value pairs. For long configuration content, click the
icon next to the value to paste and edit the value in a larger editor. -
Edit in JSON format: In the code editor, enter one or more key-value pairs. Keys and string values must be enclosed in double quotes (
""). 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 }" } -
Edit in YAML format: In the code editor, enter one or more key-value pairs. For example:
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; } }
-
-
-
Find the ConfigMap you created in the Configuration Item (ConfigMap) list. You can then Edit, Copy, View Record, or Delete it.
Important-
A ConfigMap only takes effect after you use it as an environment variable or mount it as a file to your application's file system.
-
If you modify a ConfigMap, you must redeploy any referencing application for the changes to take effect.
-
Deleting a ConfigMap can cause referencing applications to fail. Before deleting a ConfigMap, remove all references to it from your applications.
-
Use a ConfigMap as an environment variable
-
Create an application
On the SAE Application List page, select a destination region and namespace in the top navigation bar, and then click Create Application. On the Application Basic Information page, configure the parameters and click Next: Advanced Settings.
Change a running application
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.
On the SAE Application List page, select a destination region and namespace in the top navigation bar. Click the ID of the target Application to open the application details page.In the navigation pane on the left, click Basic Information. In the upper-right corner, click Deploy Application.
Change a stopped application
On the SAE Application List page, select a destination region and namespace in the top navigation bar. Click the ID of the target Application to open the application details page.Click Basic Information, and then click Modify Application Configuration.
-
In the Environment Variable section, set Type to Reference ConfigMap. Specify a custom Variable Name for the environment variable in your application, and then select the ConfigMap Name and Key to reference. You can add multiple variables. If you choose to reference all keys from the ConfigMap, you do not need to specify a Variable Name, as the original key names are used as the environment variable names.
-
Wait for the application to deploy. To verify the configuration, log on to the web shell of the application instance and run the command
env | grep <variable_name>. Replace<variable_name>with the variable name you configured. If the output shows the correct variable name and value, the configuration is successful.
Mount a ConfigMap as a file
Mounting a ConfigMap as a file writes the value of the specified key as the file content. You can customize the file path and name. The mounted file overwrites any existing file at the same path.
Use this approach to manage configuration files without rebuilding the container image. To modify an existing configuration file, copy its content into a ConfigMap value, then mount the ConfigMap to the original file path.
-
Create an application
On the SAE Application List page, select a destination region and namespace in the top navigation bar, and then click Create Application. On the Application Basic Information page, configure the parameters and click Next: Advanced Settings.
Change a running application
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.
On the SAE Application List page, select a destination region and namespace in the top navigation bar. Click the ID of the target Application to open the application details page.In the navigation pane on the left, click Basic Information. In the upper-right corner, click Deploy Application.
Change a stopped application
On the SAE Application List page, select a destination region and namespace in the top navigation bar. Click the ID of the target Application to open the application details page.Click Basic Information, and then click Modify Application Configuration.
-
In the Configuration Management section, click + Add. Select the ConfigMap Name and the Key to mount (or choose to mount all keys). Then, specify the Mount Path. If you are mounting a single key, provide an absolute path including the filename. If you are mounting all keys, provide an absolute path to a directory, and SAE automatically uses the key names as filenames. You can add multiple mounts.
-
Wait for the application to deploy. To verify the configuration, log on to the web shell of the application instance and check the content of the file at the mount path.