Alibaba Cloud Container Compute Service (ACS) integrates Alibaba Cloud Simple Log Service. You can enable Simple Log Service when creating a cluster to quickly collect container logs from Kubernetes clusters, which include the standard output of containers and text files within containers. This topic describes how to collect application logs by using pod environment variables.
Step 1: Enable the Logtail component of Simple Log Service
You can enable the Logtail component for an existing cluster.
Log on to the ACS console. In the left-side navigation pane, click Clusters.
On the Clusters page, click the ID of the cluster that you want to manage or click Details in the Actions column of the cluster.
On the Clusters page, find the cluster that you want to manage and click its ID. In the left-side navigation pane, choose Operations > Add-ons.
On the Add-ons page, find alibaba-log-controller in the Logs and Monitoring section.
Click Install on the right side of the alibaba-log-controller component, and click OK in the Install alibaba-log-controller dialog box.
If the installed Simple Log Service component is an earlier version, you can click Upgrade on the right side of the alibaba-log-controller component.
After upgrading the alibaba-log-controller component, the component parameters are reset. If you have customized the configuration and environment variables of the alibaba-log-controller, the parameters are overwritten. Please reconfigure them.
Step 2: Configure Simple Log Service when creating an application
You can configure Simple Log Service while creating an application to collect container logs. Currently, you can configure it through the console and YAML templates.
Configure through the console
Log on to the ACS console. In the left-side navigation pane, click Clusters.
On the Clusters page, find the cluster that you want to manage and click its name. In the left-side pane, choose .
On the Deployments tab, click Create from Image.
NoteThis is a Deployments application example. The configuration method is the same for other types of workloads
On the Basic Information tab, set the Name, Replicas, and Type parameters, click Next to go to the Container page.
The following section only introduces the configuration related to Simple Log Service. For other application configurations, see Create a stateless application by using a Deployment.
In the Log section, configure the log-related information.
Set the Collection Configuration.
Click the + icon to create a new collection configuration. Each collection configuration consists of two items: Logstore and Log Path in Container.
Logstore: Configure the Logstore name to specify where the collected logs are stored. If the Logstore does not exist, ACS automatically creates the corresponding Logstore in the Simple Log Service Project associated with the cluster.
NoteThe default retention period for logs in the newly created Logstore is 180 days.
Log path in a container: Specify the path where the logs to be collected are located, such as using /usr/local/tomcat/logs/catalina.*.log to collect Tomcat text logs.
NoteWhen specified as stdout, it means collecting the standard output and standard error output of containers.
Each collection configuration is automatically corresponds to a Logstore. By default, the minimal mode (by line) is used. If you need more collection methods, see Use the Simple Log Service console to collect container text logs in DaemonSet mode and Use the Simple Log Service console to collect container stdout and stderr in DaemonSet mode.
Set the Custom Tag.
Click the + icon to create a new custom tag. Each custom tag is a key-value pair that is appended to the collected logs. You can use it to tag the log data of the container, such as the version number.
After completing all configurations, click Next to proceed to the next step.
For subsequent operations, see Create a stateless application by using a Deployment.
Using YAML templates
Log on to the Container Compute Service (ACS) Management Console, and select Clusters from the left navigation bar.
On the Clusters page, find the cluster that you want to manage and click its name. In the left-side pane, choose .
In the Stateless page, set the namespace in the Namespace drop-down box at the top, and click Create Using YAML in the upper right corner of the page. Note: This example uses a stateless application. The configuration method is the same for other types of workloads (e.g., stateful).
Configure the YAML file.
The syntax of the YAML template is the same as Kubernetes syntax, but to specify the collection configuration for the container, you need to use
env
to add Collection Configuration and Custom Tag for the container. Below is a simple pod example:apiVersion: v1 kind: Pod metadata: name: my-demo spec: containers: - name: my-demo-app image: 'registry.cn-hangzhou.aliyuncs.com/log-service/docker-log-test:latest' env: # Configure environment variables - name: aliyun_logs_log-stdout value: stdout - name: aliyun_logs_log-varlog value: /var/log/*.log - name: aliyun_logs_mytag1_tags value: tag1=v1
Configure according to your needs in the following order.
NoteIf you have more log collection requirements, see Step 3: Advanced parameter settings for log collection.
Create your Collection Configuration and Custom Tag through environment variables. All environment variables related to configuration use
aliyun_logs_
as the prefix.The rules for creating collection configurations are as follows:
apiVersion: v1 kind: Pod metadata: name: my-demo spec: containers: - name: my-demo-app image: 'registry.cn-hangzhou.aliyuncs.com/log-service/docker-log-test:latest' env: - name: aliyun_logs_log-stdout value: stdout - name: aliyun_logs_log-varlog value: /var/log/*.log
The example creates two collection configurations. The format is
aliyun_logs_{key}
, and the corresponding{key}
arelog-stdout
andlog-varlog
.aliyun_logs_log-stdout
: This env indicates the creation of alog-stdout
Logstore
with a log collection path ofstdout
. The corresponding Simple Log Service collection configuration name is alsolog-stdout
. The purpose is to collect the container's standard output into thelog-stdout
Logstore
.aliyun_logs_log-varlog
: This env indicates the creation of aLogstore
namedlog-varlog
with a log collection path of /var/log/*.log. The corresponding Simple Log Service collection configuration name is alsolog-varlog
. The purpose is to collect the content of the /var/log/*.log files in the container into thelog-varlog
Logstore
.
The rules for creating Custom Tags are as follows:
apiVersion: v1 kind: Pod metadata: name: my-demo spec: containers: - name: my-demo-app image: 'registry.cn-hangzhou.aliyuncs.com/log-service/docker-log-test:latest' env: - name: aliyun_logs_mytag1_tags value: tag1=v1
After configuring the tag, when the logs of the container are collected, the corresponding fields will be automatically appended to the logs in Simple Log Service. The
mytag1
is anarbitrary name that does not contain '_'
.
If your collection configuration specifies a non-stdout collection path, you need to create the corresponding
volumeMounts
in this section.In the example, the collection configuration adds the collection of /var/log/*.log, so the corresponding
volumeMounts
for /var/log is added.
After writing the YAML, click Create to submit the corresponding configuration to the Kubernetes cluster for execution.
Step 3: Advanced parameter settings for log collection
Log collection supports multiple configuration parameters by using container environment variables . You can set advanced parameters according to actual needs to meet special log collection requirements.
Log collection through container environment variables is not suitable for edge scenarios.
Environment Variable Name | Description | Example | Notes |
aliyun_logs_{key} |
|
|
|
aliyun_logs_{key}_tags | Optional. The value is the type of the {tag-key}={tag-value}, used to tag the logs. |
| Not involved. |
aliyun_logs_{key}_project | Optional. The value is the specified Simple Log Service project. If this environment variable does not exist, it defaults to the project selected during installation. |
| The project must be in the same region as your Logtail. |
aliyun_logs_{key}_logstore | Optional. The value is the specified Simple Log Service logstore. If this environment variable does not exist, the logstore and {key} are the same. |
| Not involved. |
aliyun_logs_{key}_shard | Optional. The value is the number of shards when creating the logstore, ranging from [1, 10]. If this environment variable does not exist, the value is 2. Note If the Logstore already exists, this parameter does not take effect. |
| Not involved. |
aliyun_logs_{key}_ttl | Optional. The value is the specified log retention period, ranging from [1, 3650].
Note If the logstore already exists, this parameter does not take effect. |
| Not involved. |
aliyun_logs_{key}_machinegroup | Optional. The value is the machine group of the application. If this environment variable does not exist, it defaults to the machine group where Logtail is installed. For detailed usage of this parameter, see Special Scenario 2: Collect data from different applications into different projects. |
| Not involved. |
aliyun_logs_{key}_logstoremode | Optional. The value is the specified type of Simple Log Service logstore. If this parameter is not specified, the default value is standard. Valid values: Note If the logstore already exists, this parameter does not take effect.
|
| Not involved. |
Special Scenario 1: Collect data from multiple applications into the same Logstore
If you need to collect data from multiple applications into the same Logstore, you can set the aliyun_logs_{key}_logstore parameter. For example, the following configuration collects stdout from two applications into stdout-logstore.
In the example, the
{key}
for application 1 isapp1-stdout
, and the{key}
for application 2 isapp2-stdout
.The environment variables for application 1 are set as follows:
apiVersion: v1 kind: Pod metadata: name: my-demo spec: containers: - name: my-demo-app image: 'registry.cn-hangzhou.aliyuncs.com/log-service/docker-log-test:latest' env: # Configure environment variables - name: aliyun_logs_app1-stdout value: stdout - name: aliyun_logs_app1-stdout_logstore value: stdout-logstore
The environment variables for application 2 are set as follows:
apiVersion: v1 kind: Pod metadata: name: my-demo spec: containers: - name: my-demo-app image: 'registry.cn-hangzhou.aliyuncs.com/log-service/docker-log-test:latest' env: # Configure environment variables - name: aliyun_logs_app2-stdout value: stdout - name: aliyun_logs_app2-stdout_logstore value: stdout-logstore
Special Scenario 2: Collect data from different applications into different projects
If you need to collect data from different applications into multiple projects, you need to perform the following operations:
Create a machine group in each project, select a custom ID, and name it
k8s-group-{cluster-id}
, where{cluster-id}
is your cluster ID. The machine group name can be customized.Configure the project, Logstore, and machine group information in the environment variables of each application. The machine group name is the one you created in the previous step.
In the following example, the
{key}
for application 1 isapp1-stdout
, and the{key}
for application 2 isapp2-stdout
. If the two applications are in the same Kubernetes cluster, the corresponding machine group can use the same machine group.The environment variables for application 1 are set as follows:
apiVersion: v1 kind: Pod metadata: name: my-demo spec: containers: - name: my-demo-app image: 'registry.cn-hangzhou.aliyuncs.com/log-service/docker-log-test:latest' env: # Configure environment variables - name: aliyun_logs_app1-stdout value: stdout - name: aliyun_logs_app1-stdout_project value: app1-project - name: aliyun_logs_app1-stdout_logstore value: app1-logstore - name: aliyun_logs_app1-stdout_machinegroup value: app1-machine-group
The environment variables for application 2 are set as follows:
apiVersion: v1 kind: Pod metadata: name: my-demo spec: containers: - name: my-demo-app image: 'registry.cn-hangzhou.aliyuncs.com/log-service/docker-log-test:latest' env: # Configure environment variables - name: aliyun_logs_app2-stdout value: stdout - name: aliyun_logs_app2-stdout_project value: app2-project - name: aliyun_logs_app2-stdout_logstore value: app2-logstore - name: aliyun_logs_app2-stdout_machinegroup value: app1-machine-group
Step 4: View logs
This example shows how to view the logs of a Tomcat application created through the console. After configuration, the logs of the Tomcat application have been collected and stored in Simple Log Service. You can view the container logs in the Simple Log Service console.
View logs by using Simple Log Service console
After the installation is successful, go to the Simple Log Service console.
After entering the console, select the project corresponding to the Kubernetes cluster (default: k8s-log-{Kubernetes cluster ID}) in the Projects area, click and go to the Logstore.
Find the corresponding Logstore (specified in the collection configuration) in the list, hover the mouse over the right side of the Logstore name, click the icon, and click Search & Analysis.
In this example, you can view the standard output logs and text logs inside the container of the Tomcat application on the log query page, and you can find custom tags appended to the log fields.
More information
To collect Kubernetes container logs by using Simple Log Service, you can view all log information of Container Compute Service ACS in the Simple Log Service console.
For troubleshooting, see What do I do if errors occur when I use Logtail to collect logs?.