Help Center/ Cloud Container Instance (CCI)/ User Guide/ Using CCI with CCE/ Using a Sidecar Container/ Replicating Environment Variables and Volume Mount Points of a Service Container
Updated on 2025-08-12 GMT+08:00

Replicating Environment Variables and Volume Mount Points of a Service Container

Scenarios

In scenarios such as container log collection and processing, configuration management, and data synchronization, you can transfer the configuration file path through environment variables or mount the volume of the main service container to the sidecar container through the shared volume mount point to implement specific functions. CCI allows you to replicate the environment variables (env) and volume mount points (volumeMounts) of a service container to implement specific functions. This simplifies the operations.

You can replicate environment variables and volume mount points of a service container in the following ways:

Prerequisites

If you use a ClusterExtensionProfile or ExtensionProfile to replicate environment variables and volume mount points of a service container, you need to configure the sidecar container first. For details, see Configuring the Sidecar Container.

Functions

Table 1 Functions

Function

Description

Replicate environment variables of the service container when the sidecar container is injected.

  • Replicate environment variables by container name and environment variable name.
  • Replicate environment variables by container name and environment variable name.

Replicate the volume mount points of the service container when the sidecar container is injected.

  • Replicate volume mount points by container name and volumeMountName.
  • Replicate volume mount points by container name and volumeMountName.

Constraints

Table 2 Constraints

Field

Constraints

envFromContainer

  • name in envFromContainer cannot be empty.
  • name in envFromContainer cannot be duplicate or the same as any env name in the env configuration of the sidecar container.
  • Either name or index in containerFieldRef must be configured.
  • The value of fieldPath in containerFieldRef must be name or start with env..

volumeMountFromContainer

  • volumeMountName in volumeMountFromContainer cannot be empty.
  • volumeMountName in volumeMountFromContainer cannot be duplicate or the same as any name in the volumeMounts configuration of the sidecar container.
  • Either name or index in containerRef must be configured.

Method 1: Using Annotations

Add the envFromContainer and volumeMountFromContainer annotations to copy the environment variables and volume mount points of the service container.

bursting.cci.io/pod-sidecars: '{"containers":[{"name":"busybox","image":"busybox:latest","command":["sh","-c","sleep 3600"],"resources":{},"envFromContainer":[{"name":"POD_IP","containerFieldRef":{"index":0,"fieldPath":"env.POD_IP"}},{"name":"POD_NAME","containerFieldRef":{"name":"nginx","fieldPath":"env.POD_NAME"}},{"name":"CONTAINER_NAME","containerFieldRef":{"index":0,"fieldPath":"name"}}],"volumeMountFromContainer":[{"volumeMountName":"log-storage","containerRef":{"index":0}},{"volumeMountName":"log-storage1","containerRef":{"name":"nginx"}}]}]}'

Method 2: Using a ClusterExtensionProfile

  1. Generate a YAML file. Use a ClusterExtensionProfile to configure the environment variables and volume mount points of the sidecar container.

    The following is an example YAML file:

    apiVersion: bursting.cci.io/v1
    kind: ClusterExtensionProfile
    metadata:  
      name: podsidecar-cluster-profile
    spec:  
      actions:    
        podSidecars:
          containers:
          - command:
            - sh
            - -c
            - sleep 3600
            image: busybox:latest
            name: busybox
            envFromContainer:
            - containerFieldRef:
                fieldPath: env.POD_IP
                index: 0
              name: POD_IP
            - containerFieldRef:
                fieldPath: env.POD_NAME
                name: nginx
              name: POD_NAME
            - containerFieldRef:
                fieldPath: name
                index: 0
              name: CONTAINER_NAME
            volumeMountFromContainer:
            - containerRef:
                index: 0
              volumeMountName: log-storage
            - containerRef:
                name: nginx
              volumeMountName: log-storage1
      namespaceLabels:    
        matchLabels:      
            key: value

  2. Create the object.

    kubectl apply -f <podsidecar-cluster-profile>     #YAML file name (Replace it with the actual file name.)

  3. (Optional) Verify that the object has been created.

    kubectl get cextp

Method 3: Using an ExtensionProfile

  1. Generate a YAML file. Use an ExtensionProfile to configure the environment variables and volume mount points of the sidecar container.

    The following is an example YAML file:

    apiVersion: bursting.cci.io/v1
    kind: ExtensionProfile
    metadata:  
      name: podsidecar-profile
    spec:  
      actions:    
        podSidecars:
          containers:
          - command:
            - sh
            - -c
            - sleep 3600
            image: busybox:latest
            name: busybox
            envFromContainer:
            - containerFieldRef:
                fieldPath: env.POD_IP
                index: 0
              name: POD_IP
            - containerFieldRef:
                fieldPath: env.POD_NAME
                name: nginx
              name: POD_NAME
            - containerFieldRef:
                fieldPath: name
                index: 0
              name: CONTAINER_NAME
            volumeMountFromContainer:
            - containerRef:
                index: 0
              volumeMountName: log-storage
            - containerRef:
                name: nginx
              volumeMountName: log-storage1
      objectLabels:
        matchLabels:
          key1: value1

  2. Create the object.

    kubectl apply -f <podsidecar-profile>     #YAML file name (Replace it with the actual file name.)

  3. (Optional) Verify that the object has been created.

    kubectl get extp

Parameter Description

Table 3 Key parameters

Parameter

Description

InitContainers

The list of init containers injected on CCI through pod.spec.initContainers. The structure of initContainer is similar to that of initContainer for the pod. restartPolicy cannot be configured. position, envFromContainer, and volumeMountFromContainer are added. For details, see Table 4.

Containers

The list of containers injected on CCI through pod.spec.containers. For details, see Table 4.

Volumes

The list of volumes injected on CCI through pod.spec.volumes.

Table 4 Container parameters

Parameter

Mandatory

Value Type

Description

position

No

String

You can configure the position field for a container to specify where the injection is to be performed. If position is set to head, the container is injected at the beginning. If position is set to tail, the container is injected at the end. By default, the container is injected at the end. Containers with the same position settings are injected in the sequence specified by actions.podSidecars.