本案例接上次案例环境继续深入的,如有疑问,链接跳转:https://blog.csdn.net/weixin_45373345/article/details/107749634
kibana图表:
登录——左侧面板选择visualize——点击“+”号——选择图表类型(Area)——选择索引——Buckets(x-Axis)——Aggregation(Terms)——Field(remote_addr.keyword)——size(3)——点击上方三角标志
kibana监控(x-pack):
登录——左侧面板选择——Monitoring——启用监控(Turn on monitoring)
构建filebeat+redis+logstash+es+kibana架构
1、安装redis,并启动
1)准备安装和数据目录
[root@efk ~]# mkdir -p /data/soft
[root@efk ~]# mkdir -p /opt/redis_cluster/redis_6379/{conf,logs,pid}
2)下载redis安装包
[root@efk ~]# cd /data/soft
[root@efk soft]# wget http://download.redis.io/releases/redis-5.0.7.tar.gz
3)解压redis到/opt/redis_cluster/
[root@efk soft]# tar xf redis-5.0.7.tar.gz -C /opt/redis_cluster/
[root@efk soft]# ln -s /opt/redis_cluster/redis-5.0.7 /opt/redis_cluster/redis
4)切换目录安装redis
[root@efk soft]# cd /opt/redis_cluster/redis
[root@efk redis]# make && make install
5)编写配置文件
[root@efk redis]# vim /opt/redis_cluster/redis_6379/conf/6379.conf
bind 127.0.0.1 192.168.1.128
port 6379
daemonize yes
pidfile /opt/redis_cluster/redis_6379/pid/redis_6379.pid
logfile /opt/redis_cluster/redis_6379/logs/redis_6379.log
databases 16
dbfilename redis.rdb
dir /opt/redis_cluster/redis_6379
6)启动当前redis服务
[root@efk redis]# redis-server /opt/redis_cluster/redis_6379/conf/6379.conf
2、修改filebeat配置文件,output给redis
参考文档:https://www.elastic.co/guide/en/beats/filebeat/6.6/index.html
1)修改filebeat配置output指向redis,重启
[root@efk redis]# cd /etc/filebeat/
[root@efk filebeat]# mv filebeat.yml filebeat.yml.bak
[root@efk filebeat]# grep -v "#" filebeat.yml.bak | grep -v "^$" > filebeat.yml #过滤掉#开头及空行输出为filebeat.yml文件
[root@efk ~]# vim /etc/filebeat/filebeat.yml
filebeat.inputs:
- type: logenabled: truepaths:- /var/log/nginx/access.logjson.keys_under_root: truejson.overwrite_keys: truetags: ["access"]- type: logenabled: truepaths:- /var/log/nginx/error.logtags: ["error"]setup.template.settings:index.number_of_shards: 3setup.kibana:output.redis:hosts: ["192.168.1.128"]key: "filebeat"db: 0timeout: 5
[root@efk ~]# systemctl restart filebeat #重启服务
2)测试访问网站,登录redis,查看键值
清除之前的数据,以防看着碍眼:
测试访问网站
[root@efk ~]# ab -n 1000 -c 50 http://192.168.1.128/ #本机访问网站1000[root@ceshiji ~]# ab -n 100 -c 50 http://192.168.1.128/ #测试机访问网站300
[root@ceshiji ~]# ab -n 100 -c 50 http://192.168.1.128/
[root@ceshiji ~]# ab -n 100 -c 50 http://192.168.1.128/
登录redis,查看键值
[root@efk ~]# redis-cli #登录
127.0.0.1:6379> keys * #列出所有键
1) "filebeat"
127.0.0.1:6379> type filebeat #filebeat为键值名
list
127.0.0.1:6379> LLEN filebeat #查看list长度
(integer) 1300
127.0.0.1:6379> lrange filebeat 0 -1 #查看list所有内容(内容过多不宜展示)
3、安装logstash,收集redis的日志,提交给es
1)安装logstash(安装包提前放在了/data/soft下)
[root@efk ~]# cd /data/soft/
[root@efk soft]# rpm -ivh logstash-6.6.0.rpm
2)配置logstash
[root@efk soft]# vim /etc/logstash/conf.d/redis.conf
#添加:
input {redis {host => "192.168.1.128"port => "6379"db => "0"key => "filebeat"data_type => "list"}
}filter {mutate {convert => ["upstream_time","float"]convert => ["request_time","float"]}
}output {stdout {}elasticsearch {hosts => ["http://192.168.1.128:9200"]index => "nginx_access-%{+YYYY.MM.dd}"manage_template => false}
}
3)启动logstash
[root@efk soft]# /usr/share/logstash/bin/logstash -f /etc/logstash/conf.d/redis.conf
启动过程较为缓慢接着会报出许多传输的消息,这时可另启终端登录redis查看在redis的键值一直在减少
……"@timestamp" => 2020-08-05T04:06:21.813Z,"@version" => "1","offset" => 2115001,"up_addr" => "-","error" => {"type" => "json","message" => "@timestamp not overwritten (parse error on 05/Aug/2020:12:06:20 +0800)"},"up_host" => "-","prospector" => {"type" => "log"}
}
……
127.0.0.1:6379> LLEN filebeat
(integer) 590
127.0.0.1:6379> LLEN filebeat
(integer) 362
127.0.0.1:6379> LLEN filebeat
(integer) 0
验证:
4)修改logstash配置文件,实现access和error日志分离
[root@efk ~]# vim /etc/logstash/conf.d/redis.conf
#修改添加:
input {redis {host => "192.168.1.128"port => "6379"db => "0"key => "filebeat"data_type => "list"}
}filter {mutate {convert => ["upstream_time","float"]convert => ["request_time","float"]}
}output {stdout {}if "access" in [tags] {elasticsearch {hosts => ["http://192.168.1.128:9200"]index => "nginx_access-%{+YYYY.MM.dd}"manage_template => false}}if "error" in [tags] {elasticsearch {hosts => ["http://192.168.1.128:9200"]index => "nginx_error-%{+YYYY.MM.dd}"manage_template => false}}
}
/usr/share/logstash/bin/logstash -f /etc/logstash/conf.d/redis.conf #重启logstash
故意制造错误日志:
[root@ceshiji ~]# ab -n 100 -c 50 http://192.168.1.128/errorrr
[root@ceshiji ~]# ab -n 100 -c 50 http://192.168.1.128/errorrr
[root@ceshiji ~]# ab -n 100 -c 50 http://192.168.1.128/errorrr
验证: