CLS supports log collection for self-built Kubernetes clusters. Before performing log collection on a self-built Kubernetes cluster, you need to use a custom resource definition (CRD) to define log collection configuration (LogConfig), and deploy Log-Provisioner, Log-Agent, and LogListener on the cluster. If you are a Tencent Kubernetes Engine (TKE) user, you can quickly access and use the CLS service by referring to Enabling log collection.
topicId
).CLS_HOST
) of the region of your log topic.TmpSecretId
) and API key (TmpSecretKey
) required for CLS authentication.Log collection on a Kubernetes cluster requires the following:
Define the LogConfig resource type using a Kubernetes CRD.
Run the wget command to download the CRD.yaml
declaration file and run the kubectl command to define the LogConfig resource type, using the master node path /usr/local/
as an example.
wget https://mirrors.tencent.com/install/cls/k8s/CRD.yaml
kubectl create -f /usr/local/CRD.yaml
Create a LogConfig object to define log collection configuration. Run the wget command to download the LogConfig.yaml
declaration file, using the master node path /usr/local/
as an example.
wget https://mirrors.tencent.com/install/cls/k8s/LogConfig.yaml
The LogConfig.yaml
declaration file consists of the following two parts:
topicId
).Note:During configuration, change the
topicId
item inclsDetail
to the ID of the log topic that you created.
In "full text in a single line" mode, a line is a full log. When CLS collects logs, it uses the line break \n
to mark the end of a log. For easier structural management, a default key value \_\_CONTENT\_\_
is given to each log, but the log data itself will no longer be structured, nor will the log field be extracted. The time attribute of a log is determined by the collection time.
Assume that the raw data of a log is as follows:
Tue Jan 22 12:08:15 CST 2019 Installed: libjpeg-turbo-static-1.2.90-6.el7.x86_64
A sample of LogConfig configuration is as follows:
apiVersion: cls.cloud.tencent.com/v1
kind: LogConfig
spec:
clsDetail:
topicId: xxxxxx-xx-xx-xx-xxxxxxxx
# Single-line log
logType: minimalist_log
The data collected to CLS is as follows:
__CONTENT__:Tue Jan 22 12:08:15 CST 2019 Installed: libjpeg-turbo-static-1.2.90-6.el7.x86_64
CLS supports the following cluster log sources:
Sample 1: collecting the standard output of all containers in the default namespace
apiVersion: cls.cloud.tencent.com/v1
kind: LogConfig
spec:
inputDetail:
type: container_stdout
containerStdout:
namespace: default
allContainers: true
...
Sample 2: collecting the container standard output in the Pod that belongs to ingress-gateway deployment in the production namespace
apiVersion: cls.cloud.tencent.com/v1
kind: LogConfig
spec:
inputDetail:
type: container_stdout
containerStdout:
allContainers: false
workloads:
- namespace: production
name: ingress-gateway
kind: deployment
...
Sample 3: collecting the container standard output in the Pod whose Pod labels contain "k8s-app=nginx" in the production namespace
apiVersion: cls.cloud.tencent.com/v1
kind: LogConfig
spec:
inputDetail:
type: container_stdout
containerStdout:
namespace: production
allContainers: false
includeLabels:
k8s-app: nginx
...
Since the LogConfig.yaml
declaration file is defined in Step 2. Define the LogConfig object, you can run the kubectl command to create a LogConfig object.
kubectl create -f /usr/local/LogConfig.yaml
To upload logs from a self-built Kubernetes cluster to CLS, you need to create a ConfigMap for storing the API key ID and API key.
ConfigMap.yaml
declaration file, using the master node path /usr/local/
as an example.wget https://mirrors.tencent.com/install/cls/k8s/ConfigMap.yaml
Note:During configuration, set TmpSecretId and TmpSecretKey in
ConfigMap.yaml
to your API key ID and API key respectively.
2. Run the kubectl command to create a ConfigMap object.
kubectl create -f /usr/local/ConfigMap.yaml
Log-Provisioner discovers and listens for CLS consumer information, log collection rules, and log file paths from LogConfig resources and synchronizes them to CLS.
Log-Provisioner.yaml
declaration file, using the master node path /usr/local/
as an example.wget https://mirrors.tencent.com/install/cls/k8s/Log-Provisioner.yaml
Note:During configuration, set the
env
environment variable field CLS_HOST inLog-Provisioner.yaml
to the domain name of the region where the target log topic belongs. For the domain names of different regions, see Available Regions. In addition, theenv
environment variable field CLUSTER_ID must be set to any name that is different from the names of all machine groups under your account. You can view all machine groups under your account on the machine group management page in the CLS console.
kubectl create -f /usr/local/Log-Provisioner.yaml
Cluster log collection requires two components:
/usr/local/
as an example.wget https://mirrors.tencent.com/install/cls/k8s/Log-Agent.yaml
Note:
- During configuration, set the
env
environment variable field CLS_HOST inLog-Agent.yaml
to the domain name of the region where the target log topic belongs. For the domain names of different regions, see Available Regions. In addition, theenv
environment variable field CLUSTER_ID must be set to any name that is different from the names of all machine groups under your account. You can view all machine groups under your account on the machine group management page in the CLS console.- If the docker root directory of the host is not under
/var/lib/docker
(root directory of the host), you need to map the docker root directory to the container in theLog-Agent.yaml
declaration file. As shown in the following figure, mount/data/docker
to the container.
- Run the kubectl command to deploy Log-Agent and LogListener in DaemonSet mode.
kubectl create -f /usr/local/Log—Agent.yaml
After the deployment for cluster log collection is completed, you can go to CLS console > Search and Analysis to view collected logs.
Was this page helpful?