谁再问elasticsearch集群Red怎么办?把这篇笔记给他

2020-01-08  本文已影响0人  独行侠梦

前言

可能你经历过这些Red.

file

。。。等等

那ES的Red是神么意思?

这里说的red,是指es集群的状态,一共有三种,green、red、yellow。具体含义:

file

冷静分析

从上图可知,集群red是由于有主分片不可用,这种情况一般是由于节点宕机。

有什么影响呢?

至少一个主分片(以及它的全部副本)都在缺失中。这意味着你在缺少数据:搜索只能返回部分数据,而分配到这个分片上的写入请求会返回一个异常。

此时我们可以执行相关的命令进行状态检查。

集群节点是否都存在、查看集群状态。
curl -uelastic:pwd  -XGET "http://ip:9200/_cluster/health?pretty"
file file

unassigned_shards原因1

unassigned还有其他原因?

file file

索引层面分析

再执行

curl -uelastic:pwd  -XGET "http://ip:9200/_cluster/health?pretty&level=indices"

没错,还是这个api,不过值得注意的是level=indices,想必读者已经心领神会。
这个api返回的是一个格式化后的json,如果太长,推荐输出到一个文本里面看。

file

从返回的信息中,我们可以看到,01-04索引目前状态为red,它有2个分片,0个副本,有一个分片正在初始化,从这个数据可以看出,受影响的是主分片,想到这里,感到慌不择路。

分片层面分析

少侠,莫慌!

知道了索引层面的故障信息,我们继续深究,看看分片层面。

curl -uelastic:pwd  -XGET "http://ip:9200/_cluster/health?level=shards"

当然,重点还是level=shards,显示如下:

file

至此,我们可以得到更多的线索:

既然是在恢复,那找恢复相关的api,看看。

curl -u elastic:pwd -XGET http://ip:9200/索引名/_recovery?pretty=true
file

从上图可以看到,花费了14.1个小时,从translog中恢复!目前进度很是堪忧。
配合kibana看一下:


file

插播一下,translog的知识

上述摘录于互联网,写得清晰明了,可以参考一下,分析看了日志也没有找到其他有用的信息,由于是历史索引,就将其删除掉了,虽然没有定位到根本原因,不过记录一下排查过程总是好的。

剩下的unassigned分片

curl -uelastic:pwd -XPOST 'http://ip:9200/_cluster/reroute' -H"Content-Type:application/json" -d '{
        "commands" : [ {
              "allocate_stale_primary" : {
                  "index" : "B_2020-01-05",
                  "shard" : 0,
                  "node" : "SL8u8zKESy6rSHjHO0jEvA"
               
              }
            }
        ]
    }'

报错:

No data for shard [0] of index [B_2020-01-05] found on node [SL8u8zKESy6rSHjHO0jEvA]"},"status":400}
curl -uelastic:pwd -XGET "http://ip:9200/_cluster/allocation/explain" -H"Content-Type:application/json" -d '{
  "index": "B_2020-01-05",
  "shard": 0,
  "primary": true
}'
file

执行集群reroute命令:

curl -XPOST "http://ip:9200/_cluster/reroute?retry_failed=true"
file

总结

一、遇到集群Red时,我们可以从如下方法排查:

二、有unassigned分片的排查思路

三、数据重放

先新建备份索引

curl -XPUT ‘http://xxxx:9200/a_index_copy/‘ -d ‘{
“settings”:{
        “index”:{
                “number_of_shards”:3,
                “number_of_replicas”:2
            }
    }
}

通过reindex,将目前可用的数据导入:
POST _reindex
{
"source": {
"index": "a_index"
},
"dest": {
"index": "a_index_copy",
"op_type": "create"
}
}

删除a_index索引,这个必须要先做,否则别名无法添加.
curl -XDELETE 'http://xxxx:9200/a_index'

创建a_index_copy索引

curl -XPUT ‘http://xxxx:9200/a_index_copy/‘ -d ‘{
“settings”:{
                “index”:{
                “number_of_shards”:3,
                “number_of_replicas”:2
            }
    }
}

通过reindex api将a_index数据copy到a_index_copy。

POST _reindex
{
"source": {
            "index": "a_index"
            },
            "dest": {
            "index": "a_index_copy",
            "op_type": "create"
    }
}

删除a_index索引,这个必须要先做,否则别名无法添加

curl -XDELETE 'http://xxxx:9200/a_index'

给a_index_copy添加别名a_index

curl -XPOST 'http://xxxx:9200/_aliases' -d '
{
        "actions": [
            {"add": {"index": "a_index_copy", "alias": "a_index"}}
    ]
}'

四、translog总结

设计目的:

1、帮助节点从失败从快速恢复。

2、辅助flush。避免在flush过程中数据丢失。

以上就是这篇笔记的所有内容,希望能帮助到你。

欢迎来公众号【侠梦的开发笔记】 一起交流进步

上一篇 下一篇

猜你喜欢

热点阅读