基於ELK的日誌收集分析系統(Elasticsearch+Filebeat+Kibana)
痛點:
傳統意義上,查找日誌都是通過shell等工具登陸服務器後臺去相應的日誌目錄下查看log文件,特別是
聯調、測試時,我們需要通過tail -f 、grep 'xxx’等命令,肉眼觀察日誌,如果是一個複雜的業務流程,
則需要輾轉多個日誌目錄下,並且沒有統計分析等功能。
思考:
對於我們的業務系統,現在已經存在了一套日誌收集系統(syslog+flume+kafka+elasticsearch),但
是是基於核心業務,收集的都是需要做分析統計和機器學習的業務日誌,對於研發人員來說,需要關心
的是程序運行時log文件的日誌。本着不影響核心業務的原則(如果也用flume接入log文件日誌有影響
業務的風險),所以筆者調研了現在主流的ELK,本文將快速部署一套供研發人員使用的日誌搜索系
統,即裝即用,非常輕量級。
ELK:
ELK 是elastic公司提供的一套完整的日誌收集以及展示的解決方案,是三個產品的首字母縮寫,分別是
ElasticSearch、Logstash 和 Kibana,整個ELK生態圈有很多周邊插件,而Logstash因爲太重量級,啓
動需要佔用大量內存資源,並且側重於分析過濾格式化,可以和flume進行替換,對於簡單的日誌收
集,官網出了一個更輕量級的替代產品filebeat,也是官方推薦使用。官網傳送門 https://www.elastic.
co/cn/
原理:
搭建ELK
一、Elasticsearch安裝配置:
1、下載地址:
https://www.elastic.co/cn/downloads/elasticsearch,3個組件需要版本一致
tar -vxf elasticsearch-7.5.1.tar.gz
2、配置文件:
打開/config/elasticsearch.yml
#ip
network.host: ***.***.*.**
#端口
http.port: 9200
#主要用於集羣配置,可以先放開,不打開會報錯,後邊具體說;ES名稱,多臺服務器配置名稱不可相同
node.name: node-1
cluster.initial_master_nodes: ["node-1"]
3、啓動:
ES默認不能用root啓動,需新增一個用戶,adduser esuser ,su esuser
進入bin目錄,執行./elasticsearch &
4、驗證:
訪問http://ip:9200,返回數據則啓動成功
二、Filebeat安裝配置:
1、下載地址:
https://www.elastic.co/cn/downloads/beats/filebeat
tar -vxf kibana-7.5.1.tar.gz
2、配置文件:
進入安裝目錄下,打開filebeat.yml
註釋:在inputs中配置了兩個目錄的.log文件,在output中也配置了兩個會在es中產生的index
#=========================== Filebeat inputs =============================
filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/ida/ida-osquery-service/*.log
multiline.pattern: '^[[:space:]]+(at|\.{3})\b|^Exception|^Caused by'
multiline.negate: false
max_lines: 20
multiline.match: after
document_type: "osquery"
tags: ["osquery"]
fields:
type: 'osquery'
- type: log
enabled: true
paths:
- /var/log/ida/ida-restful-api/*.log
multiline.pattern: '^[[:space:]]+(at|\.{3})\b|^Exception|^Caused by'
multiline.negate: false
max_lines: 20
multiline.match: after
document_type: "restful"
tags: ["restful"]
fields:
type: 'restful'
#================================ Outputs =====================================
#-------------------------- Elasticsearch output ------------------------------
output.elasticsearch:
hosts: ["ip:9200"]
indices:
- index: "osquery-%{+yyyy.MM.dd}"
when.equals:
fields.type: "osquery"
- index: "restful-%{+yyyy.MM.dd}"
when.equals:
fields.type: "restful"
3、啓動:
/usr/local/filebeat-7.5.1-linux-x86_64/filebeat -e -c /usr/local/filebeat-7.5.1-
linux-x86_64/filebeat.yml -d "publish" &
4、驗證:
ps -aux|grep filebeat
三、Kibana安裝配置:
1、下載地址:
https://www.elastic.co/cn/downloads/kibana
2、配置文件:
server.host: "ip"
elasticsearch.hosts: ["http://ip:9200"]
i18n.locale: "zh-CN"
3、啓動:
kibana默認不允許root啓動,後面加上 --allow-root &
/usr/local/kibana-7.5.1-linux-x86_64/bin/kibana --allow-root &
4、驗證:
kibana會監聽5601端口,訪問http://ip:5601
四、開始啓用
首先需要創建索引模式,需要關注filebeat的配置文件
1、創建索引模式
自動匹配到filebeat中創建的index
2、查詢日誌
模擬產生一條日誌,在搜索欄輸入關鍵字,效果如下:
3、索引管理
在filebeat中配置了index的名字,osquery-%{+yyyy.MM.dd},會每天自動生成一個ES索引日誌文件
五、總結
至此,基於ELK的日誌搜索系統搭建完成,10分鐘落地啓用,告別傳統的去服務器目錄下查log文件的方
式,擁抱新技術。下一篇將分享筆者是怎麼一鍵部署ELK的。