開源實時日志分析ELK平台能夠完美的解決我們上述的問題,ELK由ElasticSearch、Logstash和Kiabana三個開源工具組成。
官方網站:https://www.elastic.co/products
-
Elasticsearch是個開源分布式搜索引擎,它的特點有:分布式,零配置,自動發現,索引自動分片,索引副本機制,restful風格接口,多數據源,自動搜索負載等。
-
Logstash是一個完全開源的工具,他可以對你的日志進行收集、過濾,並將其存儲供以后使用(如,搜索)。
-
Kibana 也是一個開源和免費的工具,它Kibana可以為 Logstash 和 ElasticSearch 提供的日志分析友好的 Web 界面,可以幫助您匯總、分析和搜索重要數據日志。
ELK下載:https://www.elastic.co/downloads/
ELK工作原理:

ElasticSearch
配置ElasticSearch:
unzip elasticsearch-6.2.4.zip cd elasticsearch-6.2.4
然后編輯ES的配置文件:
vi config/elasticsearch.yml
修改以下配置項:
cluster.name=es_cluster node.name=node0 path.data=/tmp/elasticsearch/data path.logs=/tmp/elasticsearch/logs #當前hostname或IP,我這里是node1 network.host=node1 network.port=9200
其他的選項保持默認,然后啟動ES:
nohup sh elasticsearch > nohup.log &
注意:
1.需要添加用戶elk,ES不能以root用戶進行啟動
2.可能出現的錯誤:
- max file descriptors [4096] for elasticsearch process likely too low, increase to at least [65536]
vi /etc/security/limits.conf elk soft nofile 819200 elk hard nofile 819200
- max number of threads [1024] for user [work] likely too low, increase to at least [2048]
vi /etc/security/limits.d/90-nproc.conf * soft nproc 1024 #修改為: * soft nproc 2048
- max virtual memory areas vm.max_map_count [65530] likely too low, increase to at least [262144]
vi /etc/sysctl.conf #增加改行配置: vm.max_map_count=655360 #保存退出后,執行: sysctl -p
- 另外再配置ES的時候,threadpool.bulk.queue_size 已經變成了thread_pool.bulk.queue_size ,ES_HEAP_SIZE,ES_MAX_MEM等配置都變為ES_JAVA_OPTS這一配置項,如限制內存最大最小為1G:
export ES_JAVA_OPTS="-Xms1g -Xmx1g"
然后可以打開頁面http://node1:9200/,將會看到以下內容:(我是通過外部訪問虛擬機,因此為了簡單沒有配置host文件,直接用ip訪問)

Logstash
配置Logstash:
tar -zxvf logstash-6.2.4.tar.gz cd logstash-6.2.4
編寫配置文件(名字和位置可以隨意,這里我放在config目錄下,取名為log_app.conf):
vi config/log_app.config
#以下為內容
input {
file {
path => "/usr/local/software/elk/app.log"
start_position => "beginning" #從文件開始處讀寫
}
# stdin {} #可以從標准輸入讀數據
}
filter {
#Only matched data are send to output.
}
output {
# For detail config for elasticsearch as output,
# See: https://www.elastic.co/guide/en/logstash/current/plugins-outputs-elasticsearch.html
elasticsearch {
action => "index" #The operation on ES
hosts => "node1:9200" #ElasticSearch host, can be array.
index => "applog" #The index to write data to.
}
}
其他的選項保持默認,然后啟動Logstash:
# -f為指定配置文件 nohup sh ./bin/logstash -f ../config/log_app.config > nohup.log &
日志:

Kibana
配置Kibana:
tar -zxvf kibana-6.2.4-linux-x86_64.tar.gz cd kibana-6.2.4-linux-x86_64
修改以下幾項(由於是單機版的,因此host的值也可以使用localhost來代替,這里僅僅作為演示):
server.port: 5601 server.host: “node1” elasticsearch.url: http://node1:9200 kibana.index: “.kibana”
啟動kibana:
nohup sh ./bin/kibana > nohup.log &
啟動后界面:

然后需要創建index,步驟如下:
①點擊左邊iscover出現以下界面

②按照注釋配置,然后點擊Next step,在第二頁 選擇@timestamp點擊create創建

③創建完成之后,可以看到以下一個界面,紅框內是 自動生成的域,也可以理解為 跟數據庫中的字段類似,其中有一個message字段,就是我們想要的日志信息。

④再次點擊Discover出現以下界面,可以看到默認搜索的是最后15分鍾的日志,可以通過點擊設置搜索的時間范圍.

⑤可以點擊右側域的add設置需要顯示的字段

添加完成之后,日志顯示如下:

參考:https://my.oschina.net/itblog/blog/547250
https://blog.csdn.net/abcd_d_/article/details/53018927
https://blog.csdn.net/qq_34021712/article/details/79364592
https://www.cnblogs.com/yincheng/p/logstash.html
