前面的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中查询容器日志