All Products
Search
Document Center

Container Service for Kubernetes:Configure a pod to use a ConfigMap

Last Updated:May 31, 2024

ConfigMaps allow you to separate configuration files from images. This improves the portability of your container applications. This topic describes how to use a ConfigMap in a pod.

Scenarios

You can use a ConfigMap in a pod in the following scenarios:

For more information, see Configure a Pod to Use a ConfigMap.

Usage notes

When you use a ConfigMap in a pod, the pod and the ConfigMap must be in the same cluster and namespace.

Create a ConfigMap

In this example, a ConfigMap named Special_Config is created, which contains the SPECIAL_LEVEL: very and SPECIAL_TYPE: charm key-value pairs.

The following YAML template is used to create the ConfigMap:

apiVersion: v1
kind: ConfigMap
metadata:
   name: special-config
   namespace: default
data:
   SPECIAL_LEVEL: very
   SPECIAL_TYPE: charm

Use a ConfigMap to define environment variables for a pod.

Use the key-value pairs of a ConfigMap to define environment variables for a pod

  1. Log on to the ACK console. In the left-side navigation pane, click Clusters.

  2. On the Clusters page, click the name of the cluster that you want to manage and choose Workloads > Deployments in the left-side navigation pane.

  3. On the Deployments page, click Create from YAML in the upper-right corner.

  4. Select the sample template or enter a custom template, and click Create.

    To define an environment variable for a pod, you can use the valueFrom parameter to reference the value of SPECIAL_LEVEL. The following sample code provides an example.

    apiVersion: v1
    kind: Pod
    metadata:
       name: config-pod-1
    spec:
       containers:
         - name: test-container
           image: busybox
           command: [ "/bin/sh", "-c", "env" ]
           env:
             - name: SPECIAL_LEVEL_KEY
               valueFrom:              ##Use the valueFrom parameter to denote that env references the value of the ConfigMap. 
                 configMapKeyRef:
                   name: special-config               ##The name of the referenced ConfigMap. 
                   key: SPECIAL_LEVEL                 ##The key of the referenced key-value pair. 
       restartPolicy: Never

Use all key-value pairs of a ConfigMap to define multiple environment variables for a pod

  1. Log on to the ACK console. In the left-side navigation pane, click Clusters.

  2. On the Clusters page, click the name of the cluster that you want to manage and choose Workloads > Deployments in the left-side navigation pane.

  3. On the Deployments page, click Create from YAML in the upper-right corner.

  4. Select the sample template or enter a custom template, and click Create.

    If you want to use all key-value pairs of a ConfigMap to define multiple environment variables for a pod, you can use the envFrom parameter. The keys of the ConfigMap are used as the names of the environment variables in the pod. The following sample code provides an example.

    apiVersion: v1
    kind: Pod
    metadata:
       name: config-pod-2
    spec:
       containers:
         - name: test-container
           image: busybox
           command: [ "/bin/sh", "-c", "env" ]
           envFrom:                ##Reference all key-value pairs in the special-config configuration file. 
           - configMapRef:
               name: special-config
       restartPolicy: Never

Use a ConfigMap to set command line parameters

  1. Log on to the ACK console. In the left-side navigation pane, click Clusters.

  2. On the Clusters page, click the name of the cluster that you want to manage and choose Workloads > Deployments in the left-side navigation pane.

  3. On the Deployments page, click Create from YAML in the upper-right corner.

  4. Select the sample template or enter a custom template, and click Create.

    You can use the ConfigMap to set the commands or parameter values for a container by using the $(VAR_NAME) syntax for environment variable replacement. The following sample code provides an example.

    apiVersion: v1
    kind: Pod
    metadata:
       name: config-pod-3
    spec:
       containers:
         - name: test-container
           image: busybox
           command: [ "/bin/sh", "-c", "echo $(SPECIAL_LEVEL_KEY) $(SPECIAL_TYPE_KEY)" ]
           env:
             - name: SPECIAL_LEVEL_KEY
               valueFrom:
                 configMapKeyRef:
                   name: special-config
                   key: SPECIAL_LEVEL
             - name: SPECIAL_TYPE_KEY
               valueFrom:
                 configMapKeyRef:
                   name: special-config
                   key: SPECIAL_TYPE
       restartPolicy: Never

Use a ConfigMap in a volume.

  1. Log on to the ACK console. In the left-side navigation pane, click Clusters.

  2. On the Clusters page, click the name of the cluster that you want to manage and choose Workloads > Deployments in the left-side navigation pane.

  3. On the Deployments page, click Create from YAML in the upper-right corner.

  4. Select a sample template or customize a template, and click Create.

    You can use a ConfigMap in a volume and specify the ConfigMap name in the volumes section. This saves the key-value pairs of the ConfigMap to the directory specified in the mountPath field. In this example, the directory is /etc/config. Configuration files that are named after the keys of the ConfigMap are generated. The values of the ConfigMap are stored in the related files. The following sample code provides an example.

    apiVersion: v1
    kind: Pod
    metadata:
       name: config-pod-4
    spec:
       containers:
         - name: test-container
           image: busybox
           command: [ "/bin/sh", "-c", "ls /etc/config/" ]   ##List the names of the files in the directory. 
           volumeMounts:
           - name: config-volume
             mountPath: /etc/config
       volumes:
         - name: config-volume
           configMap:
             name: special-config
       restartPolicy: Never

    After the pod is run, the keys of the ConfigMap are returned.

    SPECIAL_TYPE
    SPECIAL_LEVEL