This topic describes how to use a ConfigMap in a Pod.

Background information

You can use a ConfigMap in a Pod in the following scenarios:
  • Use a ConfigMap to define one or multiple environment variables for a Pod.
  • Use a ConfigMap to set command line parameters.
  • Use a ConfigMap in a volume.

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

Limits

To use a ConfigMap in a Pod, make sure that the ConfigMap and Pod are in the same cluster and namespace.

Create a ConfigMap

In this example, a ConfigMap named special_config is created. It consists of two key-value pairs: SPECIAL_LEVEL: very and SPECIAL_TYPE: charm. For more information, see the

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

Use a ConfigMap to define one or multiple environment variables for a Pod

  • Use a key-value pair of a ConfigMap to define one environment variable
    1. Log on to the Container Service console.
    2. In the left-side navigation pane, choose Applications > Deployments. On the Deployments page that appears, click Create from Template in the upper-right corner.
    3. Select the cluster and namespace, select a sample template, and click Create. You can also set Sample Template to Custom and define a custom template.

      To define an environment variable for a Pod, you can use valueFrom to reference the value of SPECIAL_LEVEL of the special_config ConfigMap.

      The detailed sample template is as follows:

      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 valueFrom to denote that env references the value of a ConfigMap.
                   configMapKeyRef:
                     name: special-config               ##The name of the referenced ConfigMap.
                     key: SPECIAL_LEVEL                 ##The key of the referenced key-value pair.
         restartPolicy: Never

      To use the values of multiple ConfigMaps to define multiple environment variables, add multiple env parameters in the Pod configuration file.

  • Use all the key-value pairs of a ConfigMap to define multiple environment variables
    1. In the left-side navigation pane, choose Applications > Deployments. On the Deployments page that appears, click Create from Template in the upper-right corner.
    2. Select the cluster and namespace, select a sample template, and click Create. You can also set Sample Template to Custom and define a custom template.

      To use all the key-value pairs of a ConfigMap to define multiple environment variables, you can use the envFrom parameter. The keys of the ConfigMap are used as the environment variable names.

      The detailed sample template is as follows:

      apiVersion: v1
      kind: Pod
      metadata:
         name: config-pod-2
      spec:
         containers:
           - name: test-container
             image: busybox
             command: [ "/bin/sh", "-c", "env" ]
             envFrom:                ##Reference all the key-value pairs of the special-config ConfigMap.
             - configMapRef:
                 name: special-config
         restartPolicy: Never
  • Use a ConfigMap to set command line parameters
    1. In the left-side navigation pane, choose Applications > Deployments. On the Deployments page that appears, click Create from Template in the upper-right corner.
    2. Select the cluster and namespace, select a sample template, and click Create. You can also set Sample Template to Custom and define a custom template.

      You can use a ConfigMap to define environment variables and use the variables as command line parameters through the $(VAR_NAME) variable substitution syntax.

      The detailed sample template is as follows:

      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

      After you run the Pod, the output is as follows:

      very charm

Use a ConfigMap in a volume

Select the cluster and namespace, select a sample template, and click Create. You can also set Sample Template to Custom and define a custom template.
To use a ConfigMap in a volume, specify the ConfigMap name in the volumes section. This saves the key-value pairs of the ConfigMap to the directory specified by mountPath, which is /etc/config in this example. This generates configuration files that are named after the keys of the key-value pairs of the ConfigMap. The values of the key-value pairs are respectively stored in the corresponding files.

The detailed sample template is as follows:

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 files under the directory.
       volumeMounts:
       - name: config-volume
         mountPath: /etc/config
   volumes:
     - name: config-volume
       configMap:
         name: special-config
   restartPolicy: Never

After you run the Pod, the output is as follows:

SPECIAL_TYPE
SPECIAL_LEVEL