Elasticsearch 常见问题处理
集群访问超时
发生超时的原因一般为:
• 集群挂掉了
• 集群压力很大,来不及响应用户的请求
• 网络故障,导致访问不通
因此集群失联告警应该重点关注,及时处理。集群挂掉一般是由于jvm内存溢出导致节点hang死,如下所示:
如上图所示,节点jvm内存已使用完,经过连续的 old gc 依然不能释放内存,说明节点已发生 oom。
处理步骤如下:
• 用告警条件到kibana的搜索栏去检索,查出有告警的集群,用curl命令或者kibana(如果集群挂掉,kibana打开会异常)查看集群的监控状态,命令如下:
curl -XGET {ip:port}/_cluster/health?pretty
2.如果访问不通,首先排查是否为网络问题,若不是网络问题,则需登录到es所在的机器,查看es日志,如果大量进程都有old gc日志且gc时间较长,则集群大概率是发生内存溢出,整个集群挂掉了,此时,需要通过集群故障恢复脚本对集群进行重启恢复。故障恢复脚本所放位置为:
100.67.181.13 data/user_00/repository/tools/cluster_recovery
使用方法: 首先在配置文件中填好需要恢复的集群名、oss地址、集群所在机器的root密码等,然后直接运行基本即可。如下:
集群 Red 告警
集群出现red,代表有分片(包含主分片和从分片)丢失,此时,可以看es master的日志,查看集群变red的原因,也可以用过kibana查看分片的状态,即:
curl -XGET {ip:port}/_cat/shards?v
找到状态为 UNASSIGNED 的 shard,并找到分片所在的node,然后查看相应的node是否挂掉,如果挂掉,需要重启node,再观察集群和分片状态。 如果node都没挂掉,那么需要手动分配shard,命令如下:
curl -XPOST {ip:port}/_cluster/reroute?retry_failed=true -d
'{
"commands": [
{
"allocate_stale_primary": {
"index": "${index_name}", # shard所在的index
"shard": 1, # shard号
"node": "${node_name}", # 分配到哪个node上
"accept_data_loss":true # 是否接受数据有丢失
}
}
]
}'
例如:
curl -XPOST localhost:9200/_cluster/reroute?retry_failed=true -d
'{
"commands": [
{
"allocate_stale_primary": {
"index": "vpc_ping_detail-60@57600000_99",
"shard": 1,
"node": "1503496474000001709",
"accept_data_loss":true
}
}
]
}'
集群 Yellow 告警
集群出现 yellow,说明有从分片未分配,此时可以通过以下查看集群各分片状态的数量:
curl -XGET {ip:port}/_cluster/health
返回结果如下:
{
"cluster_name": "clog_qcloud_gz",
"status": "green",
"timed_out": false,
"number_of_nodes": 99,
"number_of_data_nodes": 99,
"active_primary_shards": 8504,
"active_shards": 17018,
"relocating_shards": 60, # 正在进行搬迁的shard数
"initializing_shards": 0, # 正在初始化的shard数
"unassigned_shards": 0, # 未分配的shard数
"delayed_unassigned_shards": 0,
"number_of_pending_tasks": 39,
"number_of_in_flight_fetch": 0,
"task_max_waiting_in_queue_millis": 80643,
"active_shards_percent_as_number": 100
}
若发现未分配的shard较多,可以适当调整集群的恢复速度,加快恢复,待集群状态变为green时,再将恢复速度调为原来的值。
集群并发恢复设置如下:
// 调大恢复速度
curl -XPUT {ip:port}/_cluster/settings -d
'{
"persistent": {
"cluster.routing.allocation.node_concurrent_recoveries": 56,
"indices.recovery.max_bytes_per_sec": "400mb"
}
}'
// 调回原来的值
curl -XPUT {ip:port}/_cluster/settings -d
'{
"persistent": {
"cluster.routing.allocation.node_concurrent_recoveries": 8,
"indices.recovery.max_bytes_per_sec": "40mb"
}
}'