1. 程式人生 > >X-Pack許可權控制之給Kibana加上登入控制以及index_not_found_exception問題解決

X-Pack許可權控制之給Kibana加上登入控制以及index_not_found_exception問題解決

0. 背景

我們在使用ELK進行日誌記錄的時候,通過網址在Kibana中檢視我們的應用程式(eg: Java Web)記錄的日誌,

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

只有通過登入使用者名稱和密碼才能通過Kibana檢視我們的日誌。

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

筆者網上查了一些博文,大部分推薦的是通過給elasticsearch安裝Shield外掛,參考連結如下:

但是,這種做法已經過時了,而且當你從官網下載的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,...看到如下結果:

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