Prerequisites:

  • The virtual-kubelet node is deployed in the target Kubernetes cluster. Note that a serverless Kubernetes cluster is embedded with the virtual-kubelet node.
  • Log Service is enabled for the Kubernetes cluster.

Collect container logs from an ECI

You can use environment variables to specify collection configurations and custom tags for a container. Then, you can use the volumes and volumeMounts fields to configure a volume and the directory to which the volume is mounted based on the log collection configuration. The following configuration file of a simple pod shows how to use environment variables to specify collection configurations and custom tags for a container:

apiVersion: v1
kind: Pod
metadata:
  name: say-hello
spec:
  containers:
  - image: registry.cn-beijing.aliyuncs.com/dzf/busybox:1.28.3
    imagePullPolicy: IfNotPresent
    name: busybox
    command: ["/bin/sh","-c","while true; do echo $(date) hello logfile. >> /var/log/sayhi.log echo $(date) hello,stdout.>>1 ; sleep 10; done"]
    env:
    - name: aliyun_logs_log-stdout
      value: stdout
    - name: aliyun_logs_log-varlog
      value: /var/log/*.log
    - name: aliyun_logs_appname_tags
      value: appname=say-hello
    - name: aliyun_logs_version_tags
      value: version=1.28.3
    volumeMounts:
    - name: volumn-sls-sayhi
      mountPath: /var/log
  volumes:
  - name: volumn-sls-sayhi
    emptyDir: {}

Note: A Logstore name cannot contain underscores (_). You can use hyphens (-) instead.

Use environment variables to specify collection configurations and custom tags. All environment variables related to log collection must be prefixed with aliyun_logs_.

Specify the following configurations in order based on your needs:

1. Logstore

- name: aliyun_logs_{Logstore name}
  value: {Log path}   

In the preceding example, two environment variables are used to specify collection configurations. The aliyun_logs_log-stdout environment variable instructs the system to create a Logstore named log-stdout, which collects the standard output of the container.

2. Custom tags

- name: aliyun_logs_{Tag name without underscores (_)}_tags
  value: {Tag name}={Tag value}

After a custom tag is specified, it is automatically appended to certain log fields when logs from the specified container are collected.

3. Path for collecting log files other than the standard output

If you specify a path for collecting log files other than the standard output, you need to add the volumnMounts field. In the preceding example, the .log files in the /var/log directory are to be collected. Therefore, the volumeMounts field is added, where mountPath is set to /var/log.

For more information about the advanced configurations of environment variables, see the Advanced configurations section in Use Log Service to collect Kubernetes cluster logs.