1. 首页 > ElasticSearch教程 > 正文

ElasticSearch教程FG044-ElasticSearch常用报错

内容简介:本文档风哥主要介绍ElasticSearch常见的报错类型、原因及解决方案,包括启动报错、集群报错、索引报错、查询报错等内容。通过学习本文,您将掌握如何快速识别和解决ElasticSearch的常见报错,确保集群的稳定运行。风哥教程参考ElasticSearch官方文档Troubleshooting部分。

Part01-基础概念与理论知识

1.1 报错类型概述

ElasticSearch常见的报错类型包括:

  • 启动报错:ElasticSearch启动时出现的错误,如端口被占用、权限不足等
  • 集群报错:集群状态异常,如节点离线、脑裂等
  • 索引报错:索引操作失败,如索引创建失败、分片分配失败等
  • 查询报错:查询操作失败,如查询语法错误、内存不足等
  • 性能报错:性能相关的错误,如超时、内存溢出等
  • 安全报错:安全相关的错误,如认证失败、权限不足等

1.2 报错日志分析

ElasticSearch的报错日志位于/es/app/elasticsearch-8.10.0/logs/目录下,主要包括:

  • 集群日志fgedu-cluster.log,记录集群级别的错误和警告
  • 节点日志fgedu-cluster_node-1.log,记录节点级别的错误和警告
  • 慢查询日志fgedu-cluster_index_search_slowlog.log,记录慢查询

分析报错日志的方法:

  • 查看错误信息:找到日志中的错误信息,了解错误的具体内容
  • 分析错误原因:根据错误信息分析错误的原因
  • 查找解决方案:根据错误原因查找相应的解决方案

1.3 报错处理流程

报错处理流程包括:

  1. 错误发现:通过日志、监控或用户反馈发现错误
  2. 错误定位:分析错误日志,定位错误原因
  3. 错误分析:分析错误的影响范围和严重程度
  4. 错误处理:采取相应的措施处理错误
  5. 错误验证:验证错误是否已解决
  6. 错误总结:总结错误原因和处理经验,防止错误再次发生

Part02-生产环境规划与建议

2.1 报错预防措施

报错预防措施:

  • 合理配置:根据硬件资源和业务需求,合理配置ElasticSearch参数
  • 定期检查:定期检查集群状态、节点状态、索引状态等
  • 监控告警:建立完善的监控体系,及时发现潜在问题
  • 定期备份:定期创建快照,确保数据安全
  • 及时更新:及时更新ElasticSearch版本和补丁
  • 安全配置:配置合适的安全措施,防止安全问题

2.2 报错监控与告警

报错监控与告警:

  • 日志监控:监控ElasticSearch日志,及时发现错误信息
  • 集群状态监控:监控集群健康状态,当状态变为yellow或red时告警
  • 节点状态监控:监控节点是否在线,CPU、内存、磁盘使用情况
  • 性能监控:监控查询响应时间、索引吞吐量、搜索吞吐量等
  • 告警设置:设置合理的告警阈值,及时通知运维人员

2.3 报错处理最佳实践

报错处理最佳实践:

  • 保持冷静:在处理错误时保持冷静,避免误操作
  • 快速定位:通过日志、监控等手段快速定位错误原因
  • 及时处理:采取有效措施,尽快解决错误
  • 最小化影响:减少错误对业务的影响
  • 记录处理过程:记录错误处理过程,便于后续分析
  • 总结经验:总结错误原因和处理经验,防止错误再次发生

Part03-生产环境项目实施方案

3.1 启动报错处理

启动报错处理:

# 1. 常见启动报错及解决方案

# 报错1:端口被占用
# 错误信息:
# [ERROR][o.e.b.Bootstrap ] [node-1] Exception
# java.net.BindException: Address already in use

# 解决方案:
# 查看端口占用情况
netstat -tlnp | grep 9200

# 停止占用端口的进程
kill -9

# 报错2:权限不足
# 错误信息:
# [ERROR][o.e.b.Bootstrap ] [node-1] Exception
# java.nio.file.AccessDeniedException: /es/app/elasticsearch-8.10.0/config/elasticsearch.yml

# 解决方案:
# 检查文件权限
ls -la /es/app/elasticsearch-8.10.0/config/elasticsearch.yml

# 修改文件权限
chown -R elasticsearch:elasticsearch /es/app/elasticsearch-8.10.0

# 报错3:内存不足
# 错误信息:
# [ERROR][o.e.b.Bootstrap ] [node-1] Exception
# java.lang.RuntimeException: starting java failed with [1]
# output:
# OpenJDK 64-Bit Server VM warning: INFO: os::commit_memory(0x0000000085330000, 268435456, 0) failed; error=’Cannot allocate memory’ (errno=12)

# 解决方案:
# 调整JVM堆大小
vi /es/app/elasticsearch-8.10.0/config/jvm.options

# 修改堆大小
-Xms1g
-Xmx1g

# 报错4:文件描述符不足
# 错误信息:
# [ERROR][o.e.b.Bootstrap ] [node-1] Exception
# java.lang.RuntimeException: can not run elasticsearch as root

# 解决方案:
# 切换到非root用户
su – elasticsearch

# 报错5:无法锁定内存
# 错误信息:
# [WARN ][o.e.b.JNANatives ] [node-1] unable to lock JVM memory (ENOMEM). This can result in part of the JVM being swapped out.

# 解决方案:
# 启用内存锁定
vi /es/app/elasticsearch-8.10.0/config/elasticsearch.yml

# 添加以下内容
bootstrap.memory_lock: true

# 调整系统限制
vi /etc/security/limits.conf

# 添加以下内容
* soft memlock unlimited
* hard memlock unlimited

3.2 集群报错处理

集群报错处理:

# 1. 常见集群报错及解决方案

# 报错1:集群状态变为red
# 错误信息:
# {
# “cluster_name” : “fgedu-cluster”,
# “status” : “red”,
# “timed_out” : false,
# “number_of_nodes” : 3,
# “number_of_data_nodes” : 3,
# “active_primary_shards” : 0,
# “active_shards” : 0,
# “relocating_shards” : 0,
# “initializing_shards” : 0,
# “unassigned_shards” : 12,
# “delayed_unassigned_shards” : 0,
# “number_of_pending_tasks” : 0,
# “number_of_in_flight_fetch” : 0,
# “task_max_waiting_in_queue_millis” : 0,
# “active_shards_percent_as_number” : 0.0
# }

# 解决方案:
# 检查未分配的分片
curl -X GET “http://192.168.1.10:9200/_cat/shards?v” | grep -E “UNASSIGNED”

# 分析分片分配失败的原因
curl -X GET “http://192.168.1.10:9200/_cluster/allocation/explain?pretty”

# 尝试修复集群状态
# 重启有问题的节点
systemctl restart elasticsearch

# 报错2:脑裂
# 错误信息:
# [WARN ][o.e.d.z.ZenDiscovery ] [node-1] not enough master nodes discovered during pinging (found [[Candidate{node={node-1}{…}{…}{192.168.1.10}{192.168.1.10:9300}{m}{xpack.installed=true}]], but needed [2]), pinging again

# 解决方案:
# 配置minimum_master_nodes
curl -X PUT “http://192.168.1.10:9200/_cluster/settings” -H “Content-Type: application/json” -d ‘{
“persistent”: {
“discovery.zen.minimum_master_nodes”: 2
}
}’

# 报错3:节点无法加入集群
# 错误信息:
# [WARN ][o.e.d.z.ZenDiscovery ] [node-2] failed to connect to master [{node-1}{…}{…}{192.168.1.10}{192.168.1.10:9300}{m}{xpack.installed=true}], retrying…

# 解决方案:
# 检查网络连接
ping 192.168.1.10

# 检查集群配置
vi /es/app/elasticsearch-8.10.0/config/elasticsearch.yml

# 确保discovery.seed_hosts配置正确
discovery.seed_hosts: [“192.168.1.10”, “192.168.1.11”, “192.168.1.12”]

3.3 索引报错处理

索引报错处理:

# 1. 常见索引报错及解决方案

# 报错1:索引创建失败
# 错误信息:
# {
# “error”: {
# “root_cause”: [
# {
# “type”: “resource_already_exists_exception”,
# “reason”: “index [fgedu-index/1234567890] already exists”,
# “index_uuid”: “1234567890”,
# “index”: “fgedu-index”
# }
# ],
# “type”: “resource_already_exists_exception”,
# “reason”: “index [fgedu-index/1234567890] already exists”,
# “index_uuid”: “1234567890”,
# “index”: “fgedu-index”
# },
# “status”: 400
# }

# 解决方案:
# 检查索引是否存在
curl -X HEAD “http://192.168.1.10:9200/fgedu-index”

# 如果存在,删除索引或使用其他名称
curl -X DELETE “http://192.168.1.10:9200/fgedu-index”

# 报错2:分片分配失败
# 错误信息:
# {
# “error”: {
# “root_cause”: [
# {
# “type”: “unassigned_shards”,
# “reason”: “[fgedu-index][0] allocation failed: failed to allocate all shards”
# }
# ],
# “type”: “unassigned_shards”,
# “reason”: “[fgedu-index][0] allocation failed: failed to allocate all shards”
# },
# “status”: 500
# }

# 解决方案:
# 分析分片分配失败的原因
curl -X GET “http://192.168.1.10:9200/_cluster/allocation/explain?pretty”

# 尝试强制分配分片
curl -X POST “http://192.168.1.10:9200/_cluster/reroute” -H “Content-Type: application/json” -d ‘{
“commands”: [
{
“allocate”: {
“index”: “fgedu-index”,
“shard”: 0,
“node”: “node-2”,
“allow_primary”: true
}
}
]
}’

# 报错3:索引大小超过限制
# 错误信息:
# {
# “error”: {
# “root_cause”: [
# {
# “type”: “cluster_block_exception”,
# “reason”: “index [fgedu-index] blocked by: [TOO_MANY_REQUESTS/12/disk usage exceeded flood-stage watermark, index has read-only-allow-delete block]”
# }
# ],
# “type”: “cluster_block_exception”,
# “reason”: “index [fgedu-index] blocked by: [TOO_MANY_REQUESTS/12/disk usage exceeded flood-stage watermark, index has read-only-allow-delete block]”
# },
# “status”: 429
# }

# 解决方案:
# 检查磁盘使用情况
df -h

# 清理磁盘空间
# 删除不需要的索引
curl -X DELETE “http://192.168.1.10:9200/fgedu-logs-2023.*”

# 解除索引只读状态
curl -X PUT “http://192.168.1.10:9200/fgedu-index/_settings” -H “Content-Type: application/json” -d ‘{
“index”: {
“blocks”: {
“read_only_allow_delete”: “false”
}
}
}’

3.4 查询报错处理

查询报错处理:

# 1. 常见查询报错及解决方案

# 报错1:查询语法错误
# 错误信息:
# {
# “error”: {
# “root_cause”: [
# {
# “type”: “parsing_exception”,
# “reason”: “[match] query does not support [query]”,
# “line”: 1,
# “col”: 20
# }
# ],
# “type”: “parsing_exception”,
# “reason”: “[match] query does not support [query]”,
# “line”: 1,
# “col”: 20
# },
# “status”: 400
# }

# 解决方案:
# 检查查询语法
# 正确的match查询语法
curl -X POST “http://192.168.1.10:9200/fgedu-index/_search” -H “Content-Type: application/json” -d ‘{
“query”: {
“match”: {
“name”: “张三”
}
}
}’

# 报错2:内存不足
# 错误信息:
# {
# “error”: {
# “root_cause”: [
# {
# “type”: “circuit_breaking_exception”,
# “reason”: “[parent] Data too large, data for [_search] would be [1234567890/1.1gb], which is larger than the limit of [1073741824/1gb]”,
# “bytes_wanted”: 1234567890,
# “bytes_limit”: 1073741824
# }
# ],
# “type”: “circuit_breaking_exception”,
# “reason”: “[parent] Data too large, data for [_search] would be [1234567890/1.1gb], which is larger than the limit of [1073741824/1gb]”,
# “bytes_wanted”: 1234567890,
# “bytes_limit”: 1073741824
# },
# “status”: 429
# }

# 解决方案:
# 优化查询,减少返回结果
curl -X POST “http://192.168.1.10:9200/fgedu-index/_search” -H “Content-Type: application/json” -d ‘{
“size”: 10,
“query”: {
“match”: {
“name”: “张三”
}
}
}’

# 增加JVM堆大小
vi /es/app/elasticsearch-8.10.0/config/jvm.options

# 修改堆大小
-Xms2g
-Xmx2g

# 报错3:查询超时
# 错误信息:
# {
# “error”: {
# “root_cause”: [
# {
# “type”: “search_phase_execution_exception”,
# “reason”: “all shards failed”,
# “phase”: “query”,
# “grouped”: true,
# “failed_shards”: [
# {
# “shard”: 0,
# “index”: “fgedu-index”,
# “node”: “node-1”,
# “reason”: {
# “type”: “query_shard_exception”,
# “reason”: “Failed to execute query”,
# “caused_by”: {
# “type”: “timeout_exception”,
# “reason”: “Query timed out after [10.0s]”
# }
# }
# }
# ]
# }
# ],
# “type”: “search_phase_execution_exception”,
# “reason”: “all shards failed”,
# “phase”: “query”,
# “grouped”: true,
# “failed_shards”: [
# {
# “shard”: 0,
# “index”: “fgedu-index”,
# “node”: “node-1”,
# “reason”: {
# “type”: “query_shard_exception”,
# “reason”: “Failed to execute query”,
# “caused_by”: {
# “type”: “timeout_exception”,
# “reason”: “Query timed out after [10.0s]”
# }
# }
# }
# ]
# },
# “status”: 500
# }

# 解决方案:
# 优化查询语句
# 增加查询超时时间
curl -X POST “http://192.168.1.10:9200/fgedu-index/_search?timeout=30s” -H “Content-Type: application/json” -d ‘{
“query”: {
“match”: {
“name”: “张三”
}
}
}’

Part04-生产案例与实战讲解

4.1 启动报错处理实战

启动报错处理实战:

# 1. 模拟启动报错
# 修改elasticsearch.yml配置文件,故意配置错误
vi /es/app/elasticsearch-8.10.0/config/elasticsearch.yml

# 添加错误配置
cluster.name: fgedu-cluster
node.name: node-1
network.host: 192.168.1.10
http.port: 9200
transport.port: 9300
discovery.seed_hosts: [“192.168.1.10”, “192.168.1.11”, “192.168.1.12”]
cluster.initial_master_nodes: [“node-1”]
# 添加错误配置
this_is_an_error: true

# 2. 启动ElasticSearch
systemctl start elasticsearch

# 3. 检查启动状态
systemctl status elasticsearch

# 4. 查看错误日志
tail -f /es/app/elasticsearch-8.10.0/logs/fgedu-cluster.log

# 5. 修复错误
# 编辑elasticsearch.yml,删除错误配置
vi /es/app/elasticsearch-8.10.0/config/elasticsearch.yml

# 删除错误配置
# this_is_an_error: true

# 6. 重新启动ElasticSearch
systemctl restart elasticsearch

# 7. 验证启动成功
systemctl status elasticsearch
curl -X GET “http://192.168.1.10:9200”

4.2 集群报错处理实战

集群报错处理实战:

# 1. 模拟集群报错
# 停止一个节点
systemctl stop elasticsearch

# 2. 检查集群状态
curl -X GET “http://192.168.1.10:9200/_cluster/health?pretty”

# 3. 检查节点状态
curl -X GET “http://192.168.1.10:9200/_cat/nodes?v”

# 4. 检查分片分配情况
curl -X GET “http://192.168.1.10:9200/_cat/shards?v”

# 5. 重启故障节点
systemctl start elasticsearch

# 6. 检查集群状态
curl -X GET “http://192.168.1.10:9200/_cluster/health?pretty”

# 7. 检查节点状态
curl -X GET “http://192.168.1.10:9200/_cat/nodes?v”

# 8. 检查分片分配情况
curl -X GET “http://192.168.1.10:9200/_cat/shards?v”

4.3 索引报错处理实战

索引报错处理实战:

# 1. 模拟索引报错
# 创建一个索引
curl -X PUT “http://192.168.1.10:9200/fgedu-test” -H “Content-Type: application/json” -d ‘{
“settings”: {
“number_of_shards”: 3,
“number_of_replicas”: 2
}
}’

# 尝试再次创建相同的索引
curl -X PUT “http://192.168.1.10:9200/fgedu-test” -H “Content-Type: application/json” -d ‘{
“settings”: {
“number_of_shards”: 3,
“number_of_replicas”: 2
}
}’

# 2. 查看错误信息
# 上面的命令会返回错误信息

# 3. 修复错误
# 检查索引是否存在
curl -X HEAD “http://192.168.1.10:9200/fgedu-test”

# 删除索引
curl -X DELETE “http://192.168.1.10:9200/fgedu-test”

# 重新创建索引
curl -X PUT “http://192.168.1.10:9200/fgedu-test” -H “Content-Type: application/json” -d ‘{
“settings”: {
“number_of_shards”: 3,
“number_of_replicas”: 2
}
}’

# 4. 验证修复成功
curl -X GET “http://192.168.1.10:9200/_cat/indices?v”

4.4 查询报错处理实战

查询报错处理实战:

# 1. 模拟查询报错
# 创建测试索引
curl -X PUT “http://192.168.1.10:9200/fgedu-test” -H “Content-Type: application/json” -d ‘{
“settings”: {
“number_of_shards”: 3,
“number_of_replicas”: 2
},
“mappings”: {
“properties”: {
“id”: {
“type”: “integer”
},
“name”: {
“type”: “text”
}
}
}
}’

# 插入测试数据
curl -X POST “http://192.168.1.10:9200/fgedu-test/_bulk” -H “Content-Type: application/json” -d ‘{
“index”: {}
{“id”: 1, “name”: “张三”}
{“index”: {}
{“id”: 2, “name”: “李四”}
{“index”: {}
{“id”: 3, “name”: “王五”}
}’

# 执行错误的查询
curl -X POST “http://192.168.1.10:9200/fgedu-test/_search” -H “Content-Type: application/json” -d ‘{
“query”: {
“match”: {
“query”: “张三”
}
}
}’

# 2. 查看错误信息
# 上面的命令会返回错误信息

# 3. 修复错误
# 执行正确的查询
curl -X POST “http://192.168.1.10:9200/fgedu-test/_search” -H “Content-Type: application/json” -d ‘{
“query”: {
“match”: {
“name”: “张三”
}
}
}’

# 4. 验证修复成功
# 上面的命令会返回正确的查询结果

Part05-风哥经验总结与分享

5.1 常用报错汇总

  • 启动报错:端口被占用、权限不足、内存不足、文件描述符不足、无法锁定内存等
  • 集群报错:集群状态变为red、脑裂、节点无法加入集群等
  • 索引报错:索引创建失败、分片分配失败、索引大小超过限制等
  • 查询报错:查询语法错误、内存不足、查询超时等
  • 性能报错:超时、内存溢出、CPU过载等
  • 安全报错:认证失败、权限不足等

5.2 报错处理技巧

  • 查看日志:通过查看ElasticSearch日志,了解错误的具体内容和原因
  • 分析错误:根据错误信息分析错误的原因,找出解决方案
  • 搜索解决方案:通过搜索引擎搜索错误信息,找到相关的解决方案
  • 测试解决方案:在测试环境中测试解决方案,确保其有效性
  • 应用解决方案:在生产环境中应用解决方案,解决错误
  • 记录解决方案:记录错误原因和解决方案,便于后续参考

5.3 报错预防建议

  • 合理配置:根据硬件资源和业务需求,合理配置ElasticSearch参数
  • 定期检查:定期检查集群状态、节点状态、索引状态等
  • 监控告警:建立完善的监控体系,及时发现潜在问题
  • 定期备份:定期创建快照,确保数据安全
  • 及时更新:及时更新ElasticSearch版本和补丁
  • 安全配置:配置合适的安全措施,防止安全问题
  • 培训与文档:对运维人员进行培训,建立完善的文档体系

更多视频教程www.fgedu.net.cn

学习交流加群风哥微信: itpux-com

学习交流加群风哥QQ113257174

风哥提示:报错处理是ElasticSearch运维的重要环节,需要建立完善的报错处理流程和应急预案

更多学习教程公众号风哥教程itpux_com

from ElasticSearch视频:www.itpux.com

本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html

联系我们

在线咨询:点击这里给我发消息

微信号:itpux-com

工作日:9:30-18:30,节假日休息