Configuration item

更新时间:
复制 MD 格式

A ConfigMap is a resource type that stores configuration data for workloads. You must define the content of the ConfigMap.

Create a ConfigMap

This section describes how to create a ConfigMap in the Container Application Service console.

Procedure

  1. Log on to the Container Application Service console. In the navigation pane on the left, click Configuration > ConfigMaps.

  2. Click Create. On the Create ConfigMap page, set the following parameters:

    • Namespace: Select a namespace. If you do not select a namespace, the system uses default.

    • Name: Enter a name for the ConfigMap. The name must be unique within the namespace and can be up to 32 characters long.

    • Configuration Source:

      • Key-value pair: The key represents the file name, and the value represents the file content.

      • File: Upload a file to create the ConfigMap.

  3. After you complete the configuration, click Create.

Use a ConfigMap

You can use a ConfigMap in a pod. The main scenarios are as follows:

For more information about ConfigMaps, see Configure a Pod to Use a ConfigMap.

Define pod environment variables with a ConfigMap

You can use a ConfigMap to define environment variables in a pod using valueFrom to reference the ConfigMap data.

  1. Log on to the Container Application Service console. In the navigation pane on the left, click Workloads > Pods.

  2. Click Create from YAML. Enter the following content in the editor and click Create.

    The following is an example of orchestration.

    apiVersion: v1
    kind: Pod
    metadata:
       name: config-pod-1
       namespace: yournamespace  # Replace with your namespace name
    spec:
       containers:
       - name: test-container
         image: busybox
         command: [ "/bin/sh", "-c", "env" ]
         env:
         - name: SPECIAL_LEVEL_KEY
           valueFrom:                             # Use valueFrom to specify that the env references the value of the ConfigMap
           configMapKeyRef:
             name: special-config               # The name of the referenced ConfigMap
             key: SPECIAL_LEVEL                 # The key of the referenced configuration item
       restartPolicy: Never

Set command-line arguments with a ConfigMap

You can use a ConfigMap to set command or argument values in a container. To do this, use the environment variable substitution syntax $(VAR_NAME).

  1. Log on to the Container Application Service console. In the navigation pane on the left, click Workloads > Pods.

  2. Click Create from YAML. Enter the following content in the editor and click Create.

    The following is an orchestration example.

    apiVersion: v1
    kind: Pod
    metadata:
       name: config-pod-3
       namespace: yournamespace  # Replace with your namespace name
    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 the pod runs, the following output is returned.

    very charm

Use a ConfigMap in a volume

You can also use a ConfigMap in a volume. To do this, specify the ConfigMap name in the `volumes` section. Each key-value pair in the ConfigMap data becomes a file in the directory specified by `mountPath`. The key becomes the file name and the value becomes the file content. In this example, the path is /etc/config.

  1. Log on to the Container Application Service console. In the navigation pane on the left, click Workloads > Pods.

  2. Click Create from YAML. Enter the following content in the editor and click Create.

    The following is an example of an orchestration.

    apiVersion: v1  
    kind: Pod  
    metadata:  
       name: config-pod-4  
       namespace: yournamespace  # Replace with your namespace name  
    spec:  
       containers:  
       - name: test-container  
         image: busybox  
         command: [ "/bin/sh", "-c", "ls /etc/config/" ]   # List the file names in this directory  
         volumeMounts:  
         - name: config-volume  
         mountPath: /etc/config  
       volumes:  
       - name: config-volume  
         configMap:  
         name: special-config  
       restartPolicy: Never  

    After the pod runs, the output lists the keys from the ConfigMap.

    SPECIAL_TYPE SPECIAL_LEVEL

Modify a ConfigMap

Changes to a ConfigMap can affect the applications that use it.

Procedure

  1. Log on to the Container Application Service console. In the navigation pane on the left, click Configuration > ConfigMaps.

  2. On the configuration item list page, click an item's Name to open its details page.

  3. You can make the following changes:

    • Click Edit, modify the existing ConfigMap data, and click Submit.

    • Click Add Configuration Item Data, enter a new variable name and value, and click Submit.

View configuration items

Prerequisites

A ConfigMap has been created.

Procedure

  1. Log on to the Container Application Service console. In the navigation pane on the left, click Configuration > ConfigMaps.

  2. On the configuration item list page, click the Name of a configuration item to view its details.