All Products
Search
Document Center

:Configure application log collection using pod environment variables

Last Updated:Aug 29, 2024

Alibaba Cloud Container Compute Service (ACS) integrates Alibaba Cloud Simple Log Service. You can enable Simple Log Service when creating a cluster to quickly collect container logs from Kubernetes clusters, which include the standard output of containers and text files within containers. This topic describes how to collect application logs by using pod environment variables.

Step 1: Enable the Logtail component of Simple Log Service

You can enable the Logtail component for an existing cluster.

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

  2. On the Clusters page, click the ID of the cluster that you want to manage or click Details in the Actions column of the cluster.

  3. On the Clusters page, find the cluster that you want to manage and click its ID. In the left-side navigation pane, choose Operations > Add-ons.

  4. On the Add-ons page, find alibaba-log-controller in the Logs and Monitoring section.

  5. Click Install on the right side of the alibaba-log-controller component, and click OK in the Install alibaba-log-controller dialog box.

If the installed Simple Log Service component is an earlier version, you can click Upgrade on the right side of the alibaba-log-controller component.

Important

After upgrading the alibaba-log-controller component, the component parameters are reset. If you have customized the configuration and environment variables of the alibaba-log-controller, the parameters are overwritten. Please reconfigure them.

Step 2: Configure Simple Log Service when creating an application

You can configure Simple Log Service while creating an application to collect container logs. Currently, you can configure it through the console and YAML templates.

Configure through the console

  1. Log on to the ACS 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. On the Deployments tab, click Create from Image.

    Note

    This is a Deployments application example. The configuration method is the same for other types of workloads

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

    The following section only introduces the configuration related to Simple Log Service. For other application configurations, see Create a stateless application by using a 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 collection configuration consists of two items: Logstore and Log Path in Container.

      • Logstore: Configure the Logstore name to specify where the collected logs are stored. If the Logstore does not exist, ACS automatically creates the corresponding Logstore in the Simple Log Service Project associated with the cluster.

        Note

        The default retention period for logs in the newly created Logstore is 180 days.

      • Log path in a container: Specify the path where the logs to be collected are located, such as using /usr/local/tomcat/logs/catalina.*.log to collect Tomcat text logs.

        Note

        When specified as stdout, it means collecting the standard output and standard error output of containers.

        Each collection configuration is automatically corresponds to a Logstore. By default, the minimal mode (by line) is used. If you need more collection methods, see Use the Simple Log Service console to collect container text logs in DaemonSet mode and Use the Simple Log Service console to collect container stdout and stderr in DaemonSet mode.

      采集配置

    2. Set the 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. You can use it to tag the log data of the container, such as the version number.

      自定义tag

  6. After completing all configurations, click Next to proceed to the next step.

    For subsequent operations, see Create a stateless application by using a Deployment.

Using YAML templates

  1. Log on to the Container Compute Service (ACS) Management Console, and select Clusters from the left navigation bar.

  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 Stateless page, set the namespace in the Namespace drop-down box at the top, and click Create Using YAML in the upper right corner of the page. Note: This example uses a stateless application. The configuration method is the same for other types of workloads (e.g., stateful).

  4. Configure the YAML file.

    The syntax of the YAML template is the same as Kubernetes syntax, but to specify the collection configuration for the container, you need to use env to add Collection Configuration and Custom Tag for the container. Below is a simple pod example:

    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

    Configure according to your needs in the following order.

    Note

    If you have more log collection requirements, see Step 3: Advanced parameter settings for log collection.

    1. Create your Collection Configuration and Custom Tag through environment variables. All environment variables related to configuration use aliyun_logs_ as the prefix.

      • The rules for creating collection configurations are as follows:

        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                        

        The example creates two collection configurations. The format is aliyun_logs_{key}, and the corresponding {key} are log-stdout and log-varlog.

        • aliyun_logs_log-stdout: This env indicates the creation of a log-stdout Logstore with a log collection path of stdout. The corresponding Simple Log Service collection configuration name is also log-stdout. The purpose is to collect the container's standard output into the log-stdout Logstore.

        • aliyun_logs_log-varlog: This env indicates the creation of a Logstore named log-varlog with a log collection path of /var/log/*.log. The corresponding Simple Log Service collection configuration name is also log-varlog. The purpose is to collect the content of the /var/log/*.log files in the container into the log-varlog Logstore.

      • The rules for creating Custom Tags are as follows:

        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                       

        After configuring the tag, when the logs of the container are collected, the corresponding fields will be automatically appended to the logs in Simple Log Service. The mytag1 is an arbitrary name that does not contain '_'.

    2. If your collection configuration specifies a non-stdout collection path, you need to create the corresponding volumeMounts in this section.

      In the example, the collection configuration adds the collection of /var/log/*.log, so the corresponding volumeMounts for /var/log is added.

  5. After writing the YAML, click Create to submit the corresponding configuration to the Kubernetes cluster for execution.

Step 3: Advanced parameter settings for log collection

Log collection supports multiple configuration parameters by using container environment variables . You can set advanced parameters according to actual needs to meet special log collection requirements.

Important

Log collection through container environment variables is not suitable for edge scenarios.

Environment Variable Name

Description

Example

Notes

aliyun_logs_{key}

  • Required. {key} can only contain lowercase letters, numbers, and -.

  • If aliyun_logs_{key}_logstore does not exist, it is created by default and collected into a logstore named {key}.

  • When the value is stdout, it means collecting the standard output of container; other values are the log paths inside the container.

  • - name: aliyun_logs_catalina
    
      value: stdout
  • - name: aliyun_logs_access-log
    
      value: /var/log/nginx/access.log

aliyun_logs_{key}_tags

Optional. The value is the type of the {tag-key}={tag-value}, used to tag the logs.

- name: aliyun_logs_catalina_tags

  value: app=catalina

Not involved.

aliyun_logs_{key}_project

Optional. The value is the specified Simple Log Service project. If this environment variable does not exist, it defaults to the project selected during installation.

- name: aliyun_logs_catalina_project

  value: my-k8s-project

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

aliyun_logs_{key}_logstore

Optional. The value is the specified Simple Log Service logstore. If this environment variable does not exist, the logstore and {key} are the same.

- name: aliyun_logs_catalina_logstore

  value: my-logstore

Not involved.

aliyun_logs_{key}_shard

Optional. The value is the number of shards when creating the logstore, ranging from [1, 10]. If this environment variable does not exist, the value is 2.

Note

If the Logstore already exists, this parameter does not take effect.

- name: aliyun_logs_catalina_shard

  value: 4

Not involved.

aliyun_logs_{key}_ttl

Optional. The value is the specified log retention period, ranging from [1, 3650].

  • When the value is 3650, the log retention period is specified as permanent.

  • If this environment variable does not exist, the default log retention period is 90 days.

Note

If the logstore already exists, this parameter does not take effect.

- name: aliyun_logs_catalina_ttl

  value: 3650

Not involved.

aliyun_logs_{key}_machinegroup

Optional. The value is the machine group of the application. If this environment variable does not exist, it defaults to the machine group where Logtail is installed. For detailed usage of this parameter, see Special Scenario 2: Collect data from different applications into different projects.

- name: aliyun_logs_catalina_machinegroup

  value: my-machine-group

Not involved.

aliyun_logs_{key}_logstoremode

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

Note

If the logstore already exists, this parameter does not take effect.

  • standard: Supports Simple Log Service one-stop data analysis functions, suitable for real-time monitoring, interactive analysis, and building a complete observability system.

  • query: Supports high-performance queries. The index traffic fee is about half of standard. It does not support SQL analysis. 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 

Not involved.

  • Special Scenario 1: Collect data from multiple applications into the same Logstore

    If you need 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 stdout from two applications into stdout-logstore.

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

    The environment variables for application 1 are set as follows:

    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_app1-stdout
          value: stdout
        - name: aliyun_logs_app1-stdout_logstore
          value: stdout-logstore

    The environment variables for application 2 are set as follows:

    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_app2-stdout
          value: stdout
        - name: aliyun_logs_app2-stdout_logstore
          value: stdout-logstore
  • Special Scenario 2: Collect data from different applications into different projects

    If you need to collect data from different applications into multiple projects, you need to perform the following operations:

    1. Create a machine group in each project, select a custom ID, and name it k8s-group-{cluster-id}, where {cluster-id} is your cluster ID. The machine group name can be customized.

    2. Configure the project, Logstore, and machine group information in the environment variables of each application. The machine group name is the one 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 the two applications are in the same Kubernetes cluster, the corresponding machine group can use the same machine group.

      The environment variables for application 1 are set as follows:

      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_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 set as follows:

      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_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 4: View logs

This example shows how to view the logs of a Tomcat application created through the console. After configuration, the logs of the Tomcat application have been collected and stored in Simple Log Service. You can view the container logs in the Simple Log Service console.

View logs by using Simple Log Service console

  1. After the installation is successful, go to the Simple Log Service console.

  2. After entering the console, select the project corresponding to the Kubernetes cluster (default: k8s-log-{Kubernetes cluster ID}) in the Projects area, click and go to the Logstore.

  3. Find the corresponding Logstore (specified in the collection configuration) in the list, hover the mouse over the right side of the Logstore name, click the button icon, and click Search & Analysis.

    In this example, you can view the standard output logs and text logs inside the container of the Tomcat application on the log query page, and you can find custom tags appended to the log fields.

More information