Help Center/ Cloud Container Instance (CCI)/ User Guide/ Using CCI with CCE/ Logging/ Collecting Logs Using a Sidecar Container
Updated on 2025-08-12 GMT+08:00

Collecting Logs Using a Sidecar Container

You can use hostpath-replacement to replace hostPath with emptyDir supported by CCI, inject a sidecar container to collect logs through pod-sidecars, and mount the standard output logs to the sidecar container through mount-stdlog-containers.

Prerequisites

You have configured the sidecar container by referring to Configuring a Sidecar Container.

Constraints

volume.cci.io/mount-stdlog-containers and volume.cci.io/mount-stdlog-containers-path cannot be configured at the same time.

Using a YAML File

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    version: v1
    bursting.cci.io/burst-to-cci: enforce
  name: deploy-example
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      app: deploy-example
      version: v1
  template:
    metadata:
      annotations:
        bursting.cci.io/hostpath-replacement: '[{"name":"log","policyType":"replaceByEmptyDir","emptyDir":{"sizeLimit":"10Gi"}}]'
        bursting.cci.io/pod-sidecars: '{"containers":[{"name":"log-sidecar","image":"fluent-bit:latest","resources":{},"volumeMounts":[{"name":"log","mountPath":"/tmp/log"}],"position":"tail"}]}'
        volume.cci.io/mount-stdlog-containers: log-sidecar # Name of the container where standard output logs are to be mounted
      labels:
        app: deploy-example
        version: v1
    spec:
      containers:
      - image: nginx:latest
        name: nginx
        resources:
          limits:
            cpu: "1"
            memory: 2Gi
          requests:
            cpu: "1"
            memory: 2Gi
        volumeMounts:
        - mountPath: /tmp/log
          name: log
      imagePullSecrets:
        - name: default-secret
      volumes:
      - hostPath:
          path: /local/log
        name: log

With the preceding configurations, the sidecar container can collect two types of logs:

  • Standard output logs. The sidecar container can collect the standard output logs of service containers as files and store these standard output logs to the /var/log/pods directory.
  • File logs. Service containers output log files to the emptyDir volume shared with the sidecar container, which then collects the files from the volume.
Table 1 Pod annotations

Annotation

Type

Description

Example Value

volume.cci.io/mount-stdlog-containers

String

  1. Name of the container where standard output logs are mounted. To mount the standard output logs of multiple containers, separate their names with commas (,). You can also set the value to an asterisk (*) to mount the standard output logs of all containers. If the value is set to *, it cannot be set to any other container name.
  2. After a pod that matches the annotation starts, the directory that contains the standard output logs of all containers in the pod is mounted to the /var/log/pods directory of the container.

Example 1:

"container-0,container-1"

Example 2:

"*"

volume.cci.io/mount-stdlog-containers-path

String

  1. Container name and mount path that the standard output logs are mounted. The value is in JSON format. If the container name is set to *, the logs of all containers are mounted to the specified container. If the value is set to *, it cannot be set to any other container name.
  2. After any container that matches the annotation starts, the directory that contains the standard output logs of all containers in the pod is mounted to the specified path of the container.

Example 1:

"{\"container-0\":\"/var/log/pods\",\"container-1\":\"/tmp/log/pods\"}"

Example 2:

"{\"*\":\"/tmp/log/pods\"}"