Cluster 集群
一個 Elasticsearch 集群由一個或多個節(jié)點(Node)組成,每個集群都有一個共同的集群名稱作為標識劈愚。
Node 節(jié)點
一個 Elasticsearch 實例即一個 Node,一臺機器可以有多個實例,正常使用下每個實例應該會部署在不同的機器上。Elasticsearch 的配置文件中可以通過 node.master蚓再、node.data 來設(shè)置節(jié)點類型。
node.master:表示節(jié)點是否具有成為主節(jié)點的資格
node.data:表示節(jié)點是否存儲數(shù)據(jù)
注意:此屬性的值為true包各,并不意味著這個節(jié)點就是主節(jié)點摘仅。
因為真正的主節(jié)點,是由多個具有主節(jié)點資格的節(jié)點進行選舉產(chǎn)生的问畅。
所以娃属,這個屬性只是代表這個節(jié)點是不是具有主節(jié)點選舉資格。
主節(jié)點+數(shù)據(jù)節(jié)點(默認)
<figure class="highlight plain" style="display: block; margin: 20px 0px; overflow: auto; padding: 0px; font-size: 13px; color: rgb(77, 77, 76); background: rgb(247, 247, 247); line-height: 1.6; border-radius: 1px; font-family: Lato, "PingFang SC", "Microsoft YaHei", sans-serif; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">
|
<pre style="overflow: auto; font-family: consolas, Menlo, "PingFang SC", "Microsoft YaHei", monospace; font-size: 13px; margin: 0px; padding: 10px; color: rgb(134, 145, 148); background: rgb(239, 242, 243); line-height: 1.6; border: none; text-align: right;">1
2
</pre>
|
<pre style="overflow: auto; font-family: consolas, Menlo, "PingFang SC", "Microsoft YaHei", monospace; font-size: 13px; margin: 0px; padding: 10px; color: rgb(77, 77, 76); background: rgb(247, 247, 247); line-height: 1.6; border: none; width: 142px;">node.master: true
node.data: true
</pre>
|
</figure>
節(jié)點即有成為主節(jié)點的資格护姆,又存儲數(shù)據(jù)膳犹。這個時候如果某個節(jié)點被選舉成為了真正的主節(jié)點,那么他還要存儲數(shù)據(jù)签则,這樣對于這個節(jié)點的壓力就比較大了。Elasticsearch 默認每個節(jié)點都是這樣的配置铐料,在測試環(huán)境下這樣做沒問題渐裂。實際工作中建議不要這樣設(shè)置,這樣相當于主節(jié)點和數(shù)據(jù)節(jié)點的角色混合到一塊了钠惩。
數(shù)據(jù)節(jié)點
<figure class="highlight plain" style="display: block; margin: 20px 0px; overflow: auto; padding: 0px; font-size: 13px; color: rgb(77, 77, 76); background: rgb(247, 247, 247); line-height: 1.6; border-radius: 1px; font-family: Lato, "PingFang SC", "Microsoft YaHei", sans-serif; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">
|
<pre style="overflow: auto; font-family: consolas, Menlo, "PingFang SC", "Microsoft YaHei", monospace; font-size: 13px; margin: 0px; padding: 10px; color: rgb(134, 145, 148); background: rgb(239, 242, 243); line-height: 1.6; border: none; text-align: right;">1
2
</pre>
|
<pre style="overflow: auto; font-family: consolas, Menlo, "PingFang SC", "Microsoft YaHei", monospace; font-size: 13px; margin: 0px; padding: 10px; color: rgb(77, 77, 76); background: rgb(247, 247, 247); line-height: 1.6; border: none; width: 149px;">node.master: false
node.data: true
</pre>
|
</figure>
節(jié)點沒有成為主節(jié)點的資格柒凉,不參與選舉,只會存儲數(shù)據(jù)篓跛。在集群中需要單獨設(shè)置幾個這樣的節(jié)點負責存儲數(shù)據(jù)膝捞,后期提供存儲和查詢服務(wù)。主要消耗磁盤愧沟,內(nèi)存蔬咬。
主節(jié)點
<figure class="highlight plain" style="display: block; margin: 20px 0px; overflow: auto; padding: 0px; font-size: 13px; color: rgb(77, 77, 76); background: rgb(247, 247, 247); line-height: 1.6; border-radius: 1px; font-family: Lato, "PingFang SC", "Microsoft YaHei", sans-serif; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">
|
<pre style="overflow: auto; font-family: consolas, Menlo, "PingFang SC", "Microsoft YaHei", monospace; font-size: 13px; margin: 0px; padding: 10px; color: rgb(134, 145, 148); background: rgb(239, 242, 243); line-height: 1.6; border: none; text-align: right;">1
2
</pre>
|
<pre style="overflow: auto; font-family: consolas, Menlo, "PingFang SC", "Microsoft YaHei", monospace; font-size: 13px; margin: 0px; padding: 10px; color: rgb(77, 77, 76); background: rgb(247, 247, 247); line-height: 1.6; border: none; width: 142px;">node.master: true
node.data: false
</pre>
|
</figure>
不會存儲數(shù)據(jù),有成為主節(jié)點的資格沐寺,可以參與選舉林艘,有可能成為真正的主節(jié)點。普通服務(wù)器即可(CPU混坞、內(nèi)存消耗一般)狐援。
客戶端節(jié)點
<figure class="highlight plain" style="display: block; margin: 20px 0px; overflow: auto; padding: 0px; font-size: 13px; color: rgb(77, 77, 76); background: rgb(247, 247, 247); line-height: 1.6; border-radius: 1px; font-family: Lato, "PingFang SC", "Microsoft YaHei", sans-serif; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">
|
<pre style="overflow: auto; font-family: consolas, Menlo, "PingFang SC", "Microsoft YaHei", monospace; font-size: 13px; margin: 0px; padding: 10px; color: rgb(134, 145, 148); background: rgb(239, 242, 243); line-height: 1.6; border: none; text-align: right;">1
2
</pre>
|
<pre style="overflow: auto; font-family: consolas, Menlo, "PingFang SC", "Microsoft YaHei", monospace; font-size: 13px; margin: 0px; padding: 10px; color: rgb(77, 77, 76); background: rgb(247, 247, 247); line-height: 1.6; border: none; width: 149px;">node.master: false
node.data: false
</pre>
|
</figure>
不會成為主節(jié)點钢坦,也不會存儲數(shù)據(jù),主要是針對海量請求的時候可以進行負載均衡啥酱。普通服務(wù)器即可(如果要進行分組聚合操作的話爹凹,建議這個節(jié)點內(nèi)存也分配多一點)
在生產(chǎn)環(huán)境下,如果不修改 Elasticsearch 節(jié)點的角色信息镶殷,在高數(shù)據(jù)量禾酱,高并發(fā)的場景下集群容易出現(xiàn)腦裂等問題。
Index 索引
一個集群下可以有多個索引批钠,每個索引是一系列相同格式文檔的集合(Elasticsearch 6.x 已不支持一個索引下多個Type)宇植。
Shard 分片
每個索引有一個或多個分片,每個分片存儲不同的數(shù)據(jù)埋心。分片可分為主分片( primary shard)和復制分片(replica shard)指郁,復制分片是主分片的拷貝。默認每個主分片有一個復制分片(默認一個索引創(chuàng)建后會有5個主分片拷呆,即:5主+5復制=10個分片)闲坎,一個索引的復制分片的數(shù)量可以動態(tài)地調(diào)整,復制分片從不與它的主分片在同一個節(jié)點上(防止單點故障)茬斧。
復制分片有兩個作用:
- 提高恢復能力:當主分片掛掉時腰懂,某個復制分片可以變成主分片;
- 提高性能:get 和 search 請求既可以由主分片又可以由復制分片處理项秉;
集群健康值
green:所有主要分片和復制分片都可用
yellow:所有主要分片可用绣溜,但不是所有復制分片都可用
-
red:不是所有的主要分片都可用
當集群狀態(tài)為 red,它仍然正常提供服務(wù)娄蔼,它會在現(xiàn)有存活分片中執(zhí)行請求怖喻,我們需要盡快修復故障分片,防止查詢數(shù)據(jù)的丟失岁诉;
Windows 環(huán)境搭建集群(3個Node锚沸,全部是主節(jié)點+數(shù)據(jù)節(jié)點)
下載安裝包
解壓后復制3份(每份啟動一個實例)
編輯每個文件下的 config/elasticsearch.yml
根據(jù)以下說明調(diào)整 elasticsearch.yml 對應參數(shù)配置,node2涕癣、node3 其他配置與node1一致哗蜈。
node1
<figure class="highlight plain" style="display: block; margin: 20px 0px; overflow: auto; padding: 0px; font-size: 13px; color: rgb(77, 77, 76); background: rgb(247, 247, 247); line-height: 1.6; border-radius: 1px; font-family: Lato, "PingFang SC", "Microsoft YaHei", sans-serif; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">
|
<pre style="overflow: auto; font-family: consolas, Menlo, "PingFang SC", "Microsoft YaHei", monospace; font-size: 13px; margin: 0px; padding: 10px; color: rgb(134, 145, 148); background: rgb(239, 242, 243); line-height: 1.6; border: none; text-align: right;">1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
</pre>
|
<pre style="overflow: auto; font-family: consolas, Menlo, "PingFang SC", "Microsoft YaHei", monospace; font-size: 13px; margin: 0px; padding: 10px; color: rgb(77, 77, 76); background: rgb(247, 247, 247); line-height: 1.6; border: none; width: 642px;"># 集群名稱,默認是 elasticsearch
cluster.name: es
節(jié)點名稱
node.name: node1
是否作為集群的主節(jié)點 坠韩,默認 true
node.master: true
是否作為集群的數(shù)據(jù)節(jié)點 距潘,默認 true
node.data: true
數(shù)據(jù)存儲位置,默認是es根目錄下的data文件夾
path.data: E:\elasticsearch\node1\data
日志存儲位置只搁,默認是es根目錄下的logs文件夾
path.logs: E:\elasticsearch\node1\logs
配置訪問本節(jié)點的地址
network.host: 0.0.0.0
設(shè)置對外服務(wù)的http端口绽昼,默認為9200
http.port: 9200
設(shè)置節(jié)點間交互的tcp端口,默認是9300
transport.tcp.port: 9300
配置所有用來組建集群的機器的IP地址
discovery.zen.ping.unicast.hosts: ["127.0.0.1:9300", "127.0.0.1:9301","127.0.0.1:9302"]
配置當前集群中最少具有 master 資格節(jié)點數(shù),對于多于兩個節(jié)點的集群環(huán)境须蜗,建議配置大于1
discovery.zen.minimum_master_nodes: 2
</pre>
|
</figure>
node2
<figure class="highlight plain" style="display: block; margin: 20px 0px; overflow: auto; padding: 0px; font-size: 13px; color: rgb(77, 77, 76); background: rgb(247, 247, 247); line-height: 1.6; border-radius: 1px; font-family: Lato, "PingFang SC", "Microsoft YaHei", sans-serif; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">
|
<pre style="overflow: auto; font-family: consolas, Menlo, "PingFang SC", "Microsoft YaHei", monospace; font-size: 13px; margin: 0px; padding: 10px; color: rgb(134, 145, 148); background: rgb(239, 242, 243); line-height: 1.6; border: none; text-align: right;">1
2
3
4
</pre>
|
<pre style="overflow: auto; font-family: consolas, Menlo, "PingFang SC", "Microsoft YaHei", monospace; font-size: 13px; margin: 0px; padding: 10px; color: rgb(77, 77, 76); background: rgb(247, 247, 247); line-height: 1.6; border: none; width: 292px;">path.data: E:\elasticsearch\node2\data
path.logs: E:\elasticsearch\node2\logs
http.port: 9201
transport.tcp.port: 9301
</pre>
|
</figure>
node3
<figure class="highlight plain" style="display: block; margin: 20px 0px; overflow: auto; padding: 0px; font-size: 13px; color: rgb(77, 77, 76); background: rgb(247, 247, 247); line-height: 1.6; border-radius: 1px; font-family: Lato, "PingFang SC", "Microsoft YaHei", sans-serif; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">
|
<pre style="overflow: auto; font-family: consolas, Menlo, "PingFang SC", "Microsoft YaHei", monospace; font-size: 13px; margin: 0px; padding: 10px; color: rgb(134, 145, 148); background: rgb(239, 242, 243); line-height: 1.6; border: none; text-align: right;">1
2
3
4
</pre>
|
<pre style="overflow: auto; font-family: consolas, Menlo, "PingFang SC", "Microsoft YaHei", monospace; font-size: 13px; margin: 0px; padding: 10px; color: rgb(77, 77, 76); background: rgb(247, 247, 247); line-height: 1.6; border: none; width: 292px;">path.data: E:\elasticsearch\node3\data
path.logs: E:\elasticsearch\node3\logs
http.port: 9202
transport.tcp.port: 9302
</pre>
|
</figure>
到目前位置硅确,集群的配置就完成了目溉,下面我們分別啟動每個實例。
根據(jù)配置文件中的注釋:
Prevent the “split brain” by configuring the majority of nodes (total number of master-eligible nodes / 2 + 1)
所以我們配置了 discovery.zen.minimum_master_nodes: 2 菱农,所以必須有兩個主節(jié)點啟動成功缭付,集群才算生效。
測試
進入目錄 elasticsearch-6.2.1-1 啟動第一個節(jié)點循未,執(zhí)行命令:bin\elasticsearch.bat陷猫。從日志中可以看出并沒有成功,因為沒發(fā)現(xiàn)足夠的master節(jié)點的妖。
當?shù)诙€master節(jié)點啟動成功時绣檬,整個集群狀態(tài)變?yōu)檎!?br>
3個節(jié)點全部啟動成功嫂粟,通過 elasticsearch-head 插件查看集群狀態(tài)娇未,通過集群健康值:green,表示集群一切正常星虹。目前集群內(nèi)沒有任何數(shù)據(jù)零抬,所以看不出索引與分片的情況。
Elasticsearch 一般會配合 Kibana + X-Pack 對集群數(shù)據(jù)分析宽涌、監(jiān)控等平夜,官方標配。這里使用了 elasticsearch-head 插件卸亮,一個比較小巧的工具忽妒。插件的安裝方法請看:elasticsearch-head 安裝介紹
從截圖可以看出,目前一共3個節(jié)點兼贸,一個索引 test段直,test 索引有5個主分片(邊框加粗),5個復制分片(邊框不加粗)寝受,分片會別均勻的分布到每個節(jié)點中。
我們嘗試干掉node3罕偎,node3 從集群退出之后很澄,集群在短時間內(nèi)會對分片進行重新分布,當然依賴遵循主颜及、復制分片不會在同一個Node甩苛。
如果我們繼續(xù)把node2干掉,那么整個集群就掛了俏站,集群健康值:未連接讯蒲。因為當前可用的主節(jié)點數(shù) 1 < discovery.zen.minimum_master_nodes 設(shè)置的 2。
我們嘗試把 discovery.zen.minimum_master_nodes 設(shè)置成 1肄扎,然后重啟啟動一個節(jié)點墨林,會發(fā)現(xiàn)有一個 Unassigned 的節(jié)點赁酝,集群健康值:yellow (5 of 10)。這種情況下代表主分片全部可用旭等,存在不可用的復制分片酌呆,5個復制分片沒有分配到節(jié)點上,不過此時的集群是可用的搔耕,我們?nèi)魏握埱蠖寄芴幚硐对皇撬械牟僮鞫悸涞街鞣制希铱赡芤l(fā)單點故障弃榨。當我們把第二個節(jié)點啟動后菩收,一切就恢復正常了,主分片的數(shù)據(jù)會同步到復制分片鲸睛。
實際生產(chǎn)環(huán)境娜饵,每個節(jié)點可能設(shè)置不同的節(jié)點類型,我們在3個節(jié)點的基礎(chǔ)上再增加兩個節(jié)點腊凶,然后調(diào)整 node.master 和node.data 的值划咐,最終設(shè)置為2個主節(jié)點,2個數(shù)據(jù)節(jié)點钧萍,1個客戶端節(jié)點褐缠。
node1 和 node2 是具有主節(jié)點選舉權(quán)限的節(jié)點,這里 node1 被選舉為master節(jié)點风瘦。node3 和 node4 是數(shù)據(jù)節(jié)點队魏,所以數(shù)據(jù)分片只會分配在這兩個節(jié)點上。node5 是客戶端節(jié)點万搔,最終是對請求起到負載均衡的作用胡桨。
如果是Linux環(huán)境下,啟動可能沒有這么順利瞬雹,可以參考 Linux 環(huán)境下安裝 elasticsearch 5.x昧谊、6.x 問題匯總 。