ECI allows you to pass custom log configurations to containers via environment variables. This topic explains how to use them to configure log collection and automatically send container logs to Log Service (SLS).
-
For an ACK Serverless cluster, use an SLS CRD to collect logs. For more information, see Collect logs by using an SLS CRD.
-
Do not use an SLS CRD and environment variables together. This can cause log collection to fail.
Background information
Simple Log Service is a one-stop service for log data. It lets you quickly collect, consume, ship, query, and analyze log data without writing code. For more information, see What is Simple Log Service?.
Prerequisites
Ensure that Simple Log Service is activated. If the service is not activated, you will be prompted to activate it when you log on to the Simple Log Service console. Follow the on-screen instructions to complete the activation.
Usage notes
-
Log-related environment variables take effect only at instance creation. Variables set during instance updates are ignored.
-
Delete unused custom projects and Logtail configurations to avoid charges.
-
Renaming an environment variable does not delete the original configuration. Manually delete it to avoid charges.
Configuration
Log-related environment variables must be prefixed with aliyun_logs_{key}. The following table describes the available environment variables.
{key} specifies the name of the Logtail configuration. It can only contain lowercase letters, digits, and hyphens (-). You can customize {key}, but it must be unique within the Kubernetes cluster.
|
Configuration target |
Required |
Environment variable |
Description |
|
Logtail configuration |
Yes |
aliyun_logs_{key} |
Creates a Logtail configuration. The value specifies the log source.
|
|
project |
No |
aliyun_logs_{key}_project |
Specifies the Log Service project. If this variable is not set, logs are sent to the default project, which is named in the |
|
Logstore |
No |
aliyun_logs_{key}_logstore |
Specifies the Logstore. If this variable is not set, a Logstore that has the same name as |
|
machine group |
No |
aliyun_logs_{key}_machinegroup |
Specifies the machine group. If this variable is not set, the default machine group is used. |
|
shard |
No |
aliyun_logs_{key}_shard |
Specifies the number of shards for the Logstore.
|
|
log retention period |
No |
aliyun_logs_{key}_ttl |
Specifies the log retention period in days.
|
|
tag |
No |
aliyun_logs_{key}_tags |
Adds a tag to the logs in |
Configuration example
-
Connect to the cluster.
-
Create an application.
Set log configurations as environment variables for the container. The following YAML file provides an example Deployment.
apiVersion: apps/v1 kind: Deployment metadata: labels: app: sls name: eci-sls-demo spec: replicas: 1 selector: matchLabels: app: sls template: metadata: labels: app: sls alibabacloud.com/eci: "true" spec: containers: - image: registry-vpc.cn-beijing.aliyuncs.com/eci_open/alpine:3.5 imagePullPolicy: Always args: - -c - mkdir -p /log;while true; do echo hello world; date; echo hello sls >> /log/alpine.log; sleep 1;done command: - /bin/sh name: alpine env: # Specify a project. You can omit this to use the default project. - name: aliyun_logs_test-stdout_project value: k8s-log-c21492 - name: aliyun_logs_test-file_project value: k8s-log-c21492 # Specify a machine group. You can omit this to use the default machine group. - name: aliyun_logs_test-stdout_machinegroup value: k8s-group-app-alpine - name: aliyun_logs_test-file_machinegroup value: k8s-group-app-alpine # Collect stdout logs to a Logstore named test-stdout. - name: aliyun_logs_test-stdout value: stdout # Collect logs from the /log/*.log files to a Logstore named test-file. - name: aliyun_logs_test-file value: /log/*.log # Set the log retention period. This setting applies only to the test-stdout Logstore. - name: aliyun_logs_test-stdout_ttl value: "7" # Set the number of shards. This setting applies only to the test-stdout Logstore. - name: aliyun_logs_test-stdout_shard value: "2" # Mount an emptyDir volume. This is required for collecting logs from custom file paths. volumeMounts: - name: volume-sls mountPath: /log volumes: - name: volume-sls emptyDir: {}Save the preceding YAML content to a file named test-sls-env.yaml and run the following command to create the application.
kubectl create -f test-sls-env.yaml -
Check the application status.
kubectl get podExpected output:
NAME READY STATUS RESTARTS AGE eci-sls-demo-b97bbd7d6-z9twz 1/1 Running 0 2m45s -
View the logs.
-
Log on to the Log Service console.
-
Click the name of the target project.
-
Find the target Logstore and click its name to view the logs.
-
stdout logs
In the test-stdout Logstore, you can view the collected stdout logs. The log fields include
_container_name_: alpine,_image_name_: registry-vpc.cn-beijing.aliyuncs.com/eci_open/alpine:3.5,_namespace_: default, and_source_: stdout. An example log content ishello world. -
File logs
In the Log Service console, select the test-file Logstore from the left navigation pane. Then, click the test-file tab and click Raw in the toolbar to view the raw logs. The query result shows the collected container logs. Each log entry includes metadata tag fields, such as
__tag__:__container_name__:alpine,__tag__:__namespace__:default,__tag__:__container_ip__,__tag__:__pod_name__,__tag__:__pod_uid__, and__tag__:__eci_id__, and the log contentcontent:hello sls.
-
-