All Products
Search
Document Center

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

Last Updated:Sep 16, 2022

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

Background information

If you enable log collection when you use an elastic container instance, a project and a machine group are generated in each region.

  • 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 customize 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 customize log configurations. If no project, Logstore, or machine group is created for the environment variables in Log Service, the system automatically creates a project, Logstore, or machine group.

  • Use the Log Service console or OpenAPI Explorer

    You can use the 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, Log Service exports logs of the elastic container instances to the Logstore.

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

Description

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

  • Configuration (Logtail)

    The following example shows how to set a Logtail:

    -name: aliyun_logs_{The configuration name}
    -value: {The logging path}
    Notice

    If you want to collect stdout logs, set the logging path to stdout.

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

    • The name can contain only lowercase letters, digits, and hyphens (-). The name 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 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.

    • The name can contain only lowercase letters, digits, hyphens (-), and underscores (_). The name 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.

    • The name can contain only lowercase letters, digits, hyphens (-), and underscores (_). The name 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

If you need to customize the following log configurations:

  • Logstore and Logtail: 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 customize the log configurations:

# Enable log collection
'SlsEnable': 'true',
# 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, Log Service cannot collect logs for the elastic container instance.

    Log Collection 3
    Note

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

The following figure shows the configuration effect.

  • Collection of stdout logs

    Collection of stdout logs
  • Collection of text logs

    Collection of text logs