本文共 6577 字,大约阅读时间需要 21 分钟。
点击下方公众号「关注」和「星标」
回复“1024”获取独家整理的学习资料!
关于日志收集、处理、分析的方案,其实是很多,常见的就是ELK组合,即:,官方网站:https://www.elastic.co/products
后面随着架构的优化与演进,又引入另一个轻量级的组件 ,Filebeat和Logstash一样属于日志收集处理工具,基于原先 Logstash-fowarder 的源码改造出来的。与Logstash相比,filebeat更加轻量,占用资源更少。
Logstash 和Fluentd相比,它在效能上表现略逊一筹,故而逐渐被fluentd取代,ELK也随之变成EFK。EFK由ElasticSearch、Fluentd和Kiabana三个开源工具组成,这三款开源工具的组合为日志数据提供了分布式的实时搜集与分析的监控系统。
Fluentd 简介
Fluentd 是一个免费,而且完全开源的日志管理工具,简化了日志的收集、处理、和存储,你可以不需要在维护编写特殊的日志处理脚本。
特性介绍
使用json来记录log
Fluentd使用Json来结构化数据,这让Fluentd统一了数据处理果层,包括日志收集,过滤,并输出日志缓冲(多个源和目标),这使得下游数据处理也变的容易得多。
插件式体系结构
Fluentd具有灵活的插件系统,让社区来扩展它的功能。我们的300 +社区贡献的插件可以连接几十个数据源和数据输出。通过使用插件,你可以充分利用你的日志。目前开源社区已经贡献了下面一些存储插件:MongoDB, Redis, CouchDB,Amazon S3, Amazon SQS, Scribe, 0MQ, AMQP, Delayed, Growl 等等。
最小所需资源
Fluentd 使用C和Ruby语言编写,仅需要很少的系统资源,一个运行再30-40MB内存的实例单核每秒可以处理13000次事件。
可靠性
fluentd支持基于内存或文件的数据缓冲,以防止数据丢失。fluentd还有强大的容错性,并且可设置高可用性。2000 +的数据驱动的企业依靠fluentd,通过他们日志数据的理解和使用来提供更好的产品和服务。
安装
https://docs.fluentd.org/installation/install-by-rpm
Centos 系统
curl -L https://toolbelt.treasuredata.com/sh/install-redhat-td-agent3.sh | sh
启动服务
systemctl start td-agent
更多平台的安装方式:https://docs.fluentd.org/installation
默认启动是通过td-agent用户启动的,如果需要修改成其它用户,使用下面的方法:
[root@centos7 ~]# vim /usr/lib/systemd/system/td-agent.service[Unit]Description=td-agent: Fluentd based data collector for Treasure DataDocumentation=https://docs.treasuredata.com/articles/td-agentAfter=network-online.targetWants=network-online.target[Service]User=td-agent #用户Group=td-agent #用户组LimitNOFILE=65536Environment=LD_PRELOAD=/opt/td-agent/embedded/lib/libjemalloc.soEnvironment=GEM_HOME=/opt/td-agent/embedded/lib/ruby/gems/2.4.0/Environment=GEM_PATH=/opt/td-agent/embedded/lib/ruby/gems/2.4.0/Environment=FLUENT_CONF=/etc/td-agent/td-agent.confEnvironment=FLUENT_PLUGIN=/etc/td-agent/pluginEnvironment=FLUENT_SOCKET=/var/run/td-agent/td-agent.sockEnvironment=TD_AGENT_LOG_FILE=/var/log/td-agent/td-agent.logEnvironment=TD_AGENT_OPTIONS=EnvironmentFile=-/etc/sysconfig/td-agentPIDFile=/var/run/td-agent/td-agent.pidRuntimeDirectory=td-agentType=forkingExecStart=/opt/td-agent/embedded/bin/fluentd --log $TD_AGENT_LOG_FILE --daemon /var/run/td-agent/td-agent.pid $TD_AGENT_OPTIONSExecStop=/bin/kill -TERM ${MAINPID}ExecReload=/bin/kill -HUP ${MAINPID}Restart=alwaysTimeoutStopSec=120
配置文件介绍
配置文件目录:/etc/td-agent/td-agent.conf
[root@centos7 ~]# cd /etc/td-agent/[root@centos7 td-agent]# lltotal 4drwxr-xr-x 2 root root 6 Jun 4 05:15 plugin-rw-r--r-- 1 root root 2381 Jun 4 05:15 td-agent.conf
可以通过下面的命令来检测配置的配置是否正确
[root@centos7 ~]# /opt/td-agent/embedded/bin/fluentd -c /etc/td-agent/td-agent.conf
配置文件包含以下指令:
source #输入源,数据的来源match #确定输出目的地filter #确定事件处理管道system #设置系统范围的配置 label #对内部路由的输出和过滤器进行分组 @include #包括其他文件
官方文档:https://docs.fluentd.org/configuration
配置文件中还包括 fluentd 支持的数据格式,有下列这些:
string:字符串,最常见的格式integer:整数float:浮点数size 大小,仅支持整数k 或 K; m 或 M; g 或 G; t 或 T。time:时间,也只支持整数; s 或 S; m 或 M; h 或 H; d 或 D。array:按照 JSON array 解析;hash:按照 JSON object 解析。
去除注释之后的完整配置文件
[root@centos7 ~]# egrep -v "^#|^$" /etc/td-agent/td-agent.conf@type tdlog @id output_td apikey YOUR_API_KEY auto_create_table @type file path /var/log/td-agent/buffer/td @type file path /var/log/td-agent/failed_records @type stdout @id output_stdout
官方也给出一个简单有demo,如下
[root@centos7 ~]# netstat -utpln |grep rubytcp 0 0 0.0.0.0:8888 0.0.0.0:* LISTEN 7008/ruby tcp 0 0 0.0.0.0:24224 0.0.0.0:* LISTEN 7013/ruby tcp 0 0 127.0.0.1:24230 0.0.0.0:* LISTEN 7013/ruby udp 0 0 0.0.0.0:24224 0.0.0.0:* 7013/ruby
通过8888端口提交一条测试日志并查看
[root@centos7 ~]# curl -X POST -d 'json={"json":"message"}' http://localhost:8888/debug.test[root@centos7 ~]# tail -n 1 /var/log/td-agent/td-agent.log2021-06-04 05:56:11.728512891 -0400 debug.test: {"json":"message"}
插件介绍及安装
插件介绍
Fluentd 常用插件如下:
Input:完成输入数据的读取,由source部分配置常用类型:tail、http、forward、tcp、udp、exechttps://docs.fluentd.org/inputParser:解析插件,常与输入、输处配合使用,多见于format字段后面常用类型:ltsv、json、自定义等https://docs.fluentd.org/parserOutput:完成输出数据的操作,由match部分配置常用配置:file、forward、copy、stdout、exechttps://docs.fluentd.org/outputfilter:过滤插件常用配置:grep、ignore、record_transformerhttps://docs.fluentd.org/filterBuffer:缓存插件,用于缓存数据常用配置:file、memhttps://docs.fluentd.org/bufferFormatter:消息格式化的插件,用于输出,允许用户扩展和重新使用自定义输出格式常用类型:ltsv、json等https://docs.fluentd.org/formatter
安装
官方也有详细的介绍,不做过多的赘述,比较简单
文档:https://docs.fluentd.org/deployment/plugin-management
简单的应用实例
解析系统日志
[root@centos7 ~]# vim /etc/rsyslog.conf #增加下面的配置行*.* @127.0.0.1:5140
重启服务
[root@centos7 ~]# systemctl restart rsyslog[root@centos7 ~]# ps -ef|grep rsysroot 7492 1 0 06:20 ? 00:00:00 /usr/sbin/rsyslogd -nroot 7497 6893 0 06:20 pts/0 00:00:00 grep --color=auto rsys
配置Fluentd
[root@centos7 td-agent]# vim td-agent.conf#增加下面的配置行@type syslog port 5140 tag system @type stdout
重启服务
[root@centos7 td-agent]# systemctl restart td-agent
查看收集的日志
2021-06-04 06:40:02.000000000 -0400 system.daemon.info: {"host":"centos7","ident":"systemd","message":"Started Session 119 of user root."}2021-06-04 06:40:02.000000000 -0400 system.cron.info: {"host":"centos7","ident":"CROND","pid":"7658","message":"(root) CMD (/usr/lib64/sa/sa1 1 1)"}
收集Nginx服务器的日志
fluentd配置文件
@type tail path /var/log/nginx/access.log pos_file /var/log/nginx/access.log.pos tag nginx.access format /^(? [^ ]*) [^ ]* (? [^ ]*) \[(?
然后在/var/log/nginx/目录下创建.pos文件
[root@centos7 nginx]# touch access.log.pos[root@centos7 nginx]# chown a+rw access.log.pos
重启Fluentd服务
[root@centos7 ~]# systemctl restart td-agent
重启nginx
[root@centos7 ~]# nginx -s reload
最后,还可以将日志统一整合至Kinbana界面显示,这个和之前的ELK操作没有区别,创建 fluentd-* 索引。
以上就是今天,民工哥给大家带的开源日志收集系统 Fluentd的介绍,有兴趣的读者可以查阅官方文档,更深入的学习与探索,也欢迎前来交流使用经验。
推荐阅读 点击标题可跳转
转载地址:https://mingongge.blog.csdn.net/article/details/118004639 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!