Elastic Stack日誌採集方案
說明
本文基於elastic stack 相關組件版本號爲:6.1.2,linux操作系統,單節點測試部署。
Elastic Stack簡介:
ElasticStack 是一系列開源產品的合集,包括 Elasticsearch、Logstash 、Kibana、Beat等等。
Elasticsearch是個開源分佈式搜索引擎,提供蒐集、分析、存儲數據三大功能。它的特點有:分佈式,零配置,自動發現,索引自動分片,索引副本機制,restful風格接口,多數據源,自動搜索負載等。
Logstash主要是用來日誌的蒐集、分析、過濾日誌的工具,支持大量的數據獲取方式。一般工作方式爲c/s架構,client端安裝在需要收集日誌的主機上,server端負責將收到的各節點日誌進行過濾、修改等操作在一併發往elasticsearch上去。Logstash處理數據主要分爲三個階段,inputs、filters、outputs,其中各個階段,官方提供了豐富的插件,可以對數據進行處理。
Kibana也是一個開源和免費的工具,Kibana可以爲Logstash 和ElasticSearch 提供的日誌分析友好的 Web 界面,可以幫助彙總、分析和搜索重要數據日誌。
Filebeat隸屬於Beats。目前Beats包含四種工具:
- Packetbeat(蒐集網絡流量數據)
- Topbeat(蒐集系統、進程和文件系統級別的CPU 和內存使用情況等數據)
- Filebeat(輕量級的日誌收集處理工具(Agent),數據佔用資源少,適合於在各個服務器上搜集日誌後傳輸給Logstash,官方推薦)
- Winlogbeat(蒐集 Windows 事件日誌數據)
官方還提供X-Pack以支持安全驗證,服務器監控,報警與通知,報表,以及機器學習相關功能,但是收費,安裝時,默認30天試用期。
日誌採集方案:
日誌採集,可以分爲兩種:
- 常規應用日誌採集,由filebeat中model提供的日誌採集模塊進行日誌採集工作,目前官網提供以下日誌採集模塊:
- 特定應用日誌採集,可以選擇自定義beat採集日誌,或者由beat採集日誌搬運給logstach,由logstach的fileter、output、codec相關插件處理。
採集流程filebeat--> elasticsearch--> kibana 或者 filebeat -->logstach--> elasticsearch--> kibana
elasticsearch和kibana的安裝,並整合x-pack
1. 在官網下載elasticsearch-6.1.2.tar.gz;
2. 在官網下載kibana-6.1.2-linux-x86_64.tar.gz;
3. 安裝x-pack到elasticsearch,相關命令:
tar –xvf elasticsearch-6.1.2.tar.gz
cd elasticsearch-6.1.2
bin/elasticsearch-plugininstall x-pack
bin/elasticsearch
bin/x-pack/setup-passwordsauto #生成默認的密碼,需要記錄下相關用戶密碼
4. 安裝x-pack到kibana,相關命令:
tar –xvf kibana-6.1.2-linux-x86_64.tar.gz
cd kibana-6.1.2-linux-x86_64
bin/kibana-plugininstall x-pack
5. 把認證信息填到kibana.yml中
elasticsearch.username:"kibana"
elasticsearch.password: "<pwd>"
#"<pwd>是第三步生成的kibana用戶的密碼。
6. 啓動kibana
bin/kibana
7. 打開kibana
在瀏覽器中輸入http://ip:5601
輸入認證信息
Username:elastic Password: <pwd>
整合logstash與filebeat
1. 在官網下載logstash-6.1.2.tar.gz安裝包
2. 解壓安裝包,編寫配置文件first-pipeline.conf,配置文件信息如下:
input {
beats {
port => "5044"
}
}
# Thefilter part of this file is commented out to indicate that it is
#optional.
filter {
grok {
match => { "message" =>"%{COMBINEDAPACHELOG}"}
}
geoip {
source => "clientip"
}
}
output {
elasticsearch {
user => "elastic"
password => "pwd"
hosts=> [ "localhost:9200" ]
}
}
3. 啓動logstash
bin/logstash -ffirst-pipeline.conf --config.reload.automatic
4. 在官網下載filebeat-6.1.2-linux-x86_64.tar.gz安裝包
5. 解壓安裝包,配置filebeat.yml
setup.kibana:
host: "ip:5601"
username: "elastic"
password: "elastic"
output.logstash:
hosts: ["localhost:5044"]
6. 進入modules.d目錄,把nginx.yml.disable重命名爲nginx.yml,並配置以下信息
- module:nginx
# Access logs
access:
enabled: true
# Set custom paths for the log files. Ifleft empty,
# Filebeat will choose the paths dependingon your OS.
var.paths:["/usr/local/nginx/logs/access.log*"]
# Error logs
error:
enabled: true
# Set custom paths for the log files. Ifleft empty,
# Filebeat will choose the paths dependingon your OS.
var.paths:["/usr/local/nginx/logs/error.log*"]
7. 啓動filebeat
./filebeat -e -c filebeat.yml -d"publish"
8. 查詢數據
filebeat配置輸出可以直接輸出到elasticsearch,效果如下
查看dashboard數據
參考文獻:
https://www.elastic.co/cn/start
https://www.elastic.co/guide/index.html
https://www.cnblogs.com/aresxin/p/8035137.html