ELK是elastic.co發(fā)布的三個產(chǎn)品庄蹋,分別是elasticsearch, logstash, kibana限书,分別用來做搜索引擎章咧、日志收集和報表展現(xiàn)。這三個東西經(jīng)常被用到的業(yè)務(wù)場景就是日志收集展現(xiàn)调限。
本文將從實用角度出發(fā)耻矮,教你如何用ELK快速搭建一個日志收集平臺忆谓。
elasticsearch
- 運(yùn)行elasticsearch
./bin/elasticsearch -d
-
測試運(yùn)行狀態(tài)
curl localhost:9200
-
創(chuàng)建索引
elasticsearch里面的表叫做索引(index)倡缠,可以通過http請求的方式來創(chuàng)建索引,創(chuàng)建的方式是通過put請求琢唾,curl腳本如下:
curl -X PUT \ http://localhost:9200/test1 \ -H 'cache-control: no-cache' \ -H 'postman-token: 438a164f-2ded-b73e-b2ab-e53dbd7f800c' \ -d '{ "settings" : { "index" : { "number_of_shards" : 3, "number_of_replicas" : 2 } } }'
-
常見問題
- 啟動失敗
ERROR: bootstrap checks failed
max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
這個問題是操作系統(tǒng)的vm.max_map_count參數(shù)設(shè)置太小導(dǎo)致的采桃,解決方法:
$ sudo sysctl -w vm.max_map_count=262144
vm.max_map_count = 262144
logstash
-
運(yùn)行l(wèi)ogstash
./bin/logstash -e 'input { stdin {} } output {stdout{}}'
如果運(yùn)行正常丘损,則輸入hello的運(yùn)行結(jié)果如下:
hello 2017-06-21T10:12:40.471Z MacBook-Pro.local hello
一般我們都是將配置寫到配置文件,用-f參數(shù)來運(yùn)行衔蹲,如:
./bin/logstash -f test.config
其中test.config里面包含的內(nèi)容就是之前-e參數(shù)所指向的運(yùn)行參數(shù)舆驶。
-
搭建http接口和json格式化
logstash配置文件由三個部分組成,上面已經(jīng)提到了input和output兩個部分畴博,另外還有一個是filter蓝仲,分別代表輸入->過濾->輸出袱结,現(xiàn)在我們配置一個http輸入途凫、通過json格式化维费、輸出到elasticsearch的logstash配置,配置文件內(nèi)容如下:
dengzongrongdeMacBook-Pro:logstash-5.4.2 RoyDeng$ cat test.config input { http { port => 31311 type => "http" codec => "json" } } filter { json { source => "message" } } output { elasticsearch { hosts => "localhost" index => "test" } }
通過post請求犀盟,可以看到elasticsearch里面增加了數(shù)據(jù)而晒,post請求如下:
curl -X POST \ http://localhost:31311/ \ -H 'cache-control: no-cache' \ -H 'postman-token: 888428ef-ee19-1030-9de4-6c4b333e4bca' \ -d '{"action":"test"}'
Kibana
-
配置運(yùn)行kibana
下載kibana
修改config/kibana.yml配置文件,修改elastic search url阅畴,如下:
# The URL of the Elasticsearch instance to use for all your queries. elasticsearch.url: "http://localhost:9200"
然后運(yùn)行
./bin/kibana
啟動kibana 配置報表
上面顯示的Time-field表示日志的時間倡怎,這個參數(shù)是必須的,因為kibana展現(xiàn)的一個重要維度就是時間贱枣,你上傳的數(shù)據(jù)必須有一個字段代表時間监署。
當(dāng)然,如果你是用logstash上傳的數(shù)據(jù)纽哥,那么這個字段不需要你配置钠乏,logstash會自動幫你加上,但是logstash幫你加的時間是上傳的時間晓避。如果你不是希望用上傳時間作為時間維度的話,這個字段就需要你上傳的時候自己加上了摔笤。
然后在首頁就能看到剛剛上傳上去的數(shù)據(jù)了
以上就是快速搭建日志平臺的方法够滑,后續(xù)會陸續(xù)補(bǔ)充ELK框架的一些高級用法,歡迎大家一起討論吕世。