飞道的博客

Elasticsearch:如何调试集群状态 - 定位错误信息

547人阅读  评论(0)

针对 Elasticsearch 集群时,我们可以通过如下的 _cluster/health 命令来查询集群的状态:

GET _cluster/health

在正常的情况下,它会显示健康的状态,也就是绿色。关于监控的颜色的描述,我们可以参考我之前的文章 “Elasticsearch中的一些重要概念:cluster, node, index, document, shards及replica”。但是当我们的集群有没有被分配的 shard,或者数据有缺失,那么它的状态就会显示为黄色或者红色

  • 红色:集群中未分配至少一个主分片
  • 黄色:已分配所有主副本,但未分配至少一个副本
  • 绿色:分配所有分片

上面的命令返回的结果如下:


  
  1. {
  2. "cluster_name" : "my_cluster",
  3. "status" : "red",
  4. "timed_out" : false,
  5. "number_of_nodes" : 1,
  6. "number_of_data_nodes" : 1,
  7. "active_primary_shards" : 104,
  8. "active_shards" : 104,
  9. "relocating_shards" : 0,
  10. "initializing_shards" : 0,
  11. "unassigned_shards" : 60,
  12. "delayed_unassigned_shards" : 0,
  13. "number_of_pending_tasks" : 0,
  14. "number_of_in_flight_fetch" : 0,
  15. "task_max_waiting_in_queue_millis" : 0,
  16. "active_shards_percent_as_number" : 63.41463414634146
  17. }

上面显示,我们当前的集群状态为红色,表示数据有丢失的情况。那么我们怎么查出来是那个 shard 和哪个索引出问题了呢?

我们可以使用如下的命令来对集群进行查看:

GET _cluster/health?level=indices

上面的命令可以让我们定位到到底是哪一个或者哪一些索引有问题。上面的命令显示的结果为:

从上面我们可以看出来 restored_logs_4 这个索引是有问题的。它显示的状态为 red,也即是红色。

我们也可以对 shard 进行查询:

GET _cluster/health?level=shards

上面的命令显示的结果为:

上面的命令显示 restored_logs_4 这个索引的 shard 0 的状态是0。这种情况发生在这个 shard 从未被分配过,或者曾经被分配过,但是整个 node 可能由于某种原因而造成这个 shard 的丢失。

我们甚至直接使用如下的方法来得到这个索引的所有情况:

GET _cluster/health/restored_logs_4?level=shards

上面显示的结果为:


  
  1. {
  2. "cluster_name" : "my_cluster",
  3. "status" : "red",
  4. "timed_out" : false,
  5. "number_of_nodes" : 1,
  6. "number_of_data_nodes" : 1,
  7. "active_primary_shards" : 0,
  8. "active_shards" : 0,
  9. "relocating_shards" : 0,
  10. "initializing_shards" : 0,
  11. "unassigned_shards" : 2,
  12. "delayed_unassigned_shards" : 0,
  13. "number_of_pending_tasks" : 0,
  14. "number_of_in_flight_fetch" : 0,
  15. "task_max_waiting_in_queue_millis" : 0,
  16. "active_shards_percent_as_number" : 63.41463414634146,
  17. "indices" : {
  18. "restored_logs_4" : {
  19. "status" : "red",
  20. "number_of_shards" : 1,
  21. "number_of_replicas" : 1,
  22. "active_primary_shards" : 0,
  23. "active_shards" : 0,
  24. "relocating_shards" : 0,
  25. "initializing_shards" : 0,
  26. "unassigned_shards" : 2,
  27. "shards" : {
  28. "0" : {
  29. "status" : "red",
  30. "primary_active" : false,
  31. "active_shards" : 0,
  32. "relocating_shards" : 0,
  33. "initializing_shards" : 0,
  34. "unassigned_shards" : 2
  35. }
  36. }
  37. }
  38. }
  39. }

为了能够更进一步查出来到底是什么原因造成的,我们可以如下的命令来进行查询:

GET _cluster/allocation/explain

在实际的使用中,我们需要配置一些参数来得到某个具体索引的分配情况,比如:


  
  1. GET _cluster/allocation/ explain
  2. {
  3. "index": "restored_logs_4",
  4. "shard": 0,
  5. "primary": true
  6. }

上面的命令显示的结果为:


  
  1. {
  2. "index" : "restored_logs_4",
  3. "shard" : 0,
  4. "primary" : true,
  5. "current_state" : "unassigned",
  6. "unassigned_info" : {
  7. "reason" : "CLUSTER_RECOVERED",
  8. "at" : "2020-10-05T08:08:54.241Z",
  9. "last_allocation_status" : "no_valid_shard_copy"
  10. },
  11. "can_allocate" : "no_valid_shard_copy",
  12. "allocate_explanation" : "cannot allocate because a previous copy of the primary shard existed but can no longer be found on the nodes in the cluster",
  13. "node_allocation_decisions" : [
  14. {
  15. "node_id" : "Ohi9yhffThGZ5X8gq4AXLw",
  16. "node_name" : "node1",
  17. "transport_address" : "127.0.0.1:9300",
  18. "node_attributes" : {
  19. "ml.machine_memory" : "34359738368",
  20. "xpack.installed" : "true",
  21. "transform.node" : "true",
  22. "ml.max_open_jobs" : "20",
  23. "my_rack" : "rack1"
  24. },
  25. "node_decision" : "no",
  26. "store" : {
  27. "found" : false
  28. }
  29. }
  30. ]
  31. }

从上面的描述中,我们可以看到为啥我们的 shard 是分配不成功的。


转载:https://blog.csdn.net/UbuntuTouch/article/details/108973356
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场