前面的 ELK 中我们是用 Filebeat 收集 Docker 容器的日志,利用的是 Docker 默认的 logging driver json-file,本节我们将使用 fluentd 来收集容器的日志。

Fluentd 是一个开源的数据收集器,它目前有超过 500 种的 plugin,可以连接各种数据源和数据输出组件。在接下来的实践中,Fluentd 会负责收集容器日志,然后发送给 Elasticsearch。日志处理流程如下:

444.png



这里我们用 Filebeat 将 Fluentd 收集到的日志转发给 Elasticsearch。这当然不是唯一的方案,Fluentd 有一个 plugin fluent-plugin-elasticsearch 可以直接将日志发送给 Elasticsearch。条条道路通罗马,开源世界给予了我们多种可能性,可以根据需要选择合适的方案。

安装 Fluentd

同样的,最高效的实践方式是运行一个 fluentd 容器。

docker run -d -p 24224:24224 -p 24224:24224/udp -v /data:/fluentd/log fluent/fluentd

fluentd 会在 TCP/UDP 端口 24224 上接收日志数据,日志将保存在 Host 的 /data 目录中。

重新配置 Filebeat

编辑 Filebeat 的配置文件 /etc/filebeat/filebeat.yml,将 /data 添加到监控路径中。

重启 Filebeat。

systemctl restart filebeat.service

监控容器日志

启动测试容器。

docker run -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 -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 告诉 Docker 使用 Fluentd 的 logging driver。

--log-opt fluentd-address=localhost:24224 将容器日志发送到 Fluentd 的数据接收端口。

--log-opt tag="log-test-container-A"  --log-opt tag="log-test-container-B" 在日志中添加一个可选的 tag,用于区分不同的容器。

容器启动后,Kibana 很快就能够查询到容器的日志。

Fluentd 咱们就讨论到这里,下一节开始学习 Graylog。

书籍:
1.《每天5分钟玩转Docker容器技术》
https://item.jd.com/16936307278.html

2.《每天5分钟玩转OpenStack》
https://item.jd.com/12086376.html

posted on 2017-11-08 07:51  CloudMan  阅读(10584)  评论(3编辑  收藏  举报