Deploying enterprise applications requires extensive configuration management. Hard-coding this configuration into your application image couples the application to a specific environment, requiring you to build a new image for each environment. Kubernetes ConfigMaps solve this problem by decoupling configuration from your application code. They allow you to store non-sensitive configuration data externally and inject it into your pods at runtime. This enables you to use the same application image across multiple environments. For sensitive data, use Secrets.
Choose a method
Mounting (Recommended): Exposes configuration data as files. Changes to the ConfigMap are automatically synced to the mounted files without requiring a pod restart. Use this method to manage complete configuration files and for scenarios that require dynamic updates.
Injecting: Injects configurations directly into the container as environment variables. Use this method for simple key-value parameters, such as runtime arguments or feature flags.
Create a ConfigMap
This section describes how to create a ConfigMap for a sample Nginx deployment.
Use the console
Log on to the ACK console. In the left navigation pane, click Clusters, and click the target cluster. In the left navigation pane, choose .
Set Namespace to
defaultand click Create.Configure the new ConfigMap:
ConfigMap Name: app-config
Add configuration item: Click + Add to add a new entry with the key
nginx.confand the following value. You can also click Import to create a ConfigMap from a JSON file. Then, click OK.nginx.conf:server { listen 80; location / { root /usr/share/nginx/html; index index.html; } location /health { return 200 "healthy\n"; add_header Content-Type text/plain; } }
Use 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 was created. The output
DATA: 1indicates that the ConfigMap was created.kubectl get configmap app-config
Use the ConfigMap in a pod
The Deployment and the ConfigMap it uses must be in the same cluster and namespace.
Method 1: Mount the ConfigMap as a volume
Use the console
Log on to the ACK console. In the left navigation pane, click Clusters, and click the target cluster. In the left navigation pane, choose .
Create a Deployment application.
On the Deployments page, click Create from Image.
In the Basic Information step, configure the following:
Name:
nginx-volume-demoNamespace:
defaultReplicas:
2Type:
Deployment
Then, click Next to go to the Container step.
Configure Image Name and Port.
Image Name:
anolis-registry.cn-zhangjiakou.cr.aliyuncs.com/openanolis/nginx:1.14.1-8.6Container Port: 80, Protocol: TCP.
ImportantBefore pulling this image, the cluster must have Internet access. If you selected Configure SNAT for VPC when creating the cluster (this option is enabled by default), no additional configuration is required. Otherwise, see Enable an existing ACK cluster to access the Internet.
In the Volume section, click Add Local Storage. After configuring the following, click Next.
PV Type: ConfigMap
Key:
nginx-configMount Source:
app-configContainer Path:
/etc/nginx/conf.d
In the Advanced step, configure access, scaling, scheduling, labels, and annotations as needed, then click Create.
The Complete step shows the status of the application.
In the Creation Task Submitted panel, click to view details, and verify that the application is
Running.
Verify that the ConfigMap file is mounted.
Select the target container, for example,
nginx-volume-demo-7xxxxxx****. In the Actions column, click Terminal and log on to the nginx container.The output contains the
nginx.conffile. This indicates that the ConfigMap was mounted.ls -la /etc/nginx/conf.d/Verify the file content.
cat /etc/nginx/conf.d/nginx.confThe output is the same as the content of the ConfigMap. This indicates that the application can access the ConfigMap data by mounting the ConfigMap as a volume.
Use kubectl
Create a file named
nginx-volume-demo.yamlwith the following content. This manifest mounts theapp-configConfigMap to the/etc/nginx/conf.ddirectory inside the container.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.confCreate the Deployment application.
kubectl apply -f nginx-volume-demo.yamlCheck the status of the Deployment. If the pod status is
Running, the application was created.kubectl get pods -l app=nginx-volume -n defaultConnect to one of the running pods and verify that the
nginx.conffile is present. This indicates that the ConfigMap was mounted.kubectl exec deployment/nginx-volume-demo -n default -- ls -la /etc/nginx/conf.d/Verify the file content.
kubectl exec deployment/nginx-volume-demo -n default -- cat /etc/nginx/conf.d/nginx.confThe output is identical to the content of the ConfigMap. This demonstrates that the application can access the ConfigMap data by mounting the ConfigMap as a volume.
Method 2: Inject the ConfigMap as environment variables
Use the console
Log on to the ACK console. In the left navigation pane, click Clusters, and click the target cluster. Then, in the left navigation pane, choose .
Create a Deployment application.
On the Deployments page, click Create from Image.
In the Basic Information step, configure the following:
Name:
nginx-env-demoNamespace:
defaultReplicas:
2Type:
Deployment
Then, click Next to go to the Container step.
Configure Image Name and Port.
Image Name:
anolis-registry.cn-zhangjiakou.cr.aliyuncs.com/openanolis/nginx:1.14.1-8.6Container Port: 80, Protocol: TCP.
ImportantBefore pulling this image, the cluster must have Internet access. If you selected Configure SNAT for VPC when creating the cluster (this option is enabled by default), no additional configuration is required. Otherwise, see Enable an existing ACK cluster to access the Internet.
In the Environments section, click Add. After entering the required information, click Next.
Type: ConfigMaps
Variable Key:
CONFIG_Value/ValueFrom:
app-config
In the Advanced step, configure access, scaling, scheduling, labels, and annotations as required, then click Create.
The Complete step shows the status of the application.
In the Creation Task Submitted panel, click to view details and verify that the container status is
Running.
Verify that the environment variables are injected.
Select the container you want to log on to, for example, nginx-env-demo-7xxxxxx****. In the Actions column, click Terminal and log on to the nginx container.
env | grep CONFIG_The output is the same as the content of the ConfigMap. This indicates that the application can access the ConfigMap data through environment variables.
Use kubectl
Create a file named
nginx-env-demo.yaml. This manifest usesenvFromto inject all keys from theapp-configConfigMap as environment variables with the prefixCONFIG_.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. envFrom: - prefix: CONFIG_ configMapRef: name: app-configCreate the Deployment application.
kubectl apply -f nginx-env-demo.yamlCheck the status of the Deployment. If the pod status is
Running, the application was created.kubectl get pods -l app=nginx-env -n defaultVerify that the environment variables are injected.
kubectl exec deployment/nginx-env-demo -n default -- env | grep CONFIG_The output is the same as the content of the ConfigMap. This indicates that the application can access the ConfigMap data through environment variables.
Manage ConfigMaps
You can manage your ConfigMaps from the ConfigMaps page in the ACK console.
Operation | Description |
Edit a ConfigMap | Click Edit in the Actions column to modify a ConfigMap's Key and Value. Important Modifying a ConfigMap that is in use can cause application disruptions. Perform this action with caution. |
Delete a ConfigMap | Click Delete in the Actions column to remove an unneeded ConfigMap. Important Do not delete system-generated ConfigMaps in the |
References
Troubleshoot pod exceptions describes the diagnostic process, troubleshooting methods, common issues, and solutions for pod exceptions.
Create a stateless Deployment describes how to create a stateless application in an ACK cluster using the console and kubectl.
For configuration details, see the Kubernetes documentation ConfigMaps.