Elasticsearch 启动失败?从日志分析到最终解决(磁盘空间不足案例)_服务器断电后elasticsearch无法启动
问题背景
最近在本地运行一个 Spring Boot 项目时,突然遇到 Elasticsearch (ES) 连接超时的问题:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name \'awardController\': Unsatisfied dependency expressed through field \'esService\'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name \'esService\': Invocation of init method failed; nested exception is java.net.SocketTimeoutException: 30,000 milliseconds timeout on connection http-outgoing-0 [ACTIVE]
奇怪的是,浏览器和 curl
都能访问 ES,但 Java 应用就是连不上!经过一番排查,最终发现是 服务器磁盘空间不足 导致 ES 无法正常写入数据。
本文将详细介绍 如何从日志分析到最终解决问题,适用于类似 ES 启动失败、连接超时等场景。
1. 初步排查
1.1 检查 ES 是否正常运行
在浏览器访问 http://123.57.248.153:9200
,返回正常:
{ \"name\": \"node-1\", \"cluster_name\": \"class-search-cluster\", \"cluster_uuid\": \"mZoMZHM3QTSKqttbMOpwgw\", \"version\": { \"number\": \"8.17.4\", \"build_flavor\": \"default\", \"build_type\": \"zip\", \"build_hash\": \"c63c7f5f8ce7d2e4805b7b3d842e7e792d84dda1\", \"build_date\": \"2025-03-20T15:39:59.811110136Z\", \"build_snapshot\": false, \"lucene_version\": \"9.12.0\", \"minimum_wire_compatibility_version\": \"7.17.0\", \"minimum_index_compatibility_version\": \"7.0.0\" }, \"tagline\": \"You Know, for Search\"}
结论:ES HTTP 接口正常,但 Java 客户端无法连接。
1.2 检查 Java 客户端配置
Spring Boot 的 application.yml
配置:
elasticsearch: host: /////// scheme: http port: 9200 username: elastic password: 123456
看起来没问题,但为什么连不上?
2. 深入排查
2.1 检查 ES 服务日志
ES 真正的日志在 logs/elasticsearch.log
,而不是 Windows 服务日志(prunsrv.c
)。
查看日志:
tail -f /var/log/elasticsearch/class-search-cluster.log
发现关键错误:
[2025-04-01T18:48:14,123][ERROR][o.e.b.ElasticsearchUncaughtExceptionHandler] [node-1] fatal error in thread [main], exitingjava.io.IOException: No space left on device at java.base/java.io.RandomAccessFile.writeBytes0(Native Method) at java.base/java.io.RandomAccessFile.writeBytes(RandomAccessFile.java:569) at org.apache.lucene.store.MMapDirectory$MMapIndexOutput.ensureOpen(MMapDirectory.java:355) ...
问题确认:磁盘空间不足,导致 ES 无法写入数据!
2.2 检查服务器磁盘空间
df -h
输出:
Filesystem Size Used Avail Use% Mounted on/dev/vda1 50G 50G 0B 100% /
磁盘已用满!
3. 解决方案
3.1 清理磁盘空间
删除不必要的日志、缓存文件:
# 删除旧日志rm -rf /var/log/elasticsearch/old_logs_*# 清理 apt/yum 缓存apt clean # Debian/Ubuntuyum clean all # CentOS/RHEL# 查找大文件find / -type f -size +100M -exec ls -lh {} \\;
3.2 调整 ES 数据存储路径
如果无法立即清理空间,可以修改 elasticsearch.yml
,更换数据目录:
path: data: /mnt/another_disk/elasticsearch/data logs: /mnt/another_disk/elasticsearch/logs
3.3 重启 ES
systemctl restart elasticsearch
或(Windows 服务):
elasticsearch-service.bat restart
4. 验证修复
4.1 检查 ES 状态
curl http://localhost:9200/_cluster/health?pretty
返回:
{ \"cluster_name\": \"class-search-cluster\", \"status\": \"green\", \"timed_out\": false, \"number_of_nodes\": 1, \"number_of_data_nodes\": 1, ...}
ES 恢复正常!
4.2 Java 客户端连接成功
Spring Boot 项目启动后,ES 操作正常,不再报超时错误。
5. 总结
jvm.options
curl
能访问,但 Java 连不上-Dhttp.nonProxyHosts
关键点:
-
ES 日志 (
elasticsearch.log
) 是最重要的排查依据,不要只看服务控制日志。 -
磁盘空间不足会导致 ES 无法写入数据,但 HTTP 接口可能仍然能访问(误导性现象)。
-
Java 客户端超时可能是 ES 内部异常,不一定是网络问题。
6. 预防措施
-
监控磁盘空间:
df -h # 每天检查
-
设置 ES 自动清理旧索引:
curl -X DELETE \"http://localhost:9200/old_index_*\"
-
使用
cron
定期清理日志:0 3 * * * find /var/log/elasticsearch -type f -mtime +7 -delete
希望这篇博客能帮你解决 Elasticsearch 启动失败的问题! 🚀
如果有其他问题,欢迎留言讨论!