ELK日志收集處理

https://www.elastic.co/

1)收集-能夠采集多種來源的日志數(shù)據(jù) Log4js PM2
2)傳輸-能夠穩(wěn)定的把日志數(shù)據(jù)傳輸?shù)街醒胂到y(tǒng) Filebeat
3)存儲-如何存儲日志數(shù)據(jù) Elasticsearch
4)分析-可以支持 UI 分析 Kibana
5)警告-能夠提供錯誤報告,監(jiān)控機(jī)制 Monitor

ELK :Elasticsearch Logstash Kibana

  • FIilbeat :將服務(wù)器上收集的日志發(fā)送到Logstash中(格式使用log4js規(guī)范化)
  • Logstash:Logstash將日志接受過濾并轉(zhuǎn)發(fā)到Elasticsearch存儲
  • Elasticsearch:存儲收集來的日志,提供實時的數(shù)據(jù)查詢
  • Kibana:數(shù)據(jù)可視化服務(wù)

多應(yīng)用的監(jiān)控架構(gòu)

監(jiān)控系統(tǒng).png

當(dāng)然也可做更加分布式的部署


  1. 下載 Filebeat

配置 filebeat.yml

filebeat.inputs:
  type: log    
  enabled: true    // 默認(rèn)關(guān)閉
  paths:
    ~/logs/*.log     //  ~/logs/*/*.log  僅匹配二級目錄下的.log文件

output.logstash:
  hosts: ["localhost: 9900"]

運(yùn)行filebeat

sudo ./filebeat -e -c filebeat.yml 

// 如果提示權(quán)限問題,則使用以下代碼運(yùn)行
sudo chown root filebeat.yml
sudo ./filebeat -e -c filebeat.yml -d "publish"
  1. 下載 Logstash
    配置 config/logstash-sample.conf (沒有則新建)
input {
  beats {
    port => 9900
  }
}

output {
  elasticsearch {
    hosts => ["http://localhost:9200"]
    index => "testlog"
  }
}

運(yùn)行l(wèi)ogstash, logstash 自己占用9600-9700 之間第一個可用的端口
可在config/logstash.yml 中配置

bin/logstash -f config/logstash-sample.conf  // 使用配置文件啟動
bin/logstash -e "input { stdin{} } output { stdout {} }"  // 使用字符串配置啟動
// 在終端接受并輸出

測試logstash正確開啟: curl localhost:9600 (具體端口看啟動時的日志/自己配置)


image.png
  1. 下載 Elasticsearch
    配置 config/elasticsearch.yml
path.data: /Users/rogers/Project/Agora/test/util/data
path.data: /Users/rogers/Project/Agora/test/util/logs

運(yùn)行elasticsearch

bin/elasticsearch

測試elasticsearch正確開啟


image.png

使用elasticsearch提供的API 在elasticsearch上進(jìn)行查找數(shù)據(jù)

http://localhost:9200/_cat/indices?v // 查找當(dāng)前所有Index

http://localhost:9200/[Index][Type][ID/_search]
http://localhost:9200/testlog/doc/_search // testlog下doc類中所有的數(shù)據(jù)
http://localhost:9200/testlog/doc/_Ze_f2cBIQjv8oYr3PEN testlog下doc類中ID為_Ze_f2cBIQjv8oYr3PEN的數(shù)據(jù)
等等豐富的API供用戶調(diào)用
注: 當(dāng)加入未指定ID的數(shù)據(jù)時,自動為其生成隨機(jī)ID

  1. 下載 Kibana

配置 config/kibana.yml

運(yùn)行bin/kibana 默認(rèn)端口為5601

此時在頁面中選擇對應(yīng)之前創(chuàng)建的Index,即可在discover中或其他頁面看到所存在的數(shù)據(jù)


至此,已將簡單的日志收集轉(zhuǎn)發(fā)并在kibana上進(jìn)行展示的邏輯調(diào)通,
接下來需要做兩件事:

  1. 根據(jù)elasticsearch提供的API,做一個monitor達(dá)到告警的效果
  2. 得到告警后在kibana(多功能)中根據(jù)不同的條件,查看具體的日志信息。
  3. 由于本次調(diào)試僅在本地調(diào)試,所以很多配置直接采用了默認(rèn)的配置,后續(xù)記錄其他配置
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容