# 配置 EMQX Logs 采集
# 任务目标
- 如何通过 Telegraf 收集 EMQX 集群日志,并且输出到容器的标准输出
# 部署 telegraf-operator
Telegraf 是 InfluxData 开发的一个开源数据采集代理,可以收集、处理、聚合和写入指标。 它支持四类插件,包括输入,输出,聚合器,处理器。 更多关于 Telegraf 的文章可以参考:telegraf (opens new window) ,telegraf-operator 的文档可以参考: telegraf-operator (opens new window)
执行如下命令部署 telegraf-operator
helm repo add influxdata https://helm.influxdata.com/ helm upgrade --install telegraf-operator influxdata/telegraf-operator
Copied!
2
# 全局配置 - classes
全局配置通过 secret 挂载,指定 class 名称为 logs
agent
用来配置 telegraf agent,详细定义参考:telegraf agent (opens new window)
inputs.tail
用来配置输入的 tail 插件,详细定义参考:tail (opens new window)
outputs.file
用来配置输出的 file 插件,详细定义参考:file (opens new window)
apiVersion: v1 kind: Secret metadata: name: telegraf-operator-classes namespace: default stringData: logs: |+ [agent] interval = "60s" flush_jitter = "5s" flush_interval = "15s" debug = true quiet = false metric_batch_size = 128 metric_buffer_limit = 256 [[inputs.tail]] files = ["/opt/emqx/log/emqx.log.[1-9]"] from_beginning = false max_undelivered_lines = 64 character_encoding = "utf-8" data_format = "grok" grok_patterns = ['^%{TIMESTAMP_ISO8601:timestamp:ts-"2006-01-02T15:04:05.999999999-07:00"} \[%{LOGLEVEL:level}\] (?m)%{GREEDYDATA:messages}$'] [inputs.tail.tags] collection = "log" [[outputs.file]] files = ["stdout"]
Copied!
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
将上述内容保存为:classes.yaml
- 创建 secret
kubectl apply -f classes.yaml
Copied!
- 检查创建的 secret
kubectl get secret telegraf-operator-classes
Copied!
输出类似于:
NAME TYPE DATA AGE telegraf-operator-classes Opaque 1 11h
Copied!
2
# 部署 EMQX 集群
Telegraf 使用 annotations 的方式为 Pod 注入日志采集的 sidecar ,详细的 annotations 定义参考文档:telegraf annotations (opens new window)
telegraf.influxdata.com/internal
设置为 false, 表示不收集 telegraf agent 自己的指标
telegraf.influxdata.com/volume-mounts
设置日志的挂载路径
telegraf.influxdata.com/class
logs 引用上面指定的 class 的名称
spec.bootstrapConfig
配置输出日志到文件,并且日志级别为 debug
spec.coreTemplate.spec.extraVolumes
和 spec.coreTemplate.spec.extraVolumeMounts
配置日志挂载
apiVersion: apps.emqx.io/v2alpha1 kind: EMQX metadata: name: emqx spec: image: emqx/emqx:5.0.9 bootstrapConfig: | log { file_handlers { my_debug_log { enable = true level = debug file = "log/emqx.log" rotation { enable = true count = 10 } } } } coreTemplate: metadata: name: emqx-core labels: apps.emqx.io/instance: emqx apps.emqx.io/db-role: core annotations: telegraf.influxdata.com/class: "logs" telegraf.influxdata.com/internal: "false" telegraf.influxdata.com/volume-mounts: "{\"log-volume\":\"/opt/emqx/log\"}" spec: replicas: 1 extraVolumes: - name: log-volume emptyDir: {} extraVolumeMounts: - name: log-volume mountPath: /opt/emqx/log
Copied!
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
将上述内容保存为:emqx-telegraf.yaml
- 部署 EMQX 集群
kubectl apply -f emqx-telegraf.yaml
Copied!
- 检查 EMQX 集群状态
kubectl get pods -l apps.emqx.io/instance=emqx
Copied!
输出类似于:
NAME READY STATUS RESTARTS AGE emqx-core-0 2/2 Running 0 54s emqx-replicant-c868c79cd-9m5rw 1/1 Running 0 41s emqx-replicant-c868c79cd-qv8mk 1/1 Running 0 41s emqx-replicant-c868c79cd-z8bvj 1/1 Running 0 41s
Copied!
2
3
4
5
备注: 当 telegraf sidecar 注入到 EMQX core pod 中后,EQMX core pod 中的容器数量会达到2个
- 查看收集到的日志
kubectl logs -f emqx-core-0 -c telegraf
Copied!
采集到的 EMQX log 都输出到了标准输出