Container Compute Service (ACS) is integrated with Alibaba Cloud Simple Log Service (SLS). You can enable the log service when you create an ACS cluster to quickly collect container logs from the cluster, including container stdout and text files in containers. This topic describes how to collect application logs in an ACS cluster by using pod environment variables.
Step 1: Enable the Log Service add-on
You can select Enable Log Service when you create an ACS cluster to automatically enable the Log Service add-on. If you did not select this option during cluster creation, you can follow these steps to enable the add-on for an existing cluster.
-
Log on to the ACS console. In the left navigation pane, click Clusters.
-
On the Clusters page, click the name of the target cluster. In the left navigation pane, click Add-ons.
-
In the Logs and Monitoring section, find alibaba-log-controller. Click Install. In the Install dialog box, click OK.
If you have customized the configuration and environment variables for alibaba-log-controller, the upgrade overwrites these parameters, and you must reconfigure them.
Step 2: Configure log collection during application creation
When you create an application, you can configure it to collect container logs using the console or a YAML file.
Console wizard
-
Log on to the ACS console. In the left navigation pane, click Clusters.
-
On the Clusters page, click the name of the target cluster. In the left navigation pane, choose Workloads > Deployments.
-
On the Deployments page, select a namespace from the Namespace drop-down list. Then, in the upper-right corner, click Create from Image.
NoteThis example uses a Deployment, but the process is the same for other workload types, such as StatefulSets.
-
On the Basic Information tab, set the Name:, Replicas:, and Type. Click Next to go to the Container page.
NoteThis section describes only the configurations related to Simple Log Service. For information about other application configuration items, see Create a stateless workload (Deployment).
-
In the Log section, configure the log-related information.
-
Set the Collection Configuration.
Click the + icon to create a new collection configuration. Each configuration consists of a Logstore and a Log Path in Container (Can be set to stdout).
-
Logstore: Specify a name for the Logstore to store the collected logs. If the Logstore does not exist, ACS automatically creates it in the Simple Log Service Project associated with the cluster.
NoteBy default, logs in a newly created Logstore are retained for 90 days.
-
Log Path in Container: Specify the path from which to collect logs. For example, use /usr/local/tomcat/logs/catalina.*.log to collect text logs from Tomcat.
NoteIf you set this parameter to stdout, the container stdout and stderr are collected.
Each entry creates a collection configuration for the specified Logstore. By default, logs are collected in simple mode (line by line).
Example: Set the Logstore name to access and the log path in the container to
/usr/local/tomcat/logs/catalina.*.log. Set another Logstore name to catalina and the path tostdout. Note: Make sure that the log agent add-on is deployed to the cluster.
-
-
Set a 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. Use these tags to add metadata, such as a version number, to your container logs.
In the Tag Name field, enter
release. In the Tag Value field, enter1.0.0.
-
-
After you complete the configuration, click Next in the upper-right corner.
For information about subsequent operations, see Create a stateless workload (Deployment).
YAML file
-
Log on to the ACS console. In the left navigation pane, click Clusters.
On the Clusters page, click the name of your cluster. In the left navigation pane, click .
-
On the Deployments page, select a namespace from the Namespace drop-down list. Then, in the upper-right corner, click Create from YAML.
NoteThis example uses a Deployment, but the process is the same for other workload types, such as StatefulSets.
-
Configure the YAML file.
The syntax of the YAML file is the same as the Kubernetes syntax. To specify a collection configuration for the container, you must use
envto add a Collection Configuration and a Custom Tag. The following code provides an example of a simple pod: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 command: ["sh", "-c"] args: ["echo 'Starting my demo app'; sleep 3600"]Configure the settings in the following order based on your requirements.
NoteFor more information about advanced log collection requirements, see Step 3: Advanced log collection parameters.
-
Use environment variables to create a Collection Configuration and a Custom Tag. All configuration-related environment variables are prefixed with
aliyun_logs_.-
Create a collection configuration.
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 command: ["sh", "-c"] args: ["echo 'Starting my demo app'; sleep 3600"]In the example, two collection configurations are created using the
aliyun_logs_{key}format. The corresponding{key}values arelog-stdoutandlog-varlog.-
aliyun_logs_log-stdout: This variable creates a collection configuration for the container'sstdout. Collected logs are sent to aLogstorenamedlog-stdout. The corresponding Simple Log Service collection configuration is also namedlog-stdoutand uses thelog-stdoutLogstore. -
aliyun_logs_log-varlog: This environment variable specifies a configuration that creates aLogstorenamedlog-varlogand sets the log collection path to /var/log/*.log. The name of the corresponding Simple Log Service collection configuration is alsolog-varlog. The purpose of this configuration is to collect the content of /var/log/*.log files from a container and send it to thelog-varlogLogstore.
-
-
Create a custom tag.
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 command: ["sh", "-c"] args: ["echo 'Starting my demo app'; sleep 3600"]Simple Log Service automatically adds this tag as a field to all logs collected from the container. In this example, the
mytag1part of the variable name can beany name that does not contain an underscore (_).
-
-
If you specify a collection path other than stdout, you must also define a corresponding
volumeMountsin the pod specification.In the example, the collection configuration includes the collection of /var/log/*.log. Therefore, a
volumeMountsfor /var/log is added accordingly.
-
-
After you write the YAML file, click Create to apply the configuration to the ACS cluster.
Step 3: Advanced log collection parameters
You can use various environment variables to configure advanced parameters for specific log collection requirements.
|
Parameter |
Description |
Example |
Notes |
|
aliyun_logs_{key} |
|
|
|
|
aliyun_logs_{key}_tags |
Optional. The value must be in the {tag-key}={tag-value} format and helps identify logs. |
|
Not applicable. |
|
aliyun_logs_{key}_project |
Optional. The SLS Project. If this environment variable is not set, it defaults to the Project selected during the add-on installation. |
|
The Project must be in the same region where the Log Service add-on runs. |
|
aliyun_logs_{key}_logstore |
Optional. The SLS Logstore. If this environment variable is not set, the Logstore name defaults to the value of {key}. |
|
Not applicable. |
|
aliyun_logs_{key}_shard |
Optional. The number of shards to create for the Logstore. Valid values: 1 to 10. If this environment variable is not set, the default value is 2. Note
This parameter applies only when a Logstore is created; it does not affect existing Logstores. |
|
Not applicable. |
|
aliyun_logs_{key}_ttl |
Optional. The retention period of logs in days. Valid values: 1 to 3650.
Note
This parameter applies only when a Logstore is created; it does not affect existing Logstores. |
|
Not applicable. |
|
aliyun_logs_{key}_machinegroup |
Optional. The machine group for the application. If this environment variable is not set, it defaults to the machine group selected during the Log Service add-on installation. For more information about how to use this parameter, see Special use case 2: Route logs from different applications to different Projects. |
|
Not applicable. |
|
aliyun_logs_{key}_logstoremode |
Optional. The type of the SLS Logstore. If you do not specify this parameter, the default value is standard. Valid values: Note
This parameter applies only when a Logstore is created; it does not affect existing Logstores.
|
|
Not applicable. |
-
Use case 1: Collect to a single Logstore
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 the stdout of two applications and sends it to the
stdout-logstoreLogstore.In the example, the
{key}for Application 1 isapp1-stdout, and the{key}for Application 2 isapp2-stdout.Environment variables for Application 1:
apiVersion: v1 kind: Pod metadata: name: my-demo-1 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 command: ["sh", "-c"] args: ["echo 'Starting my demo app'; sleep 3600"]Environment variables for Application 2:
apiVersion: v1 kind: Pod metadata: name: my-demo-2 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 command: ["sh", "-c"] args: ["echo 'Starting my demo app'; sleep 3600"] -
Use case 2: Route logs to different Projects
To collect logs from different applications and route them to different Projects, follow these steps:
-
In each Project, create a machine group with a custom identifier named
k8s-group-{cluster-id}, where{cluster-id}is the cluster ID. The machine group name can be customized. -
Configure the Project, Logstore, and machine group information in the environment variables for each application. The machine group name must be the one that 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 both applications are in the same Kubernetes cluster, they can use the same machine group.Environment variables for Application 1:
apiVersion: v1 kind: Pod metadata: name: my-demo-1 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 command: ["sh", "-c"] args: ["echo 'Starting my demo app'; sleep 3600"]Environment variables for Application 2:
apiVersion: v1 kind: Pod metadata: name: my-demo-2 spec: containers: - name: my-demo-app image: 'registry.cn-hangzhou.aliyuncs.com/log-service/docker-log-test:latest' env: # Configure environment variables for Application 2 - 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 command: ["sh", "-c"] args: ["echo 'Starting my demo app'; sleep 3600"]
-
Step 4: View logs in the SLS console
This example shows how to view logs for an application created with the console wizard. Once configured, Simple Log Service automatically collects the application logs, which you can then view in the SLS console.
-
Log on to the SLS console.
-
In the Projects section, select the Project that corresponds to the ACS cluster. By default, the Project is named k8s-log-{ACS-Cluster-ID}. Then, go to the Logstores tab.
-
In the list of Logstores, find the Logstore that you specified in the collection configuration. Hover over the Logstore name. On the right side of the Logstore name, click the
icon, and then click Search & Analysis.On the log query page, you can view the application stdout logs and text logs from the container. The custom tag appears as a field in the logs.
More information
-
After you collect Kubernetes container logs using Simple Log Service, you can view the collected Kubernetes container logs in the SLS console.
-
For troubleshooting information, see Troubleshoot log collection failures.