Configure application log collection using pod environment variables

更新时间:
复制 MD 格式

Container Compute Service (ACS) is integrated with Alibaba Cloud Simple Log Service (SLS). You can enable the log service when you create an ACS cluster to quickly collect container logs from the cluster, including container stdout and text files in containers. This topic describes how to collect application logs in an ACS cluster by using pod environment variables.

Step 1: Enable the Log Service add-on

You can select Enable Log Service when you create an ACS cluster to automatically enable the Log Service add-on. If you did not select this option during cluster creation, you can follow these steps to enable the add-on for an existing cluster.

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

  2. On the Clusters page, click the name of the target cluster. In the left navigation pane, click Add-ons.

  3. In the Logs and Monitoring section, find alibaba-log-controller. Click Install. In the Install dialog box, click OK.

Important

If you have customized the configuration and environment variables for alibaba-log-controller, the upgrade overwrites these parameters, and you must reconfigure them.

Step 2: Configure log collection during application creation

When you create an application, you can configure it to collect container logs using the console or a YAML file.

Console wizard

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

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

  3. On the Deployments page, select a namespace from the Namespace drop-down list. Then, in the upper-right corner, click Create from Image.

    Note

    This example uses a Deployment, but the process is the same for other workload types, such as StatefulSets.

  4. On the Basic Information tab, set the Name:, Replicas:, and Type. Click Next to go to the Container page.

    Note

    This section describes only the configurations related to Simple Log Service. For information about other application configuration items, see Create a stateless workload (Deployment).

  5. In the Log section, configure the log-related information.

    1. Set the Collection Configuration.

      Click the + icon to create a new collection configuration. Each configuration consists of a Logstore and a Log Path in Container (Can be set to stdout).

      • Logstore: Specify a name for the Logstore to store the collected logs. If the Logstore does not exist, ACS automatically creates it in the Simple Log Service Project associated with the cluster.

        Note

        By default, logs in a newly created Logstore are retained for 90 days.

      • Log Path in Container: Specify the path from which to collect logs. For example, use /usr/local/tomcat/logs/catalina.*.log to collect text logs from Tomcat.

        Note

        If you set this parameter to stdout, the container stdout and stderr are collected.

        Each entry creates a collection configuration for the specified Logstore. By default, logs are collected in simple mode (line by line).

        Example: Set the Logstore name to access and the log path in the container to /usr/local/tomcat/logs/catalina.*.log. Set another Logstore name to catalina and the path to stdout. Note: Make sure that the log agent add-on is deployed to the cluster.

    2. Set a Custom Tag.

      Click the + icon to create a new custom tag. Each custom tag is a key-value pair that is appended to the collected logs. Use these tags to add metadata, such as a version number, to your container logs.

      In the Tag Name field, enter release. In the Tag Value field, enter 1.0.0.

  6. After you complete the configuration, click Next in the upper-right corner.

    For information about subsequent operations, see Create a stateless workload (Deployment).

YAML file

  1. Log on to the ACS 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 the Namespace drop-down list. Then, in the upper-right corner, click Create from YAML.

    Note

    This example uses a Deployment, but the process is the same for other workload types, such as StatefulSets.

  4. Configure the YAML file.

    The syntax of the YAML file is the same as the Kubernetes syntax. To specify a collection configuration for the container, you must use env to add a Collection Configuration and a Custom Tag. The following code provides an example of a simple pod:

    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'
        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
        command: ["sh", "-c"]
        args: ["echo 'Starting my demo app'; sleep 3600"]

    Configure the settings in the following order based on your requirements.

    Note

    For more information about advanced log collection requirements, see Step 3: Advanced log collection parameters.

    1. Use environment variables to create a Collection Configuration and a Custom Tag. All configuration-related environment variables are prefixed with aliyun_logs_.

      • Create a collection configuration.

        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'
            env:
            - name: aliyun_logs_log-stdout
              value: stdout
            - name: aliyun_logs_log-varlog
              value: /var/log/*.log    
            command: ["sh", "-c"]
            args: ["echo 'Starting my demo app'; sleep 3600"]                    

        In the example, two collection configurations are created using the aliyun_logs_{key} format. The corresponding {key} values are log-stdout and log-varlog.

        • aliyun_logs_log-stdout: This variable creates a collection configuration for the container's stdout. Collected logs are sent to a Logstore named log-stdout. The corresponding Simple Log Service collection configuration is also named log-stdout and uses the log-stdout Logstore.

        • aliyun_logs_log-varlog: This environment variable specifies a configuration that creates a Logstore named log-varlog and sets the log collection path to /var/log/*.log. The name of the corresponding Simple Log Service collection configuration is also log-varlog. The purpose of this configuration is to collect the content of /var/log/*.log files from a container and send it to the log-varlog Logstore.

      • Create a custom tag.

        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'
            env:
            - name: aliyun_logs_mytag1_tags
              value: tag1=v1           
            command: ["sh", "-c"]
            args: ["echo 'Starting my demo app'; sleep 3600"]            

        Simple Log Service automatically adds this tag as a field to all logs collected from the container. In this example, the mytag1 part of the variable name can be any name that does not contain an underscore (_).

    2. If you specify a collection path other than stdout, you must also define a corresponding volumeMounts in the pod specification.

      In the example, the collection configuration includes the collection of /var/log/*.log. Therefore, a volumeMounts for /var/log is added accordingly.

  5. After you write the YAML file, click Create to apply the configuration to the ACS cluster.

Step 3: Advanced log collection parameters

You can use various environment variables to configure advanced parameters for specific log collection requirements.

Parameter

Description

Example

Notes

aliyun_logs_{key}

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

  • If you do not set the aliyun_logs_{key}_logstore environment variable, a Logstore named {key} is created by default.

  • A value of stdout collects the container's standard output. Other values specify the log path within the container.

  • - name: aliyun_logs_catalina
      value: stdout
  • - name: aliyun_logs_access-log
      value: /var/log/nginx/access.log
  • The default collection mode is simple mode. To parse log content, we recommend that you use the SLS console.

  • {key} is the name of the collection configuration created by the Log Service add-on and must be unique within the Kubernetes cluster.

aliyun_logs_{key}_tags

Optional. The value must be in the {tag-key}={tag-value} format and helps identify logs.

- name: aliyun_logs_catalina_tags
  value: app=catalina

Not applicable.

aliyun_logs_{key}_project

Optional. The SLS Project. If this environment variable is not set, it defaults to the Project selected during the add-on installation.

- name: aliyun_logs_catalina_project
  value: my-k8s-project

The Project must be in the same region where the Log Service add-on runs.

aliyun_logs_{key}_logstore

Optional. The SLS Logstore. If this environment variable is not set, the Logstore name defaults to the value of {key}.

- name: aliyun_logs_catalina_logstore
  value: my-logstore

Not applicable.

aliyun_logs_{key}_shard

Optional. The number of shards to create for the Logstore. Valid values: 1 to 10. If this environment variable is not set, the default value is 2.

Note

This parameter applies only when a Logstore is created; it does not affect existing Logstores.

- name: aliyun_logs_catalina_shard
  value: 4

Not applicable.

aliyun_logs_{key}_ttl

Optional. The retention period of logs in days. Valid values: 1 to 3650.

  • A value of 3650 specifies that logs are retained permanently.

  • If this environment variable is not set, the default retention period is 90 days.

Note

This parameter applies only when a Logstore is created; it does not affect existing Logstores.

- name: aliyun_logs_catalina_ttl
  value: 3650

Not applicable.

aliyun_logs_{key}_machinegroup

Optional. The machine group for the application. If this environment variable is not set, it defaults to the machine group selected during the Log Service add-on installation. For more information about how to use this parameter, see Special use case 2: Route logs from different applications to different Projects.

- name: aliyun_logs_catalina_machinegroup
  value: my-machine-group

Not applicable.

aliyun_logs_{key}_logstoremode

Optional. The type of the SLS Logstore. If you do not specify this parameter, the default value is standard. Valid values:

Note

This parameter applies only when a Logstore is created; it does not affect existing Logstores.

  • standard: Supports the comprehensive data analysis features of SLS. This type is suitable for scenarios such as real-time monitoring, interactive analysis, and building a complete observability system.

  • query: Supports high-performance queries. Index traffic fees are about half those of the standard type, but it does not support SQL analysis. This type is suitable for scenarios with large data volumes, long retention periods (weeks or months), and no requirement for SQL-based analysis.

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

Not applicable.

  • Use case 1: Collect to a single Logstore

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

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

    Environment variables for Application 1:

    apiVersion: v1
    kind: Pod
    metadata:
      name: my-demo-1
    spec:
      containers:
      - name: my-demo-app
        image: 'registry.cn-hangzhou.aliyuncs.com/log-service/docker-log-test:latest'
        env:    # Configure environment variables
        - name: aliyun_logs_app1-stdout
          value: stdout
        - name: aliyun_logs_app1-stdout_logstore
          value: stdout-logstore
        command: ["sh", "-c"]
        args: ["echo 'Starting my demo app'; sleep 3600"]

    Environment variables for Application 2:

    apiVersion: v1
    kind: Pod
    metadata:
      name: my-demo-2
    spec:
      containers:
      - name: my-demo-app
        image: 'registry.cn-hangzhou.aliyuncs.com/log-service/docker-log-test:latest'
        env:	# Configure environment variables
        - name: aliyun_logs_app2-stdout
          value: stdout
        - name: aliyun_logs_app2-stdout_logstore
          value: stdout-logstore
        command: ["sh", "-c"]
        args: ["echo 'Starting my demo app'; sleep 3600"]
  • Use case 2: Route logs to different Projects

    To collect logs from different applications and route them to different Projects, follow these steps:

    1. In each Project, create a machine group with a custom identifier named k8s-group-{cluster-id}, where {cluster-id} is the cluster ID. The machine group name can be customized.

    2. Configure the Project, Logstore, and machine group information in the environment variables for each application. The machine group name must be the one that you created in the previous step.

      In the following example, the {key} for Application 1 is app1-stdout, and the {key} for Application 2 is app2-stdout. If both applications are in the same Kubernetes cluster, they can use the same machine group.

      Environment variables for Application 1:

      apiVersion: v1
      kind: Pod
      metadata:
        name: my-demo-1
      spec:
        containers:
        - name: my-demo-app
          image: 'registry.cn-hangzhou.aliyuncs.com/log-service/docker-log-test:latest'
          env:	# 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
          command: ["sh", "-c"]
          args: ["echo 'Starting my demo app'; sleep 3600"]

      Environment variables for Application 2:

      apiVersion: v1
      kind: Pod
      metadata:
        name: my-demo-2
      spec:
        containers:
        - name: my-demo-app
          image: 'registry.cn-hangzhou.aliyuncs.com/log-service/docker-log-test:latest'
          env:	# Configure 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
          command: ["sh", "-c"]
          args: ["echo 'Starting my demo app'; sleep 3600"]

Step 4: View logs in the SLS console

This example shows how to view logs for an application created with the console wizard. Once configured, Simple Log Service automatically collects the application logs, which you can then view in the SLS console.

  1. Log on to the SLS console.

  2. In the Projects section, select the Project that corresponds to the ACS cluster. By default, the Project is named k8s-log-{ACS-Cluster-ID}. Then, go to the Logstores tab.

  3. In the list of Logstores, find the Logstore that you specified in the collection configuration. Hover over the Logstore name. On the right side of the Logstore name, click the button icon, and then click Search & Analysis.

    On the log query page, you can view the application stdout logs and text logs from the container. The custom tag appears as a field in the logs.

More information