📣 极限科技诚招搜索运维工程师(Elasticsearch/Easysearch)- 全职/北京 👉 : 立即申请加入

配置项作用 #

cluster.routing.rebalance.enable 配置项控制集群中分片的重新平衡(Rebalancing)行为。重新平衡是指集群自动将分片从负载较高的节点移动到负载较低的节点,以实现集群负载的均匀分布。

配置项类型 #

该配置项为动态配置,可以在运行时通过集群设置 API 进行修改。

默认值 #

all(允许所有分片重新平衡)

是否必需 #

可选配置项(有默认值)

可选值 #

说明
all允许所有分片(主分片和副本分片)重新平衡
primaries只允许主分片重新平衡
replicas只允许副本分片重新平衡
none禁止所有分片重新平衡

配置格式 #

# 默认配置
cluster.routing.rebalance.enable: all

# 只允许主分片重新平衡
cluster.routing.rebalance.enable: primaries

# 只允许副本分片重新平衡
cluster.routing.rebalance.enable: replicas

# 禁止重新平衡
cluster.routing.rebalance.enable: none

相关配置项 #

配置项默认值说明
cluster.routing.rebalance.enableall集群级别的重新平衡控制
index.routing.rebalance.enableall索引级别的重新平衡控制
cluster.routing.allocation.enableall分片分配控制
cluster.routing.allocation.allow_rebalanceindices_all_active重新平衡的触发条件

工作原理 #

重新平衡是指集群自动调整分片分布的过程:

┌─────────────────────────────────────────────────────────────────┐
│                   重新平衡触发条件                              │
└─────────────────────────────────────────────────────────────────┘

集群状态变化:
    │
    ├── 节点加入/离开
    ├── 分片分配完成
    ├── 集群设置变更
    └── 定期检查
        │
        ▼
    ┌──────────────────────────────────────────────────────────┐
    │ 检查条件:                                                  │
    │ 1. cluster.routing.rebalance.enable != none               │
    │ 2. 集群状态为 green 或 yellow                              │
    │ 3. 节点间分片分布不均衡                                    │
    └──────────────────────────────────────────────────────────┘
                    │
                    ▼
            根据配置决定重新平衡
        ┌───────────┼───────────┐
        │           │           │
      all       primaries    replicas
        │           │           │
        ▼           ▼           ▼
    所有分片    只移动主分片  只移动副本分片
    可移动      可移动        可移动
        │           │           │
        └───────────┴───────────┘
                    │
                    ▼
            执行分片迁移

配置值详解 #

all(全部重新平衡) #

效果:

  • 主分片和副本分片都可以被重新平衡
  • 最全面的负载均衡
  • 默认推荐配置

适用场景:

  • 正常生产环境
  • 需要完整的负载均衡
  • 节点资源差异较大

primaries(仅主分片重新平衡) #

效果:

  • 只有主分片可以被重新平衡
  • 副本分片保持在原节点
  • 减少迁移开销

适用场景:

  • 减少网络传输
  • 主分片负载不均衡
  • 临时调试场景

replicas(仅副本分片重新平衡) #

效果:

  • 只有副本分片可以被重新平衡
  • 主分片保持在原节点
  • 降低对主分片的影响

适用场景:

  • 优化副本分布
  • 减少对主分片的影响
  • 读取负载主要在副本

none(禁止重新平衡) #

效果:

  • 所有分片保持当前位置
  • 不会自动进行任何重新平衡
  • 手动分配仍然有效

适用场景:

  • 节点维护期间
  • 减少网络负载
  • 临时停止重新平衡

使用场景 #

1. 正常运行(默认) #

cluster.routing.rebalance.enable: all

允许集群自动进行负载均衡。

2. 节点维护 #

# 维护前禁止重新平衡
PUT /_cluster/settings
{
  "transient": {
    "cluster.routing.rebalance.enable": "none"
  }
}

# 维护完成后恢复
PUT /_cluster/settings
{
  "transient": {
    "cluster.routing.rebalance.enable": "all"
  }
}

3. 减少网络负载 #

# 只移动副本分片,减少主分片迁移
cluster.routing.rebalance.enable: replicas

4. 特定索引配置 #

# 集群级别:允许所有重新平衡
PUT /_cluster/settings
{
  "persistent": {
    "cluster.routing.rebalance.enable": "all"
  }
}

# 某个索引:禁止重新平衡
PUT /important_index/_settings
{
  "index.routing.rebalance.enable": "none"
}

使用示例 #

通过 API 动态修改:

# 禁止所有重新平衡
PUT /_cluster/settings
{
  "transient": {
    "cluster.routing.rebalance.enable": "none"
  }
}

# 只允许副本重新平衡
PUT /_cluster/settings
{
  "transient": {
    "cluster.routing.rebalance.enable": "replicas"
  }
}

# 恢复默认(全部重新平衡)
PUT /_cluster/settings
{
  "transient": {
    "cluster.routing.rebalance.enable": "all"
  }
}

索引级别配置:

# 针对特定索引设置
PUT /my_index/_settings
{
  "index.routing.rebalance.enable": "primaries"
}

节点维护流程 #

推荐的节点维护流程:

1. 禁止重新平衡
   PUT /_cluster/settings
   { "transient": { "cluster.routing.rebalance.enable": "none" } }
        │
        ▼
2. 排空节点(可选)
   PUT /_cluster/settings
   { "transient": { "cluster.routing.allocation.exclude._host": "node-to-maintain" } }
        │
        ▼
3. 执行维护操作
        │
        ▼
4. 恢复节点
        │
        ▼
5. 恢复重新平衡
   PUT /_cluster/settings
   { "transient": { "cluster.routing.rebalance.enable": "all" } }
        │
        ▼
6. 集群自动恢复平衡

推荐设置建议 #

生产环境:使用默认值 all

状态推荐值说明
正常运行all完整的负载均衡
节点维护none防止不必要的迁移
网络受限replicas只移动副本分片
特定场景primaries只平衡主分片

与 allocation.enable 的区别 #

配置项作用范围默认值说明
cluster.routing.allocation.enable分片分配all控制新分片的分配
cluster.routing.rebalance.enable分片重新平衡all控制已有分片的迁移

两者的关系:

allocation.enable vs rebalance.enable

allocation.enable:                    rebalance.enable:
控制是否允许分配分片                  控制是否允许移动已有分片
│                                    │
├── all: 允许所有分配                ├── all: 允许所有重新平衡
├── primaries: 只分配主分片          ├── primaries: 只移动主分片
├── new_primaries: 只分配新主分片    ├── replicas: 只移动副本分片
└── none: 禁止所有分配               └── none: 禁止所有重新平衡

监控建议 #

# 查看重新平衡配置
GET /_cluster/settings?filter_path=*.cluster.routing.rebalance.enable

# 查看集群平衡状态
GET /_cluster/allocation/explain?pretty

# 查看分片迁移状态
GET /_cat/shards?v&h=index,shard,prirep,state,node,relocatingnode

# 查看节点分布
GET /_cat/allocation?v

常见问题 #

问题 1:分片未自动平衡

检查重新平衡是否启用:

GET /_cluster/settings?filter_path=*.cluster.routing.rebalance.enable

解决方案:

PUT /_cluster/settings
{
  "transient": {
    "cluster.routing.rebalance.enable": "all"
  }
}

问题 2:频繁的分片迁移

可能原因:

  • 允许重新平衡但节点资源差异持续变化
  • 阈值设置过低

解决方案:

  1. 临时禁用重新平衡
PUT /_cluster/settings
{
  "transient": {
    "cluster.routing.rebalance.enable": "replicas"
  }
}
  1. 调整平衡阈值
PUT /_cluster/settings
{
  "transient": {
    "cluster.routing.allocation.balance.threshold": 2.0
  }
}

问题 3:维护期间分片仍在迁移

解决方案:

# 完全禁止重新平衡
PUT /_cluster/settings
{
  "transient": {
    "cluster.routing.rebalance.enable": "none"
  }
}

索引级别配置 #

索引级别配置会覆盖集群级别配置:

# 集群级别:禁止重新平衡
PUT /_cluster/settings
{
  "persistent": {
    "cluster.routing.rebalance.enable": "none"
  }
}

# 某个索引:允许重新平衡(覆盖集群设置)
PUT /my_index/_settings
{
  "index.routing.rebalance.enable": "all"
}

注意事项 #

  1. 动态更新:此配置为动态配置,可在线修改
  2. 优先级:索引级别配置会覆盖集群级别配置
  3. 手动分配:使用 reroute API 进行手动分配时会忽略此限制
  4. 恢复操作:完成维护后务必恢复为 all
  5. 网络影响:重新平衡会产生网络流量,谨慎在高负载时操作
  6. 与分配配合:与 allocation.enable 配合使用可实现精细控制