All Products
Search
Document Center

Container Service for Kubernetes:Use a ConfigMap

Last Updated:Oct 30, 2026

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

  1. 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 Configurations > ConfigMaps.

  2. On the ConfigMaps page, select default from the Namespace drop-down list and click Create.

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

  1. Connect to a cluster by 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 is created. The output DATA: 1 confirms 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

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

  2. Create a Deployment.

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

    2. In the Basic Information step, configure the application.

      • For Application Name, use nginx-volume-demo. For Namespace, use default.

      • Replicas is 2. Type is Stateless (Deployment).

      Click Next to go to the Container wizard page.

    3. Configure Image Name and Port.

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

      • Port: 80, with TCP as the protocol.

      Important

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

    4. In the Volume section, click Add Local Storage. Configure the parameters and click Next.

      • Volume Type is ConfigMap, Name is nginx-config, Mount Source is app-config, and Container Path is /etc/nginx/conf.d.

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

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

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

    1. Run the following command. If the output contains the nginx.conf file, the ConfigMap is mounted.

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

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

      The output matches the ConfigMap content, confirming that the application can access the data from the mounted Volume.

Kubectl

  1. 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.conf
  2. Run the following command to create the Deployment.

    kubectl apply -f  nginx-volume-demo.yaml
  3. Check the status of the pods. A Running status indicates a successful deployment.

    kubectl get pods -l app=nginx-volume -n default
  4. Run the following command to verify that the nginx.conf file 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/
  5. Run the following command to verify the file content.

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

    The output matches the ConfigMap content, confirming that the application can access the data from the mounted Volume.

Method 2: Inject as environment variables

Console

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

  2. Create a Deployment.

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

    2. In the Basic Information step, configure the application.

      • Application Name is nginx-env-demo. Namespace is default.

      • Replicas is 2, and Type is Stateless (Deployment).

      Then, click Next to go to the Container wizard page.

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

      • Port: 80, with TCP as the protocol.

      Important

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

    4. In the Environment Variable section, click Add. Configure the parameters and click Next.

      • Set Type to ConfigMap, Name to CONFIG_, and Variable/Variable Reference to app-config.

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

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

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

  1. 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-config
    
  2. Run the following command to create the Deployment.

    kubectl apply -f nginx-env-demo.yaml
  3. Check the status of the pods. A Running status indicates a successful deployment.

    kubectl get pods -l app=nginx-env -n default
  4. Run 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 kube-system and kube-public namespaces, such as the CoreDNS configuration. Deleting these ConfigMaps may cause the Cluster to malfunction.

Related topics