0. 背景瀏覽器
咱們在使用ELK進行日誌記錄的時候,經過網址在Kibana中查看咱們的應用程序(eg: Java Web)記錄的日誌,安全
可是默認是任何客戶端均可以訪問Kibana的, 這樣就會形成很不安全,咱們應該設置相應的用戶名和密碼,ruby
只有經過登陸用戶名和密碼才能經過Kibana查看咱們的日誌。app
1. 在elasticsearch 2.x的版本是怎麼作的elasticsearch
筆者網上查了一些博文,大部分推薦的是經過給elasticsearch安裝Shield插件,參考連接以下:測試
http://blog.csdn.net/xuplus/article/details/51611658字體
可是,這種作法已通過時了,並且當你從官網下載的elasticsearch的最新版本,筆者寫博文時候是5.x(5.2.2)spa
照着博文上安裝插件的作法,根本是不行的.net
通常博文會建議進入elasticsearch的根目錄,執行以下命令: bin/plugin install shield插件
可是,當你用的是5.x的時候,你會發現根本就沒有plugin這條命令,進入es的根目錄,發現只有
筆者恍然大悟,原來在5.x之後Shield插件已經做爲X-Pack的一部分了,因此,必須查找關於X-Pack的相關文檔。
2. X-Pack是什麼?
如下是官網給出的解釋:
(X-Pack is an Elastic Stack extension that bundles security, alerting, monitoring, reporting, and graph capabilities into one easy-to-install package.
Prior to Elasticsearch 5.0.0, you had to install separate Shield, Watcher, and Marvel plugins to get the features that are bundled together in X-Pack.
With X-Pack, you no longer have to worry about whether or not you have the right version of each plugin,
just install the X-Pack for the Elasticsearch version you’re running)
X-Pack是Elastic技術棧的擴展,它集安全,提醒,監控,報表以及圖標功能於一體。
在Elasticsearch 5.0以前,你必須單獨安裝Shield插件,還要配套Watcher, Marvel等插件,如今X-Pack把它們都整合到一起了。
原來是這樣啊!
3. 安裝X-Pack
3-1) 爲elasticsearch安裝X-Pack插件
進入 elasticsearch根目錄
執行:
bin/elasticsearch-plugin install x-pack
3-2) 配置elasticsearch.yml
進入config目錄
修改配置文件,在末尾加上以下行:
action.auto_create_index: .security,.monitoring*,.watches,.triggered_watches,.watcher-history*
這是爲elasticsearch增長自動建立索引功能
3-3) 啓動elasticsearch
bin/elasticsearch
3-4) 爲Kibana安裝X-Pack插件
進入Kibana根目錄
執行命令:
bin/kibana-plugin install x-pack
3-5) 啓動Kibana
bin/kibana
3-6) 爲Logstash節點安裝X-Pack插件
進入Logstash根目錄
執行命令:
bin/logstash-plugin install x-pack
3-7) 用配置文件啓動Logstash
bin/logstash -f config/log4j_multi_input.conf
3-8) 驗證
瀏覽器打開路徑:
默認用戶名和密碼是:
elastic
changeme
4. LogStash::Outputs::ElasticSearch::HttpClient::Pool::BadResponseCodeError, :error=>"Got response code '401' contact Elasticsearch at URL
這個時候,你可能認爲咱們已經大功告成了,然而並非這樣。
當你用用戶名和密碼登陸Kibana了之後,你會發現沒有任何索引,你以前使用Java程序寫的日誌到哪裏去了呢?
筆者十分納悶,後來查看了Logstash的控制檯,筆者發現了以下錯誤:
LogStash::Outputs::ElasticSearch::HttpClient::Pool::BadResponseCodeError, :error=>"Got response code '401' contact Elasticsearch at URL
由於咱們剛纔安裝了X-Pack插件,所以,咱們須要在咱們logstash的配置文件中指定用戶名和密碼,否則是沒有權限訪問的,
筆者的配置文件以下:
input { file { path => ["/Users/KG/Documents/logs/app-a/*.log"] type => "app-a" } file { path => ["/Users/KG/Documents/logs/app-b/*.log"] type => "app-b" } } output { stdout { codec => rubydebug } if [type] == "app-a" { elasticsearch { hosts => "localhost:9200" index => "app-a-%{+YYYY.MM.dd}" document_type => "log4j_type" user => elastic password => changeme } } else if [type] == "app-b" { elasticsearch { hosts => "localhost:9200" index => "app-b-%{+YYYY.MM.dd}" document_type => "log4j_type" user => elastic password => changeme } } }
紅色字體部分爲新加的
而後,再次從新啓動Logstash
5. 沒法查看索引下的日誌問題解決
好事多磨,咱們仍是沒法在Kibana下看到數據,到底是怎麼一回事呢?
筆者再次查看了logstash的控制檯,又發現了以下錯誤:
logstash outputs elasticsearch error 404 >>index_not_found_exception
上網查了下資料,原來須要在elasticsearch中建立自動索引
還記得剛纔咱們在elasticsearch.yml配置文件最後一行加的那句代碼嗎,看一下:
筆者修改以下:
action.auto_create_index: .security,.monitoring*,.watches,.triggered_watches,.watcher-history*,app-a-*,app-b-*
其中紅色字體部分爲筆者測試程序所用的索引
再次從新啓動elasticsearch
6. 最後的驗證
好了,筆者使用Java代碼進行驗證(以前的博文中有提到怎麼使用log4j進入日誌到ELK)
再次訪問Kibana,...看到以下結果:
好了,這回真的成功了,哈哈,是否是頗有成就感啊?^_^