news 2026/5/1 3:29:02

Elasticsearch 分片迁移与重新平衡监控指南

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Elasticsearch 分片迁移与重新平衡监控指南

目录标题

  • Elasticsearch 分片迁移与重新平衡监控指南
    • 目录
    • 概述
    • 环境信息
      • 当前集群状态
      • Kubernetes 集群节点
    • 分片重新平衡机制
      • ES 默认平衡策略
      • 分片状态流转
    • 查看分片迁移的命令
      • 1. 查看集群健康状态
      • 2. 查看所有分片状态
      • 3. 查看迁移进度(最详细)
      • 4. 只看正在进行的迁移
      • 5. 查看节点分配统计
      • 6. 查看集群平衡设置
      • 7. 在 Kubernetes 环境中执行
    • 实际案例分析
      • 案例:节点 es-data-3 加入集群
        • 时间线
        • 日志记录
        • 分片分布变化
        • 分片迁移详情
    • 实时监控迁移进度
      • 持续监控命令
      • 查看迁移日志
    • 手动触发分片迁移
      • 方法1: 排除节点
      • 方法2: 调整平衡配置
      • 方法3: 手动移动分片
    • 常见问题
      • Q1: 为什么新节点加入后没有立即开始迁移?
      • Q2: es-data-4 为什么一直是 Pending 状态?
      • Q3: 如何加快分片迁移速度?
      • Q4: 迁移过程中对服务有影响吗?
      • Q5: 如何取消正在进行的迁移?
    • 附录
      • 快速参考命令表
      • 相关配置参数

Elasticsearch 分片迁移与重新平衡监控指南

目录

  1. 概述
  2. 环境信息
  3. 分片重新平衡机制
  4. 查看分片迁移的命令
  5. 实际案例分析
  6. 常见问题

概述

当 Elasticsearch 集群添加或移除节点时,会自动触发分片重新平衡(Rebalancing),以实现:

  • 负载均衡- 分片均匀分布到各节点
  • 高可用性- 主分片和副本分片分布在不同节点
  • 性能优化- 充分利用新节点的存储和计算资源

环境信息

当前集群状态

集群名称状态节点数分片数备注
es-dcdc4a67green220es-data-2 Pending

Kubernetes 集群节点

NAME STATUS ROLES AGE VERSION qfusion1 Ready control-plane,master 21d v1.24.10 qfusion2 Ready control-plane,master 21d v1.24.10 qfusion3 Ready control-plane,master 21d v1.24.10 qfusion4 Ready <none> 21d v1.24.10

分片重新平衡机制

ES 默认平衡策略

┌─────────────────────────────────────────────────────────────────┐ │ Elasticsearch 平衡决策 │ ├─────────────────────────────────────────────────────────────────┤ │ │ │ 1. 节点加入检测 2. 平衡计算 3. 分片迁移 │ │ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │ │ │ 新节点加入 │ ──> │ 计算差异 │──> │ 迁移分片 │ │ │ │ 触发重平衡 │ │ 调度决策 │ │ 恢复副本 │ │ │ └──────────────┘ └──────────────┘ └──────────────┘ │ │ │ │ 平衡考虑因素: │ │ • 每个节点的分片数量均衡 │ │ • 磁盘使用率均衡 │ │ • 副本分片与主分片不在同一节点 │ │ • 数据量大小(避免迁移大分片) │ │ │ └─────────────────────────────────────────────────────────────────┘

分片状态流转

INITIALIZING ──> RELOCATING ──> STARTED ↑ ↑ ↑ │ │ │ 正在初始化 正在迁移 正常运行
状态说明
STARTED正常运行,可提供服务
RELOCATING正在迁移到其他节点
INITIALIZING正在初始化(新建分片)
UNASSIGNED未分配(可能资源不足)

查看分片迁移的命令

1. 查看集群健康状态

curl-u elastic:password"localhost:9200/_cluster/health?pretty"

输出示例:

{"cluster_name":"es-dcdc4a67","status":"green","number_of_nodes":2,"number_of_data_nodes":2,"active_primary_shards":10,"active_shards":20,"relocating_shards":0,// 正在迁移的分片数"initializing_shards":0,// 正在初始化的分片数"unassigned_shards":0// 未分配的分片数}

关键字段:

字段说明
relocating_shards>0 表示有迁移正在进行
initializing_shards正在初始化的分片数
unassigned_shards未分配的分片数(异常)

2. 查看所有分片状态

curl-u elastic:password"localhost:9200/_cat/shards?v"

输出示例:

index shard prirep state docs store ip node test_index_01 0 p STARTED 9000 345kb 10.255.253.41 es-data-0 test_index_01 0 r STARTED 9000 345kb 10.255.253.67 es-data-1 test_index_02 0 p RELOCATING 9000 230kb 10.255.253.41 es-data-0 test_index_02 0 r STARTED 9000 345kb 10.255.253.67 es-data-1

3. 查看迁移进度(最详细)

curl-u elastic:password"localhost:9200/_cat/recovery?v"

输出示例:

index shard time type stage source_node target_node bytes bytes_pct files files_pct myindex 0 45s RELOCATION final node-0 node-3 150.2mb 100.0% 12 100.0% myindex 1 30s RELOCATION translog node-1 node-3 89.5mb 85.3% 8 90.0%
字段说明
typeRELOCATION= 迁移,REPLICA = 副本恢复
stageinit(初始化)→ translog(事务日志)→ finalize(完成)
bytes_pct数据传输进度百分比
source_node源节点
target_node目标节点

4. 只看正在进行的迁移

curl-u elastic:password"localhost:9200/_cat/recovery?v&active_only=true"

5. 查看节点分配统计

curl-u elastic:password"localhost:9200/_cat/allocation?v"

输出示例:

shards disk.indices disk.used disk.avail disk.total disk.percent host ip node 12 83.3mb 102.1gb 97.7gb 199.9gb 51 245.0.2.130 245.0.2.130 es-data-3 11 42.5mb 110.2gb 79.7gb 189.9gb 58 245.0.3.73 245.0.3.73 es-data-1 12 2.4mb 22.6gb 177.2gb 199.9gb 11 10.255.254.189 10.255.254.189 es-data-0 11 134.9mb 91.4gb 108.4gb 199.9gb 45 245.0.1.150 245.0.1.150 es-data-2

6. 查看集群平衡设置

curl-u elastic:password"localhost:9200/_cluster/settings?flat_settings=true"

7. 在 Kubernetes 环境中执行

# 设置环境变量exportKUBECONFIG=/bpx/.148-admin.conf# 获取密码PASSWORD=$(kubectl get secret -n qfusion-admin es-dcdc4a67-es-elastic-user -ojsonpath='{.data.elastic}'|base64 -d)# 查看集群状态kubectlexec-n qfusion-admin es-dcdc4a67-es-data-0 --curl-s -u elastic:$PASSWORD"localhost:9200/_cluster/health?pretty"# 查看分片迁移kubectlexec-n qfusion-admin es-dcdc4a67-es-data-0 --curl-s -u elastic:$PASSWORD"localhost:9200/_cat/recovery?v"

实际案例分析

案例:节点 es-data-3 加入集群

时间线
时间事件
02:08:32es-data-3 加入集群
02:08:34集群状态更新,版本 2220
02:08:35新索引开始分配到新节点
日志记录
[es-data-3] master node changed {previous [], current [...es-data-2...]} [es-data-2] node-join[{es-data-3}... join existing leader] [es-data-0] added {es-data-3}, term: 5, version: 2220
分片分布变化

加入前(3节点):

节点分片数
es-data-015
es-data-115
es-data-216

加入后(4节点):

节点分片数
es-data-012
es-data-111
es-data-211
es-data-312
分片迁移详情
分片迁移前迁移后
filebeat 主分片es-data-2es-data-3
test_index_02 主分片(新建)es-data-3
test_index_08 主分片(新建)es-data-3

实时监控迁移进度

持续监控命令

# 方法1: 使用 watchwatch-n1'curl -s -u elastic:pass "localhost:9200/_cat/recovery?v&active_only=true"'# 方法2: 使用 while 循环whiletrue;doclearecho"===$(date)==="curl-s -u elastic:pass"localhost:9200/_cat/recovery?v"sleep2done# 方法3: 在 Kubernetes 中kubectlexec-n qfusion-admin<pod>--bash-c' while true; do curl -s -u elastic:pass "localhost:9200/_cat/recovery?v&active_only=true" sleep 1 done '

查看迁移日志

# 查看 Pod 日志中的迁移记录kubectl logs -n qfusion-admin<pod-name>-c elasticsearch --tail=500|grep-i"relocat"# 查看 ES 日志文件kubectlexec-n qfusion-admin<pod-name>--tail-f /usr/share/elasticsearch/logs/es-dcdc4a67.log

手动触发分片迁移

方法1: 排除节点

# 排除特定节点(触发迁移出该节点)curl-u elastic:pass -X PUT"localhost:9200/_cluster/settings"-H'Content-Type: application/json'-d' { "transient": { "cluster.routing.allocation.exclude._name": "es-data-0" } }'# 取消排除curl-u elastic:pass -X PUT"localhost:9200/_cluster/settings"-H'Content-Type: application/json'-d' { "transient": { "cluster.routing.allocation.exclude._name": "" } }'

方法2: 调整平衡配置

# 启用自动平衡curl-u elastic:pass -X PUT"localhost:9200/_cluster/settings"-H'Content-Type: application/json'-d' { "transient": { "cluster.routing.rebalance.enable": "all" } }'# 调整并发迁移数curl-u elastic:pass -X PUT"localhost:9200/_cluster/settings"-H'Content-Type: application/json'-d' { "transient": { "cluster.routing.allocation.cluster_concurrent_rebalance": 3 } }'

方法3: 手动移动分片

# 将特定分片移动到指定节点curl-u elastic:pass -X POST"localhost:9200/_cluster/reroute"-H'Content-Type: application/json'-d' { "commands": [ { "move": { "index": "test_index", "shard": 0, "from_node": "node-1", "to_node": "node-2" } } ] }'

常见问题

Q1: 为什么新节点加入后没有立即开始迁移?

A:ES 会根据以下因素决定是否迁移:

  • 磁盘使用率差异
  • 分片数量差异
  • 数据量大小(避免迁移大分片影响性能)

可以通过以下配置调整:

curl-u elastic:pass -X PUT"localhost:9200/_cluster/settings"-H'Content-Type: application/json'-d' { "transient": { "cluster.routing.allocation.balance.shard": 0.45, "cluster.routing.allocation.balance.index": 0.55, "cluster.routing.allocation.balance.threshold": 1.0 } }'

Q2: es-data-4 为什么一直是 Pending 状态?

A:Pod Anti-Affinity 调度失败:

  • 集群只有 4 个节点
  • ES 配置了 Pod 反亲和性(每个 ES 节点必须在不同 K8s 节点上)
  • 第 5 个 ES 节点无法调度

解决方案:

  1. 添加新的 Kubernetes 节点
  2. 或调整 Pod 反亲和性配置
  3. 或减少 ES 数据节点数量

Q3: 如何加快分片迁移速度?

A:调整并发恢复参数:

curl-u elastic:pass -X PUT"localhost:9200/_cluster/settings"-H'Content-Type: application/json'-d' { "transient": { "cluster.routing.allocation.node_concurrent_recoveries": 6, "cluster.routing.allocation.node_initial_primaries_recoveries": 4, "indices.recovery.max_bytes_per_sec": "100mb" } }'

Q4: 迁移过程中对服务有影响吗?

A:

  • 副本分片迁移:无影响,服务正常
  • 主分片迁移:短暂影响(毫秒级),自动切换到副本

Q5: 如何取消正在进行的迁移?

A:

# 取消特定分片的迁移curl-u elastic:pass -X POST"localhost:9200/_cluster/reroute"-H'Content-Type: application/json'-d' { "commands": [ { "cancel": { "index": "test_index", "shard": 0, "node": "target_node", "allow_primary": false } } ] }'

附录

快速参考命令表

操作命令
查看集群状态curl -u elastic:pass "/_cluster/health?pretty"
查看分片列表curl -u elastic:pass "/_cat/shards?v"
查看迁移进度curl -u elastic:pass "/_cat/recovery?v"
查看节点分配curl -u elastic:pass "/_cat/allocation?v"
查看节点列表curl -u elastic:pass "/_cat/nodes?v"
查看集群设置curl -u elastic:pass "/_cluster/settings?flat_settings=true"

相关配置参数

参数默认值说明
cluster.routing.rebalance.enableall启用平衡类型
cluster.routing.allocation.cluster_concurrent_rebalance2集群并发平衡数
cluster.routing.allocation.node_concurrent_recoveries2节点并发恢复数
indices.recovery.max_bytes_per_sec40mb恢复速度限制
cluster.routing.allocation.balance.shard0.45分片平衡权重
cluster.routing.allocation.balance.index0.55索引平衡权重
cluster.routing.allocation.balance.threshold1.0平衡阈值

文档版本: 1.0
最后更新: 2026-01-30
适用版本: Elasticsearch 7.16.2

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/26 17:26:51

单片机温度测量和控制系统的设计与实现

单片机温度测量和控制系统的设计与实现 一、设计背景与意义 温度测量与控制在工业生产、智能家居、医疗设备、科研实验等领域至关重要&#xff0c;传统温控系统存在测量精度低、控温响应慢、操作复杂等问题&#xff0c;难以满足高精度场景需求。现有单片机温控设计多采用单一传…

作者头像 李华
网站建设 2026/4/23 12:09:37

单片机控制的自动门系统

单片机控制的自动门系统设计与实现 一、设计背景与意义 自动门作为便捷通行与智能安防的重要设备&#xff0c;广泛应用于商业建筑、办公场所、住宅小区等场景。传统自动门系统多依赖专用控制器&#xff0c;存在成本高、功能固化、兼容性差等问题&#xff0c;且部分设计感应灵敏…

作者头像 李华
网站建设 2026/4/18 7:26:18

用AI生成中文,然后翻译成英文,英文内容会被判定为AI生成吗?

“用AI生成中文&#xff0c;然后翻译成英文&#xff0c;英文内容会被判定为AI生成吗&#xff1f;”这是很多同学在写作中经常会问到的问题。 今天给大家分析一下&#xff1a; 大概率会被判定为AI生成的&#xff0c;现在很多AIGC系统都升级了&#xff0c;检测都比较严格。所以…

作者头像 李华
网站建设 2026/4/17 19:45:51

RuoYi-Plus-Soybean:现代化企业级多租户管理系统的技术实践与深度解析

引言 随着企业数字化转型的加速&#xff0c;多租户架构&#xff08;SaaS模式&#xff09;逐渐成为企业级应用的核心需求。如何快速构建一个既具备强大后端能力&#xff0c;又拥有现代化前端体验的管理系统&#xff0c;成为开发者关注的焦点。RuoYi-Plus-Soybean 正是为此而生—…

作者头像 李华
网站建设 2026/4/25 16:03:58

Git学习笔记

01_学习目标学习目标&#xff1a;了解Git基本概念 能够概述git工作流程 能够使用Git常用命令 熟悉Git代码托管服务 能够使用idea操作git(最重要)02_版本控制器的方式2.1开发中实际应用场景场景一:备份小明负责的模块就要完成了&#xff0c;就在即将Release之前的一瞬间&#xf…

作者头像 李华
网站建设 2026/4/24 9:55:09

日志分析缓慢的核心原因与AI解决路径

在软件测试中&#xff0c;日志分析涉及错误追踪、性能监控和报告生成&#xff0c;传统手动操作面临多重效率瓶颈&#xff1a; 人工处理耗时巨大&#xff1a;测试工程师需逐行比对日志文件&#xff0c;识别异常模式&#xff0c;单个项目平均消耗20-30小时&#xff0c;尤其在自动…

作者头像 李华