Elasticsearch 报错 `failed this action would add 2 total shards but this cluster currently has 300`

failed this action would add 2 total shards but this cluster currently has 300

ES 集群默认静态限制:集群最大分片总数 cluster.max_shards_per_cluster = 300 本次操作要新增 2 个分片,300+2=302,超过上限,创建索引 / 开副本失败。

1. 查看集群当前分片统计

# 总分片数量(主分片+副本分片)
curl -XGET 'http://127.0.0.1:9200/_cluster/stats?pretty' |grep total_shards

# 看每个索引分片情况
curl -XGET 'http://127.0.0.1:9200/_cat/indices?v'

2. 临时快速放行(应急,不建议长期)

调高集群全局最大分片数,允许超过 300,动态生效,不需要重启 ES

curl -XPUT -H "Content-Type: application/json" http://127.0.0.1:9200/_cluster/settings -d '{
  "persistent" : {
    "cluster.max_shards_per_cluster" : 1000
  }
}'

persistent:永久保存;transient 临时,重启失效。

3. 根本解决(优先做,不建议一味调大上限)

① 删除不用的索引,释放分片

curl -XDELETE http://127.0.0.1:9200/废弃索引名

每删除一个索引,它的主分片 + 副本分片全部释放配额。

② 创建索引时减少主分片数量

很多人习惯每个索引设置 5、10 个主分片,小索引分片过多浪费配额。 小索引主分片建议 1‑3 个。

{
  "settings":{
    "number_of_shards": 2,
    "number_of_replicas":1
  }
}

③ 合并小索引(rollover /index alias)

大量时间序列小索引(按天分索引)会快速吃光 300 分片,使用 rollover 滚动,控制索引数量。

④ 减少副本数(不生产慎用)

测试环境可以临时把 number_of_replicas:0,分片数量直接减半。

4. 查看当前这个上限配置

curl -XGET http://127.0.0.1:9200/_cluster/settings?include_defaults=true&pretty

重要注意

  1. cluster.max_shards_per_cluster 是整个集群所有索引的:主分片 + 副本分片总和上限。 比如索引:3 主 1 副本,实际占用 3+3*1 =6 个分片配额。
  2. 一味调大到几千会带来内存压力,每个分片都要占用 JVM 内存;分片不是越多越快。
  3. ES7.0 之后才引入 cluster.max_shards_per_cluster 参数,老版本没有这个限制。
posted @ 2026-08-28 10:00  苹果佳儿  阅读(13)  评论(0)    收藏  举报