Deploying enterprise applications requires extensive configuration management. Hard-coding configuration into an image couples the application to a specific environment and requires a unique image for each. ConfigMaps decouple configuration from your application, which allows a single image to run in multiple environments. ConfigMaps store non-sensitive configuration data. For sensitive data, use Secrets, which offer greater protection.
Choose a method
Mount as a Volume (Recommended): Mounts a ConfigMap as a Volume, exposing its data as files inside a container. This method supports automatic updates when the ConfigMap is modified. Recommended for managing entire configuration files and for scenarios that require dynamic updates.
Inject as environment variables: Injects key-value pairs from a ConfigMap as environment variables in the container. Recommended for simple parameters, such as runtime arguments or feature flags.
Create a ConfigMap
This section shows how to create a ConfigMap for an Nginx example deployment.
Console
Log on to the ACK console. In the navigation pane on the left, click Clusters. Click the name of your Cluster. In the navigation pane on the left, choose .
On the ConfigMaps page, select
defaultfrom the Namespace drop-down list and click Create.Configure the following parameters, then click OK.
ConfigMap Name: app-config
Click + Add, then enter a Name and Value to add a data entry. You can also click Import to create a ConfigMap from a file.
Set Key to
nginx.confand Value to the following content:server { listen 80; location / { root /usr/share/nginx/html; index index.html; } location /health { return 200 "healthy\n"; add_header Content-Type text/plain; } }
Kubectl
Run the following command to create the ConfigMap from a literal value:
kubectl create configmap app-config \ --namespace=default \ --from-literal=nginx.conf="$(cat <<'EOF' server { listen 80; location / { root /usr/share/nginx/html; index index.html; } location /health { return 200 "healthy\n"; add_header Content-Type text/plain; } } EOF )"Verify that the ConfigMap is created. The output
DATA: 1confirms that the ConfigMap was created.kubectl get configmap app-config
Use the ConfigMap in a pod
The Workload and the ConfigMap that it uses must be in the same Cluster and Namespace.
Method 1: Mount as a volume
Console
Log on to the ACK console. In the navigation pane on the left, click Clusters. Click the name of your Cluster. In the navigation pane on the left, choose .
Create a Deployment.
On the Deployments page, click Create from Image.
In the Basic Information step, configure the application.
For Application Name, use
nginx-volume-demo. For Namespace, usedefault.Replicas is
2. Type isStateless (Deployment).
Click Next to go to the Container wizard page.
Configure Image Name and Port.
Image Name:
anolis-registry.cn-zhangjiakou.cr.aliyuncs.com/openanolis/nginx:1.14.1-8.6Port: 80, with TCP as the protocol.
ImportantTo pull this Image, your Cluster must be able to access the internet. If you selected Configure SNAT for VPC when you created the Cluster (this option is enabled by default), no further action is required. Otherwise, see Enable internet access for an existing ACK cluster for instructions.
In the Volume section, click Add Local Storage. Configure the parameters and click Next.
Volume Type is ConfigMap, Name is
nginx-config, Mount Source isapp-config, and Container Path is/etc/nginx/conf.d.
In the Advanced Settings step, configure scaling, Scheduling, labels, and annotations as needed, and then click Create.
On the Complete page, check the status of the Task.
On the Creation Task Submitted panel, click View Details, and verify that the application status is
Running.
Verify that the ConfigMap file is mounted.
Find the pod that you created, such as
nginx-volume-demo-xxxxxxxxxx-xxxxx. In the Actions column, click Terminal to log on to the container.Run the following command. If the output contains the
nginx.conffile, the ConfigMap is mounted.ls -la /etc/nginx/conf.d/Verify the file content.
cat /etc/nginx/conf.d/nginx.confThe output matches the ConfigMap content, confirming that the application can access the data from the mounted Volume.
Kubectl
Create a file named nginx-volume-demo.yaml with the following content.
apiVersion: apps/v1 kind: Deployment metadata: name: nginx-volume-demo namespace: default spec: replicas: 2 selector: matchLabels: app: nginx-volume template: metadata: labels: app: nginx-volume spec: containers: - name: nginx image: anolis-registry.cn-zhangjiakou.cr.aliyuncs.com/openanolis/nginx:1.14.1-8.6 ports: - containerPort: 80 volumeMounts: - name: nginx-config mountPath: /etc/nginx/conf.d volumes: - name: nginx-config configMap: name: app-config items: - key: nginx.conf path: nginx.confRun the following command to create the Deployment.
kubectl apply -f nginx-volume-demo.yamlCheck the status of the pods. A
Runningstatus indicates a successful deployment.kubectl get pods -l app=nginx-volume -n defaultRun the following command to verify that the
nginx.conffile is mounted. The file's presence in the output confirms a successful Mount.kubectl exec deployment/nginx-volume-demo -n default -- ls -la /etc/nginx/conf.d/Run the following command to verify the file content.
kubectl exec deployment/nginx-volume-demo -n default -- cat /etc/nginx/conf.d/nginx.confThe output matches the ConfigMap content, confirming that the application can access the data from the mounted Volume.
Method 2: Inject as environment variables
Console
Log on to the ACK console. In the navigation pane on the left, click Clusters. Click the name of your Cluster. In the navigation pane on the left, choose .
Create a Deployment.
On the Deployments page, click Create from Image.
In the Basic Information step, configure the application.
Application Name is
nginx-env-demo. Namespace isdefault.Replicas is
2, and Type isStateless (Deployment).
Then, click Next to go to the Container wizard page.
In the Container step, configure the Image Name and Port.
Image Name:
anolis-registry.cn-zhangjiakou.cr.aliyuncs.com/openanolis/nginx:1.14.1-8.6Port: 80, with TCP as the protocol.
ImportantTo pull this Image, your Cluster must be able to access the internet. If you selected Configure SNAT for VPC when you created the cluster (this option is enabled by default), no further action is required. Otherwise, see Enable internet access for an existing ACK cluster for instructions.
In the Environment Variable section, click Add. Configure the parameters and click Next.
Set Type to ConfigMap, Name to
CONFIG_, and Variable/Variable Reference toapp-config.
In the Advanced Settings step, configure scaling, scheduling, labels, and annotations as needed, and then click Create.
On the Complete page, check the status of the Task.
On the Creation Task Submitted panel, click View Details, and verify that the container status is
Running.
Verify that the environment variables are injected.
Find the pod that you created, such as
nginx-env-demo-xxxxxxxxxx-xxxxx. In the Actions column, click Terminal to log on to the container.env | grep CONFIG_The output matches the ConfigMap content, confirming that the application can access data through environment variables.
Kubectl
Create a file named nginx-env-demo.yaml with the following content.
apiVersion: apps/v1 kind: Deployment metadata: name: nginx-env-demo namespace: default spec: replicas: 2 selector: matchLabels: app: nginx-env template: metadata: labels: app: nginx-env spec: containers: - name: nginx image: anolis-registry.cn-zhangjiakou.cr.aliyuncs.com/openanolis/nginx:1.14.1-8.6 ports: - containerPort: 80 # Inject all key-value pairs from the ConfigMap as environment variables. envFrom: - prefix: CONFIG_ configMapRef: name: app-configRun the following command to create the Deployment.
kubectl apply -f nginx-env-demo.yamlCheck the status of the pods. A
Runningstatus indicates a successful deployment.kubectl get pods -l app=nginx-env -n defaultRun the following command to verify that the environment variables are injected.
kubectl exec deployment/nginx-env-demo -n default -- env | grep CONFIG_The output matches the ConfigMap content, confirming that the application can access data through environment variables.
Manage ConfigMaps
After you create a ConfigMap, you can manage it on the ConfigMaps page.
Actions | Description |
Edit a ConfigMap | In the Actions column of the ConfigMap that you want to modify, click Edit. You can modify the Name and Value of the configuration entries. Important Modifying a ConfigMap in use can directly affect associated applications, potentially causing them to restart or malfunction. Before making changes, assess the impact and perform the operation during off-peak hours. |
Delete a ConfigMap | In the Actions column of the ConfigMap that you want to remove, click Delete. Important Do not delete ConfigMaps that are automatically generated by the system. These ConfigMaps are located in the |
Related topics
For more information, see the official Kubernetes documentation topic, Configure a Pod to Use a ConfigMap.