Customize ECI log collection

更新时间:
复制 MD 格式

Set environment variables to route ECI logs to custom projects, Logstores, and machine groups in SLS.

Background

Simple Log Service (SLS) is a one-stop service for log data. It lets you collect, consume, ship, query, and analyze logs without custom development. For more information, see Introduction to Simple Log Service.

Important

SLS is billed based on usage. Billing overview.

When you enable log collection during ECI instance creation, the system creates a default project and machine group:

  • Project name: eci-log-default-project-{Region}-{AccountID}.

  • Machine group name: eci-log-default-machine-group-{Region}.

To use a custom project, Logstore, or machine group, set log configurations as container environment variables. If the specified resources do not exist in SLS, the system creates them automatically.

Usage notes

  • Log-related environment variables take effect only at instance creation. Variables set during instance updates are ignored.

  • Delete unused custom projects and Logtail configurations to avoid charges.

  • Renaming an environment variable does not delete the original configuration. Manually delete it to avoid charges.

Configuration

Pass custom log configurations (project, Logstore, Logtail configuration, machine group, shard count, retention period, and tag) as container environment variables.

Note

Log-related environment variable names must start with aliyun_logs_{key}, where {key} is the Logtail configuration name. The key supports only lowercase letters, digits, and hyphens (-).

Parameter

Environment variable

Value description

Logtail configuration

aliyun_logs_{key}

Creates a Logtail configuration and specifies its log collection path.

  • To collect standard output, use one of the following values:

    • stdout: collects both normal output and error messages.

    • stderr-only: collects only error messages.

    • stdout-only: collects only normal output.

  • To collect text files, set the value to the file path in the container.

Project

aliyun_logs_{key}_project

Specifies the project. Name requirements:

  • It must be 3 to 63 characters in length.

  • It can contain only lowercase letters, digits, and hyphens (-). It must start and end with a lowercase letter or a digit.

Note

Project names must be globally unique and cannot be changed after creation. Invalid names are ignored and the default project is used.

Logstore

aliyun_logs_{key}_logstore

Specifies the Logstore. Name requirements:

  • It must be 3 to 63 characters in length.

  • It can contain only lowercase letters, digits, hyphens (-), and underscores (_). It must start and end with a lowercase letter or a digit.

Note

Invalid names are ignored and the default Logstore is used. The default Logstore name matches the Logtail configuration name {key}.

Machine group

aliyun_logs_{key}_machinegroup

Specifies the machine group. Name requirements:

  • It must be 3 to 63 characters in length.

  • It can contain only lowercase letters, digits, hyphens (-), and underscores (_). It must start and end with a lowercase letter or a digit.

Note

Invalid names are ignored and the default machine group is used.

Shard

aliyun_logs_{key}_shard

Sets the number of shards for the Logstore. The value can be an integer from 1 to 10.

Logs read from and written to a Logstore are stored in shards. Each Logstore has two shards by default.

Log retention period

aliyun_logs_{key}_ttl

Sets the log retention period in days. The value can be an integer from 1 to 3,650.

  • A value of 3650 indicates that logs are stored permanently.

  • The default retention period is 90 days.

Tag

aliyun_logs_{key}_tags

Adds a tag to the logs. The value must be in the tag-key=tag-value format.

Examples

The following example uses these custom log configurations:

  • Logtail configuration and Logstore names: Standard output logs are saved to stdout-eci, and text file logs are saved to file-eci.

  • Project name: project-eci-test

  • Machine group name: mg-eci-test

Note

These are example values. Use your own configurations and ensure the project name is globally unique.

API

Call the CreateContainerGroup operation and set environment variables with the Container.N.EnvironmentVar.N.Key and Container.N.EnvironmentVar.N.Value parameters:

ContainerGroupName=test-sls
Container.1.Name=busybox
Container.1.Image=registry-vpc.cn-shanghai.aliyuncs.com/eci_open/busybox:1.30
Container.1.Command.1=/bin/sh
Container.1.Arg.1=-c
Container.1.Arg.2=mkdir -p /log;while true; do echo hello world; date; echo hello sls >> /log/busy.log; sleep 1;done
# Collect stdout logs
Container.1.EnvironmentVar.1.Key=aliyun_logs_stdout-eci
Container.1.EnvironmentVar.1.Value=stdout
Container.1.EnvironmentVar.2.Key=aliyun_logs_stdout-eci_project
Container.1.EnvironmentVar.2.Value=project-eci-test
Container.1.EnvironmentVar.3.Key=aliyun_logs_stdout-eci_machinegroup
Container.1.EnvironmentVar.3.Value=mg-eci-test
# Collect text files
Container.1.EnvironmentVar.4.Key=aliyun_logs_file-eci
Container.1.EnvironmentVar.4.Value=/log/*.log
Container.1.EnvironmentVar.5.Key=aliyun_logs_file-eci_project
Container.1.EnvironmentVar.5.Value=project-eci-test
Container.1.EnvironmentVar.6.Key=aliyun_logs_file-eci_machinegroup
Container.1.EnvironmentVar.6.Value=mg-eci-test

Console

When you create an ECI instance in the Elastic Container Instance console, in the Container Configurations section, select a container, expand its Advanced Settings, and then enable Environment Variable to add your variables.

To collect stdout logs, add the following environment variables: aliyun_logs_stdout-eci = stdout, aliyun_logs_stdout-eci_project = project-eci-test, and aliyun_logs_stdout-eci_machinegroup = mg-eci-test. To collect text file logs, add the following environment variables: aliyun_logs_file-eci = /log/*.log, aliyun_logs_file-eci_project = project-eci-test, and aliyun_logs_file-eci_machinegroup = mg-eci-test.

Verify the results

In the Simple Log Service console, open the target project to view logs in the Logstore.

Standard output

Open the project-eci-test project and select the stdout-eci Logstore. The stdout logs include fields such as _container_name_, _image_name_, _namespace_, _pod_name_, _pod_uid_, _source_, and content.

Text file

The Logstore shows the text file logs collected from the ECI instance, with content hello sls.