All Products
Search
Document Center

Container Service for Kubernetes:Collect application logs by using pod environment variables

Last Updated:Mar 26, 2026

Simple Log Service collects stdout and log files from application pods in your ACK Serverless cluster using environment variables. Configure the aliyun_logs_ environment variables in your pod spec — no application code changes required. For more information about Simple Log Service, see What is Simple Log Service?

Important

Use Simple Log Service CustomResourceDefinitions (CRDs) as the preferred method for log collection. For details, see Use a Simple Log Service CRD to collect application logs. Do not use CRDs and environment variables at the same time — this causes log collection failures.

How it works

When a pod starts, LoongCollector reads the aliyun_logs_ environment variables and automatically creates a collection configuration for each variable. Each variable maps a Logstore name to a log source (stdout or a file path). If the specified Logstore does not exist, Simple Log Service creates it automatically in the project associated with your cluster (using the k8s-log prefix).

By default, logs are collected in simple mode (line by line). To parse logs using custom patterns, modify the collection configuration in the Simple Log Service console after deployment.

Prerequisites

Before you begin, ensure that you have:

  • An ACK Serverless cluster with Simple Log Service enabled

  • Permission to create Deployments in the target namespace

Step 1: Create an application with log collection

Choose one of the following methods to create an application and configure log collection.

Use the ACK console

  1. Log on to the ACK console. In the left navigation pane, click Clusters.

  2. On the Clusters page, click the name of the cluster you want to manage. In the left navigation pane, choose Workloads > Deployments.

  3. Select a namespace from the Namespace drop-down list, then click Create from Image in the upper-right corner.

  4. In the Basic Information step, set Name, Replicas, and Type. Click Next.

  5. In the Container step, configure the Log Service section. The following figure shows an example configuration.采集配置

    Note The table below covers only the Log Service parameters. For all other parameters, see Create a Deployment.
    Parameter Description
    Collection configuration Click Collection Configuration to set the Logstore name and Log Path in Container (Can be set to stdout). The Logstore name can contain only lowercase letters, digits, and hyphens (-). If the Logstore does not exist, it is created automatically. Set the log path to the file path you want to collect (for example, /usr/local/tomcat/logs/catalina.*.log), or set it to stdout to collect stdout and stderr. After you modify a collection configuration, manually delete the previous version to avoid duplicate data collection.
    Custom Tag Click Custom Tag to add key-value pairs that are appended to every collected log entry. Use custom tags to mark metadata such as application version.
  6. Click Next to configure advanced settings. For remaining steps, see Create a Deployment.

Use a YAML template

  1. Log on to the ACK console. In the left navigation pane, click Clusters.

  2. On the Clusters page, click the name of the cluster you want to manage. In the left navigation pane, choose Workloads > Deployments.

  3. Select a namespace from the Namespace drop-down list, then click Create from YAML in the upper-right corner.

  4. Select a template from the Sample Template drop-down list, edit the template in the Template code editor, then click Create. The following example pod YAML collects stdout to the log-stdout Logstore and log files from /var/log/*.log to the log-varlog Logstore:

    • Set the value to stdout to collect stdout and stderr.

    • Set the value to a file path (for example, /var/log/*.log) to collect log files.

    apiVersion: v1
    kind: Pod
    metadata:
      name: my-demo
    spec:
      containers:
      - name: my-demo-app
        image: 'registry.cn-hangzhou.aliyuncs.com/log-service/docker-log-test:latest'
        args:
        - -c
        - mkdir -p /log;while true; do echo hello world; date; echo hello sls >> /var/log/test.log; sleep 1;done
        command:
        - /bin/sh
        env:
        # Log collection: collect stdout to the log-stdout Logstore
        - name: aliyun_logs_log-stdout
          value: stdout
        # Log collection: collect /var/log/*.log files to the log-varlog Logstore
        - name: aliyun_logs_log-varlog
          value: /var/log/*.log
        # Custom tag: append tag1=v1 to all collected logs
        - name: aliyun_logs_mytag1_tags
          value: tag1=v1
        volumeMounts:
        - name: volumn-sls-mydemo
          mountPath: /var/log
      volumes:
      - name: volumn-sls-mydemo
        emptyDir: {}

    All environment variables for log collection must use the aliyun_logs_ prefix. Log collection variables use the format aliyun_logs_{key}, where {key} becomes both the Logstore name and the collection configuration name. The {key} can contain only lowercase letters, digits, and hyphens (-), and must be unique across the cluster. Custom tag variables use the format aliyun_logs_{key}_tags with a {tag-key}={tag-value} value. The tag is automatically appended to all logs collected from the pod. Volume configuration: When collecting log files (not stdout), configure volumeMounts so that LoongCollector can access the log directory. In the example above, mountPath: /var/log gives LoongCollector access to /var/log/*.log.

Step 2: Configure advanced log collection settings

Important

Environment variable-based log collection is not supported in edge computing scenarios.

The following environment variables let you customize the Logstore, project, retention period, and other settings. All variables that modify behavior follow the aliyun_logs_{key}_<option> pattern, where {key} matches the collection configuration name defined in Step 1.

Variable Required Description Example
aliyun_logs_{key} Yes Specifies the log source. Set to stdout for container stdout/stderr, or set to a file path inside the container. The {key} value becomes the Logstore name if aliyun_logs_{key}_logstore is not set. - name: aliyun_logs_catalina<br> value: stdout
aliyun_logs_{key}_tags No Adds key-value tags to log data. Format: {tag-key}={tag-value}. - name: aliyun_logs_catalina_tags<br> value: app=catalina
aliyun_logs_{key}_project No Specifies the Simple Log Service project. Defaults to the project associated with the cluster. The project must be in the same region as LoongCollector. - name: aliyun_logs_catalina_project<br> value: my-k8s-project
aliyun_logs_{key}_logstore No Specifies the Logstore name. Defaults to {key}. - name: aliyun_logs_catalina_logstore<br> value: my-logstore
aliyun_logs_{key}_shard No Sets the number of shards for a new Logstore. Valid values: 1–10. Default: 2. Has no effect if the Logstore already exists. - name: aliyun_logs_catalina_shard<br> value: '4'
aliyun_logs_{key}_ttl No Sets the log retention period in days. Valid values: 1–3650. Default: 90. Set to 3650 to retain logs permanently. Has no effect if the Logstore already exists. - name: aliyun_logs_catalina_ttl<br> value: '3650'
aliyun_logs_{key}_machinegroup No Specifies the machine group for the application. Defaults to the machine group where LoongCollector is deployed. For usage details, see Collect container logs from an ACK cluster. - name: aliyun_logs_catalina_machinegroup<br> value: my-machine-group
aliyun_logs_{key}_logstoremode No Sets the Logstore type for a new Logstore. Default: standard. Has no effect if the Logstore already exists. Requires logtail-ds image version 1.3.1 or later. - name: aliyun_logs_catalina_logstoremode<br> value: query

Logstore types (aliyun_logs_{key}_logstoremode):

Type Best for Limitations
standard Real-time monitoring, interactive analysis, observability systems None
query Large data volumes, long retention periods (weeks or months), scenarios that don't require log analysis No SQL analysis; index traffic fee is approximately half that of a standard Logstore

Collect logs from multiple applications into the same Logstore

Set aliyun_logs_{key}_logstore to the same Logstore name across applications. The {key} must differ for each application to keep configuration names unique.

The following example collects stdout from two applications into stdout-logstore:

Application 1:

- name: aliyun_logs_app1-stdout
  value: stdout
- name: aliyun_logs_app1-stdout_logstore
  value: stdout-logstore

Application 2:

- name: aliyun_logs_app2-stdout
  value: stdout
- name: aliyun_logs_app2-stdout_logstore
  value: stdout-logstore

Collect logs from multiple applications into different projects

When applications must write to separate Simple Log Service projects, create a machine group in each project and configure the project, Logstore, and machine group for each application.

  1. In each Simple Log Service project, create a machine group with a custom identifier in the format k8s-group-{cluster-id}, where {cluster-id} is your cluster ID. You can use a custom machine group name.

  2. Set the project, Logstore, and machine group environment variables for each application. Applications deployed in the same ACK cluster can share the same machine group. The following example routes logs from two applications to separate projects: Application 1:

    - name: aliyun_logs_app1-stdout
      value: stdout
    - name: aliyun_logs_app1-stdout_project
      value: app1-project
    - name: aliyun_logs_app1-stdout_logstore
      value: app1-logstore
    - name: aliyun_logs_app1-stdout_machinegroup
      value: app1-machine-group

    Application 2:

    - name: aliyun_logs_app2-stdout
      value: stdout
    - name: aliyun_logs_app2-stdout_project
      value: app2-project
    - name: aliyun_logs_app2-stdout_logstore
      value: app2-logstore
    - name: aliyun_logs_app2-stdout_machinegroup
      value: app1-machine-group

Step 3: View logs

  1. Log on to the ACK console. In the left navigation pane, click Clusters.

  2. On the Clusters page, click the name of the cluster you want to manage. In the left navigation pane, choose Operations > Log Center.

  3. On the Log Center page, click the Application Logs tab and select a Logstore to view the collected container logs.

What's next