Updated on 2024-09-30 GMT+08:00

Using a ConfigMap

You can use a ConfigMap to set environment variables or command line parameters, or mount a ConfigMap as a data volume.

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 is used, it must be in the same cluster and namespace as the workload.
  • When a ConfigMap mounted as a data volume is updated, Kubernetes updates the data in the data volume at the same time.

    If a ConfigMap is mounted as a data volume using subPath, data in the data volume cannot be automatically updated when the ConfigMap is updated.

  • When a ConfigMap is used as an environment variable, data in the environment variable cannot be automatically updated when the ConfigMap is updated. To update the data, restart the pod.

Configuring Environment Variables for a Workload

Using the console

  1. Log in to the CCE console and click the cluster name to access the cluster console.
  2. In the navigation pane, choose Workloads. In the upper right corner of the displayed page, click Create Workload.

    When creating a workload, click Environment Variables in the Container Settings area, and click Add Variable.

    • Added from ConfigMap: Select a ConfigMap to import all of its keys as environment variables.

    • Added from ConfigMap key: Import a key in a ConfigMap as the value of an environment variable.
      • Variable Name: Name of the environment variable. The name can be customized and is set to the key name selected in the ConfigMap by default.
      • Variable Value/Reference: Select a ConfigMap and the key to be imported. The corresponding value is imported as a workload environment variable.

      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.

  3. Configure other workload parameters and click Create Workload.

    When the workload runs normally, log in to the container and run the following statement to check whether the ConfigMap has been set as an environment variable for the workload:

    printenv SPECIAL_LEVEL

    The following is an example output:

    Hello

Using kubectl

  1. Use kubectl to connect to the cluster. For details, see Connecting to a Cluster Using kubectl.
  2. Create a file named nginx-configmap.yaml and edit it.

    vi nginx-configmap.yaml

    The content of the YAML file is as follows:

    • Added from ConfigMap: To add all data in a ConfigMap to environment variables, use the envFrom parameter. The keys in the ConfigMap will become names of environment variables in the workload.
      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
    • Added from ConfigMap key: When creating a workload, you can use a ConfigMap to set environment variables and use the valueFrom parameter to reference the key-value pair in the ConfigMap separately.
      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 an environment variable for the workload.
              - name: SPECIAL_LEVEL           # Name of the environment variable for the workload.
                valueFrom:                    # Specify a ConfigMap to be referenced by the environment variable.
                  configMapKeyRef:
                    name: cce-configmap       # Name of the referenced ConfigMap.
                    key: SPECIAL_LEVEL        # Key in the referenced ConfigMap.
              - name: SPECIAL_TYPE            # Add multiple environment variables to import them at the same time.
                valueFrom:
                  configMapKeyRef:
                    name: cce-configmap
                    key: SPECIAL_TYPE
            imagePullSecrets:
            - name: default-secret

  3. Create a workload.

    kubectl apply -f nginx-configmap.yaml

  4. View the environment variables in the pod.

    1. Run the following command to view the created pod:
      kubectl get pod | grep nginx-configmap
      Expected output:
      nginx-configmap-***   1/1     Running   0              2m18s
    2. Run the following command to view the environment variables in the pod:
      kubectl exec nginx-configmap-*** -- printenv SPECIAL_LEVEL SPECIAL_TYPE

      Expected output:

      Hello
      CCE

      The ConfigMap has been set as environment variables of the workload.

Configuring Command Line Parameters

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

  1. Log in to the CCE console and click the cluster name to access the cluster console.
  2. In the navigation pane, choose Workloads. In the upper right corner of the displayed page, click Create Workload.

    When creating a workload, click Environment Variables in the Container Settings area, and click Add Variable. In this example, select Added from ConfigMap.

    • Added from ConfigMap: Select a ConfigMap to import all of its keys as environment variables.

  3. Click Lifecycle in the Container Settings area, click the Post-Start tab on the right, and configure the following parameters:

    • Processing Method: CLI
    • Command: Enter the following three command lines. SPECIAL_LEVEL and SPECIAL_TYPE are the environment variable names in the workload, that is, the key names in the cce-configmap ConfigMap.
      /bin/bash
      -c
      echo $SPECIAL_LEVEL $SPECIAL_TYPE > /usr/share/nginx/html/index.html

  4. Configure other workload parameters and click Create Workload.

    When the workload runs normally, log in to the container and run the following statement to check whether the ConfigMap has been set as an environment variable for the workload:

    cat /usr/share/nginx/html/index.html

    The following is an example output:

    Hello CCE

Using kubectl

  1. Use kubectl to connect to the cluster. For details, see Connecting to a Cluster Using kubectl.
  2. Create a file named nginx-configmap.yaml and edit it.

    vi nginx-configmap.yaml

    In this example, the cce-configmap ConfigMap is imported to the workload. SPECIAL_LEVEL and SPECIAL_TYPE are the environment variable names in the workload, that is, the key names in the cce-configmap ConfigMap.
    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

  3. Create a workload.

    kubectl apply -f nginx-configmap.yaml

  4. Wait until the workload runs normally. The following content will be added to the /usr/share/nginx/html/index.html file of the container:

    1. Run the following command to view the created pod:
      kubectl get pod | grep nginx-configmap
      Expected output:
      nginx-configmap-***   1/1     Running   0              2m18s
    2. Run the following command to view the environment variables in the pod:
      kubectl exec nginx-configmap-*** -- cat /usr/share/nginx/html/index.html

      Expected output:

      Hello CCE

Mounting a ConfigMap Volume to a Workload

A ConfigMap can be mounted as a volume to a specified container path. The workload code is separated from the configuration files. ConfigMap volumes are used to store workload configuration parameters. You need to create ConfigMaps in advance by following the instructions in Creating a ConfigMap.

Using the console

  1. Log in to the CCE console and click the cluster name to access the cluster console.
  2. In the navigation pane, choose Workloads. In the upper right corner of the displayed page, click Create Workload.

    When creating a workload, click Data Storage in the Container Settings area. Click Add Volume and select ConfigMap from the drop-down list.

  3. Configure the parameters for mounting a ConfigMap volume based on Table 1.

    Table 1 Parameters for mounting a ConfigMap volume

    Parameter

    Description

    ConfigMap

    Select the desired ConfigMap.

    A ConfigMap must be created beforehand. For details, see Creating a ConfigMap.

    Mount Path

    Enter a mount point. 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 mount path of the container.

    This parameter indicates the container path that the volume will be mounted to. Do not mount the volume to a system directory such as / or /var/run. This may cause container errors. Mount the volume to an empty directory. If the directory is not empty, ensure that there are no files that affect container startup, or the files will be replaced, which will lead to a container startup or workload creation failure.
    NOTICE:

    If a volume is mounted to a high-risk directory, use an account with minimum permissions to start the container, or high-risk files on the host may be damaged.

    Subpath

    Enter a subpath of the mount path.
    • A subpath is used to mount a local volume so that the same data volume is used in a single pod. If this parameter is left blank, the root path is used by default.
    • The subpath can be the key and value of a ConfigMap or secret. If the subpath is a key-value pair that does not exist, the data import does not take effect.
    • The data imported using a subpath will not be updated along with the ConfigMap.

    Permission

    Read-only permission. The data volume in the path is read-only.

    Figure 1 Mounting a ConfigMap volume

  4. Click Create Workload.

    When the workload runs normally, the SPECIAL_LEVEL and SPECIAL_TYPE files will be generated in the /etc/config directory in this example. The content of file SPECIAL_LEVEL is Hello, and that of SPECIAL_TYPE is CCE.

    Access the container and run the following statement to view the SPECIAL_LEVEL or SPECIAL_TYPE file in the container:

    cat /etc/config/SPECIAL_LEVEL

    Expected output:

    Hello

Using kubectl

  1. Use kubectl to connect to the cluster. For details, see Connecting to a Cluster Using kubectl.
  2. Create a file named nginx-configmap.yaml and edit it.

    vi nginx-configmap.yaml

    In this 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 the volume to the /etc/config directory.
              readOnly: true
        volumes:
        - name: config-volume
          configMap:
            name: cce-configmap                 # Name of the referenced ConfigMap.

  3. Create a workload.

    kubectl apply -f nginx-configmap.yaml

  4. When the workload runs normally, the SPECIAL_LEVEL and SPECIAL_TYPE files will be generated in the /etc/config directory in this example. The content of file SPECIAL_LEVEL is Hello, and that of SPECIAL_TYPE is CCE.

    1. Run the following command to view the created pod:
      kubectl get pod | grep nginx-configmap
      Expected output:
      nginx-configmap-***   1/1     Running   0              2m18s
    2. Run the following command to view the SPECIAL_LEVEL or SPECIAL_TYPE file in the pod:
      kubectl exec nginx-configmap-*** -- cat /etc/config/SPECIAL_LEVEL

      Expected output:

      Hello