ES系列十六、集群配置和維護管理


一、修改配置文件

1.節點配置

1.vim elasticsearch.yml

# ======================== Elasticsearch Configuration =========================
#
# NOTE: Elasticsearch comes with reasonable defaults for most settings.
#       Before you set out to tweak and tune the configuration, make sure you
#       understand what are you trying to accomplish and the consequences.
#
# The primary way of configuring a node is via this file. This template lists
# the most important settings you may want to configure for a production cluster.
#
# Please consult the documentation for further information on configuration options:
# https://www.elastic.co/guide/en/elasticsearch/reference/index.html
#
# ---------------------------------- Cluster -----------------------------------
#
# Use a descriptive name for your cluster:
# 集群名稱
cluster.name: es-test
#
# ------------------------------------ Node ------------------------------------
#
# Use a descriptive name for the node:
# 節點名稱
node.name: node-1
#
# Add custom attributes to the node:
#
node.attr.rack: r1
#
# ----------------------------------- Paths ------------------------------------
#
# Path to directory where to store the data (separate multiple locations by comma):
# 
path.data: /home/esdata/data
#
# Path to log files:
#
path.logs: /home/esdata/log
#
# ----------------------------------- Memory -----------------------------------
#
# Lock the memory on startup:
#
bootstrap.memory_lock: false
bootstrap.system_call_filter: false
#
# 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 -----------------------------------
#
# Set the bind address to a specific IP (IPv4 or IPv6):
#
network.host: 0.0.0.0
#
# Set a custom port for HTTP:
#
http.port: 9200
#
# For more information, consult the network module documentation.
#
# --------------------------------- Discovery ----------------------------------
#
# Pass an initial list of hosts to perform discovery when new node is started:
# The default list of hosts is ["127.0.0.1", "[::1]"]
# 節點主機
discovery.zen.ping.unicast.hosts: ["192.168.1.102","192.168.1.103","192.168.1.104"]
#
# Prevent the "split brain" by configuring the majority of nodes (total number of master-eligible nodes / 2 + 1):
# 主節點數量
discovery.zen.minimum_master_nodes: 1 
#
# For more information, consult the zen discovery module documentation.
#
# ---------------------------------- Gateway -----------------------------------
#
# Block initial recovery after a full cluster restart until N nodes are started:
# 節點發現多少開始恢復
gateway.recover_after_nodes: 3
#
# For more information, consult the gateway module documentation.
#
# ---------------------------------- Various -----------------------------------
#
# Require explicit names when deleting indices:
#
action.destructive_requires_name: true
http.cors.enabled: true
http.cors.allow-origin: "*"

其他節點文件除了節點名稱不一樣,其他都一樣。

2.驗證

1.啟動三台機器ES和head

2.訪問head地址查看

集群一共兩個三個節點,test有5分片,每個分片兩個副本,停掉節點二,集群副本0,2,4不見了

 

二、集群規划

搭建一個集群我們需要考慮如下幾個問題:

1. 我們需要多大規模的集群?

2. 集群中的節點角色如何分配?

3. 如何避免腦裂問題?

4. 索引應該設置多少個分片?

5. 分片應該設置幾個副本?

下面我們就來分析和回答這幾個問題

1、我們需要多大規模的集群?

需要從以下兩個方面考慮:

1.1 當前的數據量有多大?數據增長情況如何?
1.2 你的機器配置如何?cpu、多大內存、多大硬盤容量?

推算的依據:

ES JVM heap 最大可以設置32G 。
30G heap 大概能處理的數據量 10 T。如果內存很大如128G,可在一台機器上運行多個ES節點實例。

備注:集群規划滿足當前數據規模+適量增長規模即可,后續可按需擴展。

兩類應用場景:

A. 用於構建業務搜索功能模塊,且多是垂直領域的搜索。數據量級幾千萬到數十億級別。一般2-4台機器的規模。
B. 用於大規模數據的實時OLAP(聯機處理分析),經典的如ELK Stack,數據規模可能達到千億或更多。幾十到上百節點的規模。

2、集群中的節點角色如何分配?

2.1 節點角色:

Master
node.master: true 節點可以作為主節點
DataNode
node.data: true 默認是數據節點。
Coordinate node 協調節點
如果僅擔任協調節點,將上兩個配置設為false。

說明:

一個節點可以充當一個或多個角色,默認三個角色都有

協調節點:一個節點只作為接收請求、轉發請求到其他節點、匯總各個節點返回數據等功能的節點。就叫協調節點

2.2 如何分配:

A. 小規模集群,不需嚴格區分。
B. 中大規模集群(十個以上節點),應考慮單獨的角色充當。特別並發查詢量大,查詢的合並量大,可以增加獨立的協調節點。角色分開的好處是分工分開,不互影響。如不會因協調角色負載過高而影響數據節點的能力。

3、如何避免腦裂問題?

3.1 腦裂問題

一個集群中只有一個A主節點,A主節點因為需要處理的東西太多或者網絡過於繁忙,從而導致其他從節點ping不通A主節點,這樣其他從節點就會認為A主節點不可用了,就會重新選出一個新的主節點B。過了一會A主節點恢復正常了,這樣就出現了兩個主節點,導致一部分數據來源於A主節點,另外一部分數據來源於B主節點,出現數據不一致問題,這就是腦裂

3.2 盡量避免腦裂,需要添加最小數量的主節點配置:

discovery.zen.minimum_master_nodes: (有master資格節點數/2) + 1

這個參數控制的是,選舉主節點時需要看到最少多少個具有master資格的活節點,才能進行選舉。官方的推薦值是(N/2)+1,其中N是具有master資格的節點的數量。

3.3 常用做法(中大規模集群):

1. Master 和 dataNode 角色分開,配置奇數個master,如3
2. 單播發現機制,配置master資格節點:

discovery.zen.ping.multicast.enabled: false —— 關閉多播發現機制,默認是關閉的

discovery.zen.ping.unicast.hosts: ["master1", "master2", "master3"] —— 配置單播發現的主節點ip地址,其他從節點要加入進來,就得去詢問單播發現機制里面配置的主節點我要加入到集群里面了,主節點同意以后才能加入,然后主節點再通知集群中的其他節點有新節點加入

3. 配置選舉發現數,及延長ping master的等待時長

discovery.zen.ping_timeout: 30(默認值是3秒)——其他節點ping主節點多久時間沒有響應就認為主節點不可用了
discovery.zen.minimum_master_nodes: 2 —— 選舉主節點時需要看到最少多少個具有master資格的活節點,才能進行選舉

4、索引應該設置多少個分片?

說明:分片數指定后不可變,除非重索引。

思考:

分片對應的存儲實體是什么?

  存儲的實體是索引

分片是不是越多越好?

  不是

分片多有什么影響?

  分片多浪費存儲空間、占用資源、影響性能

4.1 分片過多的影響:

每個分片本質上就是一個Lucene索引, 因此會消耗相應的文件句柄, 內存和CPU資源。
每個搜索請求會調度到索引的每個分片中. 如果分片分散在不同的節點倒是問題不太. 但當分片開始競爭相同的硬件資源時, 性能便會逐步下降。
ES使用詞頻統計來計算相關性. 當然這些統計也會分配到各個分片上. 如果在大量分片上只維護了很少的數據, 則將導致最終的文檔相關性較差。

4.2 分片設置的可參考原則:

ElasticSearch推薦的最大JVM堆空間是30~32G, 所以把你的分片最大容量限制為30GB, 然后再對分片數量做合理估算. 例如, 你認為你的數據能達到200GB, 推薦你最多分配7到8個分片。
在開始階段, 一個好的方案是根據你的節點數量按照1.5~3倍的原則來創建分片. 例如,如果你有3個節點, 則推薦你創建的分片數最多不超過9(3x3)個。當性能下降時,增加節點,ES會平衡分片的放置。
對於基於日期的索引需求, 並且對索引數據的搜索場景非常少. 也許這些索引量將達到成百上千, 但每個索引的數據量只有1GB甚至更小. 對於這種類似場景, 建議只需要為索引分配1個分片。如日志管理就是一個日期的索引需求,日期索引會很多,但每個索引存放的日志數據量就很少。

5、分片應該設置幾個副本?

說明:副本數是可以隨時調整的!

思考:

副本的用途是什么?

  備份數據保證高可用數據不丟失,高並發的時候參與數據查詢
針對它的用途,我們該如何設置它的副本數?

  一般一個分片有1-2個副本即可保證高可用
集群規模沒變的情況下副本過多會有什么影響?

  副本多浪費存儲空間、占用資源、影響性能

5.1 副本設置基本原則:

為保證高可用,副本數設置為2即可。要求集群至少要有3個節點,來分開存放主分片、副本。
如發現並發量大時,查詢性能會下降,可增加副本數,來提升並發查詢能力。

注意:新增副本時主節點會自動協調,然后拷貝數據到新增的副本節點

 

三、集群管理

1. 監控API

http://localhost:9200/_cat

GET  /_cat

/_cat/health
/_cat/nodes
/_cat/master
/_cat/indices
/_cat/allocation
/_cat/shards
/_cat/shards/{index}
/_cat/thread_pool
/_cat/segments
/_cat/segments/{index}

2. x-pack

為集群提供安全防護、監控、告警、報告等功能的收費組件;
部分免費:https://www.elastic.co/subscriptions
6.3開始已開源,並並入了elasticsearch核心中。

官網安裝介紹:

https://www.elastic.co/guide/en/elasticsearch/reference/6.2/installing-xpack-es.html

 

ES安裝參考:

ES系列一、CentOS7安裝ES 6.3.1

ES系列二、CentOS7安裝ES head6.3.1

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM