All Products
Search
Document Center

Container Service for Kubernetes:Use Log Service to collect application logs

Last Updated:Sep 01, 2023

This topic describes how to use Log Service to collect stdout and log files from application containers in an ACK Serverless cluster.

Prerequisites

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

You can configure Log Service to collect log data from containers when you create an application. You can create applications from images or by using YALM templates.

Method 1: Create an application from an image and configure Log Service to collect application logs

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

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

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

    1. On the Basic Information wizard page, set Name, Replicas, and Type. Then, click Next.

    2. On the Container wizard page, set the parameters in the Log Service section.

      Note

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

      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 log data. If the specified Logstore does not exist, the system automatically creates a Logstore in the 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 log data. 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.

      After you add a configuration entry, the system automatically creates a Logstore. By default, Logtail collects log data in simple mode. In this mode, log data is collected by line. To use more log collection methods, log on to the Log Service console and modify the log collection configurations of the project and Logstore. By default, the project uses k8s-log as the 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 the configuration is complete, click Next. For more information about how to set the remaining parameters, see Create a stateless application by using a Deployment.

Method 2: Create an application from a YAML template and configure Log Service to collect application logs

  1. In the left-side navigation pane of the details page, choose Workloads > Deployments.

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

  3. On the Create page, select a template from the Sample Template drop-down list and modify the template content in the Template section based on your requirements

    The syntax of the YAML template is the same as that of Kubernetes. You can use the env parameter to define log collection configurations and custom tags. You must also set the volumeMounts and volumes parameters. The following code block shows a sample YAML template of a 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:
        ######### 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 more requirements for 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.

        • Environment variable aliyun_logs_log-stdout indicates that a Logstore named log-stdout is created to store the stdout files 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.

        • Environment variable aliyun_logs_log-varlog indicates that a Logstore named log-varlog is created to store the collected log files to the /var/log/*.log path. The name of the collection configuration is log-varlog. This way, text files of the /var/log/*.log file 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.

  4. After you configure the YAML template, click Create.

Step 2: Configure advanced settings in the env field

You can specify various environment variables to configure log collection. The following table describes the variables that are supported.

Important

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

Variable

Description

Example

Considerations

aliyun_logs_{key}

  • This variable is 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.

  • To collect the stdout of the 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
    
       stdout
  • - name: aliyun_logs_access-log
    
       /var/log/nginx/access.log
  • By default, the Log Service component collects log files in simple mode. In this case, the collected log data is not parsed. If you want to parse the log data, we recommend that you change the collection mode in the Log Service console.

  • The value of {key} must be unique in the cluster.

aliyun_logs_{key}_tags

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

- name: aliyun_logs_catalina_tags

   app=catalina

-

aliyun_logs_{key}_project

This variable is optional. This variable specifies a project in Log Service. The default project is the one that you specified when you created the cluster.

- name: aliyun_logs_catalina_project

   my-k8s-project

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

aliyun_logs_{key}_logstore

This variable is optional. This variable specifies a Logstore in Log Service. By default, the Logstore is named {key}.

- name: aliyun_logs_catalina_logstore

   my-logstore

-

aliyun_logs_{key}_shard

This variable is optional. This variable specifies the number of shards in the Logstore. Valid values: 1 to 10. Default value: 2.

- name: aliyun_logs_catalina_shard

   4

-

aliyun_logs_{key}_ttl

This variable is optional. This variable specifies the number of days for which log data is retained. Valid values: 1 to 3650.

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

  • Default value: 90.

- name: aliyun_logs_catalina_ttl

   3650

-

aliyun_logs_{key}_machinegroup

This variable is optional. This variable specifies the node group in which the application is deployed. The default node group is the one in which Logtail is deployed.

- name: aliyun_logs_catalina_machinegroup

   my-machine-group

-

  • Scenario 1: Collect log data from multiple applications and store them in the same Logstore

    In this scenario, set the aliyun_logs_{key}_logstore variable. The following example shows how to collect stdout from two applications and store the output in stdout-logstore.

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

    Configure the following environment variables for Application 1:

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

    ######### Specify environment variables. ###########
        - name: aliyun_logs_app2-stdout
          value: stdout
        - name: aliyun_logs_app2-stdout_logstore
          value: stdout-logstore
  • Scenario 2: Collect log data from different applications and store them in different projects

    In this scenario, perform the following steps:

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

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

      In the following example, the {key} of Application 1 is set to app1-stdout. 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:

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

      Configure the following environment variables for Application 2:

      ######### 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 log data

To view the collected log data in the ACK console, perform the following steps:

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

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

  3. On the Log Center page, click the Application Logs tab and specify the filter conditions. Then, click Select Logstore to view the logs of containers.

    View the container log