Elasticsearch分片数量超过限制:解决日志存储问题的详细指南
问题分析
查看logstash日志,发现在报错:
[2025-02-11T00:25:36,449][WARN ][logstash.outputs.elasticsearch][main][c93c36dd0e0e63abd56deea599477880a68fbcd96c84aeaab1135ece78953a92] Could not index event to Elasticsearch. {:status=>400, :action=>["index", {:_id=>nil, :_index=>"patient-master-2025.02.11", :routing=>nil}, {"level"=>"INFO", "@version"=>"1", "host"=>"ecs-140-210-198-2.compute.hwclouds-dns.com", "port"=>37962, "type"=>"patient-master", "@timestamp"=>2025-02-11T00:25:36.239Z, "tags"=>["_grokparsefailure", "_geoip_lookup_failure"], "message"=>"<-- 200 http://192.168.0.176:31401/api/smartwatch/current_task/message/dispatch (4ms, 62-byte body)", "level_value"=>20000, "logger_name"=>"com.github.lianjiatech.retrofit.spring.boot.interceptor.DefaultLoggingInterceptor", "thread_name"=>"pool-3-thread-2"}], :response=>{"index"=>{"_index"=>"patient-master-2025.02.11", "_type"=>"_doc", "_id"=>nil, "status"=>400, "error"=>{"type"=>"validation_exception", "reason"=>"Validation Failed: 1: this action would add [2] shards, but this cluster currently has [1000]/[1000] maximum normal shards open;"}}}}
笔者的日志数据在2025年2月9日之后无法被成功索引到Elasticsearch中。根据错误日志,问题出在“分片数量已达到或超过最大允许值”。具体来说,cluster.max_shards_per_node 的值为1000/1000,意味着每个节点最多支持1000个分片。在这种情况下,新索引的创建被阻止,导致日志数据无法继续存储。
解决方案
1. 临时增加分片限制
如果您需要快速恢复日志存储,可以通过以下步骤临时调整分片限制:
PUT /_cluster/settings
{
"persistent": {
"cluster.max_shards_per_node": 5000
}
}
这将立即生效,但需要注意:
- 风险与后果:增加分片数量可能导致内存使用增加、查询速度变慢和集群状态复杂化。请确保硬件资源(如内存)足够支持更高的分片数。
- 恢复设置:重启节点后,
cluster.max_shards_per_node会回到默认值1000/1000。如果需要长期使用较高的分片限制,请进行永久调整。
2. 永久调整分片限制
如果临时解决方案不足以满足需求,可以通过修改配置文件来永久调整:
-
打开每个节点上的
elasticsearch.yml文件,添加以下内容:cluster.max_shards_per_node: 5000 -
修改完成后,重启每个Elasticsearch节点:
systemctl restart elasticsearch
注意事项:
- 确保硬件资源足够支持较高的分片数。建议监控CPU、内存和磁盘I/O使用情况。
- 过多的分片会增加管理复杂性,可能影响集群性能。
3. 优化现有索引策略
如果日志数据量在2月9日之后突然增加,导致分片超限,可以考虑以下优化措施:
-
减少主分片和副本分片数量:对于较小的数据量或频率较低的日志,可以设置更少的主分片和副本。
-
使用动态分片策略:配置Elasticsearch自动根据数据量调整分片数,避免手动干预。
4. 清理不必要的索引或数据
如果存在大量旧数据占用了分片,可以删除不再需要的索引或执行滚动删除操作,以释放分片资源。
DELETE /your_old_index_name
或者,利用索引生命周期管理(ILM)自动化处理:
PUT /_opendistro/jobs/your_index_name/_rollopping
5. 应用索引生命周期管理(ILM)
利用ILM来自动管理索引的创建、滚动和删除,减少手动干预带来的分片问题。
实施步骤
-
检查现有配置:
确认当前cluster.max_shards_per_node的值。如果已为1000/1000,可以增加到较高的数值(如5000)以解决问题。 -
临时调整分片限制(可选):
如果需要快速恢复日志存储,可以使用API命令临时增加分片数。 -
永久调整配置文件:
修改elasticsearch.yml,添加新的分片限制,并重启节点。#cat elasticsearch.yml cluster.name: "docker-cluster" network.host: 0.0.0.0 http.port: 9200 # 开启es跨域 http.cors.enabled: true http.cors.allow-origin: "*" http.cors.allow-headers: Authorization # # 开启安全控制 xpack.security.enabled: true xpack.security.transport.ssl.enabled: true #调整默认分片为5000 cluster.max_shards_per_node: 5000 -
优化索引策略:
减少主分片和副本分片数量,或利用动态分片策略来适应数据量变化。 -
清理旧数据:
删除不再需要的索引,以释放分片资源。 -
监控集群性能:
监控调整后的Elasticsearch集群性能,确保没有出现过载或其他异常情况。
总结
通过临时或永久调整cluster.max_shards_per_node,优化索引策略和清理旧数据,可以解决日志存储问题。请确保在进行硬件资源分配和配置调整之前充分评估其影响,以避免潜在的性能问题。
欢迎关注我的公众号 辣个男人Devin ,新鲜技术文章第一时间推送。
参考链接: