All Products
Search
Document Center

Container Service for Kubernetes:Collect application logs with pod environment variables

Last Updated:Jun 23, 2026

Simple Log Service (SLS) is a one-stop service for log data that lets you collect, consume, ship, and analyze logs without development. An ACK Serverless cluster provides managed SLS features. provides managed Simple Log Service (SLS) features. Use pod environment variables in an ACK Serverless cluster to collect container stdout and log files to SLS.

Important
  • Use the SLS CRD method first for log collection. See Collect application logs by using CRDs.

  • Do not use CRD and environment variable methods together. This may cause log collection to fail.

Step 1: Create an application and configure log collection

Create an application from an image or a YAML template and configure log collection. See What is Simple Log Service?.

Create from an image

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

  2. On the Clusters page, click the name of your cluster. In the left navigation pane, click Workloads > Deployments.

  3. On the Deployments page, select a namespace from Namespace and click Create from Image.

    1. On the Basic Information tab, set the Application Name, number of Replicas:, and Type, then click Next.

    2. On the Container tab, configure the Log Service section.

      Note

      Only log collection parameters are covered here. See Create a stateless deployment for other parameters.

      Before configuring, ensure the logging plug-in is deployed on the cluster.logging plug-in is installed on the cluster.

      Parameter

      Description

      Collection configuration

      When the collection configuration changes, manually delete the previous configuration to prevent duplicate log collection.

      Click Collection Configuration and configure Logstore and Log Path in Container (Can be set to stdout).

      • Logstore name. Can contain only lowercase letters, digits, and hyphens (-).

        If the specified Logstore does not exist, ACK Serverless creates it in the SLS project associated with the cluster.

      • Log Path in Container: Path to collect. Example: /usr/local/tomcat/logs/catalina.*.log for Tomcat text logs.

        Note

        Setting the path to stdout collects both stdout and stderr from the container.

      Each collection configuration creates a Logtail configuration for the corresponding Logstore. By default, logs are collected in simple mode (line by line). For advanced collection methods, log on to the Simple Log Service console and modify the configuration in the corresponding project (prefixed with k8s-log by default) and Logstore.

      Custom tag

      Click Custom Tag and configure the Tag Key and Tag Value.

      A key-value pair appended to collected log entries. Use custom tags to label container log data, such as a version number.

    3. Click Next. See Create a stateless deployment for subsequent steps.

Create from a YAML file

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

  2. On the Clusters page, click the name of your cluster. In the left navigation pane, click Workloads > Deployments.

  3. On the Deployments page, select a namespace from Namespace and click Create from YAML.

  4. On the Create page, select a sample template, edit the Template in the YAML editor, and click Create.

    The YAML template uses standard Kubernetes syntax. To configure log collection, use env to add collection configurations and custom tags, and create corresponding volumeMounts and volumes. Sample pod YAML:

    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:
        ######### Configure environment variables ###########
        - name: aliyun_logs_log-stdout
          value: stdout
        - name: aliyun_logs_log-varlog
          value: /var/log/*.log
        - name: aliyun_logs_mytag1_tags
          value: tag1=v1
        ######### Configure Volume Mounts ###########
        volumeMounts:
        - name: volumn-sls-mydemo
          mountPath: /var/log
      volumes:
      - name: volumn-sls-mydemo
        emptyDir: {}

    Add these configurations to the pod YAML as needed:

    Note

    See Step 2: Advanced environment variable parameters for advanced requirements.

    1. Create collection configurations and custom tags with environment variables. All use the aliyun_logs_ prefix.

      1. To create a collection configuration:

        - name: aliyun_logs_log-stdout
          value: stdout
        - name: aliyun_logs_log-varlog
          value: /var/log/*.log

        This example creates two collection configurations using aliyun_logs_{key}, where {key} is log-stdout and log-varlog.

        • aliyun_logs_log-stdout: This env variable defines a log-stdout collection configuration, creates a log-stdout Logstore, and collects container stdout into the log-stdout Logstore.

        • aliyun_logs_log-varlog: This env variable creates a Logstore (log-varlog) and collection configuration (log-varlog) for path /var/log/*.log. Files from /var/log/*.log are sent to the log-varlog Logstore.

      2. To create a custom tag:

        - name: aliyun_logs_mytag1_tags
          value: tag1=v1

        The tag is appended to logs collected from this container. The tag key (mytag1 in this case) can contain only lowercase letters, digits, and hyphens (-).

    2. If the collection path is not stdout, create a corresponding volumeMounts. In this example, collecting /var/log/*.log requires a volumeMounts for /var/log.

Step 2: Advanced environment variable parameters

Environment variable-based LoongCollector configuration supports various parameters. You can use environment variables to configure advanced settings to meet your log collection requirements.

Important

You cannot use environment variables to configure log collection in edge computing scenarios.

Variable

Description

Example

Usage note

aliyun_logs_{key}

  • This variable is required. {key} can contain only lowercase letters, digits, and hyphens (-).

  • If the specified aliyun_logs_{key}_logstore does not exist, a logstore named {key} is created.

  • To collect the stdout of a container, set the value to stdout. You can also set the value to a path inside the container to collect the log files.

  • - name: aliyun_logs_catalina
    
      value: stdout
  • - name: aliyun_logs_access-log
    
      value: /var/log/nginx/access.log
  • By default, logs are collected in simple mode. If you want to parse the collected logs, we recommend that you configure the related settings in the Simple Log Service console or by using CRDs.

  • {key} specifies the name of the Logtail configuration. The configuration name must be unique in the Kubernetes cluster.

aliyun_logs_{key}_tags

Optional. This variable is used to add tags to log data. The value must be in the {tag-key}={tag-value} format.

- name: aliyun_logs_catalina_tags

  value: app=catalina

N/A.

aliyun_logs_{key}_project

Optional. The variable specifies a Simple Log Service project. The default project is the one that you specified when you created the cluster.

- name: aliyun_logs_catalina_project

  value: my-k8s-project

The project must be deployed in the same region as Logtail.

aliyun_logs_{key}_logstore

Optional. The variable specifies a Simple Log Service logstore. Default value: {key}.

- name: aliyun_logs_catalina_logstore

  value: my-logstore

N/A.

aliyun_logs_{key}_shard

Optional. The variable specifies the number of shards of the logstore. Valid values: 1 to 10. Default value: 2.

Note

If the logstore that you specify already exists, this variable does not take effect.

- name: aliyun_logs_catalina_shard

  value: '4'

N/A.

aliyun_logs_{key}_ttl

Optional. The variable specifies the log retention period. Valid values: 1 to 3650.

  • To retain log data permanently, set the value to 3650.

  • The default retention period is 90 days.

Note

If the logstore that you specify already exists, this variable does not take effect.

- name: aliyun_logs_catalina_ttl

  value: '3650'

N/A.

aliyun_logs_{key}_machinegroup

Optional. The variable specifies the node group in which the application is deployed. The default machine group is the one in which Logtail is deployed. For more information about how to use this variable, see Collect container logs from an ACK cluster.

- name: aliyun_logs_catalina_machinegroup

  value: my-machine-group

N/A.

aliyun_logs_{key}_logstoremode

Optional. The variable specifies the type of logstore. Default value: standard. Valid values: standard and query.

Note

If the logstore that you specify already exists, this variable does not take effect.

  • standard: Standard logstore. This type of logstore supports the log analysis feature and is suitable for scenarios such as real-time monitoring and interactive analysis. You can use this type of logstore to build a comprehensive observability system.

  • query: Query logstore. This type of logstore supports high-performance queries. The index traffic fee of a Query logstore is approximately half that of a standard logstore. Query logstores do not support SQL analysis. Query logstores are suitable for scenarios in which the amount of data is large, the log retention period is long, or log analysis is not required. If logs are stored for weeks or months, the log retention period is considered long.

  • - name: aliyun_logs_catalina_logstoremode
      value: standard 
  • - name: aliyun_logs_catalina_logstoremode
      value: query 

To use this variable, make sure that the logtail-ds image version is 1.3.1 or later.

  • Custom Requirement 1: Collect Data From Multiple Applications To The Same Logstore

    In this scenario, configure the aliyun_logs_{key}_logstore parameter. The following example shows how to collect stdout from two applications to the stdout-logstore.

    The {key} of Application 1 is set to app1-stdout, and the {key} of Application 2 is set to app2-stdout.

    Configure the following environment variables for Application 1:

    # Configure environment variables
        - name: aliyun_logs_app1-stdout
          value: stdout
        - name: aliyun_logs_app1-stdout_logstore
          value: stdout-logstore

    Configure the following environment variables for Application 2:

    # Configure environment variables
        - name: aliyun_logs_app2-stdout
          value: stdout
        - name: aliyun_logs_app2-stdout_logstore
          value: stdout-logstore
  • Custom Requirement 2: Collect Data From Multiple Applications To Different Projects

    In this scenario, perform the following steps:

    1. Create a machine group in each project and set the custom identifier of the machine group in the following format: k8s-group-{cluster-id}, where {cluster-id} is the ID of the cluster. You can specify a custom machine group name.

    2. Specify the project, logstore, and machine group in the environment variables for each application. The name of the machine group is the same as the one that you create in the previous step.

      In the following example, the {key} of Application 1 is set to app1-stdout, and the {key} of Application 2 is set to app2-stdout. If the two applications are deployed in the same ACK cluster, you can use the same machine group for the applications.

      Configure the following environment variables for Application 1:

      # Configure environment variables
          - 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

      Configure the following environment variables for Application 2:

      # Configure environment variables
          - 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 your cluster. In the left navigation pane, click Operations > Log Center.

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