解决ElasticSearch的maximum shards open问题

背景:

今天,开发反馈开发环境的日志没有了。

问题现象:

kibana无最新日志。

查看elk各个容器,发现容器启动正常
64e4553efa33   kibana:7.6.0            "/usr/local/bin/dumb…"   2 months ago   Up 3 days       0.0.0.0:5601->5601/tcp, :::5601->5601/tcp                                                  kibana
9ce332c49392   logstash:7.6.0          "/usr/local/bin/dock…"   2 months ago   Up 23 minutes   0.0.0.0:5044->5044/tcp, :::5044->5044/tcp, 9600/tcp                                        logstash
0c138a5cc89b   elasticsearch:7.6.0     "/usr/local/bin/dock…"   2 months ago   Up 19 minutes   0.0.0.0:9200->9200/tcp, :::9200->9200/tcp, 9300/tcp                                        elasticsearch


查看filebeat服务,发现服务也正常
[root@localhost logs]$ systemctl status filebeat
● filebeat.service - Filebeat sends log files to Logstash or directly to Elasticsearch.
   Loaded: loaded (/usr/lib/systemd/system/filebeat.service; enabled; vendor preset: disabled)
   Active: active (running) since 四 2025-02-13 13:53:28 CST; 4s ago
     Docs: https://www.elastic.co/products/beats/filebeat
 Main PID: 23647 (filebeat)
    Tasks: 19
   CGroup: /system.slice/filebeat.service
           └─23647 /usr/share/filebeat/bin/filebeat -e -c /etc/filebeat/filebeat.yml -path.home /usr/share/filebeat -path.config /etc/filebeat.
排查日志发现,logstash的日志报错
[2025-02-05T01:19:12,745][WARN ][logstash.outputs.elasticsearch][main] Could not index event to Elasticsearch. {:status=>400, :action=>["index", {:_id=>nil, :_index=>"服务名_2025.02.05", :routing=>nil, :_type=>"_doc"}, #<LogStash::Event:0x628bd73a>], :response=>{"index"=>{"_index"=>"服务名_2025.02.05", "_type"=>"_doc", "_id"=>nil, "status"=>400, "error"=>{"type"=>"validation_exception", "reason"=>"Validation Failed: 1: this action would add [2] total shards, but this cluster currently has [999]/[1000] maximum shards open;"}}}}

原因:

这是因为集群最大shard(分片)数不足引起的,从Elasticsearch v7.0 开始,集群中的每个节点默认限制1000个分片。

解决思路:

在服务器执行

curl -XPUT http://localhost:9200/_cluster/settings \
-u elastic:password \
-H "Content-Type: application/json" \
-d '{"transient":{"cluster":{"max_shards_per_node":10000}}}' 
返回{"acknowledged":true,"persistent":{},"transient":{"cluster":{"max_shards_per_node":"10000"}}}表示执行成功!

参考笔记:https://blog.51cto.com/u_11954248/2455379
https://blog.51cto.com/u_13972012/5745091

AI返回
‌一、命令解析与执行方式‌
‌命令作用‌
此命令通过 transient 参数动态调整集群分片限制,将每个节点的最大分片数临时设置为 100000‌26。

‌正确执行方式‌

‌Kibana Dev Tools‌:
bash
Copy Code
PUT _cluster/settings
{
  "transient": {
    "cluster.max_shards_per_node": 100000
  }
}
‌Curl 命令‌(需指定 Content-Type 头):
bash
Copy Code
curl -XPUT -H "Content-Type: application/json" http://localhost:9200/_cluster/settings -d '
{
  "transient": { "cluster.max_shards_per_node": 100000 }
}'
‌二、临时与永久配置的区别‌
‌配置类型‌	‌生效方式‌	‌适用场景‌	‌参考来源‌
transient	临时生效,重启后失效	紧急扩容或测试环境	‌24
persistent	永久生效,重启后保留	生产环境长期调整	‌26
配置文件	需重启集群生效	初始化部署或与代码化运维流程集成	‌23
‌三、注意事项‌
‌语法问题‌

原始命令中 cluster 字段层级冗余,应简化为 "cluster.max_shards_per_node"‌26。
若使用 ‌curl‌ 需确保 JSON 格式正确,避免引号未转义导致的语法错误‌24。
‌分片管理建议‌

优先优化索引分片数量(如合并小索引、减少分片大小),而非盲目提高分片上限‌24。
监控分片增长趋势,避免因分片过多引发性能问题(参考命令 GET _cat/indices?v)‌26。
‌验证配置生效‌

bash
Copy Code
GET _cluster/settings?include_defaults=true
返回结果中 cluster.max_shards_per_node 应为 100000‌6。

‌四、长期优化建议‌
‌启用 ILM(索引生命周期管理)‌
自动删除过期索引或合并分片,减少分片总数‌24。
‌调整索引模板‌
创建新索引时指定更合理的分片数(如 number_of_shards: 1)‌24。
‌定期清理无用索引‌
结合脚本或 Kibana 控制台清理测试或临时索引‌46。
通过以上调整,可快速解决分片限制问题,但需同步实施长期优化以避免频繁触发阈值。
posted @ 2025-02-13 14:14  wh459086748  阅读(175)  评论(0)    收藏  举报