设为首页 收藏本站
查看: 981|回复: 0

[经验分享] Filebeat +Redis+ELK处理Nginx日志系统

[复制链接]

尚未签到

发表于 2019-1-28 08:35:04 | 显示全部楼层 |阅读模式
  (一)简述:
  filebeat:具有日志收集功能,是下一代的Logstash收集器,但是filebeat更轻量,占用资源更少,适合客户端使用。
  redis:Redis 服务器通常都是用作 NoSQL 数据库,不过 logstash 只是用来做消息队列。
  logstash:主要是用来日志的搜集、分析、过滤日志的工具,支持大量的数据获取方式。一般工作方式为c/s架构,client端安装在需要收集日志的主机上,server端负责将收到的各节点日志进行过滤、修改等操作在一并发往elasticsearch上去。
  elasticsearch:Elasticsearch是个开源分布式搜索引擎,提供搜集、分析、存储数据三大功能。它的特点有:分布式,零配置,自动发现,索引自动分片,索引副本机制,restful风格接口,多数据源,自动搜索负载等。
  kibana:Kibana可以为 Logstash 和 ElasticSearch 提供的日志分析友好的 Web 界面,可以帮助汇总、分析和搜索重要数据日志。
  下图展示的基本的架构图。

  (二)具体步骤
  1、filebeat的简介和具体配置
Filebeat是一个日志文件托运工具,在你的服务器上安装客户端后,filebeat会监控日志目录或者指定的日志文件,追踪读取这些文件(追踪文件的变化,不停的读),并且转发这些信息到elasticsearch或者logstarsh、redis中存放
  1.1、工作原理
filebeat由2个主要组件构成:prospector和harvesters。这两类组件一起协同完成Filebeat的工作,从指定文件中把数据读取出来,然后发送事件数据到配置的output中。
  harvesters:主要负责进行单个文件的内容收集;在运行过程中,每一个Harvester会对一个文件逐行进行内容读取,并且把读写到的内容发送到配置的output中。
  Prospector负责管理Harvsters,并且找到所有需要进行读取的数据源。如果input type配置的是log类型,Prospector将会去配置度路径下查找所有能匹配上的文件,然后为每一个文件创建一个Harvster。每个Prospector都运行在自己的Go routine里
  1.2工作流程
当你开启filebeat程序的时候,它会启动一个或多个探测器(prospectors)去检测你指定的日志目录或文件,对于探测器找出的每一个日志文件,filebeat启动收割进程(harvester),每一个收割进程读取一个日志文件的新内容,并发送这些新的日志数据到处理程序(spooler),处理程序会集合这些事件,最后filebeat会发送集合的数据到你指定的地点

  1.3、具体的相关配置

[root@localhost ~]# vim /etc/filebeat/filebeat.yml
#=========================== Filebeat inputs =============================
filebeat.inputs:
# Each - is an input. Most options can be set at the input level, so
# you can use different inputs for various configurations.
# Below are the input specific configurations.
- type: log
# Change to true to enable this input configuration.
enabled: true
# Paths that should be crawled and fetched. Glob based paths.
paths:
- /opt/access.log
#================================ Outputs =====================================
# Configure what output to use when sending the data collected by the beat.
output.redis:
hosts: ["172.20.67.50:6379"]
#port: 6379
#password: "123456"  
db: 2
timeout: 10
key: "nginx-log"
#####备注:目前使用filebeat向redis写日志的时候不能向redis集群里写,会提示报错,所有redis只能写到单台里,
root@localhost ~]# systemctl start filebeat
[root@localhost ~]# systemctl status filebeat
● filebeat.service - Filebeat sends log files to Logstash or directly to Elasticsearch.
Loaded: loaded (/usr/lib/systemd/system/filebeat.service; enabled; vendor preset: disabled)
Active: active (running) since Thu 2018-09-20 19:48:36 CST; 3s ago
Docs: https://www.elastic.co/products/beats/filebeat
Main PID: 11947 (filebeat)
CGroup: /system.slice/filebeat.service
└─11947 /usr/share/filebeat/bin/filebeat -c /etc/filebeat/filebeat.yml -path.home /usr/share/filebeat -path.config /etc/filebeat -path.data /var/lib/filebeat -path.logs /var/log/filebeat
Sep 20 19:48:36 localhost.localdomain systemd[1]: Started Filebeat sends log files to Logstash or directly to Elasticsearch..
Sep 20 19:48:36 localhost.localdomain systemd[1]: Starting Filebeat sends log files to Logstash or directly to Elasticsearch....
[root@localhost ~]#
  2、Redis具体的操作
在redis上查看具体导入的数据

  3、logstash 具体的配置

[root@localhost ~]# vim /usr/local/logstash/data/redis.conf
input {
redis {
host => "172.20.67.50"
port => "6379"
data_type => "list"
db => 2
batch_count => 1     ###这个值是指从队列中读取数据时,一次性取出多少条。不写会报错,解决办法就是,不使用这个功能,将batch_size设置为1
#type => "log"
key => "nginx-log"
}
}
filter {
grok {
match => { "message" => "%{IPORHOST:remote_addr} - - \[%{HTTPDATE:time_local}\] \"%{WORD:method} %{URIPATHPARAM:request} HTTP/%{NUMBER:httpversion}\" %{INT:status} %{INT:body_bytes_sent} %{QS:http_referer} %{QS:http_user_agent}"
}
}
}
output {  
elasticsearch {  
hosts => "172.20.67.50:9200"
index => "nginx-access-%{+YYYY.MM.dd}"
}   
}
  4、查看ES集群是否有数据
可t通过head进行查看是否有数据。
  5、在kibana中创建索引,即可在主页查看相关的数据了。
  错误集锦:
错误一:

“2018-09-20T17:58:05.029+0800   ERROR   redis/client.go:231 Failed to RPUSH to redis list with: MISCONF Redis is configured to save RDB snapshots, but it is currently not able to persist on disk. Commands
that may modify the data set are disabled, because this instance is configured to report errors during writes if RDB snapshotting fails (stop-writes-on-bgsave-error option). Please check the Redis logs for det
ails about the RDB error.”
解决方案:去redis上查看日志,出现了“
18180:M 20 Sep 18:10:35.061 * 10 changes in 300 seconds. Saving...
18180:M 20 Sep 18:10:35.061 # Can't save in background: fork: Cannot al”
彻底解决的办法是在redis服务器上依次执行:
1、vim /etc/sysctl.conf
2、添加 vm.overcommit_memory=1
3、sysctl -p
  错误二、

[WARN ][logstash.inputs.redis    ] Redis connection problem {:exception=>#}
Redis connection problem {:exception=># 1




运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-668484-1-1.html 上篇帖子: ELK搭建及kibana界面汉化 下篇帖子: ELK日志分析分享
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表