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

配置项作用 #

cluster.routing.allocation.total_shards_per_node 配置项控制每个节点上允许分配的最大分片总数(包括主分片和副本分片)。当节点的分片数量达到此限制时,新的分片将不会被分配到该节点。

配置项类型 #

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

默认值 #

-1(无限制)

是否必需 #

可选配置项(有默认值)

取值范围 #

-1 ~ 正整数(-1 表示无限制)

配置格式 #

# 默认配置(无限制)
cluster.routing.allocation.total_shards_per_node: -1

# 限制每个节点最多 1000 个分片
cluster.routing.allocation.total_shards_per_node: 1000

# 小集群限制
cluster.routing.allocation.total_shards_per_node: 500

# 大集群限制
cluster.routing.allocation.total_shards_per_node: 2000

相关配置项 #

配置项默认值说明
cluster.routing.allocation.total_shards_per_node-1每节点总分片数限制
index.routing.allocation.total_shards_per_node-1每节点每索引分片数限制
cluster.max_shards_per_node1000每节点最大分片数(软限制)

工作原理 #

分片数限制机制:

┌─────────────────────────────────────────────────────────────────┐
│                    分片分配检查                                  │
└─────────────────────────────────────────────────────────────────┘

尝试分配分片到节点
    │
    ▼
检查节点当前分片数
    │
    ├── cluster.limit <= 0
    │   │
    │   └── 无限制,允许分配
    │
    ├── 当前分片数 < cluster.limit
    │   │
    │   └── 允许分配
    │
    └── 当前分片数 >= cluster.limit
        │
        └── 拒绝分配
            │
            └── 尝试其他节点

与索引级别限制的关系 #

节点分片分配需要同时满足:
1. cluster.routing.allocation.total_shards_per_node(集群级别)
2. index.routing.allocation.total_shards_per_node(索引级别)

示例:
  cluster limit = 1000
  index limit = 100

  节点已有 950 个分片,其中索引 A 有 95 个

  分配索引 A 的新分片:
  - 集群级别: 950 + 1 = 951 < 1000 ✓
  - 索引级别: 95 + 1 = 96 < 100 ✓
  - 结果: 允许分配

  分配索引 B 的新分片(索引 B 有 100 个):
  - 集群级别: 950 + 1 = 951 < 1000 ✓
  - 索引级别: 100 + 1 = 101 > 100 ✗
  - 结果: 拒绝分配

使用场景 #

1. 默认配置(无限制) #

cluster.routing.allocation.total_shards_per_node: -1

适用于小规模集群或对分片数没有严格限制的场景。

2. 资源受限节点 #

cluster.routing.allocation.total_shards_per_node: 500

适用场景:

  • 内存较小的节点
  • CPU 性能有限
  • 避免节点过载

3. 标准生产环境 #

cluster.routing.allocation.total_shards_per_node: 1000

适用场景:

  • 中等规模集群
  • 标准硬件配置
  • 平衡性能和容量

4. 高性能节点 #

cluster.routing.allocation.total_shards_per_node: 2000

适用场景:

  • 高内存节点
  • 高性能 CPU
  • SSD 存储

推荐设置建议 #

节点内存推荐分片数说明
4GB200-300小型节点
8GB500-600中型节点
16GB1000-1200标准节点
32GB2000-2500大型节点
64GB+3000-5000高性能节点

分片数估算 #

每分片内存需求:
- 索引缓冲: index_buffer_size / 分片数
- 段数据: 取决于数据量
- 查询缓存: 取决于查询模式
- 字段数据: 取决于字段类型

一般建议:
- 小分片(< 10GB): 每节点 500-1000 个分片
- 中分片(10-50GB): 每节点 300-500 个分片
- 大分片(> 50GB): 每节点 100-300 个分片

监控建议 #

# 查看当前配置
GET /_cluster/settings?filter_path=*.cluster.routing.allocation.total_shards_per_node

# 查看每个节点的分片数
GET /_cat/allocation?v

# 查看分片分布
GET /_cat/shards?v&h=index,shard,prirep,state,node

# 查看节点统计信息
GET /_nodes/stats/indices?filter_path=nodes.*.indices.shards

动态调整 #

# 临时增加分片限制
PUT /_cluster/settings
{
  "transient": {
    "cluster.routing.allocation.total_shards_per_node": 1500
  }
}

# 恢复无限制
PUT /_cluster/settings
{
  "transient": {
    "cluster.routing.allocation.total_shards_per_node": null
  }
}

故障排查 #

分片无法分配 #

# 检查分配失败原因
GET /_cluster/allocation/explain?verbose

# 如果显示 "too many shards allocated to this node"
# 考虑增加 total_shards_per_node 限制

节点过载 #

如果节点负载过高:

  1. 检查当前分片数
  2. 降低 total_shards_per_node 限制
  3. 重新分配分片到其他节点

注意事项 #

  1. 动态更新:此配置为动态配置,可在线修改
  2. 现有分片:降低限制不会影响已分配的分片
  3. 重分配影响:提高限制可能导致分片重分配
  4. 与索引限制配合:同时考虑集群级别和索引级别的限制
  5. 资源规划:根据节点资源合理设置
  6. 分片大小:小分片过多会影响性能