This topic describes how to use an SLS CRD to configure log collection in an ACK Serverless cluster, enabling automatic container log collection with Simple Log Service (SLS).
Background information
Simple Log Service is a one-stop service for log data. It lets you quickly collect, consume, ship, query, and analyze log data without writing code. For more information, see What is Simple Log Service?.
Prerequisites
Ensure that Simple Log Service is activated. If the service is not activated, you will be prompted to activate it when you log on to the Simple Log Service console. Follow the on-screen instructions to complete the activation.
Notes
Log collection enabled by an SLS CRD applies only to subsequently created ECI pods. To collect logs from existing pods, you must perform a rolling update.
Configure log collection
After deploying the alibaba-log-controller component in your cluster, you can use the AliyunLogConfig CRD to configure log collection.
Deploy the alibaba-log-controller component
-
Log on to the ACK console.
-
In the upper-left corner of the top navigation bar, select a region.
-
On the Clusters page, click the name of the target ACK Serverless cluster to open the cluster details page.
-
Deploy the alibaba-log-controller component in the cluster.
-
In the left-side navigation pane of the cluster details page, choose .
-
Click the Logs and Monitoring tab, find the alibaba-log-controller card, and then click Install.
-
In the dialog box that appears, click OK.
After the installation is complete, the alibaba-log-controller card displays Installed in the upper-right corner.
-
Create a Logtail configuration
-
Connect to the ACK Serverless cluster.
-
Create a YAML configuration file for the AliyunLogConfig CRD.
ImportantTo avoid configuration drift, always update the configuration by modifying the CRD resource. Changes made directly in the console are not synchronized back to the CRD.
-
Create the AliyunLogConfig CRD.
Run the following commands to apply the configurations. After the Logtail configuration takes effect, Logtail starts to collect stdout or text logs from matching containers and sends them to Simple Log Service.
kubectl apply -f log-file.yaml kubectl apply -f log-stdout.yamlImportantAfter logs are collected, you must create an index before you can query and analyze logs in the Logstore. For more information, see Create Index.
Test log collection
After you create the AliyunLogConfig CRD, Simple Log Service automatically collects logs from newly created pods. You can create the following application to test log collection.
-
Create an application.
The following example uses a Deployment. Once started, the container continuously prints logs to stdout and a log file.
apiVersion: apps/v1 kind: Deployment metadata: name: eci-sls-demo labels: app: sls spec: replicas: 1 selector: matchLabels: app: sls template: metadata: name: sls-test labels: app: sls alibabacloud.com/eci: "true" spec: containers: - args: - -c - mkdir -p /log;while true; do echo hello world; date; echo hello sls >> /log/busy.log; sleep 1;done command: - /bin/sh image: registry-vpc.cn-beijing.aliyuncs.com/eci_open/busybox:1.30 imagePullPolicy: Always name: busyboxSave the preceding YAML file as test-sls-crd.yaml and run the following command to create the application.
kubectl create -f test-sls-crd.yaml -
Verify the application status.
kubectl get podExpected output:
NAME READY STATUS RESTARTS AGE eci-sls-demo-7bf8849b9f-cgpbn 1/1 Running 0 2m14s -
View the logs.
-
Log on to the Simple Log Service console.
-
Click the name of the destination project.
-
Find the destination Logstore and click its name to view the logs.
Text logs
In the Simple Log Service console, select the test-file Logstore to view the collected file logs. The log entries include key fields such as
__container_name__with the valuesls,__image_name__with the valueregistry-vpc.cn-beijing.aliyuncs.com/eci_open/busybox:1.30,__namespace__with the valuedefault, andcontentwith the valuehello sls, confirming that logs from the busybox container were successfully collected.Standard output
In the Simple Log Service console, select the test-stdout Logstore from the list on the left to view the collected stdout logs. The log entries contain fields like
_container_name_(value:sls),_image_name_(the busybox image URL),_namespace_(default), and_source_(stdout). Thecontentfield displays the container output, such ashello world.
-
Disable log collection
After you create a log collection configuration CRD, the system automatically collects logs from all matching Pods. For Pods that you do not want to collect logs from, you can add the k8s.aliyun.com/eci-sls-enable: "false" annotation to disable log collection. This prevents the system from automatically creating Logtail instances and avoids wasting resources.
Annotations must be added to the metadata in the configuration file of the pod. For example, when you create a Deployment, you must add annotations in the spec.template.metadata section.
Elastic Container Instance-related annotations are only applied when a pod is created. Adding or modifying these annotations on an existing pod will have no effect.
Example:
apiVersion: apps/v1
kind: Deployment
metadata:
name: eci-sls-demo2
labels:
app: sls
spec:
replicas: 1
selector:
matchLabels:
app: sls
template:
metadata:
name: sls-test
labels:
app: sls
alibabacloud.com/eci: "true"
annotations:
k8s.aliyun.com/eci-sls-enable: "false" # Disable log collection.
spec:
containers:
- args:
- -c
- mkdir -p /log;while true; do echo hello world; date; echo hello sls >> /log/busy.log; sleep 1;
done
command:
- /bin/sh
image: registry.cn-shanghai.aliyuncs.com/eci_open/busybox:1.30
imagePullPolicy: Always
name: busybox