--- title: "索引分片平衡权重配置" date: 2026-03-07 lastmod: 2026-03-07 description: "控制分片分配时索引级别的平衡权重配置项说明" tags: ["集群配置", "负载均衡", "分片分配"] summary: "配置项作用 # cluster.routing.allocation.balance.index 配置项定义了分片分配决策中索引级别的平衡权重。此配置控制集群在分配和重新平衡分片时,如何考虑每个索引在各节点上的分布均衡性。 配置项类型 # 该配置项为动态配置,可以在运行时通过集群设置 API 进行修改。 默认值 # 0.55 是否必需 # 可选配置项(有默认值) 取值范围 # 0.0 ~ 正无穷大 配置格式 # # 默认配置 cluster.routing.allocation.balance.index: 0.55 # 更重视索引平衡 cluster.routing.allocation.balance.index: 0.7 # 忽略索引平衡 cluster.routing.allocation.balance.index: 0.0 相关配置项 # 配置项 默认值 说明 cluster.routing.allocation.balance.index 0.55 索引平衡权重 cluster.routing.allocation.balance.shard 0.45 分片平衡权重 cluster.routing.allocation.balance.threshold 1.0 平衡操作的阈值 工作原理 # 平衡算法使用权重函数计算节点权重,权重越低越容易被分配新分片:" --- ## 配置项作用 `cluster.routing.allocation.balance.index` 配置项定义了分片分配决策中索引级别的平衡权重。此配置控制集群在分配和重新平衡分片时,如何考虑每个索引在各节点上的分布均衡性。 ## 配置项类型 该配置项为**动态配置**,可以在运行时通过集群设置 API 进行修改。 ## 默认值 ``` 0.55 ``` ## 是否必需 **可选配置项**(有默认值) ## 取值范围 ``` 0.0 ~ 正无穷大 ``` ## 配置格式 ```yaml # 默认配置 cluster.routing.allocation.balance.index: 0.55 # 更重视索引平衡 cluster.routing.allocation.balance.index: 0.7 # 忽略索引平衡 cluster.routing.allocation.balance.index: 0.0 ``` ## 相关配置项 | 配置项 | 默认值 | 说明 | |-------|-------|------| | `cluster.routing.allocation.balance.index` | 0.55 | 索引平衡权重 | | `cluster.routing.allocation.balance.shard` | 0.45 | 分片平衡权重 | | `cluster.routing.allocation.balance.threshold` | 1.0 | 平衡操作的阈值 | ## 工作原理 平衡算法使用权重函数计算节点权重,权重越低越容易被分配新分片: ``` ┌─────────────────────────────────────────────────────────────────┐ │ 平衡权重计算公式 │ └─────────────────────────────────────────────────────────────────┘ 权重计算: weight(node, index) = θ₀ × weightShard + θ₁ × weightIndex 其中: θ₀ = shard_balance / (index_balance + shard_balance) θ₁ = index_balance / (index_balance + shard_balance) weightShard = node.numShards() - avgShardsPerNode weightIndex = node.numShards(index) - avgShardsPerNode(index) ``` **平衡决策流程:** ``` ┌─────────────────────────────────────────────────────────────────┐ │ 分片分配决策 │ └─────────────────────────────────────────────────────────────────┘ │ ▼ 计算每个节点的权重 ┌────┬────┬────┬────┐ │ 节点A │ 节点B │ 节点C │ 节点D │ │ 0.2 │ 0.5 │ 0.8 │ 1.1 │ └────┴────┴────┴────┘ │ ▼ 选择权重最低的节点 (节点A) │ ▼ 分配分片到节点A ``` ## 配置值影响 ### 默认配置 (index: 0.55, shard: 0.45) ``` 平衡倾向: 索引平衡: 55% 分片平衡: 45% 效果: - 综合考虑索引分布和总分片数 - 默认推荐配置 - 适用于大多数场景 ``` ### 提高索引权重 (index: 0.8, shard: 0.2) ``` 平衡倾向: 索引平衡: 80% 分片平衡: 20% 效果: - 同一索引的分片更均匀分布 - 可能导致总分片分布不均 - 适用于多索引租户场景 ``` ### 降低索引权重 (index: 0.2, shard: 0.8) ``` 平衡倾向: 索引平衡: 20% 分片平衡: 80% 效果: - 节点总分片数更均匀 - 单个索引可能集中在某些节点 - 适用于单索引或少数索引场景 ``` ### 禁用索引平衡 (index: 0.0, shard: 1.0) ``` 平衡倾向: 索引平衡: 0% 分片平衡: 100% 效果: - 完全不考虑索引分布 - 只平衡节点总分片数 - 单索引可能完全集中在某些节点 ``` ## 使用场景 ### 1. 多租户场景(推荐提高索引权重) ```yaml # 多租户环境,每个租户一个索引 cluster.routing.allocation.balance.index: 0.7 cluster.routing.allocation.balance.shard: 0.3 ``` **原因:** 确保每个租户的索引均匀分布,避免某些节点承担特定租户的全部负载。 ### 2. 单一大索引场景(推荐降低索引权重) ```yaml # 单个或少量大索引 cluster.routing.allocation.balance.index: 0.3 cluster.routing.allocation.balance.shard: 0.7 ``` **原因:** 优先平衡节点的总分片数,索引分布已自然均匀。 ### 3. 默认配置(大多数场景) ```yaml # 通用场景 cluster.routing.allocation.balance.index: 0.55 cluster.routing.allocation.balance.shard: 0.45 ``` ## 使用示例 **动态修改平衡权重:** ```bash # 提高索引平衡权重 PUT /_cluster/settings { "transient": { "cluster.routing.allocation.balance.index": 0.7, "cluster.routing.allocation.balance.shard": 0.3 } } # 恢复默认值 PUT /_cluster/settings { "transient": { "cluster.routing.allocation.balance.index": 0.55, "cluster.routing.allocation.balance.shard": 0.45 } } ``` **配合阈值设置:** ```bash # 设置平衡阈值,减少小幅度迁移 PUT /_cluster/settings { "transient": { "cluster.routing.allocation.balance.index": 0.6, "cluster.routing.allocation.balance.shard": 0.4, "cluster.routing.allocation.balance.threshold": 2.0 } } ``` ## 权重计算示例 假设集群有 3 个节点,索引 A 有 6 个分片: ``` 初始状态: ┌───────┬─────────────┬─────────────┬─────────────┐ │ 节点 │ 总分片数 │ 索引A分片 │ 权重值 │ ├───────┼─────────────┼─────────────┼─────────────┤ │ 节点1 │ 10 │ 4 │ 计算... │ │ 节点2 │ 10 │ 1 │ 计算... │ │ 节点3 │ 10 │ 1 │ 计算... │ └───────┴─────────────┴─────────────┴─────────────┘ 使用 index=0.55, shard=0.45: θ₀ = 0.45 / (0.55 + 0.45) = 0.45 θ₁ = 0.55 / (0.55 + 0.45) = 0.55 节点1权重 = 0.45 × (10-10) + 0.55 × (4-2) = 1.1 节点2权重 = 0.45 × (10-10) + 0.55 × (1-2) = -0.55 节点3权重 = 0.45 × (10-10) + 0.55 × (1-2) = -0.55 结果: 节点2和节点3权重最低,优先获得新分片 ``` ## 推荐设置建议 | 场景 | index | shard | 说明 | |-----|-------|-------|------| | 默认/通用 | 0.55 | 0.45 | 平衡两种因素 | | 多租户 | 0.6-0.7 | 0.4-0.3 | 优先索引均匀分布 | | 单大索引 | 0.3-0.4 | 0.7-0.6 | 优先总分片均衡 | | 热点索引多 | 0.7-0.8 | 0.3-0.2 | 避免热点集中 | | 忽略索引 | 0.0 | 1.0 | 只考虑总分片 | ## 配置约束 **权重和必须大于 0:** ```yaml # ✅ 正确配置 cluster.routing.allocation.balance.index: 0.5 cluster.routing.allocation.balance.shard: 0.5 # ❌ 错误配置(启动失败) cluster.routing.allocation.balance.index: 0.0 cluster.routing.allocation.balance.shard: 0.0 ``` 错误信息: ``` IllegalArgumentException: Balance factors must sum to a value > 0 but was: 0.0 ``` ## 与 threshold 的关系 `threshold` 配置项控制平衡操作的敏感度: ```yaml # 阈值较小,频繁进行小的平衡调整 cluster.routing.allocation.balance.threshold: 0.5 # 阈值较大,只在差异大时才平衡 cluster.routing.allocation.balance.threshold: 2.0 ``` **计算逻辑:** ``` if (maxWeight - minWeight) > threshold: 执行平衡操作 else: 不执行平衡操作 ``` ## 监控建议 ```bash # 查看当前平衡配置 GET /_cluster/settings?filter_path=*.cluster.routing.allocation.balance* # 查看分片分布 GET /_cat/allocation?v # 查看特定索引的分片分布 GET /_cat/shards/my_index?v&h=shard,node,prirep,state # 查看集群平衡状态 GET /_cluster/health?pretty ``` ## 常见问题 **问题 1:分片分布不均匀** 检查当前平衡配置: ```bash GET /_cluster/settings?filter_path=*.cluster.routing.allocation.balance* ``` **解决方案:** 根据场景调整权重: - 多索引场景:提高 index 权重 - 单索引场景:提高 shard 权重 **问题 2:频繁重新平衡** **原因:** threshold 设置过低 **解决方案:** ```bash PUT /_cluster/settings { "transient": { "cluster.routing.allocation.balance.threshold": 1.5 } } ``` **问题 3:某些节点负载过高** **原因:** 平衡权重不匹配实际场景 **解决方案:** 1. 分析索引分布情况 2. 调整 balance.index 和 balance.shard 比例 3. 检查是否需要使用分片过滤规则 ## 性能考虑 | 配置 | 优点 | 缺点 | |-----|------|------| | 高 index 权重 | 索引分布均匀 | 可能总分片不均 | | 高 shard 权重 | 总分片分布均匀 | 可能索引集中 | | 低 threshold | 分布更均匀 | 频繁迁移,影响性能 | | 高 threshold | 减少迁移 | 可能容忍不均衡 | ## 注意事项 1. **动态更新**:平衡权重为动态配置,可在线修改 2. **权重和**:index + shard 必须大于 0 3. **渐进调整**:大幅调整可能导致大量分片迁移 4. **监控影响**:修改后观察分片迁移情况 5. **场景匹配**:根据实际索引特点选择合适的权重比例