Mac上搭建ELK

轉載自個人我的博客:http://blog.ywheel.cn/post/2017/03/04/setup_elk_on_mac/html

最近的項目須要對文本數據各字段進行快速檢索、組合查詢、模糊查詢,在架構選擇上選擇了Elasticsearch做爲支撐這些功能的存儲和搜索引擎。其餘的不說了,恰好個人第一臺mac到了,直接搞起。java

什麼是ELK

日誌分析平臺能夠有多種技術架構的選型,但通過了多年的演變,如今比較流行的應該就是ELK了。 ELK三個字母的意義以下:程序員

  1. Elasticsearch
  2. Logstash
  3. Kibana

架構圖下圖:web

ELK架構圖

圖中的Shipper和Indexer均可以是Logstash, Broker通常爲Redis,也能夠是kafka等。而Search & Storage則主要是Elasticsearch了,一方面接收上游index好的文檔,另外一方面提供API支持對內容的檢索。而kibana則是一個web interface, 能夠提供簡單易用的界面讓用戶方便的寫出搜索的表達式來訪問Elasticsearch.redis

對於這三部分都有不少深刻的點,之後有機會要深刻學習和記錄。sql

使用brew安裝

既然使用了mac,那麼使用brew安裝程序則是一個最簡單不過的方式了。json

首先安裝Elasticsearch,直接輸入如下命令便可:xcode

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/下建立了kibanakibana-plugin的軟鏈接, elasticsearch,elasticsearch-plugin,logstashlogstash-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」。, 以下圖:

kibana 1

剛纔在寫入數據的時候已經建立了shakespeare index, 且不是按照時間分佈的日誌文件(shakespeare只有一個json文件),所以,取消勾選Index contains time-based envents,輸入shakespeare後,就能看到create按鈕了。

kibana 2

點擊kibana的Discover頁面,輸入WESTMORELAND查詢,能夠看到有110個結果:

kibana 3

監控和安全

在Elasticsearch 5.x的時代,監控和管理由X-Pack統一完成,包含:

  1. 安全:用戶權限管理
  2. 告警:自動告警
  3. 監控:監控Elasticsearch集羣的狀態
  4. 報告:發送報告、導出數據
  5. 圖表:可視化數據

在安裝X-Pack以前,須要中止Kibana和Elasticsearch:

elasticsearch-plugin install x-pack kibana-plugin install x-pack
  • 1
  • 2

安裝完成後,啓動elasticsearch和kibana,訪問kibana時發現須要登陸了, 默認用戶名和密碼是elastic/changeme。

kibana login

後續能夠在Management面板中進行用戶和角色的配置,也能夠看到新增了Reporting。

kibana management

在Monitoring頁面中能夠看到Elasticsearch和Kibana的狀態,點擊Indices還能夠看到具體索引的狀態。

kibana monitoring 1

kibana monitoring 1

告警功能和報表功能後續再進行詳細研究。以前在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進行搜索了。好比我搜索「程序員」:

kibana search

看來中文分詞得改進一下,不過如今也已經很酷了! 之後能夠對整個博客進行全文檢索了~~

相關文章
相關標籤/搜索