All Products
Search
Document Center

Container Service for Kubernetes:Use ConfigMaps to configure pods

Last Updated:Oct 13, 2025

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

  1. Log on to the ACK console. In the left navigation pane, click Clusters, and click the target cluster. In the left navigation pane, choose Configurations > ConfigMaps.

  2. Set Namespace to default and click Create.

  3. Configure the new ConfigMap:

    • ConfigMap Name: app-config

    • Add configuration item: Click + Add to add a new entry with the key nginx.conf and 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

  1. Connect to a cluster using kubectl.

  2. 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
    )" 
  3. Verify that the ConfigMap was created. The output DATA: 1 indicates 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

  1. Log on to the ACK console. In the left navigation pane, click Clusters, and click the target cluster. In the left navigation pane, choose Workloads > Deployments.

  2. Create a Deployment application.

    1. On the Deployments page, click Create from Image.

    2. In the Basic Information step, configure the following:

      • Name: nginx-volume-demo

      • Namespace: default

      • Replicas: 2

      • Type: Deployment

      Then, click Next to go to the Container step.

    3. Configure Image Name and Port.

      • Image Name: anolis-registry.cn-zhangjiakou.cr.aliyuncs.com/openanolis/nginx:1.14.1-8.6

      • Container Port: 80, Protocol: TCP.

      Important

      Before 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.

    4. In the Volume section, click Add Local Storage. After configuring the following, click Next.

      • PV Type: ConfigMap

      • Key: nginx-config

      • Mount Source: app-config

      • Container Path: /etc/nginx/conf.d

    5. In the Advanced step, configure access, scaling, scheduling, labels, and annotations as needed, then click Create.

    6. 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.

  3. 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.

    1. The output contains the nginx.conf file. This indicates that the ConfigMap was mounted.

      ls -la /etc/nginx/conf.d/
    2. Verify the file content.

      cat /etc/nginx/conf.d/nginx.conf

      The 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

  1. Create a file named nginx-volume-demo.yaml with the following content. This manifest mounts the app-config ConfigMap to the /etc/nginx/conf.d directory 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.conf
  2. Create the Deployment application.

    kubectl apply -f  nginx-volume-demo.yaml
  3. Check the status of the Deployment. If the pod status is Running, the application was created.

    kubectl get pods -l app=nginx-volume -n default
  4. Connect to one of the running pods and verify that the nginx.conf file is present. This indicates that the ConfigMap was mounted.

    kubectl exec deployment/nginx-volume-demo -n default -- ls -la /etc/nginx/conf.d/
  5. Verify the file content.

    kubectl exec deployment/nginx-volume-demo -n default -- cat /etc/nginx/conf.d/nginx.conf

    The 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

  1. 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 Workloads > Deployments.

  2. Create a Deployment application.

    1. On the Deployments page, click Create from Image.

    2. In the Basic Information step, configure the following:

      • Name: nginx-env-demo

      • Namespace: default

      • Replicas: 2

      • Type: Deployment

      Then, click Next to go to the Container step.

    3. Configure Image Name and Port.

      • Image Name: anolis-registry.cn-zhangjiakou.cr.aliyuncs.com/openanolis/nginx:1.14.1-8.6

      • Container Port: 80, Protocol: TCP.

      Important

      Before 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.

    4. In the Environments section, click Add. After entering the required information, click Next.

      • Type: ConfigMaps

      • Variable Key: CONFIG_

      • Value/ValueFrom: app-config

    5. In the Advanced step, configure access, scaling, scheduling, labels, and annotations as required, then click Create.

    6. 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.

  3. 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

  1. Create a file named nginx-env-demo.yaml. This manifest uses envFrom to inject all keys from the app-config ConfigMap as environment variables with the prefix CONFIG_.

    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-config
    
  2. Create the Deployment application.

    kubectl apply -f nginx-env-demo.yaml
  3. Check the status of the Deployment. If the pod status is Running, the application was created.

    kubectl get pods -l app=nginx-env -n default
  4. Verify 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 kube-system and kube-public namespaces, such as the CoreDNS configuration. Deleting them can affect cluster stability.

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.