All Products
Search
Document Center

Configure log collection for an elastic container instance

Last Updated: Oct 14, 2021

Configure log collection

Elastic Container Instance predefines basic log collection settings. You can also configure custom log collection settings. By default, all elastic container instances are added to the default machine group of the default project. All logs are collected to the default Logstore. You can configure log collection settings to collect logs from an elastic container instance to a specified Logstore in a specified project. If you want to separate logs of different Services and applications, you may need to add elastic container instances to different machine groups. In this case, you can configure log collection by using one of the following methods:

Configure log collection in the Log Service console or through API calls

You can log on to the Log Service console to create projects, Logstores, and machine groups. You can create custom configurations for the Logstore and apply them to the specified machine group. Then, logs are collected to the Logstore.

If you require a more simplified procedure, you can configure log collection on the elastic container instance.

Configure log collection on the elastic container instance

Elastic container instances provide default log collection settings and also allow you to configure custom log collection settings. For example, you can specify the project name, Logstore name, machine group name, and log collection directory. You can pass environment variables to containers that are running on the elastic container instance. Set the environment variables in the following formats:

 
 
Notice

If you want to collect the stdout log, set the directory to stdout.

 
 

Logstore naming rules

  • The name can contain lowercase letters, digits, hyphens (-), and underscores (_).

  • The name must start and end with a lowercase letter or a digit.

  • The name must be 3 to 63 characters in length.

Notice

The system ignores invalid names and uses the default name.

Set the name of the project

Specify the project where you want to store the collected log data in the following format:

 
 

By default, a project is automatically created in each region to store log data generated by API calls. A default project is created for each Kubernetes cluster. The projects are named in the following format: k8s-log-{Kubernetes cluster ID}.

Project naming rules

  • The name can contain lowercase letters, digits, and hyphens (-).

  • The name must start and end with a lowercase letter or a digit.

  • The name must be 3 to 63 characters in length.

Notice

The system ignores invalid names and uses the default name.

Set the number of shards in the Logstore

For more information about shards, see Shards.

Format:

 
 

Default value: 2. Valid values: 1 to 10.

Set the log retention period for the Logstore.

Format:

 
 

Default value: 90. Valid values: 1 to 3650.

Set the name of the machine group

This setting is optional.

By default,

elastic container instances created by calling the Elastic Container Instance API are added to the default machine group created by the system. One region corresponds to one elastic container instance.

Elastic container instances where Kubernetes clusters are deployed are added to the default machine group of the cluster. The name of the machine group is in the following format: k8s-group-{Kubernetes cluster ID}.

You can specify a machine group in the following format:

   
   

Machine group naming rules

  • The name can contain letters, digits, hyphens (-), and underscores (_).

  • The name must start and end with a lowercase letter or a digit.

  • The name must be 3 to 63 characters in length.

Notice

The system ignores invalid names and uses the default name.

Collect volume logs

You can set environment variables to collect stdout and stderr logs. If you want to collect log files from specified directories, you must use a volume.

The standard log collection directory of a volume is a subdirectory under the directory where the volume is mounted. This directory is specified by you.

Example:

An emptyDir volume is mounted to the /pod/data/ directory of the container. The log file from the volume can be stored in a specified subdirectory under /pod/data/. This allows you to manage the directory where the volume is mounted and specify a log collection directory.

Create an emptyDir volume

'Volume.1.Name': 'default-volume',
'Volume.1.Type': 'EmptyDirVolume',

Mount the volume to the container directory /pod/data/

'Container.1.VolumeMount.1.Name': 'default-volume',
'Container.1.VolumeMount.1.MountPath': '/pod/data/',
'Container.1.VolumeMount.1.ReadOnly': False,

Configure the Logstore

aliyun_logs_stdout-test is the directory where the stdout logs of containers are stored. aliyun_logs_log-file is the log collection directory of the volume. aliyun_logs_log-file can match files under the /pod/data/ directory based on key words.

'Container.1.EnvironmentVar.1.Key': 'aliyun_logs_log-file',
'Container.1.EnvironmentVar.1.Value': '/pod/data/*.log',
'Container.1.EnvironmentVar.2.Key': 'aliyun_logs_stdout-test',
'Container.1.EnvironmentVar.2.Value': 'stdout',
'Container.1.EnvironmentVar.3.Key': 'aliyun_logs_log-file_project',
'Container.1.EnvironmentVar.3.Value': 'k8s-eci-project',
'Container.1.EnvironmentVar.4.Key': 'aliyun_logs_stdout-test_project',
'Container.1.EnvironmentVar.4.Value': 'k8s-eci-project',
'Container.1.EnvironmentVar.5.Key': 'aliyun_logs_log-file_machinegroup',
'Container.1.EnvironmentVar.5.Value': 'k8s-eci-mg',
'Container.1.EnvironmentVar.6.Key': 'aliyun_logs_stdout-test_machinegroup',
'Container.1.EnvironmentVar.6.Value': 'k8s-eci-mg',

Results

sls

stdout log entries:

stdin log

Collected log files

logfile