(一)简述:nginx
filebeat:具备日志收集功能,是下一代的Logstash收集器,可是filebeat更轻量,占用资源更少,适合客户端使用。redis
redis:Redis 服务器一般都是用做 NoSQL 数据库,不过 logstash 只是用来作消息队列。数据库
logstash:主要是用来日志的搜集、分析、过滤日志的工具,支持大量的数据获取方式。通常工做方式为c/s架构,client端安装在须要收集日志的主机上,server端负责将收到的各节点日志进行过滤、修改等操做在一并发往elasticsearch上去。vim
elasticsearch:Elasticsearch是个开源分布式搜索引擎,提供搜集、分析、存储数据三大功能。它的特色有:分布式,零配置,自动发现,索引自动分片,索引副本机制,restful风格接口,多数据源,自动搜索负载等。服务器
kibana:Kibana能够为 Logstash 和 ElasticSearch 提供的日志分析友好的 Web 界面,能够帮助汇总、分析和搜索重要数据日志。restful
下图展现的基本的架构图。架构
(二)具体步骤并发
一、filebeat的简介和具体配置
Filebeat是一个日志文件托运工具,在你的服务器上安装客户端后,filebeat会监控日志目录或者指定的日志文件,追踪读取这些文件(追踪文件的变化,不停的读),而且转发这些信息到elasticsearch或者logstarsh、redis中存放dom
1.一、工做原理
filebeat由2个主要组件构成:prospector和harvesters。这两类组件一块儿协同完成Filebeat的工做,从指定文件中把数据读取出来,而后发送事件数据到配置的output中。elasticsearch
harvesters:主要负责进行单个文件的内容收集;在运行过程当中,每个Harvester会对一个文件逐行进行内容读取,而且把读写到的内容发送到配置的output中。
Prospector负责管理Harvsters,而且找到全部须要进行读取的数据源。若是input type配置的是log类型,Prospector将会去配置度路径下查找全部能匹配上的文件,而后为每个文件建立一个Harvster。每一个Prospector都运行在本身的Go routine里
1.2工做流程
当你开启filebeat程序的时候,它会启动一个或多个探测器(prospectors)去检测你指定的日志目录或文件,对于探测器找出的每个日志文件,filebeat启动收割进程(harvester),每个收割进程读取一个日志文件的新内容,并发送这些新的日志数据处处理程序(spooler),处理程序会集合这些事件,最后filebeat会发送集合的数据到你指定的地点
1.三、具体的相关配置
[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 ~]#
二、Redis具体的操做
在redis上查看具体导入的数据
三、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}" } }
四、查看ES集群是否有数据
可t经过head进行查看是否有数据。
五、在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服务器上依次执行: 一、vim /etc/sysctl.conf 二、添加 vm.overcommit_memory=1 三、sysctl -p
错误2、
[WARN ][logstash.inputs.redis ] Redis connection problem {:exception=>#<Redis::CommandError: ERR unknown command 'script'>} Redis connection problem {:exception=>#<Redis::CommandError: ERR Error runni”
解决方法:
解决的办法:不使用这个功能,将batch_size设置为1batch_count => 1