All Products
Search
Document Center

:Collect logs by using environment variables

更新時間:Aug 26, 2024

You 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.

Important
  • 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

  • An ACK Serverless cluster is created. For more information, see Create an ACK Serverless cluster.

  • 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.

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.

Important

{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:

  • If Logtail collects stdout logs, valid values are:

    • stdout: collects stdout logs and standard error (stderr) logs.

    • stderr-only: collects stderr logs, but does not collect stdout logs.

    • stdout-only: collects stdout logs, but does not collect stderr logs.

  • If Logtail collects text logs, the valid value is the file directory in the container.

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.

Logstore

No

aliyun_logs_{key}_logstore

This variable specifies a Logstore. If you do not specify this variable, the Logstore that is named {key} is used.

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. Valid values: 1 to 10. If you do not specify this variable, the default number 2 is used.

Retention period of logs

No

aliyun_logs_{key}_ttl

This variable specifies the retention period of logs. Unit: days. Valid values: 1 to 3650.

  • A value of 3650 specifies permanently retaining logs.

  • If you do not specify this variable, the logs are retained for 90 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: tag-key=tag-value. Tags are used to identify logs.

Configuration example

  1. Connect to your Kubernetes cluster.

  2. 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
  3. 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
  4. View logs.

    1. Log on to the Simple Simple Log Service console.

    2. Click the name of the project.

    3. 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

        日志env0

      • Collection of text logs

        日志env1