更新时间:2026-04-11 GMT+08:00
分享

配置Sidecar容器

应用场景

Sidecar容器是一种在同一个Pod中与主业务容器协同工作的辅助容器。它通常用于以无侵入的方式扩展主应用的功能。常见的应用场景例如Sidecar容器读取主容器的日志文件并发送到日志服务(如 LTS)。

功能说明

CCI支持基于Kubernetes原生特性的Sidecar容器(即SidecarContainers特性)。传统的Init Container(初始化容器)会在主容器启动前运行并退出。而通过在Init Container中配置重启策略为Always,该容器将转变为Sidecar容器。

配置示例

您可以在创建无状态负载 (Deployment) 或容器组 (Pod) 的YAML文件中进行配置。以下示例创建了一个Deployment,其中包含一个主业务容器container-1和一个Sidecar容器container2。container2被定义在initContainers列表中,但通过配置restartPolicy: Always,它将作为Sidecar容器持续运行。
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:
      # 配置Sidecar容器
      initContainers:
        - name: container2
          image: nginx:latest   # 请替换为您实际的镜像地址
          restartPolicy: Always # 在initcontainer中配置restartPolicy为Always则表示开启initSidecarContainer
      # 配置主业务容器
      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
表1 关键参数说明

参数位置

参数名称

取值示例

说明

spec.template.spec.initContainers

restartPolicy

Always

必填。该字段用于定义容器的重启策略。

当在initContainers列表中将此字段设置为Always 时,CCI会将该容器识别为Sidecar容器。该容器将在主容器启动前启动,并持续运行,不会像普通Init容器那样运行完成后退出。

相关文档