ELK菜鳥手記 (三) - X-Pack權限控制之給Kibana加上登錄控制以及index_not_found_exception問題解決


 

0. 背景

我們在使用ELK進行日志記錄的時候,通過網址在Kibana中查看我們的應用程序(eg: Java Web)記錄的日志,

但是默認是任何客戶端都可以訪問Kibana的, 這樣就會造成很不安全,我們應該設置相應的用戶名和密碼,

只有通過登錄用戶名和密碼才能通過Kibana查看我們的日志。

 

1. 在elasticsearch 2.x的版本是怎么做的

筆者網上查了一些博文,大部分推薦的是通過給elasticsearch安裝Shield插件,參考鏈接如下:

http://blog.csdn.net/xuplus/article/details/51611658

但是,這種做法已經過時了,而且當你從官網下載的elasticsearch的最新版本,筆者寫博文時候是5.x(5.2.2)

照着博文上安裝插件的做法,根本是不行的

一般博文會建議進入elasticsearch的根目錄,執行如下命令: bin/plugin install shield

但是,當你用的是5.x的時候,你會發現根本就沒有plugin這條命令,進入es的根目錄,發現只有

elasticsearch-plugin這條命令,這是怎么回事呢?
於是筆者上了官網一探究竟(任何時候查找技術,官網永遠是最好最權威的選擇)
官網給出的解釋如下:

 

筆者恍然大悟,原來在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,...看到如下結果:

 

好了,這回真的成功了,哈哈,是不是很有成就感啊?^_^

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM