前言

本篇文章將簡單介紹如何建立 Elasticsearch Cluster(叢集)。 Cluster 包含了許多 Elasticsearch Node(節點),每個節點負責不同的工作,包含統整控制、資料同步、資料寫入與搜尋...等工作。藉由建立叢集架構,我們可以提高 Elasticsearch的可用性與。理所當然,單一 cluster 的能力有限,另在備援與災害防護考量下,大型系統可能會有多個 Cluster 互相支援。本篇文章將簡單介紹如何在 Windows 作業系統環境下建立 Elasticsearch cluster,若有錯誤或建議也請各位先進不吝提出。



介紹

本章節主要分成三個主要內容:
  1. Node 類型
  2. Cluster 實作
  3. Elasticsearch-head 使用


Node 類型

Master
用於判斷建立與刪除索引、追蹤群集內的node、確認shards在哪一個node,在群集中扮演相當重要的角色。雖然master node也能進行索引與搜尋,但為了維持群集的穩定性,官方不建議master進行此類型工作。設定方法如下:
node.master: true
node.data: false
node.ingest: false

Data
用於儲存資料的節點,主要進行 CRUD、搜尋與 aggregations 等操作。對於data node來說,其I/O、記憶體、CPU等資源相當重要,若資源不足必須加入額外的data node。設定方法如下:
node.master: false
node.data: true
node.ingest: false

Ingest
5.0版新功能節點,可以pipeline方式處理大量資料接收。設定方法如下:

node.master: false
node.data: false
node.ingest: true

Coordinating only nodes
主要能夠進行route requests, handle the search reduce phase, and distribute bulk indexing等工作。過多的coordinating only nodes可能造成cluster負擔。設定方法如下:
node.master: false
node.data: false
node.ingest: false



Cluster 實作

我們所設計的 Cluster 架構如下圖:(4個 data node、1個ingest node與 3個master node)

Step 1.首先我們依據架構圖內容,先建立下列資料夾


Step 2.我們下載Elasticsearch-5.2.2後,分別解縮到上述資料夾(如下圖):


Step 3 首先我們先設定 Data Node,開啟 dataNode\elasticsearch-5.2.2\config\elasticsearch.yml,輸入資料如下:
cluster.name: duran_test_cluster
node.name: data_1
node.master: false
node.data: true
node.ingest: false


Step 4. 往下拉,輸入資料如下:
network.host: 192.168.1.65
http.port: 9401
discovery.zen.ping.unicast.hosts: ["192.168.1.65:9201", "192.168.1.65:9202", "192.168.1.65:9203", "192.168.1.65:9301", "192.168.1.65:9302", "192.168.1.65:9401", "192.168.1.65:9402", "192.168.1.65:9403", "192.168.1.65:9404"]


Step 5. 執行elasticsearch.bat,開啟瀏覽器輸入網址與連接埠,即可看見節點狀態
http://192.168.1.65:9401/


Step 6. 輸入網址、連接埠加上 /_cluster/stats,可以檢視 Cluster 狀態
http://192.168.1.65:9401/_cluster/stats


Step 7. 依此類推,我們設定每一個 data node。
cluster.name: duran_test_cluster (全部一致)
node.name: (分別設定 data_2, data_3, data_4 共四個data node)
node.master: false 
node.data: true
node.ingest: false
network.host: 192.168.1.65
http.port: (分別設定 9402, 9403, 9404共四個data node)
discovery.zen.ping.unicast.hosts: ["192.168.1.65:9201", "192.168.1.65:9202", "192.168.1.65:9203", "192.168.1.65:9301", "192.168.1.65:9302", "192.168.1.65:9401", "192.168.1.65:9402", "192.168.1.65:9403", "192.168.1.65:9404"]


Step 8. 依此類推,我們設定每一個 master node。
cluster.name: duran_test_cluster (全部一致)
node.name: (分別設定 master_1, master_2, master_3共三個 master node)
node.master: true
node.data: false
node.ingest: false
network.host: 192.168.1.65
http.port: (分別設定 9201, 9202, 9203共三個 master node)
discovery.zen.ping.unicast.hosts: ["192.168.1.65:9201", "192.168.1.65:9202", "192.168.1.65:9203", "192.168.1.65:9301", "192.168.1.65:9302", "192.168.1.65:9401", "192.168.1.65:9402", "192.168.1.65:9403", "192.168.1.65:9404"]


Step 9. 依此類推,我們設定一個 ingest node。
cluster.name: duran_test_cluster
node.name: ingest_1
node.master: false
node.data: false
node.ingest: true
network.host: 192.168.1.65
http.port:  9301
discovery.zen.ping.unicast.hosts: ["192.168.1.65:9201", "192.168.1.65:9202", "192.168.1.65:9203", "192.168.1.65:9301", "192.168.1.65:9302", "192.168.1.65:9401", "192.168.1.65:9402", "192.168.1.65:9403", "192.168.1.65:9404"]


Step 10. 啟動所有節點 (執行elasticsearch.bat),檢視我們節點狀態
http://192.168.1.65:9401/_cluster/stats



Elasticsearch-head 使用

我們能透過 elasticsearch-head 這個圖形化介面套件,協助我們檢視與管理Elasticsearch cluster。

Step 1.我們前往 elasticsearch-head 進行下載。


Step 2.若您使用的 Elasticsearch版本為0.x、1.x、2.x 的版本,你可以透過指令安裝這個plugin;若是使用5.0版本,elasticsearch-head 為獨立運作的伺服器,您必須在網站下載zip檔案且解壓縮。


Step 3.在 elasticsearch-head 目錄下,輸入下列指令:
npm install

grunt server


Step 4.開啟瀏覽器,輸入網址:
http://localhost:9100/


Step 5.若您有執行所有節點,啟動cluster,你可以在網站輸入 http://192.168.1.65:9201/,按下連結後,檢視目前cluster狀態





參考資料