This topic describes how to create a Logtail configuration in the Simple Log Service console and use the Logtail configuration to collect stdout and stderr from containers in DaemonSet mode.
Prerequisites
The Logtail component is installed. For more information, see Install Logtail components in a Kubernetes cluster.
A Logstore is created in the project that you use to install the Logtail component. For more information, see Create a Logstore.
The container from which you want to collect logs continuously generates logs.
ImportantLogtail collects only incremental logs. If a log file on a server is not updated after the applied Logtail configuration is delivered to the server, Logtail does not collect logs from the file. For more information, see Read log files.
Features
Logtail can collect container stdout and stderr, and then upload the stdout and stderr together with container metadata to Simple Log Service. Logtail supports the following features:
Collects stdout and stderr.
Uses the container label whitelist to specify containers from which logs are collected.
Uses the container label blacklist to specify containers from which logs are not collected.
Uses the environment variable whitelist to specify containers from which logs are collected.
Uses the environment variable blacklist to specify containers from which logs are not collected.
Collects multi-line logs. For example, Logtail can collect Java stack logs.
Automatically associates container metadata that needs to be uploaded together with the collected container logs. The metadata includes container names, image names, pod names, namespaces, and environment variables.
If a container runs in a Kubernetes cluster, Logtail also supports the following features:
Uses Kubernetes namespaces, pod names, and container names to specify containers from which logs are collected.
Uses the Kubernetes label whitelist to specify containers from which logs are collected.
Uses the Kubernetes label blacklist to specify containers from which logs are not collected.
Automatically associates Kubernetes labels that need to be uploaded together with the collected container logs.
Implementation
Logtail communicates with the domain socket of Docker. Logtail queries all Docker containers and identifies the containers from which stdout and stderr must be collected by using the specified labels and environment variables. Then, Logtail runs the docker logs
command to collect the logs of the identified containers.
When Logtail collects stdout and stderr from a container, Logtail periodically stores checkpoints to a checkpoint file. If Logtail is stopped and then started, Logtail collects logs from the last stored checkpoint.
Limits
You can collect stdout and stderr from Kubernetes containers in DaemonSet mode only if Logtail is of V0.16.0 or later and runs on Linux. For more information about Logtail versions and version updates, see Install Logtail on a Linux server.
Logtail can collect data only from the containers that use the Docker engine or containerd engine.
Docker: Logtail accesses the Docker engine in the /run/docker.sock directory. Make sure that the directory exists and Logtail has the permissions to access the directory.
containerd: Logtail accesses the containerd engine in the /run/containerd/containerd.sock directory. Make sure that the directory exists and Logtail has the permissions to access the directory.
By default, the last multi-line log that is collected by Logtail is cached for 3 seconds. This prevents the multi-line log from being split into multiple logs due to output latency. You can modify the
BeginLineTimeoutMs
parameter to change the cache time. We recommend that you do not specify a value that is less than 1000 with millisecond precision. If you specify a value that is less than 1000, an error may occur.If Logtail detects the
die
event on a stopped container, Logtail no longer collects stdout or stderr from the container. If collection latency exists, some stdout and stderr that are collected before the container is stopped may be lost.If a container uses the Docker engine, the logging driver can collect stdout and stderr only in the JSON format.
By default, stdout and stderr that are collected from different containers by using the same Logtail configuration have the same context. In this case, you cannot view the context of stdout and stderr in the Simple Log Service console by using the contextual query feature or LiveTail feature. If you want to view the context in the preceding scenario, you must add aggregator configurations. For more information, see Overview of Logtail plug-ins for data processing.
By default, the collected data is stored in the
content
field. Logtail can process the collected data. For more information, see Overview of Logtail plug-ins for data processing.
Create a Logtail configuration
Form configuration
Log on to the Simple Log Service console. In the Quick Data Import section, click Import Data. In the dialog box that appears, find and click Kubernetes - Stdout and Stderr. Then, follow the on-screen instructions to configure the wizard.
In the Logtail Configuration step of the Import Data wizard, configure the following parameters.
(Recommended) CRD - AliyunPipelineConfig
Only the Logtail components V0.5.1 or later support AliyunPipelineConfig.
To create a Logtail configuration, you need to only create a CR from the AliyunPipelineConfig CRD. After you create the Logtail configuration, the system automatically applies the Logtail configuration.
Obtain the kubeconfig file of a cluster and use kubectl to connect to the cluster.
Run the following command to create a YAML file.
In the following command,
cube.yaml
is a sample file name. You can specify a different file name based on your business requirements.vim cube.yaml
Enter the following script in the YAML file and configure the parameters based on your business requirements.
ImportantYou must configure a CR for each Logtail configuration. If multiple CRs are associated with the same Logtail configuration, the CRs other than the first CR do not take effect.
For more information about the parameters related to the
AliyunPipelineConfig
CRD, see (Recommended) Use AliyunPipelineConfig to manage a Logtail configuration. In the following example, the Logtail configuration includes the basic settings used to collect container stdout and stderr. For more information about the parameters, see CreateLogtailPipelineConfig.Make sure that the
Logstore
specified by theconfig.flushers.Logstore
parameter exists. You can configure thespec.logstore
parameter to automatically create a Logstore.
Run the following command to apply the Logtail configuration. After you apply the Logtail configuration, Logtail starts to collect stdout and stderr from containers and sends the collected stdout and stderr to Simple Log Service.
In the following command,
cube.yaml
is a sample file name. You can specify a different file name based on your business requirements.kubectl apply -f cube.yaml
ImportantAfter logs are collected, you must create indexes. Then, you can query and analyze the logs in the Logstore. For more information, see Create indexes.
CRD - AliyunLogConfig
To create a Logtail configuration, you need to only create a CR from the AliyunLogConfig CRD. After the Logtail configuration is created, the system automatically applies the Logtail configuration.
Obtain the kubeconfig file of a cluster and use kubectl to connect to the cluster.
Run the following command to create a YAML file. In the following command,
cube.yaml
is a sample file name. You can specify a different file name based on your business requirements.vim cube.yaml
Enter the following script in the YAML file and configure the parameters based on your business requirements.
ImportantThe value of the
configName
parameter must be unique in the Simple Log Service project that you used to install the Logtail components.If multiple CRs are associated with the same Logtail configuration, the Logtail configuration is affected when you delete or modify one of the CRs. After a CR is deleted or modified, the status of the other associated CRs becomes inconsistent with the status of the Logtail configuration in Simple Log Service.
For more information about CR parameters, see Use AliyunLogConfig to manage a Logtail configuration. In the following example, the Logtail configuration includes the basic settings used to collect container stdout and stderr. For more information about the parameters, see CreateConfig.
Collect stdout and stderr in single-line mode from specific containers
In this example, a Logtail configuration named
example-stdout-example
is created to collect stdout and stderr in single-line mode from the containers of all the pods whose names begin withapp
in a Kubernetes cluster. The collected logs are stored in a Logstore namedk8s-stdout
, which belongs to a project namedk8s-log-clusterid
.# Configure the collection of stdout and stderr. apiVersion: log.alibabacloud.com/v1alpha1 kind: AliyunLogConfig metadata: # Specify the name of the resource. The name must be unique in the current Kubernetes cluster. name: simple-stdout-example spec: # Specify the name of the Logstore. If the specified Logstore does not exist, Simple Log Service automatically creates a Logstore. logstore: k8s-stdout # Configure the parameters for the Logtail configuration. logtailConfig: # Specify the type of the data source. If you want to collect container stdout and stderr, set the value to plugin. inputType: plugin # Specify the name of the Logtail configuration. The name must be the same as the resource name specified by the metadata.name parameter. configName: simple-stdout-example inputDetail: plugin: inputs: - # Input type type: service_docker_stdout detail: # Collect stdout and stderr. Stdout: true Stderr: true K8sPodRegex: "^(app.*)$"
Run the following command to apply the Logtail configuration. After you apply the Logtail configuration, Logtail starts to collect stdout and stderr from containers and sends the collected stdout and stderr to Simple Log Service.
In the following command,
cube.yaml
is a sample file name. You can specify a different file name based on your business requirements.kubectl apply -f cube.yaml
ImportantAfter logs are collected, you must create indexes. Then, you can query and analyze the logs in the Logstore. For more information, see Create indexes.
Preview container metadata
After you create a Logtail configuration, you can view container metadata and the reason why specific containers failed to match the filter conditions on the Logtail Configuration page.
Only Linux Logtail V1.4.0 and later or Windows Logtail V1.4.0.0 and later support this feature.
Log on to the Simple Log Service console.
In the Projects section, click the project that you want to manage.
In this example, click the project that you used to create the Logtail configuration.
On the tab, click > to the left of the Logstore that you want to manage, and choose .
In this example, click the Logstore that you used to create the Logtail configuration.
In the Logtail configuration list, click the Logtail configuration that you created.
On the Logtail Configuration page, click Edit.
Turn on Enable Container Metadata Preview.
Click Container Metadata Preview.
In the Container Preview dialog box, view container metadata.
The Matched Containers tab displays the metadata of the containers that match the specified filter conditions.
The All Containers tab displays the metadata of all containers in the current Kubernetes cluster and the reason why specific containers failed to match the filter conditions.
Log fields
The following table describes the fields that are uploaded by default for each log in a Kubernetes cluster.
Field | Description |
_time_ | The time when the log was collected. Example: |
_source_ | The type of the log source. Valid values: stdout and stderr. |
_image_name_ | The name of the image. |
_container_name_ | The name of the container. |
_pod_name_ | The name of the pod. |
_namespace_ | The namespace of the pod. |
_pod_uid_ | The unique identifier of the pod. |
Troubleshooting
If an exception occurs when you use Logtail to collect logs from containers, such as standard containers and Kubernetes containers, you can troubleshoot the issue based on the following topics: