Updated on 2024-04-25 GMT+08:00

Collecting Container Logs Using Cloud Native Logging

Constraints

  • A maximum of 50 log rules can be created for each cluster.
  • Cloud Native Logging cannot collect .gz, .tar, and .zip logs and cannot access symbolic links of logs.
  • If the node storage driver is Device Mapper, container file logs must be collected from the path where the data disk is attached to the node.
  • If the container runtime is containerd, each standard output log cannot be in multiple lines. (This does not apply to Cloud Native Logging v1.3.0 or later.)
  • If a volume is attached to the data directory of a service container, this add-on cannot collect data from the parent directory. In this case, you need to configure a complete data directory.
  • If the lifetime of a container is less than 1 minute, logs cannot be collected in a timely manner. As a result, logs may be lost.

Billing

LTS does not charge you for creating log groups and offers a free quota for log collection every month. You pay only for log volume that exceeds the quota. For details, see Price Calculator.

Log Collection

  1. Enable log collection.

    Enabling log collection during cluster creation

    1. Log in to the CCE console.
    2. Click Buy Cluster from the top menu.
    3. On the Select Add-on page, select Cloud Native Logging.
    4. Click Next: Add-on Configuration in the lower right corner and select the required logs.
      • Container logs: A log collection policy named default-stdout will be created, and standard output logs in all namespaces will be reported to LTS.
      • Kubernetes events: A log collection policy named default-event will be created, and Kubernetes events in all namespaces will be reported to LTS.
    5. Click Next: Confirm Configuration in the lower right corner. On the displayed page, click Submit.

    Enabling log collection for an existing cluster

    1. Log in to the CCE console and click the cluster name to access the details page. In the navigation pane, choose Logging.
    2. (Optional) If you are not authorized, obtain required permissions first.

      In the displayed dialog box, click Authorize.

    3. Click Enable and wait for about 30 seconds until the log page is automatically displayed.

      - Standard output logs: A log collection policy named default-stdout will be created, and standard output logs in all namespaces will be reported to LTS.

      - Kubernetes events: A log collection policy named default-event will be created, and Kubernetes events in all namespaces will be reported to LTS.

  2. View and configure log collection policies.

    1. On the CCE console, click the cluster name to access the details page. In the navigation pane, choose Logging.
    2. Click View Log Collection Policies in the upper right corner.

      All log collection policies reported to LTS are displayed.

      Figure 1 Viewing log collection policies

      If you select Container standard output and Kubernetes events while enabling logging, two log collection policies will be created, and the collected logs will be reported to the default log group and log streams.

      • default-stdout

        Standard output logs are collected to the default log stream stdout-{Cluster ID} in the default log group k8s-logs-{Cluster ID}.

      • default-event

        Kubernetes events are collected to the default log stream event-{Cluster ID} in the default log group k8s-logs-{Cluster ID}.

    3. Click Create Log Policy and configure parameters as required.

      Policy Template: If you do not select Container standard output and Kubernetes events while enabling logging or delete their log collection policies, you can use this option to create default log collection policies.

      Figure 2 Policy template

      Custom Policy: You can use this option to create custom log collection policies.

      Figure 3 Custom policy
      • To avoid log disorder, you are advised to select different log streams for reporting logs in the log collection policies of various log types.
      • The following are requirements for configuring the container and node file log paths:
        • Log directory: Enter an absolute path, for example, /log. The path must start with a slash (/) and contain a maximum of 512 characters. Only uppercase letters, lowercase letters, digits, hyphens (-), underscores (_), slashes (/), asterisks (*), and question marks (?) are allowed.
        • Log file name: It can contain only uppercase letters, lowercase letters, digits, hyphens (-), underscores (_), asterisks (*), question marks (?), and periods (.). Logs in the format of .gz, .tar, and .zip are not supported.

        The directory and file names must be complete and support asterisks (*) and question marks (?) as wildcards. A maximum of three levels of directories can be matched using wildcards. The level-1 directory does not support wildcards. An asterisk (*) can match multiple characters. A question mark (?) can match only one character. For example:

        • If the directory is /var/logs/* and the file name is *.log, any log files with the extension .log in all directories in the /var/logs directory will be reported.
        • If the directory is /var/logs/app_* and the file name is *.log, any log files with the extension .log in all directories that match app_* in the /var/logs directory will be reported.
      Table 1 Custom policy parameters

      Parameter

      Description

      Log Type

      Container standard output: used to collect container standard output logs. You can create a log collection policy by namespace, workload name, or instance label.

      Container file log: used to collect text logs. You can specify a workload or instance label to create a log collection policy.

      Node file log: used to collect logs from a node. Only one file path can be configured for a log collection policy.

      Log Source

      • All containers: You can specify all containers in a namespace. If this parameter is not specified, logs of containers in all namespaces will be collected.
      • Workload: You can specify a workload and its containers. If this parameter is not specified, logs of all containers running the workload will be collected.
      • Workload with target label: You can specify a workload by label and its containers. If this parameter is not specified, logs of all containers running the workload will be collected.
      • Workload: You can specify a workload and its containers. If this parameter is not specified, logs of all containers running the workload will be collected.
      • Workload with target label: You can specify a workload by label and its containers. If this parameter is not specified, logs of all containers running the workload will be collected.

      You also need to specify the log collection path. For details, see the log path configuration requirements.

      Collection Path: used to configure the log collection path. For details, see the log path configuration requirements.

      Log Format

      • Single-line

        Each log contains only one line of text. The newline character \n denotes the start of a new log.

      • Multi-line

        Some programs (for example, Java program) print a log that occupies multiple lines. By default, logs are collected by line. If you want to display logs as a single message, you can enable multi-line logging and use the regular pattern. When you select Multi-line, configure Log Matching Format.

        For example, if logs need to be collected by line and each log starts with a date and occupies three lines, you can set Log Matching Format to the regular expression of the date, for example, \d{4}-\d{2}-\d{2} \d{2}\:\d{2}\:\d{2}.*.

        The three lines starting with the date are regarded as a log.
        2022-01-01 00:00:00 Exception in thread "main" java.lang.RuntimeException: Something has gone wrong, aborting!
        at com.myproject.module.MyProject.badMethod(MyProject.java:22)
        at com.myproject.module.MyProject.oneMoreMethod(MyProject.java:18)

      Report to LTS

      This parameter is used to configure the log group and log stream for log reporting.

      • Default log groups/log streams: The default log group (k8s-log-{Cluster ID}) and default log stream (stdout-{Cluster ID}) are automatically selected.
      • Custom log groups/log streams: You can select any log group and log stream.
        • Log Group: A log group is the basic unit for LTS to manage logs. If you do not have a log group, CCE prompts you to create one. The default name is k8s-log-{Cluster ID}, for example, k8s-log-bb7eaa87-07dd-11ed-ab6c-0255ac1001b3.
        • Log Stream: A log stream is the basic unit for reading and writing logs. You can put different types of logs into different streams to ease management. When you install the add-on or create a log policy based on a template, the following log streams are automatically created:

          - stdout-{Cluster ID} for container logs, for example, stdout-bb7eaa87-07dd-11ed-ab6c-0255ac1001b3

          - event-{Cluster ID} for Kubernetes events, for example, event-bb7eaa87-07dd-11ed-ab6c-0255ac1001b3

    4. Click Edit to modify an existing log collection policy.
    5. Click Delete to delete an existing log collection policy.

  3. View the logs.

    1. On the CCE console, click the cluster name to access the details page. In the navigation pane, choose Logging.
    2. View different types of logs:
      • Container Logs: displays all logs in the default log stream stdout-{Cluster ID} of the default log group k8s-log-{Cluster ID}. You can search for logs by workload.
        Figure 4 Querying container logs
      • Kubernetes Events: displays all Kubernetes events in the default log stream event-{Cluster ID} of the default log group k8s-log-{Cluster ID}.
      • Control Plane Logs: displays all logs of components on the control plane in the default log stream {Component name}-{Cluster ID} of the default log group k8s-log-{Cluster ID}.
      • Control Plane Audit Logs: displays all control plane audit logs in the default log stream audit-{Cluster ID} of the default log group k8s-log-{Cluster ID}.
      • Global Log Query: You can view logs in the log streams of all log groups. You can specify a log stream to view the logs. By default, the default log group k8s-log-{Cluster ID} is selected. You can click the edit icon on the right of Switching Log Groups to switch to another log group.
        Figure 5 Global log query
    3. Click View Log Collection Policies in the upper right corner. Locate the log collection policy and click View Log to go to the log list.
      Figure 6 Viewing logs