收集日志之四: filebeat方式(重点)
收集日志的几种方式: 1.logstash (消耗内存多。功能性好) 2.logstash的TCP/UDP 监听端口,在”其他“服务器安装 nc 命令 3.通过 rsyslog 收集日志,要logstash接收再转发到ES : 4.filebeat 收集日志:写入es redis logstash kafka (消耗内存少,不使用java,不支持多输出 ,不支持IF的type判断,filebeat服务器的配置文件中先定义fields:) 使用 filebeat 替代 logstash 收集日志: Filebeat 是轻量级单用途的日志收集工具,用于在没有安装 java 的服务器上专 门收集日志,可以将日志转发到 logstash、elasticsearch 或 redis 及 kafka 等场景中进行下一步处理。 从input读取事件源,经过相应解析和处理之后,从output输出到目标存储库(elasticsearch或其他)。 输入可以从Log、Syslog、Stdin、Redis、UDP、Docker、TCP、NetFlow输入,然后可以输出到Elasticsearch、Logstash、Kafka、Redis、File、Console、Cloud。 注意1:output到服务器,不能同时有多个, 取多个inputs时,只会生成一个index文件,造成混乱,建议分类,filebeat由于不支持IF的type判断,先传给logstash或Redis。 注意1:output到ES服务器,不支持josn格式,(可以先到Redis解决此问题) 1.docker适合这种方式。 2.默认日志格式为 json 格式
实验说明 192.168.80.100 localhost7A.localdomain ES-node1 head cerebro kibana 192.168.80.110 localhost7B.localdomain ES-node2 192.168.80.120 localhost7C.localdomain ES-node3 192.168.80.130 localhost7D.localdomain logstash1 192.168.80.140 localhost7E.localdomain Redis 192.168.80.150 localhost7F.localdomain Kafka 192.168.80.160 localhost7G.localdomain logstash2 nginx 模拟客户端和filebeat接收端 192.168.80.170 localhost7H.localdomain filebeat nginx 模拟客户端 实验测试几种情况: 情况1.filebeat收集日志,ES接收。 日志混乱,所有日志成为一个index,无法分类. 情况2.filebeat收集日志redis接收,logstash在redis服务器上读取日志,并写入ES服务器。 日志混乱,在Redis中就成一个index,无法分类。 情况3.filebeat收集日志logstash接收, 接5。 情况4.logstash2收集日志,ES接收. 正常,日志能分类。 情况5.logstash2收集日志,redis接收,logstash1在redis服务器上读取日志,并写入ES服务器 正常,日志能分类
[root@localhost7H ~] yum install filebeat-7.6.1-x86_64.rpm nginx [root@localhost7H ~] systemctl restart filebeat.service [root@localhost7H ~] systemctl restart nginx.service 一、filebeat配置文件说明: [root@localhost7H ~]#cat /etc/filebeat/filebeat.yml #-------------------- Filebeat inputs ------------------- 取本地的配置文件,可以有多个。 filebeat.inputs: - type: log enabled: true #开启功能 fields: #自定义的字段名,用于日志分类 type: nginx_access host: 1.1.1.1 paths: - /var/log/nginx/access.log #- c:\programdata\elasticsearch\logs\* - type: log enabled: true fields: type: nginx_error host: 2.2.2.2 # level: debug paths: - /var/log/nginx/error.log #三行为处理多行日志,参考"收集 java 多行日志" #multiline.pattern: ^\[ #multiline.negate: false #multiline.match: after #-------------------- Elasticsearch output --------------- 输出到各服务器,不能同时多个 输出到logstash服务器,hosts: 为logstashIP地址和监听端口,loadbalance:表示对端口轮流发数据。 worker 发数据的工作进程 ,情况3 output.logstash: hosts: ["192.168.80.160:5044", "192.168.80.160.5045"] loadbalance: true worker: 3 输出到redis服务器,情况2 #output.redis: # hosts: ["192.168.80.140:6379"] # password: "12345678" # key: "filebeat_redis_nginx" # db: 1 # timeout: 5 输出到本地路径 #output.file: # path: "/tmp/" # filename: "filebeat-linux.log" 输出到ES服务器,es的index系统自己生成。情况1 #output.elasticsearch: # hosts: ["192.168.80.100:9200"]
情况1测试结果: 日志混乱,所有日志成为一个index,无法分类.
情况2测试结果:日志混乱,所有日志成为一个index,无法分类. 二、redis 配置 1.安装redis并设置密码 [root@localhost7e ~]# yum install redis [root@localhost7e ~]# redis-cli 127.0.0.1:6379> config set requirepass 12345678 #动态设置,重启后无效 OK 验证 redis 是否有数据 127.0.0.1:6379> auth 12345678 127.0.0.1:6379> SELECT 1 127.0.0.1:6379[1]> keys * 1) "filebeat_redis_nginx" 127.0.0.1:6379[1]> TYPE filebeat_redis_nginx list 127.0.0.1:6379[1]> 127.0.0.1:6379[1]> LPOP filebeat_redis_nginx
三、logstash 配置文件 [root@localhost7G ~]# cat /etc/logstash/conf.d/log-to-redis.conf #logstash的beats插件开启的监听端口,客户端为 elk的filebeat插件。 input { beats { port => 5044 codec => "json" } beats { port => 5045 codec => "json" }} #输出到redis服务器 output { #[fields][type]的值在filebeat服务器的配置文件中先定义fields:。 if [fields][type] == "nginx_error" { redis { host => "192.168.80.140" port => "6379" db => "1" password => "12345678" data_type => "list" key => "nginx-errorlog" }} if [fields][type] == "nginx_access" { redis { host => "192.168.80.140" port => "6379" db => 1 password => "12345678" data_type => "list" key => "nginx-accesslog" codec => "json" }}} 情况3测试结果:验证 redis 是否有数据 [root@localhost7G ~]# /usr/share/logstash/bin/logstash -f /etc/logstash/conf.d/log-to-redis.conf [root@localhost7e ~]# redis-cli 127.0.0.1:6379> auth 12345678 127.0.0.1:6379> SELECT 1 127.0.0.1:6379[1]> keys * 1) "nginx-accesslog-170" 2) "nginx-errorlog-170" 四.logstash 配置文件 [root@localhost7D ~]# cat /etc/logstash/conf.d/redis-to-es.conf #读取redis的服务器 input { redis { host => "192.168.80.140" port => "6379" db => 1 password => "12345678" data_type => "list" key => "nginx-errorlog" } redis { host => "192.168.80.140" port => "6379" db => 1 password => "12345678" data_type => "list" key => "nginx-accesslog" codec => "json" }} #输出到es服务器 output { if [fields][type] == "nginx_error" { elasticsearch { hosts => ["192.168.80.100:9200"] index => "nginx-errlog-170-%{+YYYY.MM.dd}" #codec => "json" }} if [fields][type] == "nginx_access" { elasticsearch { hosts => ["192.168.80.100:9200"] index => "nginx-accesslog-170-%{+YYYY.MM.dd}" codec => "json" }}}
五、在http://192.168.810.100:5601 中management--》kibana --》 index-mode 创建索引模式: 是json格式的。
在http://192.168.810.100:5601 中disover中查看日志信息

六、logstash 收集日志并写入 redis: 用一台服务器按照部署 redis 服务,专门用于日志缓存使用,用于 web 服务器产生大量日志的场景, 例如下面的服务器内存即将被使用完毕,查看是因为 redis服务保存了大量的数据没有被读取而占用了大量的内存空间。 在logstash服务器收集nginx日志和监听一个端口,并写入redis服务器 [root@localhost7G ~]# cat /etc/logstash/conf.d/logstash-to-redis.conf input { file { path => "/var/log/nginx/access_.*.log" type => "nginx-accesslog" start_position => "end" stat_interval => "3" codec => "json" } tcp { port => 7777 mode => "server" type => "tcplog" #定义type,在下面使用。 } } output { if [type] == "nginx-accesslog" { redis { data_type => "list" key => "nginx-access-160" host => "192.168.80.140" port => "6379" db => "2" password => "12345678" } } if [type] == "tcplog" { redis { data_type => "list" key => "tcplog" host => "192.168.80.140" port => "6379" db => "2" password => "12345678" } } } 测试: 验证 redis 是否有数据 1.访问nginx 2.echo "伪设备 1" > /dev/tcp/192.168.80.160/7777 登录redis查看。 [root@localhost7e ~]# redis-cli 127.0.0.1:6379> config set requirepass 12345678 127.0.0.1:6379> auth 12345678 127.0.0.1:6379> select 2 127.0.0.1:6379[1]> keys * 1) "tomcat-access-160" 2) "tcplog" logstash 配置文件 在redis服务器上读取日志,并写入ES服务器 [root@localhost7D /]# vim /etc/logstash/conf.d/redis-to-es-160.conf input { redis { data_type => "list" key => "nginx-access-160" host => "192.168.80.140" port => "6379" db => "2" password => "12345678" codec => "json" } redis { data_type => "list" key => "tcplog" host => "192.168.80.140" port => "6379" db => "2" password => "12345678" } } output { if [type] == "nginx-accesslog" { elasticsearch { hosts => ["192.168.80.100:9200"] index => "nginx_accesslog_160_%{+YYYY.MM.dd}" } } if [type] == "tcplog" { elasticsearch { hosts => ["192.168.80.100:9200"] index => "tcplog_160_%{+YYYY.MM.dd}" codec => "json" } } } 在http://192.168.80.100:5601 中management--》kibana --》 index-mode 创建索引模式: 在http://192.168.80.100:5601 中disover中查看日志信息 json格式


=====================================================
使用logstash 收集日志并写入 kafka
实验说明 192.168.80.100 localhost7A.localdomain ES-node1 head cerebro kibana 192.168.80.110 localhost7B.localdomain ES-node2 192.168.80.120 localhost7C.localdomain ES-node3 192.168.80.130 localhost7D.localdomain logstash1 192.168.80.150 localhost7F.localdomain Kafka 192.168.80.160 localhost7G.localdomain logstash2 nginx 模拟客户端 192.168.80.170 localhost7H.localdomain filebeat nginx 模拟客户端和filebeat接收端 1.安装kafka 和zookeeper : https://www.cnblogs.com/Yuanbangchen/p/17056168.html https://www.cnblogs.com/Yuanbangchen/p/17057413.html 2.收集和配置 logstash2 收集单日志文件并写入kafka中,安装nginx设置json格式的日志。 [root@localhost7G conf.d]# cat logstash-to-kafka.conf input { file { path => "/var/log/dmesg" type => "kafka-syslog" } file { path => "/var/log/nginx/access.log" start_position => "beginning" type => "nginx-accesslog-160" codec => "json" } } output { # stdout { # codec =>"rubydebug" # } if [type] == "kafka-syslog" { kafka { bootstrap_servers => "192.168.80.150:9092" #多个kafka使用,号。 topic_id => "magedu-linux39-testlog" codec => "json" }} if [type] == "nginx-accesslog-160" { kafka { bootstrap_servers => "192.168.80.150:9092" topic_id => "nginx-accesslog-160" codec => "json" }} } [root@localhost7G conf.d]# /usr/share/logstash/bin/logstash -f /etc/logstash/conf.d/logstash-to-kafka.conf -t 访问nginx产生测试日志日志,验证是否写入 kafka: [root@localhost7F ~]# /usr/local/kafka/bin/kafka-topics.sh --list --zookeeper 192.168.80.150:2181 nginx-accesslog-160 magedu-linux39-testlog3.配置 logstash1 从 kafka 读取日志并写入ES中。 [root@localhost7D /]# cat /etc/logstash/conf.d/kafka-to-es.conf input { kafka { bootstrap_servers => "192.168.80.150:9092" #多个kafka使用,号。 topics => "magedu-linux39-testlog" codec => "json" } kafka { bootstrap_servers => "192.168.80.150:9092" topics => "nginx-accesslog-160" codec => "json" } } output { if [type] == "kafka-syslog" { elasticsearch { hosts => ["192.168.80.100:9200"] index => "kafka-syslog-%{+YYYY.MM.dd}" }} if [type] == "nginx-accesslog-160" { elasticsearch { hosts => ["192.168.80.100:9200"] index => "logstash-kafka-nginxlog-%{+YYYY.MM.dd}" }} } [root@localhost7D /]# /usr/share/logstash/bin/logstash -f /etc/logstash/conf.d/logstash-to-kafka.conf -t
四、在http://192.168.80.130:5601 中management--》kibana --》 index-mode 创建索引模式:
在http://192.168.80.100:5601 中disover中查看日志信息

使用 filebeat 收集日志并写入kafka
fields 设置
[root@localhost7h ~]# vim /etc/filebeat/filebeat.yml
cat /etc/filebeat/filebeat.yml
#读本地文件
filebeat.inputs:
- type: log
enabled: true
fields:
type: nginx-access
logResource: dev-nginx-access
paths:
- /var/log/nginx/access.log
#-------------------- Elasticsearch output ---------------
输出到各服务器,不能同时多个
输出到logstash服务器,hosts: 为logstashIP地址和监听端口,loadbalance:表示对端口轮流发数据。 worker 发数据的工作进程
output.logstash:
hosts: ["192.168.80.160:5044"]
loadbalance: true
worker: 3
#logstash2监听两个端口,接收到的数据输出到kafka中。
[root@localhost7G conf.d]# vim /etc/logstash/conf.d/logstash-to-kafka-2.conf
input {
beats {
port => 5044
#codec => "json"
}
beats {
port => 5045
#codec => "json"
}}
output {
if [fields][type] == "nginx-access" {
kafka {
bootstrap_servers => "192.168.80.150:9092"
topic_id => "nginx-accesslog-170"
codec => "json"
}}
}
[root@localhost7G conf.d]# /usr/share/logstash/bin/logstash -f /etc/logstash/conf.d/logstash-to-kafka-2.conf
[root@localhost7F ~]# /usr/local/kafka/bin/kafka-topics.sh --list --zookeeper 192.168.80.150:2181
nginx-accesslog-170
#logstash1在读取kafka中的数据并输出到ES中。
cat /etc/logst/conf.d/kafka-to-es.conf
input {
kafka {
bootstrap_servers => "192.168.80.150:9092" #多个kafka使用,号。
topics => "nginx-accesslog-170"
codec => "json"
}
}
output {
if [fields][type] == "nginx-access" {
elasticsearch {
hosts => ["192.168.80.100:9200"]
index => "logstash-kafka-nginxlog-170-%{+YYYY.MM.dd}"
}}
}
验证, json序列化可以。
在http://192.168.810.100:5601 中management--》kibana --》 index-mode 创建索引模式:
在http://192.168.810.100:5601 中disover中查看日志信息
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了