ELK 中我们使用的是 Filebeat 收集Docker日志,利用的是默认的logging driver json-file。下面使用 fluentd 来收集容器的日志
Fluentd 是一个开源的数据收集器,他目前有超过500中的plugin,可以连接各种数据源和数据输出组件。
Fluentd会负责收集容器日志,然后发送给Elasticsearch。日志的处理流程如下:
用 Filebeat 将 Fluentd 收集到的日志转发给 Elasticsearch。这当然不是唯一的方案,Fluentd 有一个 plugin fluent-plugin-elasticsearch
可以直接将日志发送给 Elasticsearch
安装 Fluentd
运行一个 fluentd 容器
root@host1:/var/log# docker run -d -p 24224:24224 -p 24224:24224/udp -v /data:/fluentd/log fluent/fluentd
fluentd 会在 TCP/UDP 端口 24224 上接收日志数据,日志将保存在 Host 的 /data
目录中。
重新配置 Filebeat ,添加对 /data目录的监控
root@host1:~# vim /etc/filebeat/filebeat.yml
#=========================== Filebeat inputs =============================
filebeat.inputs:
- type: log
enabled: true
paths:
- /data/*.log
root@host1:~# systemctl restart filebeat.service
启动测试容器
docker run --name log-test-container-A -d \
--log-driver=fluentd \
--log-opt fluentd-address=localhost:24224 \
--log-opt tag="log-test-container-A" \
busybox sh -c 'while true; do echo "This is a log message from container A"; sleep 10; done;'
docker run --name log-test-container-B -d \
--log-driver=fluentd \
--log-opt fluentd-address=localhost:24224 \
--log-opt tag="log-test-container-B" \
busybox sh -c 'while true; do echo "This is a log message from container B"; sleep 10; done;'
--log-driver=fluentd
告诉容器使用fluentd的logging driver
--log-opt fluentd-address=localhost:24224
将容器的日志发送到Fluentd的数据接收端口
--log-opt tag="log-test-container-A"
在日志中添加tag,用于区分不同的容器
容器启动后,在Kibana中查询容器日志