edit-icon download-icon

Use a config map in a pod

Last Updated: May 05, 2018

You can use a config map in a pod in the following scenarios:

  • Use a config map to define the pod environment variables.
  • Use a config map to configure command line parameters.
  • Use a config map in data volumes.

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

Limits

To use a config map in a pod, make sure the config map and the pod are in the same cluster and namespace.

Create a config map

In this example, create a config map special-config, which includes two key-value pairs: SPECIAL_LEVEL: very and SPECIAL_TYPE: charm.

Create a config map by using an orchestration template

  1. Log on to the Container Service console.

  2. Click Kubernetes > Application > Deployment in the left-side navigation pane.

  3. Click Create by template in the upper-right corner.

  4. Select the cluster and namespace from the Clusters and Namespace drop-down lists.

  5. Select a sample template or Custom from the Resource Type drop-down list.

  6. Click DEPLOY.

    You can use the following YAML sample template to create a config map.

    1. apiVersion: v1
    2. kind: ConfigMap
    3. metadata:
    4. name: special-config
    5. namespace: default
    6. data:
    7. SPECIAL_LEVEL: very
    8. SPECIAL_TYPE: charm

Create a config map on Config Maps page

  1. Log on to the Container Service console.

  2. Click Kubernetes > Application > Config Maps in the left-side navigation pane.

  3. Select the cluster and namespace from the Clusters and Namespace drop-down lists.

  4. Click Create in the upper-right corner.

  5. Enter the Config Map Name.

  6. Enter the Variable Name and the Variable Value. Then, click Add on the right.

  7. Click OK after completing the configurations.

    1

Use a config map to define pod environment variables

Use config map data to define pod environment variables

  1. Log on to the Container Service console.

  2. Click Kubernetes > Application > Deployment in the left-side navigation pane.

  3. Click Create by template in the upper-right corner.

  4. Select the cluster and namespace from the Clusters and Namespace drop-down lists.

  5. Select a sample template or Custom from the Resource Type drop-down list.

  6. Click DEPLOY.

    You can define the environment variables in a pod. Use valueFrom to reference the value of SPECIAL_LEVEL to define the pod environment variables.

    See the following orchestration example:

    1. apiVersion: v1
    2. kind: Pod
    3. metadata:
    4. name: config-pod-1
    5. spec:
    6. containers:
    7. - name: test-container
    8. image: busybox
    9. command: [ "/bin/sh", "-c", "env" ]
    10. env:
    11. - name: SPECIAL_LEVEL_KEY
    12. valueFrom: ##Use valueFrom to specify env to reference the value of the config map.
    13. configMapKeyRef:
    14. name: special-config ##The referenced config map name.
    15. key: SPECIAL_LEVEL ##The referenced config map key.
    16. restartPolicy: Never

Similarly, to define the values of multiple config maps to the environment variable values of the pod, add multiple env parameters in the pod.

Configure all key-value pairs of a config map to pod environment variables

  1. Log on to the Container Service console.

  2. Click Kubernetes > Application > Deployment in the left-side navigation pane.

  3. Click Create by template in the upper-right corner.

  4. Select the cluster and namespace from the Clusters and Namespace drop-down lists.

  5. Select a sample template or Custom from the Resource Type drop-down list.

  6. Click DEPLOY.

    To configure all the key-value pairs of a config map to the environment variables of a pod, use the envFrom parameter. The key in a config map becomes the environment variable name in the pod.

    See the following orchestration example:

    1. apiVersion: v1
    2. kind: Pod
    3. metadata:
    4. name: config-pod-2
    5. spec:
    6. containers:
    7. - name: test-container
    8. image: busybox
    9. command: [ "/bin/sh", "-c", "env" ]
    10. envFrom: ## Reference all the key-value pairs in the config map special-config.
    11. - configMapRef:
    12. name: special-config
    13. restartPolicy: Never

Use a config map to configure command line parameters

  1. Log on to the Container Service console.

  2. Click Kubernetes > Application > Deployment in the left-side navigation pane.

  3. Click Create by template in the upper-right corner.

  4. Select the cluster and namespace from the Clusters and Namespace drop-down lists.

  5. Select a sample template or Custom from the Resource Type drop-down list.

  6. Click DEPLOY.

    You can use the config map to configure the commands or parameter values in the container by using the environment variable replacement syntax $(VAR_NAME).

    See the following orchestration example:

    1. apiVersion: v1
    2. kind: Pod
    3. metadata:
    4. name: config-pod-3
    5. spec:
    6. containers:
    7. - name: test-container
    8. image: busybox
    9. command: [ "/bin/sh", "-c", "echo $(SPECIAL_LEVEL_KEY) $(SPECIAL_TYPE_KEY)" ]
    10. env:
    11. - name: SPECIAL_LEVEL_KEY
    12. valueFrom:
    13. configMapKeyRef:
    14. name: special-config
    15. key: SPECIAL_LEVEL
    16. - name: SPECIAL_TYPE_KEY
    17. valueFrom:
    18. configMapKeyRef:
    19. name: special-config
    20. key: SPECIAL_TYPE
    21. restartPolicy: Never

    The output after running the pod is as follows:

    1. very charm

Use a config map in data volumes

  1. Log on to the Container Service console.

  2. Click Kubernetes > Application > Deployment in the left-side navigation pane.

  3. Click Create by template in the upper-right corner.

  4. Select the cluster and namespace from the Clusters and Namespace drop-down lists.

  5. Select a sample template or Custom from the Resource Type drop-down list.

  6. Click DEPLOY.

    You can also use a config map in data volumes. Specifying the config map name under volumes stores the key-value pair data to the mountPath directory (/etc/config in this example). Then, the configuration file with key as the name and value as the contents is generated.

    See the following orchestration example:

    1. apiVersion: v1
    2. kind: Pod
    3. metadata:
    4. name: config-pod-4
    5. spec:
    6. containers:
    7. - name: test-container
    8. image: busybox
    9. command: [ "/bin/sh", "-c", "ls /etc/config/" ] ##List the file names under this directory.
    10. volumeMounts:
    11. - name: config-volume
    12. mountPath: /etc/config
    13. volumes:
    14. - name: config-volume
    15. configMap:
    16. name: special-config
    17. restartPolicy: Never

    Keys of the config map are output after running the pod.

    1. SPECIAL_TYPE
    2. SPECIAL_LEVEL
Thank you! We've received your feedback.