轉載自我的個人博客:http://blog.ywheel.cn/post/2017/03/04/setup_elk_on_mac/
最近的項目需要對文本數據各字段進行快速檢索、組合查詢、模糊查詢,在架構選擇上選擇了Elasticsearch作為支撐這些功能的存儲和搜索引擎。其他的不說了,剛好我的第一台mac到了,直接搞起。
什么是ELK
日志分析平台可以有多種技術架構的選型,但經過了多年的演變,現在比較流行的應該就是ELK了。 ELK三個字母的意義如下:
- Elasticsearch
- Logstash
- Kibana
架構圖下圖:
圖中的Shipper和Indexer都可以是Logstash, Broker一般為Redis,也可以是kafka等。而Search & Storage則主要是Elasticsearch了,一方面接收上游index好的文檔,另一方面提供API支持對內容的檢索。而kibana則是一個web interface, 可以提供簡單易用的界面讓用戶方便的寫出搜索的表達式來訪問Elasticsearch.
對於這三部分都有很多深入的點,以后有機會要深入學習和記錄。
使用brew安裝
既然使用了mac,那么使用brew安裝程序則是一個最簡單不過的方式了。
首先安裝Elasticsearch,直接輸入以下命令即可:
brew install elasticsearch
- 1
但可能遇到問題,比如要求Java的版本是1.8(我安裝的Elasticsearch的按本是5.2.2),這里面可能涉及到還要安裝brew cast
用來安裝java8, 然后又提示還有其他依賴(后悔沒記錄下來。。。),而需要xcode-command-tool, 折騰了不少時間。
安裝完成后,可以查看elasticsearch的版本:
$ elasticsearch --version Version: 5.2.2, Build: f9d9b74/2017-02-24T17:26:45.835Z, JVM: 1.8.0_121
- 1
- 2
啟動和停止elasticsearch也很簡單:
brew services start elasticsearch brew services stop elasticsearch
- 1
- 2
瀏覽器訪問http://localhost:9200
可以看到Elasticsearch的信息:
{
name: "bWXgrRX", cluster_name: "elasticsearch_ywheel", cluster_uuid: "m99a1gFWQzKECuwnBfnTug", version: { number: "5.2.2", build_hash: "f9d9b74", build_date: "2017-02-24T17:26:45.835Z", build_snapshot: false, lucene_version: "6.4.1" }, tagline: "You Know, for Search" }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
接着安裝logstash:
brew install logstash
- 1
安裝好后查看版本:
$ logstash --version logstash 5.2.2
- 1
- 2
瀏覽器訪問http://localhost:9600
可以看到如下信息:
{
host: "ywheeldeMacBook-Pro.local", version: "5.2.2", http_address: "127.0.0.1:9600", id: "70b78f4a-fe0f-4187-bf71-fe1f60b74e0a", name: "ywheeldeMacBook-Pro.local", build_date: "2017-02-24T17:46:55Z", build_sha: "57984d20eb28b0df40a59077c600ec1a399d46f5", build_snapshot: false }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
Kibana不需要通過brew安裝,直接下載壓縮包后,解壓后執行./kibana
即可。不過我還是在/usr/local/bin/
下創建了kibana
和kibana-plugin
的軟連接, elasticsearch
,elasticsearch-plugin
,logstash
和logstash-plugin
都在這個目錄下,以后安裝插件的話,還都需要用上這些*-plugin
.
Kibana安裝完成后,需要在config/kibana.yml
文件中,確認elasticsearch.url: "http://localhost:9200"
測試寫入和查詢
寫入Elasticsearch可以很簡單,其本身就提供了RESTFul的API接口,參考https://www.elastic.co/guide/en/kibana/3.0/import-some-data.html ,通過以下命令創建shakespeare index:
curl -XPUT http://localhost:9200/shakespeare -d ' { "mappings" : { "_default_" : { "properties" : { "speaker" : {"type": "string", "index" : "not_analyzed" }, "play_name" : {"type": "string", "index" : "not_analyzed" }, "line_id" : { "type" : "integer" }, "speech_number" : { "type" : "integer" } } } } } ';
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
通過以下命令將數據寫入Elasticsearch:
curl -XPUT localhost:9200/_bulk --data-binary @shakespeare.json
- 1
數據寫入后,到kibana目錄運行./kibana
,啟動后訪問:http://localhost:5601/
, 看到kibana界面后會提示”Configure an index pattern”。, 如下圖:
剛才在寫入數據的時候已經創建了shakespeare index, 且不是按照時間分布的日志文件(shakespeare只有一個json文件),因此,取消勾選Index contains time-based envents
,輸入shakespeare
后,就能看到create
按鈕了。
點擊kibana的Discover頁面,輸入WESTMORELAND
查詢,可以看到有110個結果:
監控和安全
在Elasticsearch 5.x的時代,監控和管理由X-Pack統一完成,包含:
- 安全:用戶權限管理
- 告警:自動告警
- 監控:監控Elasticsearch集群的狀態
- 報告:發送報告、導出數據
- 圖表:可視化數據
在安裝X-Pack之前,需要停止Kibana和Elasticsearch:
elasticsearch-plugin install x-pack kibana-plugin install x-pack
- 1
- 2
安裝完成后,啟動elasticsearch和kibana,訪問kibana時發現需要登錄了, 默認用戶名和密碼是elastic/changeme。
后續可以在Management面板中進行用戶和角色的配置,也可以看到新增了Reporting。
在Monitoring頁面中可以看到Elasticsearch和Kibana的狀態,點擊Indices還可以看到具體索引的狀態。
告警功能和報表功能后續再進行詳細研究。之前在A家的時候,記得有個基於日志的告警功能:當service的日志中出現了ERROR或FATAL,可以自動觸發告警。有了X-Pack后,這個功能應該也是可以通過ELK來實現的啦。
通過訪問http://localhost:9200/_cat/indices?v
查看Elasticsearch的Index, 可以發現幾個新的與監控有關的index:
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size yellow open .monitoring-es-2-2017.03.04 COZvO_dlSkqdEtntrZrzFA 1 1 10240 154 4.5mb 4.5mb green open .security XEeHRF5NT0ud2jpxOzsoHw 1 0 1 0 2.8kb 2.8kb yellow open .kibana p-cJGBCXQySNGR0924jRdQ 1 1 2 1 9.8kb 9.8kb yellow open .monitoring-data-2 QZt0hpTISUO_58pWoG5Hyw 1 1 3 0 6.9kb 6.9kb yellow open .monitoring-kibana-2-2017.03.04 nLHKuL1KTiCE2lsWz8tdkA 1 1 849 0 245kb 245kb yellow open shakespeare zPCLp4KmTkiu7m4tYcA_Iw 5 1 111396 0 28.1mb 28.1mb
- 1
- 2
- 3
- 4
- 5
- 6
- 7
使用Logstash導入博文
在上面的操作中,直接使用了elasticsearch的API接口來進行數據的導入,而使用logstash也能夠很方便的寫入elasticsearch。 首先得生成一個logstash的conf文件,比如我想建立我的博客的索引,在我的家目錄下創建了my_blog.conf文件:
input{
file{
path => ["/Users/ywheel/my_blog/content/about.md"] } } output{ elasticsearch{ hosts => ["localhost:9200"] index => "my_blog" user => "elastic" password => "changeme" } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
注意拜X-Pack所賜,這配置文件里面對elasticsearch需要用戶名和密碼。然后敲入logstash -f my_blog.conf
來執行,但卻一直不成功。后來翻logstash的文檔 https://www.elastic.co/guide/en/logstash/current/pipeline.html ,里面寫了一句這樣的話:
Inputs
You use inputs to get data into Logstash. Some of the more commonly-used inputs are: - file: reads from a file on the filesystem, much like the UNIX command tail -0F - syslog: listens on the well-known port 514 for syslog messages and parses according to the RFC3164 format - redis: reads from a redis server, using both redis channels and redis lists. Redis is often used as a "broker" in a centralized Logstash installation, which queues Logstash events from remote Logstash "shippers". - beats: processes events sent by Filebeat.
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
file
這個input相當於使用tail
來獲取文件中的數據的啊, 我的about.md壓根就沒有變化,因此也沒有內容被寫入了Elasticsearch。於是,我把conf改成了這樣:
input{
file{
path => ["/Users/ywheel/test.md"] } } output{ elasticsearch{ hosts => ["localhost:9200"] index => "my_blog" user => "elastic" password => "changeme" } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
運行logstash -f my_blog_conf
后,再運行cat /Users/ywheel/my_blog/content/about.md > /Users/ywheel/test.md
, 然后發現數據寫入了Elasticsearch, index也多了一個my_blog
。到Kibana中添加my_blog
這個index pattern后,就可以在Discover進行搜索了。比如我搜索“程序員”:
看來中文分詞得改進一下,不過現在也已經很酷了! 以后可以對整個博客進行全文檢索了~~