Updated on 2026-04-28 GMT+08:00

Configuring a Sidecar Container

Scenarios

A sidecar container is a secondary container that runs along with the main service container in the same pod. Sidecar containers are usually used to extend the functionality of the main application in a non-intrusive manner. A common scenario is that a sidecar container reads log files of the main service container and sends the log files to a log service, such as LTS.

Function Description

CCI supports sidecar containers based on the Kubernetes sidecar container feature. A traditional init container runs and exits before the main service container starts. If an init container is created with its restartPolicy set to Always, this container becomes a sidecar container.

Configuration Example

You can configure sidecar containers in the YAML file for creating a Deployment or pod. In the following example, the Deployment contains a main service container named container-1 and a sidecar container named container2. container2 is defined in initContainers, but it runs as a sidecar container because its restartPolicy is set to Always.
kind: Deployment
apiVersion: cci/v2
metadata:
  name: test
spec:
  replicas: 1
  selector:
    matchLabels:
      app: test
  template:
    metadata:
      labels:
        app: test
      annotations:
        resource.cci.io/pod-size-specs: 0.50_1.0
    spec:
      # Configure the sidecar container.
      initContainers:
        - name: container2
          image: nginx:latest   # Replace it with the actual image path.
          restartPolicy: Always # If restartPolicy is set to Always in initContainers, the init container becomes a sidecar container.
      # Configure the main service container.
      containers:
        - name: container-1
          image: nginx:latest
          command:
            - /bin/sh
            - '-c'
            - while true ;do echo hello ;sleep 3;  done
          resources:
            limits:
              cpu: 250m
              memory: 512Mi
            requests:
              cpu: 250m
              memory: 512Mi
      imagePullSecrets:
        - name: imagepull-secret
      schedulerName: default-scheduler
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxUnavailable: 0
      maxSurge: 100%
  progressDeadlineSeconds: 600
Table 1 Key parameter

Parameter Location

Parameter

Example Value

Description

spec.template.spec.initContainers

restartPolicy

Always

(Mandatory) Restart policy of a container.

If this parameter is set to Always in initContainers, CCI identifies the container as a sidecar container. Unlike a common init container, this sidecar container starts before the main service container and remains running. It does not exit after running.