You need to enable JavaScript to run this app.
Volcengine Kubernetes Engine

Volcengine Kubernetes Engine

Copy page
Download PDF
Configuration management
Using ConfigMaps in containers
Copy page
Download PDF
Using ConfigMaps in containers
This topic describes the scenarios and procedures for using ConfigMaps in containers.​
Background​
Enterprise application deployment involves significant configuration management. Hardcoding configurations into container images creates environment-specific dependencies, requiring separate image builds per environment. ConfigMaps decouple configuration from applications, enabling a single image to run across multiple environments (development, testing, and production) by storing non-sensitive configuration data.​
After creating a ConfigMap, you can use it in containers via two methods.​
​
Usage methods​
Description​
Application scenario​
Environment variable injection​
Injects key-value pairs from the ConfigMap directly into container environment variables.​
Simple runtime parameters, feature flags, database connection strings, and other short configuration items.​
Data volume mounting​
Mounts data stored in the ConfigMap as a file into a specified container directory. This method supports automatic synchronization and hot updates.​
Complete configuration file management and scenarios requiring dynamic updates without container restarts.​
​
Preparation​
Prepare the ConfigMap in advance. For details, see Creating ConfigMaps. ‌To facilitate validation of ConfigMap usage, this topic uses the sample ConfigMap file demo-config.yaml below.​
​
apiVersion: v1​
kind: ConfigMap​
metadata:​
name: demo-config # ConfigMap name. Workloads use this name to reference the ConfigMap.​
namespace: default # Namespace of the ConfigMap. The namespace must match the target workload's namespace.​
data: # Key-value pairs in the ConfigMap.​
APP_MODE: "prod"​
LOG_LEVEL: "info"​
PLAYER_NAME: "vke-user"​
WELCOME_MESSAGE: "hello-from-configmap"​
game.properties: |​
APP_MODE=prod​
LOG_LEVEL=info​
PLAYER_NAME=vke-user​
​
Procedure​
Scenario 1: Environment variable injection​
This section illustrates how to use a ConfigMap to set environment variables for a workload (in this example, a Deployment).​
​
Configure through the console
Configure through kubectl
​
  1. Log in to the VKE console. ​
  1. In the left navigation bar, click Cluster.​
  1. On the Cluster page, click the name of the target cluster.​
  1. Click Workload > Deployment. On the Deployment page, click Create Deployment.​
  1. On the Create Deployment page, configure the following parameters as described below and other parameters as needed. For detailed instructions, see Creating Deployments.​
  • In Step 2 Container configuration, click the + icon in the Environment variables section to use the ConfigMap prepared in the prerequisites as the environment variables for the Deployment.​
  1. Click Confirm to create the Deployment.​
  1. Check the Deployment creation result and verify whether the environment variables have been injected successfully.​
  1. On the Deployment page, find the newly created Deployment and click its name to enter the Deployment details page.​
  1. On the Deployment details page, click the List of instances tab to view whether all pods corresponding to the Deployment are in the status of Running.​
  1. Click the Pod name to enter the Pods details page. Click Console in the upper-right corner of the page to log in to the Pod and then run the following command to verify whether the environment variables have been injected successfully.​
  • ​
    env | grep CONFIG_​
    ​
  • If the output matches the ConfigMap data, environment variable injection has successfully enabled the Deployment to access the ConfigMap data.​
​
Scenario 2: Data volume mounting​
This section illustrates how to mount a ConfigMap as a data volume for a workload (in this example, a Deployment).​
​
Configure through the console
Configure through kubectl
​
  1. Log in to the VKE console. ​
  1. In the left navigation bar, click Cluster.​
  1. On the Cluster page, click the name of the target cluster.​
  1. Click Workload > Deployment. On the Deployment page, click Create Deployment.​
  1. On the Create Deployment page, configure the following parameters as described below and other parameters as needed. For detailed instructions, see Creating Deployments.​
  • In Step 2 Container configuration, configure Storage configuration parameters as described below.​
  • For Add volume, click the + icon and configure the ConfigMap as a volume.​
  • For Mount volume, click the + icon to mount the ConfigMap.​
  1. Click Confirm to create the Deployment.​
  1. Check the Deployment creation result and verify whether the environment variables have been injected successfully.​
  1. On the Deployment page, find the newly created Deployment and click its name to enter the Deployment details page.​
  1. On the Deployment details page, click the List of instances tab to view whether all pods corresponding to the Deployment are in the status of Running.​
  1. Click the Pod name to enter the Pods details page. Click Console in the upper-right corner of the page to log in to the Pod and then run the following command to verify whether the ConfigMap has been mounted successfully.​
  • ​
    ls /etc/config/​
    ​
  • If the output is the filenames corresponding to each key in the ConfigMap (for example, APP_MODE and LOG_LEVEL), the mounting is successful.​
  1. Run the following command to check whether the configuration file's content is correct.​
  • ​
    cat /etc/config/APP_MODE​
    ​
  • If the output is the value corresponding to the APP_MODE file (such as prod), the configuration is correct.​
​
Subsequent operations​
To apply ConfigMap updates to your workload promptly, follow the instructions below:​
  • In environment variable scenarios, a rolling update is typically required. For example, for a Deployment, run the following command to perform a rolling update.​
  • ​
    kubectl rollout restart deployment/<deployment-name> -n <namespace>​
    # Replace <deployment-name> with your Deployment name.​
    # Replace <namespace> with the namespace of your Deployment.​
    ​
  • In data volume mounting scenarios, determine if a restart is needed based on your application's hot configuration reload mechanism.​
Last updated: 2026.07.24 14:51:38