A ConfigMap is an API object that stores non-confidential data in key-value pairs. It decouples configuration files from container images to enhance the portability of containerized applications. A ConfigMap contains key-value pairs. You can create a ConfigMap in the KCE console and use it when you mount a volume, define an environment variable, or run a command.
Operations in the KCE console
Create a ConfigMap
- Log in to the KCE console.
- In the left navigation pane, click Cluster.
- Click the ID of the cluster in which you want to create a ConfigMap. The cluster details page appears.
- In the left navigation pane, choose Configuration Management > ConfigMap. The ConfigMap list page appears.
- Click Create. On the Create ConfigMap page, complete the following configurations:
- Name: the name of the ConfigMap to be created.
- Namespace: the namespace of the cluster to which the ConfigMap belongs.
- Configuration Item: the variables of the ConfigMap. You can define the variable names and variable values as required.
- Click Create.
Use a ConfigMap
Method 1: Mount a volume of the ConfigMap type
- Log in to the KCE console.
- In the left navigation pane, click Cluster.
- Click the ID of the cluster in which you want to use a ConfigMap. The cluster details page appears.
- In the left navigation pane, click Workload and select any type of workload. The corresponding list page appears. For example, in the left navigation pane, choose Workload > Deployment to go to the Deployment list page.
- Click Create. The Create Deployment page appears.
- Set basic information and proceed to Deployment Configuration. In the Deployment Configuration step, click Add Volume, select ConfigMap for Type, and then enter the volume name, as shown in the following figure.
- Click Select ConfigMap. In the Set ConfigMap dialog box, complete the following configurations:
- Select ConfigMap: Select a ConfigMap as required.
- Mount Options: Select Mount All or Mount with Specified Key as required.
Note: When you select Mount with Specified Key, you can mount the ConfigMap to a specific path by setting Items. For example, if the mount path is /etc/config
and the subpath is dev
, data will be stored in /etc/config/dev
.
- After all configurations are completed, click Create.
Method 2: Define an environment variable for a container
- Log in to the KCE console.
- In the left navigation pane, click Cluster.
- Click the ID of the cluster in which you want to use a ConfigMap. The cluster details page appears.
- In the left navigation pane, click Workload and select any type of workload. The corresponding list page appears. For example, in the left navigation pane, choose Workload > Deployment to go to the Deployment list page.
- Click Create. The Create Deployment page appears. Set basic information and proceed to Deployment Configuration. In the Container configuration section, click Add for Environment Variable, as shown in the following figure.
-
Select Reference ConfigMap for Add Method, enter the variable name, and then select a variable value or a variable reference.
- After all configurations are completed, click Create.
Update a ConfigMap
- Log in to the KCE console.
- In the left navigation pane, click Cluster.
- Click the ID of the cluster in which you want to update a ConfigMap. The cluster details page appears.
- In the left navigation pane, choose Configuration Management > ConfigMap. The ConfigMap list page appears.
- Find the ConfigMap that you want to update and click Edit YAML in the Operation column.
- On the UpdateConfigMap page, modify the configurations as required and click Update.
Delete a ConfigMap
- Log in to the KCE console.
- In the left navigation pane, click Cluster.
- Click the ID of the cluster in which you want to delete a ConfigMap. The cluster details page appears.
- In the left navigation pane, choose Configuration Management > ConfigMap. The ConfigMap list page appears.
- Find the ConfigMap that you want to delete and click Delete in the Operation column.
- In the message that appears, click OK.
Operations by using kubectl
Create a ConfigMap
Method 1: Use a YAML file
Sample configmap-test.yaml
file:
apiVersion: v1
kind: ConfigMap
metadata:
name: config-test
namespace: default
data:
SPECIAL_LEVEL: very
SPECIAL_TYPE: charm
data: the data of the ConfigMap, which is presented as key-value pairs.
Create the configmap-test.yaml
file.
# kubectl apply -f configmap-test.yaml
Method 2: Run the kubectl create configmap
command
# kubectl create configmap <map-name> <data-source>
# <map-name>: the name of the ConfigMap.
# <data-source>: the data source for creating the ConfigMap, which can be a directory, a file, or left unspecified.
Use a ConfigMap
Method 1: Mount a volume of the ConfigMap type
Sample configmap-volume.yaml
file:
apiVersion: v1
kind: Pod
metadata:
name: pod1-test
spec:
containers:
- name: container-test
image: ksyun/nginx:latest
volumeMounts: # The mount path of the volume.
- name: config-volume
mountPath: /etc/config
volumes: # The pod-specific volumes, which are mounted to containers in the pod.
- name: config-volume
configMap:
name: config-test # The name of the ConfigMap mounted.
restartPolicy: Never
Method 2: Define an environment variable for a container
Sample configmap-env.yaml
file:
apiVersion: v1
kind: Pod
metadata:
name: pod2-test
spec:
containers:
- name: container-test
image: ksyun/nginx:latest
env:
- name: SPECIAL_LEVEL_KEY
valueFrom:
configMapKeyRef:
name: config-test # The name of the ConfigMap mounted.
key: SPECIAL_LEVEL
restartPolicy: Never