The following example shows how to use a ConfigMap.
apiVersion: v1 kind: ConfigMap metadata: name: cce-configmap data: SPECIAL_LEVEL: Hello SPECIAL_TYPE: CCE
When a ConfigMap data volume mounted in subPath mode is updated, Kubernetes cannot automatically update the data in the data volume.
Using the console
When creating a workload, click Environment Variables in the Container Settings area, and click .
For example, after you import the value Hello of SPECIAL_LEVEL in ConfigMap cce-configmap as the value of workload environment variable SPECIAL_LEVEL, an environment variable named SPECIAL_LEVEL with its value Hello exists in the container.
After the workload runs properly, access the container and run the following command to check whether the ConfigMap has been set as an environment variable of the workload:
printenv SPECIAL_LEVEL
The example output is as follows:
Hello
Using kubectl
vi nginx-configmap.yaml
Content of the YAML file:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-configmap
spec:
replicas: 1
selector:
matchLabels:
app: nginx-configmap
template:
metadata:
labels:
app: nginx-configmap
spec:
containers:
- name: container-1
image: nginx:latest
envFrom: # Use envFrom to specify a ConfigMap to be referenced by environment variables.
- configMapRef:
name: cce-configmap # Name of the referenced ConfigMap.
imagePullSecrets:
- name: default-secret
apiVersion: apps/v1 kind: Deployment metadata: name: nginx-configmap spec: replicas: 1 selector: matchLabels: app: nginx-configmap template: metadata: labels: app: nginx-configmap spec: containers: - name: container-1 image: nginx:latest env: # Set environment variables in the workload. - name: SPECIAL_LEVEL # Name of the environment variable in the workload. valueFrom: # Use valueFrom to specify an environment variable to reference a ConfigMap. configMapKeyRef: name: cce-configmap # Name of the referenced ConfigMap. key: SPECIAL_LEVEL # Key in the referenced ConfigMap. - name: SPECIAL_TYPE # Add multiple environment variables. Multiple environment variables can be imported at the same time. valueFrom: configMapKeyRef: name: cce-configmap key: SPECIAL_TYPE imagePullSecrets: - name: default-secret
kubectl apply -f nginx-configmap.yaml
kubectl get pod | grep nginx-configmap
nginx-configmap-*** 1/1 Running 0 2m18s
kubectl exec nginx-configmap-*** -- printenv SPECIAL_LEVEL SPECIAL_TYPE
Expected output:
Hello CCE
The ConfigMap has been set as an environment variable of the workload.
You can use a ConfigMap as an environment variable to set commands or parameter values for a container by using the environment variable substitution syntax $VAR_NAME.
Using the console
When creating a workload, click Environment Variables in the Container Settings area, and click . In this example, select Added from ConfigMap.
/bin/bash -c echo $SPECIAL_LEVEL $SPECIAL_TYPE > /usr/share/nginx/html/index.html
After the workload runs properly, access the container and run the following command to check whether the ConfigMap has been set as an environment variable of the workload:
cat /usr/share/nginx/html/index.html
The example output is as follows:
Hello CCE
Using kubectl
vi nginx-configmap.yaml
apiVersion: apps/v1 kind: Deployment metadata: name: nginx-configmap spec: replicas: 1 selector: matchLabels: app: nginx-configmap template: metadata: labels: app: nginx-configmap spec: containers: - name: container-1 image: nginx:latest lifecycle: postStart: exec: command: [ "/bin/sh", "-c", "echo $SPECIAL_LEVEL $SPECIAL_TYPE > /usr/share/nginx/html/index.html" ] envFrom: # Use envFrom to specify a ConfigMap to be referenced by environment variables. - configMapRef: name: cce-configmap # Name of the referenced ConfigMap. imagePullSecrets: - name: default-secret
kubectl apply -f nginx-configmap.yaml
kubectl get pod | grep nginx-configmap
nginx-configmap-*** 1/1 Running 0 2m18s
kubectl exec nginx-configmap-*** -- cat /usr/share/nginx/html/index.html
Expected output:
Hello CCE
The data stored in a ConfigMap can be referenced in a volume of type ConfigMap. You can mount such a volume to a specified container path. The platform supports the separation of workload codes and configuration files. ConfigMap volumes are used to store workload configuration parameters. Before that, you need to create ConfigMaps in advance. For details, see Creating a ConfigMap.
Using the console
When creating a workload, click Data Storage in the Container Settings area. Click Add Volume and select ConfigMap from the drop-down list.
Parameter |
Description |
---|---|
Option |
Select the desired ConfigMap name. A ConfigMap must be created in advance. For details, see Creating a ConfigMap. |
Add Container Path |
Configure the following parameters:
You can click |
Using kubectl
vi nginx-configmap.yaml
As shown in the following example, after the ConfigMap volume is mounted, a configuration file with the key as the file name and value as the file content is generated in the /etc/config directory of the container.
apiVersion: apps/v1 kind: Deployment metadata: name: nginx-configmap spec: replicas: 1 selector: matchLabels: app: nginx-configmap template: metadata: labels: app: nginx-configmap spec: containers: - name: container-1 image: nginx:latest volumeMounts: - name: config-volume mountPath: /etc/config # Mount to the /etc/config directory. readOnly: true volumes: - name: config-volume configMap: name: cce-configmap # Name of the referenced ConfigMap.
kubectl apply -f nginx-configmap.yaml
kubectl get pod | grep nginx-configmap
nginx-configmap-*** 1/1 Running 0 2m18s
kubectl exec nginx-configmap-*** -- /etc/config/SPECIAL_LEVEL
Expected output:
Hello