Fluentd是一个开源的通用日志采集和分发系统
官方网站:
官方文档: https://docs.fluentd.org/

image.png
图片来源:https://docs.fluentd.org/quickstart
Fluentd属于日志的采集和搬运工。它可以从Apache/Nginx等访问日志,前后端的应用日志、数据存储库日志、系统日志中收集日志,日志数据进入Fluentd后可根据配置进行过滤、缓存,路由等最后分发到各种后端系统中。例如告警系统(Nagios)、分析系统(MongoDB、MySQL、Hadoop)、存储系统(Amazon S3)等。

安装

安装准备

安装生产环境需要,需要对操作系统做一些配置,:

  • 设置NTP时间同步,保证实时数据的准确性
  1. server ntp.aliyun.com
  2. server ntp.ntsc.ac.cn
  3. server ntp1.aliyun.com
  4. server time.apple.com

首先安装NTP:yum install ntp
修改/etc/ntp.conf如下:

  1. server ntp1.aliyun.com prefer
  2. server ntp2.aliyun.com

手工发起同步:ntpdate ntp1.aliyun.com
启动NTP服务:service ntpd start
设置开机启动:chkconfig ntpd on
检查设置:chkconfig --list ntpd

  • 调整允许操作的文件符最大个数
  • 优化内核中与网络相关的参数等

配置文件

  1. <source>
  2. @type forward
  3. @id input1
  4. @label @mainstream
  5. port 24224
  6. </source>
  7. <filter **>
  8. @type stdout
  9. </filter>
  10. <label @mainstream>
  11. <match docker.**>
  12. @type file
  13. @id output_docker1
  14. path /fluentd/log/docker.*.log
  15. symlink_path /fluentd/log/docker.log
  16. append true
  17. time_slice_format %Y%m%d
  18. time_slice_wait 1m
  19. time_format %Y%m%dT%H%M%S%z
  20. </match>
  21. <match **>
  22. @type file
  23. @id output1
  24. path /fluentd/log/data.*.log
  25. symlink_path /fluentd/log/data.log
  26. append true
  27. time_slice_format %Y%m%d
  28. time_slice_wait 10m
  29. time_format %Y%m%dT%H%M%S%z
  30. </match>
  31. </label>
  1. docker run -d --name fluentd -p 24224:24224 -v $(pwd)/etc:/fluentd/etc -v $(pwd)/log:/fluentd/log fluent/fluentd

配置信息

  1. <source>
  2. @type http
  3. port 24224
  4. </source>
  5. <match **>
  6. @type stdout
  7. </match>

输入源source

官方文档 https://docs.fluentd.org/input

type

参数指定使用哪一个输入插件。fluentd支持各种输入插件, 比如:
in_tail
in_forward
in_udp
in_tcp
in_unix
in_http
in_syslog
in_exec
in_dummy
in_windows_eventlog

forward

接收TCP事件

  1. <source>
  2. @type forward
  3. port 24224
  4. </source>

输出目的地/match

hello world

创建配置文件 fluent.conf

  1. <source>
  2. @type forward
  3. </source>
  4. <match *>
  5. @type stdout
  6. </match>
  1. docker run -d --name nginx -p 80:80 -v $PWD/nginx/log:/var/log/nginx nginx

执行curl

  1. curl -X GET http://localhost:80
  1. 2020-08-09 07:38:07.000000000 +0000 nginx: {"container_id":"c4cb2bf69748939674488f60601e9945f5f1c5b1f01021bda38e17dc127393f6","container_name":"/nginx","source":"stdout","log":"172.17.0.1 - - [09/Aug/2020:07:38:07 +0000] \"GET / HTTP/1.1\" 200 612 \"-\" \"curl/7.54.0\" \"-\""}

参考

http://fluentd.org.cn/