You can configure log collection for a container in ECI by setting environment variables. This automatically sends the container logs to Simple Log Service (SLS).
-
For ACK Serverless clusters, use SLS CRDs for log collection. For more information, see Collect logs by using SLS CRDs.
-
Do not use SLS CRDs and environment variables at the same time. Using both can cause log collection to fail.
Background
Simple Log Service (SLS) is an all-in-one service for log data. It allows you to collect, consume, ship, query, and analyze log data without development effort. For more information, see What is Simple Log Service?.
Prerequisites
Log Service is activated. If you have not activated Log Service, you are prompted to do so when you log on to the Log Service console. Follow the on-screen instructions to activate it.
Usage notes
-
You can customize log collection by using environment variables only when you create an ECI instance. Log-related environment variables passed when you update an instance do not take effect.
-
If you no longer use a custom project or Logtail configuration, delete them promptly to avoid unnecessary charges.
-
If you rename a custom environment variable, the original configuration is not automatically deleted. You must manually delete the configuration to avoid unnecessary charges.
Configuration
The prefix for log-related environment variable names is aliyun_logs_{key}. The following table describes these variables.
{key} represents the name of the Logtail configuration in Log Service. It can contain only lowercase letters, digits, and hyphens (-). You can customize {key}, but it must be unique within your Kubernetes cluster.
|
Parameter |
Required |
Environment variable name |
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, the default project |
|
Logstore |
No |
aliyun_logs_{key}_logstore |
Specifies the Log Service Logstore. If this variable is not set, the Logstore name is the same 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 |
Sets the number of shards for the Logstore.
|
|
log retention period |
No |
aliyun_logs_{key}_ttl |
Sets the log retention period in days.
|
|
tag |
No |
aliyun_logs_{key}_tags |
Adds a tag to logs for identification. The format is |
Example
-
Connect to the cluster.
-
Create an application.
You can pass log configurations by using environment variables for the container. The following YAML file is an example of a Deployment. After the container starts, it continuously prints to stdout and writes to a log file.
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. The log source is stdout. - name: aliyun_logs_test-stdout value: stdout # Collect logs from the /log/*.log path 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 directories but not for stdout. volumeMounts: - name: volume-sls mountPath: /log volumes: - name: volume-sls emptyDir: {}Save the preceding YAML content as test-sls-env.yaml and run the following command to create the application.
kubectl create -f test-sls-env.yaml -
Verify 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 Simple Log Service console.
-
Click the name of the target project.
-
Find the target Logstore and click its name to view the logs.
-
Standard output
In the Logstore list on the left, select test-stdout to view the collected stdout logs. The log records contain fields such as
_container_name_: alpine,_namespace_: default, and_source_: stdout. An example of the log content ishello world. -
File-based log
In the Logstore list on the left, select the test-file Logstore and click the Raw button to view the raw logs. The query result shows multiple log entries, all with the content
hello sls, from the ECI alpine container. The logs include tag fields such as__tag__:__container_name__(alpine),__tag__:__namespace__(default),__tag__:__pod_name__, and__tag__:__eci_id__. This indicates that the file-based logs are successfully collected to SLS.
-
-