海量日志采集组件之filebeat
filebeat
input-->stdin,完毕--->file{path =》 }
因为logstash消耗内存等资源太高,如果在要采集的服务上都安装logstash,这样对应用服务器的压力增加。所以我们要用轻量级的采集工具才更高效,更省资源。
beats是轻量级的日志收集处理工具,Beats占用资源少
-
Packetbeat: 网络数据(收集网络流量数据)
-
Metricbeat: 指标 (收集系统、进程和文件系统级别的 CPU 和内存使用情况等数据)
-
Filebeat: 文件(收集日志文件数据)
-
Winlogbeat: windows事件日志(收集 Windows 事件日志数据)
-
Auditbeat:审计数据 (收集审计日志)
-
Heartbeat:运行时间监控 (收集系统运行时的数据)
我们这里主要是收集日志信息, 所以只讨论filebeat。
filebeat可以直接将采集的日志数据传输给ES集群(EFK), 也可以给logstash(==5044==端口接收)。
filebeat收集日志直接传输给ES集群
第1步: 下载并安装filebeat(再开一台虚拟机filebeat模拟filebeat, 内存1G就够了, 安装filebeat)
[root@filebeat ~]# rpm -ivh filebeat-7.17.5-x86_64.rpm
filebeat -h
第2步: 配置filebeat收集日志
[root@filebeat ~]# cat /etc/filebeat/filebeat.yml |grep -v '#' |grep -v '^$'
filebeat.inputs:
- type: log
enabled: true #28-改为true
paths:
- /var/log/*.log 收集的日志路径
filebeat.config.modules:
path: ${path.config}/modules.d/*.yml
reload.enabled: false
setup.template.settings:
index.number_of_shards: 3
setup.kibana:
output.elasticsearch: 输出给es集群
hosts: ["es01:9200"] #137es集群节点ip
processors:
- add_host_metadata: ~
- add_cloud_metadata: ~
第3步: 启动服务
[root@filebeat ~]# systemctl start filebeat
[root@filebeat ~]# systemctl enable filebeat
第4步: 验证
在es-head和kibana上验证
练习:可以尝试使用两台filebeat收集日志,然后在kibana用筛选器进行筛选过滤查看。(可先把logstash那台关闭logstash进行安装filebeat测试)
filebeat传输给logstash⭐⭐⭐⭐⭐
第1步: 在logstash上要重新配置,开放5044端口给filebeat连接,并重启logstash服务
[root@logstash ~]# vim /etc/logstash/conf.d/test4.conf
input {
beats {
port => 5044
}
}
output {
elasticsearch {
hosts => ["192.168.3.71:9200"]
index => "filebeat2-%{+YYYY.MM.dd}"
}
stdout {
}
}
[root@logstash ~]# cd /usr/share/logstash/bin/
如果前面有使用后台跑过logstash实例的请kill掉先
[root@logstash bin]# pkill java
[root@logstash bin]# ./logstash --path.settings /etc/logstash/ -r -f /etc/logstash/conf.d/test04.conf
第2步: 配置filebeat收集日志
[root@filebeat ~]# cat /etc/filebeat/filebeat.yml |grep -v '#' |grep -v '^$'
filebeat.inputs:
- type: log
enabled: true #28改为true
paths:
- /var/log/*.log 收集的日志路径
filebeat.config.modules:
path: ${path.config}/modules.d/*.yml
reload.enabled: false
setup.template.settings:
index.number_of_shards: 3
setup.kibana:
output.logstash: #148这两句非常重要,表示日志输出给logstash
hosts: ["logstash:5044"] #150 IP为logstash服务器的IP;端口5044对应logstash上的配置
processors:
- add_host_metadata: ~
- add_cloud_metadata: ~
第3步: 启动服务
[root@filebeat ~]# systemctl stop filebeat
[root@filebeat ~]# systemctl start filebeat
第5步: 去ES-head上验证 在filebeat上安装软件变更yum install vsftdp
第6步:在kibana创建索引模式(过程省略,参考上面的笔记操作),然后点发现验证
filebeat收集nginx日志
1, 在filebeat这台服务器上安装nginx,启动服务。并使用浏览器访问刷新一下,模拟产生一些相应的日志(==强调==: 我们在这里是模拟的实验环境,一定要搞清楚实际情况下是把filebeat安装到nginx服务器上去收集日志)
[root@filebeat ~]# yum install epel-release -y
[root@filebeat ~]# yum install nginx -y
#查看是否有日志
ll /var/log/nginx
0
[root@filebeat ~]# systemctl restart nginx
[root@filebeat ~]# systemctl enable nginx
#再次查看
[root@es04 filebeat]# ll /var/log/nginx
总用量 0
-rw-r--r-- 1 root root 0 9月 12 05:36 access.log
-rw-r--r-- 1 root root 0 9月 12 05:36 error.log
2, 修改filebeat配置文件,并重启服务
[root@filebeat ~]# cat /etc/filebeat/filebeat.yml |grep -v '#' |grep -v '^$'
filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/*.log
- /var/log/nginx/access.log 只这里加了一句nginx日志路径(可)
filebeat.config.modules:
path: ${path.config}/modules.d/*.yml
reload.enabled: false
setup.template.settings:
index.number_of_shards: 3
setup.kibana:
output.logstash:
hosts: ["logstash:5044"]
processors:
- add_host_metadata: ~
- add_cloud_metadata: ~
#由于访问日志还是0,所以,需要去外面多访问几次
http://192.168.x.74
3, 验证(在kibana或es-head上查询)
[root@filebeat ~]# systemctl stop filebeat
[root@filebeat ~]# systemctl start filebeat
练习:** 尝试收集httpd,mysql日志
故障问题总结:
-
filebeat配置里没有把output.elasticsearch改成output.logstash
-
filebeat在收集/var/log/*.log日志时,需要对日志进行数据的改变或增加,才会传。当/var/log/yum.log增加了日志数据会传输,但不会触发配置里的其它日志传输。(每个日志的传输是独立的)
-
filebeat收集的日志没有定义索引名称, 我这个实验是在logstash里定义的。(此例我定义的索引名叫filebeat2-%{+YYYY.MM.dd})
-
es-head受资源限制可能会关闭了,你在浏览器上验证可能因为缓存问题,看不到变化的结果。
-
区分索引名和索引模式(index pattern)名
filebeat日志简单过滤
[root@filebeat ~]# grep -Ev '#|^$' /etc/filebeat/filebeat.yml
filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/yum.log #第一行不用改
- /var/log/nginx/access.log
include_lines: ['Installed'] #表示收集的日志里有Installed关键字才会收集
filebeat.config.modules:
path: ${path.config}/modules.d/*.yml
reload.enabled: false
setup.template.settings:
index.number_of_shards: 3
setup.kibana:
output.logstash:
hosts: ["logstash:5044"]
processors:
- add_host_metadata: ~
- add_cloud_metadata: ~
[root@filebeat ~]# systemctl restart filebeat
测试方法:
通过yum install
和yum remove
产生日志,检验结果
结果为: yum install
安装可以收集,yum remove
卸载的不能收集
其它参数可以自行测试
-
exclude_lines
-
exclude_files
filebeat核心功能与定位
-
日志采集与转发:Filebeat 是 Elastic Stack 中的轻量级数据采集组件,专注于从日志文件、标准输入或其他数据源收集数据,并将其传输至 Elasticsearch、Logstash、Kafka 等目标进行分析和存储145。
-
低资源消耗:相较于 Logstash(基于 JVM),Filebeat 使用 Go 语言开发,具有更低的资源占用和更高的性能,适合大规模部署
- 点赞
- 收藏
- 关注作者
评论(0)