This topic describes how to configure a pod to use a ConfigMap.
Background information
You can configure a pod to use a ConfigMap in the following scenarios:
Use a ConfigMap to define 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 the pod are in the same cluster and namespace.
Create a ConfigMap
In this example, a ConfigMap named special_config is created. This ConfigMap consists of two key-value pairs: SPECIAL_LEVEL: very
and SPECIAL_TYPE: charm
.
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
- Log on to the ACK console.
- In the left-side navigation pane of the ACK console, click Clusters.
- On the Clusters page, find the cluster that you want to manage and click the name of the cluster or click Details in the Actions column. The details page of the cluster appears.
- In the left-side navigation pane of the details page, choose .
On the Deployments page, click Create from YAML in the upper-right corner.
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
field to reference the value of SPECIAL_LEVEL.The following code block is 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 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
Use all key-value pairs of a ConfigMap to define multiple environment variables for a pod
- Log on to the ACK console.
- In the left-side navigation pane of the ACK console, click Clusters.
- On the Clusters page, find the cluster that you want to manage and click the name of the cluster or click Details in the Actions column. The details page of the cluster appears.
- In the left-side navigation pane of the details page, choose .
On the Deployments page, click Create from YAML in the upper-right corner.
Select the sample template or enter a custom template, and click Create.
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.
The following code block is an example:
apiVersion: v1 kind: Pod metadata: name: config-pod-2 spec: containers: - name: test-container image: busybox command: [ "/bin/sh", "-c", "env" ] envFrom: ##All key-value pairs of the special-config ConfigMap are referenced. - configMapRef: name: special-config restartPolicy: Never
Use a ConfigMap to set command line parameters
- Log on to the ACK console.
- In the left-side navigation pane of the ACK console, click Clusters.
- On the Clusters page, find the cluster that you want to manage and click the name of the cluster or click Details in the Actions column. The details page of the cluster appears.
- In the left-side navigation pane of the details page, choose .
On the Deployments page, click Create from YAML in the upper-right corner.
Select the sample template or enter a custom template, and click Create.
You can use ConfigMaps to define the commands or parameter values for a container by using the environment variable replacement syntax
$(VAR_NAME)
.The following code block is 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.
- In the left-side navigation pane of the ACK console, click Clusters.
- On the Clusters page, find the cluster that you want to manage and click the name of the cluster or click Details in the Actions column. The details page of the cluster appears.
- In the left-side navigation pane of the details page, choose .
On the Deployments page, click Create from YAML in the upper-right corner.
Select a sample template or customize a template, and click Create.
To mount a ConfigMap as a volume, specify the name of the ConfigMap 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 code block is 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 files in 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 following output is returned:
SPECIAL_TYPE SPECIAL_LEVEL