Elastic Container Instance can export container logs to Simple Log Service. This topic describes how to use environment variables to configure log collection for an elastic container instance, including how to specify a project, a Logstore, and a machine group.
Background information
Alibaba Cloud Simple Log Service is an end-to-end data logging service that you can use to collect, consume, deliver, query, and analyze log data without the need to perform further development. For more information, see What is Simple Log Service?
Simple Log Service supports the pay-as-you-go billing method. You are charged based on your actual usage. For information about the billing of Simple Log Service, see Billing overview.
If you enable log collection when you create an elastic container instance, the system automatically generates a project and a machine group.
The name of the project is
eci-log-default-project-{Region}-{The account ID}
.The name of the machine group is
eci-log-default-machine-group-{Region}
.
To configure log collection settings, such as the settings for collecting logs to a custom project and a custom Logstore or the settings for adding elastic container instances that host different applications to different machine groups, you can use the environment variables of containers to pass in log-related custom configurations. If you do not have a project, a Logstore, or a machine group that corresponds to the environment variables, the system automatically creates a project, a Logstore, or a machine group.
You can use environment variables to configure log collection only when you create an elastic container instance. The log-related environment variables that you pass in when you update an elastic container instance do not take effect.
Configuration description
When you create an elastic container instance, you can use the environment variables of containers to pass in custom log configurations, such as the project, Logstore, Logtail configuration, machine group, shard, log retention period, and tags. The following table describes the environment variables that you can configure.
The name of each log-related environment variable contains a prefix in the following format: aliyun_logs_{key}
. {key}
specifies the name of the Logtail configuration in Simple Log Service. The value of {key} can contain only lowercase letters, digits, and hyphens (-).
Configuration object | Environment variable name | Environment variable description |
Logtail configuration | aliyun_logs_{key} | Creates a Logtail configuration. Set this variable to the log collection path.
|
Project | aliyun_logs_{key}_project | This variable specifies a project in Simple Log Service. The name of the project must meet the following requirements:
Note The name must be globally unique, and cannot be changed after the project is created. If the name does not meet the preceding requirements and fails the name format verification, the project configuration is ignored and the default project is used. |
Logstore | aliyun_logs_{key}_logstore | This variable specifies a Logstore. The name of the Logstore must meet the following requirements:
Note If the name does not meet the preceding requirements and fails the name format verification, the Logstore configuration is ignored and the default Logstore is used. By default, the values of |
Machine group | aliyun_logs_{key}_machinegroup | This variable specifies a machine group. The name of the machine group must meet the following requirements:
Note If the name does not meet the preceding requirements and fails the name format verification, the machine group configuration is ignored and the default machine group is used. |
Shard | aliyun_logs_{key}_shard | This variable specifies the number of shards in a Logstore. Valid values: 1 to 10. Read and write logs must be stored in the shards of the Logstore. By default, each Logstore contains two shards. |
Log retention period | aliyun_logs_{key}_ttl | This variable specifies the retention period of logs. Valid values: 1 to 3650. Unit: days.
|
Tag | aliyun_logs_{key}_tags | This variable specifies a tag that is added to logs. The value of this variable must be in the following format: |
Configuration examples
For example, you need to specify the following log configurations:
Logstore and Logtail configuration: Stdout logs are collected and stored in the Logstore named stdout-eci. Text logs are collected and stored in the Logstore named file-eci.
Project name: eci-test-project.
Machine group name: mg-eci-test.
The following examples describe how to specify the log configurations. In actual scenarios, specify the log configurations based on your business requirements and make sure that the project name is globally unique.
OpenAPI
When you call the CreateContainerGroup operation to create an elastic container instance, you can use the Container.N.EnvironmentVar.N.Key and Container.N.EnvironmentVar.N.Value parameters to configure environment variables, as shown in the following sample code:
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 logs.
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 elastic container instance in the Elastic Container Instance console, you can select a container in the Container Configurations section, expand the Advanced Settings of the container, and then turn on Environment Variable to add environment variables, as shown in the following figure.
Sample result
In the Simple Log Service console, find the project that you want to manage and click the name of the project to go to the project details page. Then, select Logstores from the Logstore list in the left-side navigation pane to view logs, as shown in the following figures.