All Products
Search
Document Center

Elastic Container Instance:Configure log collection for an elastic container instance

Last Updated:Aug 30, 2023

Elastic Container Instance can export container logs to Simple Log Service. This topic describes how to configure log collection for an elastic container instance, including how to specify a project, Logstore, and machine group.

Background information

Alibaba Cloud Simple Log Service is an end-to-end data logging service. You can use Simple Log Service to collect, consume, deliver, query, and analyze log data without performing further development. For more information, see What is Simple Log Service?

Important

Simple Log Service supports the pay-as-you-go billing method. You are charged based on your actual usage. For more information about the billing of Simple Log Service, see Billing overview.

Feature description

By default, if log collection is enabled for an elastic container instance, a project and a machine group are generated in each region by default:

  • 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}.

You may need to customize Logstores and projects to collect logs of elastic container instances based on your business requirements. If you use elastic container instances together with different applications and services, you may need to add elastic container instances to different machine groups. You can use one of the following methods to perform log configurations, such as the project, Logstore, and machine group:

  • Use the environment variables of elastic container instances

    You can use the environment variables of containers to specify log configurations. If no project, Logstore, or machine group is created for the environment variables in Simple Log Service, the system automatically creates a project, Logstore, or machine group.

    Important

    You can perform log collection by using environment variables only when you create an elastic container instance. Environment variables that are passed in when you update an instance do not take effect.

  • Use the Simple Log Service console or OpenAPI Explorer

    You can use the Simple Log Service console or OpenAPI Explorer to create a project, Logstore, and machine group. After you create a project, Logstore, and machine group, you can create a custom configuration file for the Logstore and apply the file to the machine group. Then, Simple Log Service exports logs of the elastic container instances to the Logstore.

This topic describes how to use environment variables to configure log collection for an elastic container instance.

Configuration description

If you create an elastic container instance by calling the CreateContainerGroup API operation, you can use the environment variables of containers to specify custom log configurations, such as the project, Logstore, Logtail configuration, machine group, shard, log retention period, and tag. The following part describes the format of and requirements on each configuration item when you specify a custom log configuration:

Logtail configuration

The following example shows how to set a Logtail configuration:

-name: aliyun_logs_{The configuration name}
-value: {The log collection path}

The valid values of collection path of stdout logs include:

  • stdout: collects normal output and error messages.

  • stderr-only: Only collects error messages, but does not collect normal output.

  • stdout-only: Only collects normal output, but does not collect error messages.

Project

The following example shows how to set a project:

-name: aliyun_logs_{The configuration name}_project
-value: {The project name}

The project name must meet the following requirements:

  • The name 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

If the name does not meet the preceding requirements, which causes the name format verification to fail, the project configuration is ignored and the default project is used.

Logstore

By default, the Logstore name is the same as the Logtail configuration name.

The following example shows how to set a Logstore:

-name: aliyun_logs_{The configuration name}_logstore
-value: {The Logstore name}

The Logstore name must meet the following requirements:

  • The name 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

If the name does not meet the preceding requirements, which causes the name format verification to fail, the Logstore configuration is ignored and the default Logstore is used.

Machine group

The following example shows how to set a machine group:

  -name: aliyun_logs_{The configuration name}_machinegroup
  -value: {The machine group name}

The machine group name must meet the following requirements:

  • The name 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

If the name does not meet the preceding requirements, which causes the name format verification to fail, the machine group configuration is ignored and the default machine group is used.

Shard

Read and write logs must be stored in shards of the Logstore. By default, each Logstore contains two shards.

The following example shows how to set a shard:

-name: aliyun_logs_{The configuration name}_shard
-value: {The number of shards}    # Default value: 2. Valid values: 1 to 10.

Log retention period

By default, logs can be retained for 90 days in a Logstore.

The following example shows how to set a log retention period:

-name: aliyun_logs_{The configuration name}_ttl
-value: {The number of days}    # Unit: days. Default value:90. Valid values: 1 to 3650.

Tag

The following example shows how to set a tag:

--name: aliyun_logs_{The configuration name}_tags
-value: {The tag key}={The tag value}

Sample configurations:

For example, you need to specify the following log configurations:

  • Logstore and Logtail configuration: Stdout logs are stored in the Logstore named stdout-test. Text logs are stored in the Logstore named file-test.

  • Project: eci-test-project

  • Machine group: eci-test-mg

The following example shows how to specify the log configurations:

# Configure the collection of stdout logs.
'Container.1.EnvironmentVar.1.Key': 'aliyun_logs_stdout-test',
'Container.1.EnvironmentVar.1.Value': 'stdout',
'Container.1.EnvironmentVar.2.Key': 'aliyun_logs_stdout-test_project',
'Container.1.EnvironmentVar.2.Value': 'eci-test-project',
'Container.1.EnvironmentVar.3.Key': 'aliyun_logs_stdout-test_machinegroup',
'Container.1.EnvironmentVar.3.Value': 'eci-test-mg',
# Configure the collection of text logs.
'Container.1.EnvironmentVar.4.Key': 'aliyun_logs_file-test',
'Container.1.EnvironmentVar.4.Value': '/log/*.log',
'Container.1.EnvironmentVar.5.Key': 'aliyun_logs_file-test_project',
'Container.1.EnvironmentVar.5.Value': 'eci-test-project',
'Container.1.EnvironmentVar.6.Key': 'aliyun_logs_file-test_machinegroup',
'Container.1.EnvironmentVar.6.Value': 'eci-test-mg',

If you enable log collection and specify environment variables based on the preceding configurations when you create an elastic container instance, the logs of the instance can be collected to the corresponding Logstore of the project.

  • If you have not created a Logstore or Logtail configuration, the system automatically creates a Logstore or Logtail configuration based on the input environment variables.

  • If you use an existing Logstore and Logtail configuration, make sure that Docker File is enabled for the Logtail configuration. Otherwise, Simple Log Service cannot collect logs for the elastic container instance.

    日志采集3
    Note

    If you do not want to enable Docker File, you must manually mount the log directory to the emptyDir volume before Simple Log Service can collect logs.

The following figure shows the configuration effect.

  • Collection of stdout logs

    标准输出
  • Collection of text logs

    文本文件