(4)ElasticSearch在linux環境中搭建集群
- 2021 年 9 月 12 日
- 筆記
- elasticsearch
1.概述
一個運行中的Elasticsearch實例稱為一個節點(node),而集群是由一個或者多個擁有相同cluster.name配置的節點組成,它們共同承擔數據和負載的壓力。當有節點加入集群中或者從集群中移除節點時,集群將會重新平均分布所有的數據。
如一個節點被選舉成為主節點時,它將負責管理集群範圍內的所有變更,例如增刪索引或者節點等。而主節點並不需要涉及到文檔級別的變更和搜索等操作,所以當集群只擁有一個主節點的情況下,即使流量的增加它也不會成為瓶頸。任何節點都可以成為主節點。
用戶可以將請求發送到集群中的任何節點,包括主節點。每個節點都知道任意文檔所處的位置,並且能夠將我們的請求直接轉發到存儲我們所需文檔的節點。無論我們將請求發送到哪個節點,它都能負責從各個包含我們所需文檔的節點收集回數據,並將最終結果返回給客戶端。Elasticsearch對這一切的管理都是透明的。
2. 為什麼要實現Elasticsearch集群
如果是單節點集群,一旦該節點出現故障,無法做到故障轉移,那麼Elasticsearch就會無法訪問,這對系統來說是災難性的,而部署多節點集群,能夠水平擴容,故障轉移,實現高可用,解決高並發。
2.1高可用
假設我們部署了三個節點集群(三個主分片P0、P1、P2,分別各對應兩個副本分片R0、R1、R2):
如果主節點Node1故障了,Elasticsearch內部機制會根據節點故障重新選舉一個節點作為主節點,而主副分片也會重新調整:
但因為副本分片是主副一比二緣故,此時集群健康狀態會是yellow。但當故障節點恢復正常時候,集群健康狀態會是green恢復正常。
2.2存儲空間
一台伺服器存儲空間肯定是有限的,當數據量上來時候,這是災難性的。如果部署多台伺服器節點集群,那麼就能解決存儲空間的問題了。
3.集群搭建
3.1環境準備
System |
IP |
Node |
Leader |
CentOS-7 |
192.168.142.129 |
node-1 |
master |
192.168.142.130 |
node-2 |
replica |
分別往129、130伺服器部署兩個Elasticsearch節點,安裝詳情可以參考我第一篇文章。
3.2修改elasticsearch.yml配置
# ---------------------------------- Cluster ----------------------------------- # # Use a descriptive name for your cluster: # cluster.name: my-ebs # # ------------------------------------ Node ------------------------------------ # # Use a descriptive name for the node: # node.name: node-1 # # Add custom attributes to the node: # #node.attr.rack: r1 # # ----------------------------------- Memory ----------------------------------- # # Lock the memory on startup: # bootstrap.memory_lock: true # # Make sure that the heap size is set to about half the memory available # on the system and that the owner of the process is allowed to use this # limit. # # Elasticsearch performs poorly when the system is swapping the memory. # # ---------------------------------- Network ----------------------------------- # # By default Elasticsearch is only accessible on localhost. Set a different # address here to expose this node on the network: # network.host: 192.168.142.129 # # By default Elasticsearch listens for HTTP traffic on the first free port it # finds starting at 9200. Set a specific HTTP port here: # #http.port: 9200 # # For more information, consult the network module documentation. # # --------------------------------- Discovery ---------------------------------- # # Pass an initial list of hosts to perform discovery when this node is started: # The default list of hosts is ["127.0.0.1", "[::1]"] # #discovery.seed_hosts: ["host1", "host2"] discovery.seed_hosts: ["192.168.142.129", "192.168.142.130"] # # Bootstrap the cluster using an initial set of master-eligible nodes: # cluster.initial_master_nodes: ["node-1", "node-2"] # # For more information, consult the discovery and cluster formation module documentation. #
這是node-1修改後的配置,node-2配置相差不大,區別如下:
node.name: node-2 network.host: 192.168.142.130
集群節點外部訪問HTTP埠默認都是9200,內部TCP連接埠默認都是9300。如果是同一個伺服器不同節點,那麼就要配置不同的埠號了。具體配置說明詳情可以查看官網。
3.3允許Elasticsearch埠訪問
輸入如下命令放開防火牆對Elasticsearch埠訪問限制:
firewall-cmd --zone=public --add-port=9200/tcp --permanent firewall-cmd --zone=public --add-port=9300/tcp –permanent
重新載入防火牆:
firewall-cmd –reload
3.4啟動Elasticsearch集群
如何啟動Elasticsearch或者啟動過程會遇到什麼樣問題,不懂的可以查看我第一篇文章,裡面基本都有介紹,這裡就不一一描述了,因為集群搭建主要是配置文件:
●node1:
●node2:
●查看集群節點狀況:
//192.168.142.129:9200/_cat/nodes?pretty
註:*符號表示主節點,-表示副節點。
●查看集群狀態:
//192.168.142.129:9200/_cluster/health
status欄位指示著當前集群在總體上是否工作正常。它的三種顏色含義如下:
green:所有的主分片和副本分片都正常運行。
yellow:所有的主分片都正常運行,但不是所有的副本分片都正常運行。
red:有主分片沒能正常運行。
參考文獻:
Important Elasticsearch configuration
應對故障