All Products
Search
Document Center

Elastic Container Instance:Collect logs by using environment variables

Last Updated:Mar 25, 2024

You can specify custom log-related configurations by using environment variables of containers in Elastic Container Instance. This topic describes how to configure log collection by using environment variables and automatically collect container logs in Simple Log Service.

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-related 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 normal output and error messages.

    • stderr-only: Only collects error messages, but does not collect normal output.

    • stdout-only: Only collects normal output, but does not collect error messages.

  • 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 the environment variable, the default project is used.

Logstore

No

aliyun_logs_{key}_logstore

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

Machine group

No

aliyun_logs_{key}_machinegroup

This variable specifies the machine group of the application. If you do not specify the environment 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 the environment 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 to permanently retain the logs.

  • If you do not specify the environment 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 shows how to create a Deployment. In the example, the system runs relevant commands after the container is started. stdout logs and text logs of the container are continuously displayed.

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