Use a ConfigMap

更新时间:
复制 MD 格式

You can use a ConfigMap in a pod. Common scenarios include the following:

Note

When you use a ConfigMap in a pod, the ConfigMap and the pod must be in the same namespace.

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

Define pod environment variables using a ConfigMap

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

  1. Log on to the console. In the navigation pane on the left, click Cluster Management > Clusters.

  2. On the cluster list page, click the name of the cluster to open the cluster details page.

  3. In the navigation pane on the left, click Workloads > Pods.

  4. Click Create from YAML. Enter the following content in the text box and click Create. The following code provides an orchestration example.

    apiVersion: v1
    kind:Pod
    metadata:
      name: config-pod-1
      namespace: yournamespace  # Replace with your current namespace name
    spec:
      containers:
      - name: test-container
        image: busybox
        command: ["/bin/sh", "-c", "env"]
        env:
          - name: SPECIAL_LEVEL_KEY
            valueFrom: # Use valueFrom to reference the value of the ConfigMap in the environment variable
              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 using a ConfigMap

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

  1. Log on to the console. In the navigation pane on the left, click Cluster Management > Clusters.

  2. On the cluster list page, click the name of the cluster to open the cluster details page.

  3. In the navigation pane on the left, click Workloads > Pods.

  4. Click Create from YAML. Enter the following content in the text box and click Create. The following code provides an orchestration example.

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

    very charm

Use a ConfigMap in a volume

You can also use a ConfigMap in a volume. Specify the ConfigMap name in the volumes section of the pod definition. The key-value data is stored in the path specified by mountPath. In this example, the path is /etc/config. For each key, a configuration file is created. The filename is the key, and the file content is the value.

  1. Log on to the console. In the navigation pane on the left, click Cluster Management > Clusters.

  2. On the cluster list page, click the name of the cluster to open the cluster details page.

  3. In the navigation pane on the left, click Workloads > Pods.

  4. Click Create from YAML. Enter the following content in the text box and click Create. The following code provides an orchestration example.

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

    After the pod runs, the command returns the keys from the ConfigMap.

    SPECIAL_TYPE SPECIAL_LEVEL