You can use container environment variables to specify custom log configurations in Elastic Container Instance. This topic describes how to use environment variables to configure log collection and automatically collect container logs in Simple Log Service.
For Container Service for Kubernetes (ACK) serverless clusters, we recommend that you preferentially use Simple Log Service custom resource definitions (CRDs) to collect logs. For more information, see Collect logs by using Simple Log Service CRDs.
Do not use Simple Log Service CRDs and environment variables at the same time. This may cause log collection failures.
Background information
Simple Log Service is an end-to-end data logging service. You can use Simple Log Service to collect, consume, deliver, query, and analyze log data without performing further development. For more information, see What is Simple Log Service?
Prerequisites
Simple Log Service is activated for the ACK Serverless cluster. Log on to the Simple Log Service console. If Simple Log Service is not activated for the cluster, you are prompted to follow on-screen instructions to activate the service.
Note
You can use environment variables to configure log collection only when you create an elastic container instance. The log-related environment variables that you pass in when you update an elastic container instance do not take effect.
If you no longer use a custom project or Logtail configuration, delete the project or Logtail configuration in a timely manner to avoid unnecessary costs.
Configuration description
The prefix of the name of a log environment variable is in the aliyun_logs_{key}
format. The following table describes environment variables that can be configured.
{key} specifies the name of the Logtail configuration in Simple Log Service. The value of {key} can contain only lowercase letters, digits, and hyphens (-). You can specify a custom value for {key}. Make sure that the value is unique in Kubernetes clusters.
Configured object | Required | Name of the environment variable | Description |
Logtail configuration | Yes | aliyun_logs_{key} | This variable creates a Logtail configuration. Valid values:
|
Project | No | aliyun_logs_{key}_project | This variable specifies a project in Simple Log Service. If you do not specify this variable, the default project is used, which is named in the k8s-log-<cluster-id> format. |
Logstore | No | aliyun_logs_{key}_logstore | This variable specifies a Logstore. If you do not specify this variable, the Logstore that is named |
Machine group | No | aliyun_logs_{key}_machinegroup | This variable specifies a machine group. If you do not specify this variable, the default machine group is used. |
Shard | No | aliyun_logs_{key}_shard | This variable specifies the number of shards in a Logstore.
|
Retention period of logs | No | aliyun_logs_{key}_ttl | This variable specifies the retention period of logs. Unit: days.
|
Tag | No | aliyun_logs_{key}_tags | This variable specifies a tag that is bound to a log. The value of the variable must be in the following format: |
Configuration example
Connect to your Kubernetes cluster.
Create an application.
You can specify log-related configurations in the environment variables of a container. The following sample YAML file describes how to create a Deployment. In the example, relevant commands are run to continuously display the stdout and log files after the container is started.
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: # Configure a project or use the default project. - name: aliyun_logs_test-stdout_project value: k8s-log-c21492 - name: aliyun_logs_test-file_project value: k8s-log-c21492 # Configure a machine group or 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 that is named test-stdout. The collection path is stdout. - name: aliyun_logs_test-stdout value: stdout # Collect logs in the /log/*.log directory to a Logstore that is named test-file. The collected objects are all .log files in the /log directory. - name: aliyun_logs_test-file value: /log/*.log # Specify the retention period of logs. The setting is valid only for the Logstore that is named test-stdout. - name: aliyun_logs_test-stdout_ttl value: "7" # Specify the number of log shards. The setting is valid only for the Logstore that is named test-stdout. - name: aliyun_logs_test-stdout_shard value: "2" # Mount an emptyDir volume. When the value of the environment variable aliyun_logs_{key} is a directory in the container, mount an emptyDir volume to the directory. volumeMounts: - name: volume-sls mountPath: /log volumes: - name: volume-sls emptyDir: {}
Create a YAML file that is named test-sls-env.yaml, copy the preceding template to the YAML file, and then run the following command to create an application:
kubectl create -f test-sls-env.yaml
Check the status of the application.
kubectl get pod
Expected output:
NAME READY STATUS RESTARTS AGE eci-sls-demo-b97bbd7d6-z9twz 1/1 Running 0 2m45s
View logs.
Log on to the Simple Simple Log Service console.
Click the name of the project.
Find the Logstore in which the logs of your containers are stored. Click the name of the Logstore to view the logs.
Collection of stdout logs
Collection of text logs