前面的 ELK 中我們是用 Filebeat 收集 Docker 容器的日志,利用的是 Docker 默認的 logging driver json-file
,本節我們將使用 fluentd
來收集容器的日志。
Fluentd 是一個開源的數據收集器,它目前有超過 500 種的 plugin,可以連接各種數據源和數據輸出組件。在接下來的實踐中,Fluentd 會負責收集容器日志,然后發送給 Elasticsearch。日志處理流程如下:
這里我們用 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