轉(zhuǎn)~Elasticsearch 集群

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é)點上(防止單點故障)茬斧。

復制分片有兩個作用:

  1. 提高恢復能力:當主分片掛掉時腰懂,某個復制分片可以變成主分片;
  2. 提高性能:get 和 search 請求既可以由主分片又可以由復制分片處理项秉;

集群健康值

  1. green:所有主要分片和復制分片都可用

  2. yellow:所有主要分片可用绣溜,但不是所有復制分片都可用

  3. red:不是所有的主要分片都可用

    當集群狀態(tài)為 red,它仍然正常提供服務(wù)娄蔼,它會在現(xiàn)有存活分片中執(zhí)行請求怖喻,我們需要盡快修復故障分片,防止查詢數(shù)據(jù)的丟失岁诉;

Windows 環(huán)境搭建集群(3個Node锚沸,全部是主節(jié)點+數(shù)據(jù)節(jié)點)

下載安裝包

http://www.elastic.co/downloads/elasticsearch

解壓后復制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 安裝介紹

添加測試數(shù)據(jù):


test索引分片分布

從截圖可以看出,目前一共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 問題匯總

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末酗捌,一起剝皮案震驚了整個濱河市呢诬,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌胖缤,老刑警劉巖尚镰,帶你破解...
    沈念sama閱讀 216,919評論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異哪廓,居然都是意外死亡狗唉,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,567評論 3 392
  • 文/潘曉璐 我一進店門涡真,熙熙樓的掌柜王于貴愁眉苦臉地迎上來分俯,“玉大人肾筐,你說我怎么就攤上這事“钠龋” “怎么了局齿?”我有些...
    開封第一講書人閱讀 163,316評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長橄登。 經(jīng)常有香客問我抓歼,道長,這世上最難降的妖魔是什么拢锹? 我笑而不...
    開封第一講書人閱讀 58,294評論 1 292
  • 正文 為了忘掉前任谣妻,我火速辦了婚禮,結(jié)果婚禮上卒稳,老公的妹妹穿的比我還像新娘蹋半。我一直安慰自己,他們只是感情好充坑,可當我...
    茶點故事閱讀 67,318評論 6 390
  • 文/花漫 我一把揭開白布减江。 她就那樣靜靜地躺著,像睡著了一般捻爷。 火紅的嫁衣襯著肌膚如雪辈灼。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,245評論 1 299
  • 那天也榄,我揣著相機與錄音巡莹,去河邊找鬼。 笑死甜紫,一個胖子當著我的面吹牛降宅,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播囚霸,決...
    沈念sama閱讀 40,120評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼腰根,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了拓型?” 一聲冷哼從身側(cè)響起额嘿,我...
    開封第一講書人閱讀 38,964評論 0 275
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎吨述,沒想到半個月后岩睁,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體钞脂,經(jīng)...
    沈念sama閱讀 45,376評論 1 313
  • 正文 獨居荒郊野嶺守林人離奇死亡揣云,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,592評論 2 333
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了冰啃。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片邓夕。...
    茶點故事閱讀 39,764評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡刘莹,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出焚刚,到底是詐尸還是另有隱情点弯,我是刑警寧澤,帶...
    沈念sama閱讀 35,460評論 5 344
  • 正文 年R本政府宣布矿咕,位于F島的核電站抢肛,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏碳柱。R本人自食惡果不足惜捡絮,卻給世界環(huán)境...
    茶點故事閱讀 41,070評論 3 327
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望莲镣。 院中可真熱鬧福稳,春花似錦、人聲如沸瑞侮。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,697評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽半火。三九已至越妈,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間慈缔,已是汗流浹背叮称。 一陣腳步聲響...
    開封第一講書人閱讀 32,846評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留藐鹤,地道東北人瓤檐。 一個月前我還...
    沈念sama閱讀 47,819評論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像娱节,于是被迫代替她去往敵國和親挠蛉。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,665評論 2 354

推薦閱讀更多精彩內(nèi)容