(一)簡述: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