Fluentd是一个开源的通用日志采集和分发系统
官方网站:
官方文档: https://docs.fluentd.org/
图片来源:https://docs.fluentd.org/quickstart
Fluentd属于日志的采集和搬运工。它可以从Apache/Nginx等访问日志,前后端的应用日志、数据存储库日志、系统日志中收集日志,日志数据进入Fluentd后可根据配置进行过滤、缓存,路由等最后分发到各种后端系统中。例如告警系统(Nagios)、分析系统(MongoDB、MySQL、Hadoop)、存储系统(Amazon S3)等。
安装
安装准备
安装生产环境需要,需要对操作系统做一些配置,:
- 设置NTP时间同步,保证实时数据的准确性
server ntp.aliyun.com
server ntp.ntsc.ac.cn
server ntp1.aliyun.com
server time.apple.com
首先安装NTP:yum install ntp
修改/etc/ntp.conf如下:
server ntp1.aliyun.com prefer
server ntp2.aliyun.com
手工发起同步:ntpdate ntp1.aliyun.com
启动NTP服务:service ntpd start
设置开机启动:chkconfig ntpd on
检查设置:chkconfig --list ntpd
- 调整允许操作的文件符最大个数
- 优化内核中与网络相关的参数等
配置文件
<source>
@type forward
@id input1
@label @mainstream
port 24224
</source>
<filter **>
@type stdout
</filter>
<label @mainstream>
<match docker.**>
@type file
@id output_docker1
path /fluentd/log/docker.*.log
symlink_path /fluentd/log/docker.log
append true
time_slice_format %Y%m%d
time_slice_wait 1m
time_format %Y%m%dT%H%M%S%z
</match>
<match **>
@type file
@id output1
path /fluentd/log/data.*.log
symlink_path /fluentd/log/data.log
append true
time_slice_format %Y%m%d
time_slice_wait 10m
time_format %Y%m%dT%H%M%S%z
</match>
</label>
docker run -d --name fluentd -p 24224:24224 -v $(pwd)/etc:/fluentd/etc -v $(pwd)/log:/fluentd/log fluent/fluentd
配置信息
<source>
@type http
port 24224
</source>
<match **>
@type stdout
</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事件
<source>
@type forward
port 24224
</source>
输出目的地/match
hello world
创建配置文件 fluent.conf
<source>
@type forward
</source>
<match *>
@type stdout
</match>
docker run -d --name nginx -p 80:80 -v $PWD/nginx/log:/var/log/nginx nginx
执行curl
curl -X GET http://localhost:80
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\" \"-\""}