All Products
Search
Document Center

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

Last Updated:Mar 20, 2025

Simple Log Service is a one-stop log service of Alibaba Cloud. It allows you to directly collect, consume, deliver, query, and analyze logs in the console. Simple Log Service is hosted in ACK Serverless clusters. You can configure Simple Log Service environment variables in an ACK Serverless cluster to allow Simple Log Service to collect stdout and log files from application pods in the cluster.

Important
  • We recommend that you preferentially use Simple Log Service CustomResourceDefinitions (CRDs) to collect logs. For more information, see Use a Simple Log Service CRD to collect application logs.

  • Do not use Simple Log Service CRDs and environment variables at the same time. This may cause log collection failures.

Step 1: Create an application and configure Simple Log Service to collect application logs

You can create an application by using an image or YAML template that contains log collection configurations based on Simple Log Service. For more information about Simple Log Service, see What is Simple Log Service?

Image

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

  2. On the Clusters page, find the cluster that you want to manage and click its name. In the left-side pane, choose Workloads > Deployments.

  3. In the upper part of the Deployments page, select the namespace where you want to create an application from the Namespace drop-down list. Then, click Create from Image in the upper-right corner of the page.

    1. In the Basic Information step, set the Name, Replicas, and Type parameters. Then, click Next.

    2. In the Container step, set the parameters in the Log Service section.

      Note

      The following table describes only the parameters related to Simple Log Service. For more information about other parameters, see Create a Deployment.

      The following figure is an example.采集配置

      Parameter

      Description

      Collection Configuration

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

      • Logstore: the name of the Logstore that you want to use. The name can contain only lowercase letters, digits, and hyphens (-).

        You can use this parameter to specify the Logstore that stores the collected logs. If the specified Logstore does not exist, the system automatically creates a Logstore in the Simple Log Service project that is associated with the cluster.

      • Log Path in Container (Can be set to stdout): the path from which you want to collect logs. A value of /usr/local/tomcat/logs/catalina.*.log indicates that the log files of a Tomcat application are collected.

        Note

        If you set the value to stdout, stdout and stderr are collected.

      All settings are added as configuration entries to the corresponding Logstore. By default, logs are collected in simple mode (by row). If you want to use other methods to collect logs, log on to the Simple Log Service console and modify the log collection configurations of the project and Logstore. By default, the project uses the k8s-log prefix.

      Custom Tag

      Click Custom Tag. Set Tag Key and Tag Value.

      Each tag is a key-value pair that is appended to the collected log data. You can use custom tags to mark log data. For example, you can use a tag to denote the application version.

    3. After you configure the parameters, click Next to configure advanced settings. For more information about the subsequent steps, see Create a Deployment.

YAML template

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

  2. On the Clusters page, find the cluster that you want to manage and click its name. In the left-side pane, choose Workloads > Deployments.

  3. In the upper part of the Deployments page, select the namespace where you want to create an application from the Namespace drop-down list. Then, click Create from YAML in the upper-right corner of the page.

  4. On the Create page, select a template from the Sample Template drop-down list and modify the template content in the Template code editor based on your requirements. Then, click Create.

    YAML templates comply with the Kubernetes syntax. You can use env to define log collection configurations and custom tags. You must also set the volumeMounts and volumes parameters. Sample pod YAML template:

    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:
        ######### Specify 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
        ######### Specify volume mounting parameters. ###########
        volumeMounts:
        - name: volumn-sls-mydemo
          mountPath: /var/log
      volumes:
      - name: volumn-sls-mydemo
        emptyDir: {}

    You can add the following configurations based on your requirements:

    Note

    If you have other requirements on log collection, see Step 2: Configure advanced settings in the env field.

    1. Add log collection configurations and custom tags by using environment variables. All environment variables related to log collection must use aliyun_logs_ as the prefix.

      1. Add log collection configurations in the following format:

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

        In the preceding example, two environment variables in the following format are added to the log collection configuration: aliyun_logs_{key}. The {keys} of the environment variables are log-stdout and log-varlog.

        • The aliyun_logs_log-stdout environment variable indicates that a Logstore named log-stdout is created to store the stdout collected from containers. The name of the collection configuration is log-stdout. This way, the stdout of containers is collected to the Logstore named log-stdout.

        • The aliyun_logs_log-varlog environment variable indicates that a Logstore named log-varlog is created to store the collected log files from the /var/log/*.log path. The name of the collection configuration is log-varlog. This way, the log files in the /var/log/*.log path are collected to the Logstore named log-varlog.

      2. Add custom tags in the following format:

        - name: aliyun_logs_mytag1_tags
          value: tag1=v1

        After a tag is added, the tag is automatically appended to the log data that is collected from the container. The value of mytag1 can contain only lowercase letters, digits, and hyphens (-).

    2. If you specify a log path to collect log files other than stdout, you must set the volumeMounts parameter. In the preceding YAML template, the mountPath field in volumeMounts is set to /var/log. This allows Logtail to collect log data from the /var/log/*.log file.

Step 2: Configure advanced settings in the env field

Container environment variables can be set to customize log collection, with advanced parameters available for specific needs. Note that environment variables cannot be used for log collection configuration in edge computing scenarios.

Important

Environment variables cannot be used to configure log collection in edge computing scenarios.

Field

Description

Example

Notes

aliyun_logs_{key}

  • Required. {key} can contain only lowercase letters, digits, and hyphens (-).

  • If the aliyun_logs_{key}_logstore variable is not configured, a Logstore named {key} is created to store the collected log data.

  • If the value is stdout, it indicates collecting the standard output of the container. Other values indicate the log path in the container.

  • - name: aliyun_logs_catalina
    
      value: stdout
  • - name: aliyun_logs_access-log
    
      value: /var/log/nginx/access.log
  • The default log collection mode is simple mode. If you want to parse log content, it is recommended to use the Simple Log Service Console or configure it through CRD.

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

aliyun_logs_{key}_tags

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

- name: aliyun_logs_catalina_tags

  value: app=catalina

N/A.

aliyun_logs_{key}_project

Optional. The variable specifies a project in Simple Log Service. If this environment variable is not configured, the project you selected during installation is used.

- 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 Logstore in Simple Log Service. If this environment variable is not configured, the Logstore is named {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. If this environment variable is not configured, the value is 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.

  • If this environment variable is not configured, the default log 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. If this environment variable is not configured, the default node group in which Logtail is deployed is used. For more information about this parameter, see Collect Kubernetes Container Text Logs via DaemonSet.

- name: aliyun_logs_catalina_machinegroup

  value: my-machine-group

N/A.

aliyun_logs_{key}_logstoremode

Optional. The variable specifies the type of the Logstore in Simple Log Service. If this parameter is not specified, the default value is standard. Valid values:

Note

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

  • standard: Supports one-stop data analysis features of Simple Log Service. Suitable for scenarios such as real-time monitoring, interactive analysis, and building a complete observability system.

  • query: Supports high-performance queries. The index traffic cost is about half of that of standard, but SQL analysis is not supported. Suitable for scenarios with large data volumes, long storage periods (weekly or monthly), and no log analysis.

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

This parameter requires the logtail-ds image version to be 1.3.1 or later.

  • Customization Requirement 1: Collect Data From Multiple Applications Into The Same Logstore

    To collect data from multiple applications into the same Logstore, set the aliyun_logs_{key}_logstore parameter. For example, the following configuration collects stdout from two applications into stdout-logstore.

    In the example, the {key} for Application 1 is app1-stdout, while for Application 2 it is {key} app2-stdout.

    The environment variables for Application 1 are:

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

    The environment variables for Application 2 are:

    # Specify environment variables
        - name: aliyun_logs_app2-stdout
          value: stdout
        - name: aliyun_logs_app2-stdout_logstore
          value: stdout-logstore
  • Customization Requirement 2: Collect Data From Different Applications Into Different Projects

    To collect data from different applications into multiple projects, follow these steps:

    1. Create a machine group in each project with a custom ID named k8s-group-{cluster-id}, where {cluster-id} is your cluster ID. The machine group name is customizable.

    2. Set the project, logstore, and machine group information in the environment variables for each application. The machine group name is the one you created previously.

      In the example below, the {key} for Application 1 is app1-stdout, while for Application 2 it is {key} app2-stdout. If both applications are deployed within the same ACK cluster, you can utilize a single machine group for both.

      The environment variables for Application 1 are:

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

      The environment variables for Application 2 are:

      # Specify environment variables for 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-side navigation pane, click Clusters.

  2. On the Clusters page, find the cluster that you want to manage and click its name. In the left-side pane, choose Operations > Log Center.

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