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

📅 发布时间:2026/7/6 22:41:13 👁️ 浏览次数:
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 分片迁移与重新平衡监控指南目录概述环境信息分片重新平衡机制查看分片迁移的命令实际案例分析常见问题概述当 Elasticsearch 集群添加或移除节点时会自动触发分片重新平衡Rebalancing以实现负载均衡- 分片均匀分布到各节点高可用性- 主分片和副本分片分布在不同节点性能优化- 充分利用新节点的存储和计算资源环境信息当前集群状态集群名称状态节点数分片数备注es-dcdc4a67green220es-data-2 PendingKubernetes 集群节点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:passwordlocalhost: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_shards0 表示有迁移正在进行initializing_shards正在初始化的分片数unassigned_shards未分配的分片数异常2. 查看所有分片状态curl-u elastic:passwordlocalhost: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-13. 查看迁移进度最详细curl-u elastic:passwordlocalhost: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:passwordlocalhost:9200/_cat/recovery?vactive_onlytrue5. 查看节点分配统计curl-u elastic:passwordlocalhost: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-26. 查看集群平衡设置curl-u elastic:passwordlocalhost:9200/_cluster/settings?flat_settingstrue7. 在 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:$PASSWORDlocalhost:9200/_cluster/health?pretty# 查看分片迁移kubectlexec-n qfusion-admin es-dcdc4a67-es-data-0 --curl-s -u elastic:$PASSWORDlocalhost:9200/_cat/recovery?v实际案例分析案例节点 es-data-3 加入集群时间线时间事件02:08:32es-data-3 加入集群02:08:34集群状态更新版本 222002: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-015es-data-115es-data-216加入后4节点节点分片数es-data-012es-data-111es-data-211es-data-312分片迁移详情分片迁移前迁移后filebeat 主分片es-data-2es-data-3test_index_02 主分片(新建)es-data-3test_index_08 主分片(新建)es-data-3实时监控迁移进度持续监控命令# 方法1: 使用 watchwatch-n1curl -s -u elastic:pass localhost:9200/_cat/recovery?vactive_onlytrue# 方法2: 使用 while 循环whiletrue;doclearecho$(date)curl-s -u elastic:passlocalhost:9200/_cat/recovery?vsleep2done# 方法3: 在 Kubernetes 中kubectlexec-n qfusion-adminpod--bash-c while true; do curl -s -u elastic:pass localhost:9200/_cat/recovery?vactive_onlytrue sleep 1 done 查看迁移日志# 查看 Pod 日志中的迁移记录kubectl logs -n qfusion-adminpod-name-c elasticsearch --tail500|grep-irelocat# 查看 ES 日志文件kubectlexec-n qfusion-adminpod-name--tail-f /usr/share/elasticsearch/logs/es-dcdc4a67.log手动触发分片迁移方法1: 排除节点# 排除特定节点触发迁移出该节点curl-u elastic:pass -X PUTlocalhost:9200/_cluster/settings-HContent-Type: application/json-d { transient: { cluster.routing.allocation.exclude._name: es-data-0 } }# 取消排除curl-u elastic:pass -X PUTlocalhost:9200/_cluster/settings-HContent-Type: application/json-d { transient: { cluster.routing.allocation.exclude._name: } }方法2: 调整平衡配置# 启用自动平衡curl-u elastic:pass -X PUTlocalhost:9200/_cluster/settings-HContent-Type: application/json-d { transient: { cluster.routing.rebalance.enable: all } }# 调整并发迁移数curl-u elastic:pass -X PUTlocalhost:9200/_cluster/settings-HContent-Type: application/json-d { transient: { cluster.routing.allocation.cluster_concurrent_rebalance: 3 } }方法3: 手动移动分片# 将特定分片移动到指定节点curl-u elastic:pass -X POSTlocalhost:9200/_cluster/reroute-HContent-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 PUTlocalhost:9200/_cluster/settings-HContent-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 节点无法调度解决方案添加新的 Kubernetes 节点或调整 Pod 反亲和性配置或减少 ES 数据节点数量Q3: 如何加快分片迁移速度A:调整并发恢复参数curl-u elastic:pass -X PUTlocalhost:9200/_cluster/settings-HContent-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 POSTlocalhost:9200/_cluster/reroute-HContent-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_settingstrue相关配置参数参数默认值说明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