ES 分片分配(Shard Allocation)時間點:
- 初始恢復(fù)(Initial Recovery)
- 副本分配(Replica Allocation)
- 重平衡(Rebalance)
- 節(jié)點添加或移除
小結(jié):
準備移除節(jié)點時,使用過濾器禁止路由到節(jié)點
增加新節(jié)點時万细,降低磁盤水位線觸發(fā)重平衡络它,使分片分布均勻
舊節(jié)點分片不均,綜合考慮每個節(jié)點分片數(shù)量和磁盤占有率調(diào)整
一. 集群級別分片分配設(shè)置
下方均為動態(tài)設(shè)置
cluster.routing.allocation.enable
不影響重啟主分片恢復(fù)
all
默認primaries
只允許主分片分配new_primaries
只允許新索引的主分片none
不允許分片分配-
cluster.routing.allocation.node_concurrent_incoming_recoveries
- 單節(jié)點并發(fā)恢復(fù)分片請求數(shù)评也,默認為2晌端。指在節(jié)點上分配目標分片
-
cluster.routing.allocation.node_concurrent_outgoing_recoveries
- 單節(jié)點源分片分配到新節(jié)點請求數(shù),默認2
cluster.routing.allocation.node_concurrent_recoveries
上方兩個配置的合并
cluster.routing.allocation.node_initial_primaries_recoveries
副本通過網(wǎng)絡(luò)恢復(fù)婉陷,未分配的主分片使用本地數(shù)據(jù)恢復(fù)帚称。此配置用于配置并行未分配主分片恢復(fù),默認4cluster.routing.allocation.same_shard.host
禁止多個副本分片被分配到相同ip節(jié)點上
1.1 分片重平衡設(shè)置
保證分片數(shù)量在每個節(jié)點上盡可能相同秽澳。受分配過濾(Allocation Filtering)和 forced awareness 影響
-
cluster.routing.rebalance.enable
all
默認primaries
只允許主分片重平衡replicas
只允許副本重平衡none
-
cluster.routing.allocation.allow_rebalance
always
indices_primaries_active
僅當所有主分片被分配時indices_all_active
默認闯睹。僅當集群中所有分片被分配時
cluster.routing.allocation.cluster_concurrent_rebalance
1.2 分片平衡啟發(fā)式(Heruistics)設(shè)置
(黑盒設(shè)置,未找到計算公式)
計算權(quán)重担神,根據(jù)權(quán)重從高分到低楼吃,從高的重平衡到權(quán)重低的節(jié)點,受到閾值的配置影響妄讯,低于閾值不需要重平衡(這個數(shù)值沒有說明是怎么計算的)
cluster.routing.allocation.balance.shard
0.45孩锡,提升此值會使所有節(jié)點分片數(shù)量趨于平衡cluster.routing.allocation.balance.index
0.55cluster.routing.allocation.balance.threshold
1.0f, 大于0,越大則越不激進
1.3 基于磁盤的分片分配設(shè)置
通過水位線來控制重平衡
當高于洪水線(flood_stage)時亥贸,寫入請求會被阻塞躬窜,只要有一個節(jié)點到達,所有節(jié)點被阻塞
當高于水位線(high watermark)炕置,ES 嘗試將節(jié)點數(shù)據(jù)重平衡到其他節(jié)點
當?shù)陀谒痪€ (low watermark)荣挨,不會觸發(fā)重平衡或者重平衡將停止男韧。同時不再向該節(jié)點派發(fā)新分片
cluster.routing.allocation.disk.threshold_enabled
啟用磁盤閾值限制,默認開啟cluster.routing.allocation.disk.watermark.low
最低水位線默垄,默認85%cluster.routing.allocation.disk.watermark.low.max_headroom
默認200GBcluster.routing.allocation.disk.watermark.high
最高水位線此虑,默認90%cluster.routing.allocation.disk.watermark.high.max_headroom
默認 150GBcluster.routing.allocation.disk.watermark.flood_stage
洪水線,默認 95%cluster.routing.allocation.disk.watermark.flood_stage.max_headroom
默認 100GBcluster.info``.update.interval
集群信息檢查間隔口锭,默認 30s
1.4 集群級別分片過濾器
上下線節(jié)點或節(jié)點處于不平衡狀態(tài)時朦前,可以通過過濾器臨時阻止分片路由到該節(jié)點
PUT _cluster/settings
{
"persistent" : {
"cluster.routing.allocation.exclude._ip" : "10.0.0.1"
}
}
cluster.routing.allocation.include.{attribute}
cluster.routing.allocation.require.{attribute}
-
cluster.routing.allocation.exclude.{attribute}
- 屬性列表:
_name
(node name),_host_ip
(node ip)_publish_ip
,_ip
(short cuts of_host_ip
and_publish_ip
), _host, _id, _tier
- 屬性列表:
二. 索引分片分配設(shè)置
與集群級別一樣,可以設(shè)置分片過濾器鹃操、數(shù)據(jù)層(data-tier)過濾器韭寸,但增加了每個節(jié)點的分片數(shù)設(shè)置
假設(shè)索引共 A 個節(jié)點,B 個分片组民,C 個副本共 BC 個分片棒仍,則每個節(jié)點最大分片數(shù)設(shè)置為 BC/A 得到最平均的分片數(shù),(B*C/A)+1 允許節(jié)點掛掉后還有一個可以正常分配分片
index.routing.allocation.total_shards_per_node
默認不設(shè)置臭胜,可能會導(dǎo)致數(shù)據(jù)很不平衡
三. 實操
現(xiàn)象:節(jié)點 10.15.213.12 磁盤高于 86%莫其,雖然停止寫入,但無法到達重平衡線耸三,其他節(jié)點的分片數(shù)不均
10.15.213.12: app-es110(86.8%)
10.15.219.125: app-es 138(77.66%)
10.15.220.165: app-es 158(77.53%)
10.15.213.8: app-es 138 (69.05%)
10.15.213.9: app-es 129 (68.65%)
10.15.213.11: app-es 132 (56%)
操作:默認水位線為 90%乱陡,降低至 85%,手動觸發(fā)重平衡仪壮,重平衡完畢后再恢復(fù)設(shè)置
PUT _cluster/settings
{
"persistent": {
"cluster": {
"routing": {
"allocation.disk.watermark.low": "80%",
"allocation.allow_rebalance": "always",
"allocation.disk.watermark.high": "85%"
}
}
}
}
POST /_cluster/reroute?dry_run=false&explain=true&retry_failed=false
另外針對節(jié)點10.15.220.165 上分片較多憨颠,先計算平衡時的分片數(shù):
- app-es 為 410 個分片,1副本积锅,6個節(jié)點爽彤,則平均為 410*2/6=136,設(shè)置為 136+6=142
PUT clue-online-*/_settings
{
"routing": {
"allocation": {
"total_shards_per_node": 142
}
}
}
操作后:10.15.213.12 降至 83%缚陷,報警解除
四. 相關(guān)命令
// 查看未分配或待分配分片狀態(tài)适篙,如果為 STARTED 則正常,為 UNASSIGNED 異常
GET _cat/shards?h=index,shard,prirep,state,unassigned.reason
// 集群設(shè)置
GET _cluster/settings
// 解釋待分配分片去向
GET _cluster/allocation/explain
PUT clue-online-*/_settings
{
"routing": {
"allocation": {
"total_shards_per_node": 142
}
}
}
// 強制 Reroute
POST /_cluster/reroute?dry_run=false&explain=true&retry_failed=false
五. 重平衡可能的問題
- 數(shù)據(jù)極速寫入導(dǎo)致負載上升
a. 解決方式:使用寫入限流器等方式控制重平衡速度 - 重平衡進入無限循環(huán)狀態(tài)
a. 解決方式:檢查集群整體的磁盤比例箫爷,如各分片均處在水位線附近嚷节,此問題可能出現(xiàn)。建議臨時調(diào)整水位線虎锚,待重平衡結(jié)束后硫痰,刪除不需要的數(shù)據(jù)或擴容
Ref: Cluster-level shard allocation and routing settings | Elasticsearch Guide [7.10] | Elastic