Collect logs by using environment variables

更新时间:
复制 MD 格式

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

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

Important

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

  • To collect standard output, use one of the following values:

    • stdout: collects both normal output and error messages.

    • stderr-only: collects only error messages.

    • stdout-only: collects only normal output.

  • To collect file-based logs, set the value to a file path inside the container.

project

No

aliyun_logs_{key}_project

Specifies the Log Service project.

If this variable is not set, the default project k8s-log-<cluster-id> is used.

Logstore

No

aliyun_logs_{key}_logstore

Specifies the Log Service Logstore.

If this variable is not set, the Logstore name is the same as {key}.

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.

  • Valid values: 1 to 10.

  • If this variable is not set, the default value is 2.

log retention period

No

aliyun_logs_{key}_ttl

Sets the log retention period in days.

  • Valid values: 1 to 3650. A value of 3650 means that logs are stored permanently.

  • If this variable is not set, logs are retained for 90 days by default.

tag

No

aliyun_logs_{key}_tags

Adds a tag to logs for identification. The format is tag-key=tag-value.

Example

  1. Connect to the cluster.

  2. 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
  3. Verify the application status.

    kubectl get pod

    Expected output:

    NAME                             READY   STATUS    RESTARTS   AGE
    eci-sls-demo-b97bbd7d6-z9twz     1/1     Running   0          2m45s
  4. View the logs.

    1. Log on to the Simple Log Service console.

    2. Click the name of the target project.

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