📜  elasticsearch 检查状态 (1)

📅  最后修改于: 2023-12-03 15:00:35.130000             🧑  作者: Mango

Elasticsearch 检查状态

Elasticsearch 是一个流行的分布式搜索和分析引擎,它是基于 Apache Lucene 构建的。开发人员可以使用 Elasticsearch 来存储、搜索和分析大量的数据。

在使用 Elasticsearch 的过程中,我们需要经常检查集群的状态以确保其正常运行。本文将介绍如何检查 Elasticsearch 集群的状态。

使用 Elasticsearch API

Elasticsearch 提供了一个 RESTful API,可以通过 HTTP 请求来访问。我们可以使用该 API 来查询 Elasticsearch 集群的状态信息。以下是几个有用的 API:

1. _cluster/health

该 API 可以用来获取集群的健康状态、分片数量和节点数量等信息。以下是一个例子:

GET /_cluster/health?pretty

响应结果:

{
  "cluster_name" : "elasticsearch",
  "status" : "yellow",
  "timed_out" : false,
  "number_of_nodes" : 1,
  "number_of_data_nodes" : 1,
  "active_primary_shards" : 5,
  "active_shards" : 5,
  "relocating_shards" : 0,
  "initializing_shards" : 0,
  "unassigned_shards" : 5,
  "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" : 50.0
}

从响应结果中可以看出,该集群的健康状态为黄色,有一个节点,其中有 5 个分片处于活跃状态。

2. _cluster/state

该 API 可以用来获取集群和索引的详细信息。以下是一个例子:

GET /_cluster/state?pretty

响应结果:

{
  "cluster_name" : "elasticsearch",
  "compressed_size_in_bytes" : 84866,
  "version" : 3,
  "state_uuid" : "zOjyKkQ4Q4ikby4QHqh0JQ",
  "master_node" : "x0FMDtYWTHKtDMgkPmKBLw",
  "blocks" : { },
  "nodes" : {
    "x0FMDtYWTHKtDMgkPmKBLw" : {
      "name" : "node1",
      "transport_address" : "127.0.0.1:9300",
      "attributes" : { }
    }
  },
  "metadata" : {
    "templates" : {
      "template_1" : {
        "order" : 0,
        "index_patterns" : [
          "logs-*"
        ],
        "settings" : {
          "index" : {
            "number_of_shards" : "5",
            "number_of_replicas" : "1"
          }
        },
        "mappings" : {
          "properties" : {
            "message" : {
              "type" : "text"
            }
          }
        }
      }
    },
    "indices" : {
      "logs-2018-01-01" : {
        "uuid" : "A3eAdOw5Q5e-eZvov5W2FA",
        "state" : "open",
        "settings" : {
          "index" : {
            "number_of_shards" : "5",
            "provided_name" : "logs-2018-01-01",
            "creation_date" : "1607892361703",
            "analysis" : { },
            "number_of_replicas" : "1",
            "uuid" : "A3eAdOw5Q5e-eZvov5W2FA",
            "version" : {
              "created" : "7100299"
            }
          }
        },
        "mappings" : {
          "properties" : {
            "message" : {
              "type" : "text"
            }
          }
        }
      }
    }
  },
  "routing_table" : {
    "indices" : {
      "logs-2018-01-01" : {
        "shards" : {
          "0" : [
            {
              "state" : "STARTED",
              "primary" : true,
              "node" : "x0FMDtYWTHKtDMgkPmKBLw",
              "relocating_node" : null,
              "shard" : 0,
              "index" : "logs-2018-01-01",
              "allocation_id" : {
                "id" : "TmXKf1RyTJGIyEP1CjYGug"
              }
            }
          ],
          "1" : [
            {
              "state" : "STARTED",
              "primary" : true,
              "node" : "x0FMDtYWTHKtDMgkPmKBLw",
              "relocating_node" : null,
              "shard" : 1,
              "index" : "logs-2018-01-01",
              "allocation_id" : {
                "id" : "92876sqkQm6V1UQXflNo7g"
              }
            }
          ],
          "2" : [
            {
              "state" : "STARTED",
              "primary" : true,
              "node" : "x0FMDtYWTHKtDMgkPmKBLw",
              "relocating_node" : null,
              "shard" : 2,
              "index" : "logs-2018-01-01",
              "allocation_id" : {
                "id" : "6DUcU6FIQd6soLjbjYQ2Mw"
              }
            }
          ],
          "3" : [
            {
              "state" : "STARTED",
              "primary" : true,
              "node" : "x0FMDtYWTHKtDMgkPmKBLw",
              "relocating_node" : null,
              "shard" : 3,
              "index" : "logs-2018-01-01",
              "allocation_id" : {
                "id" : "q3yfLUS2QOqnMw-WKdhlqA"
              }
            }
          ],
          "4" : [
            {
              "state" : "STARTED",
              "primary" : true,
              "node" : "x0FMDtYWTHKtDMgkPmKBLw",
              "relocating_node" : null,
              "shard" : 4,
              "index" : "logs-2018-01-01",
              "allocation_id" : {
                "id" : "mNCzye12Spm1tQy4OdXRyw"
              }
            }
          ]
        }
      }
    }
  },
  "routing_nodes" : {
    "unassigned" : [ ],
    "nodes" : {
      "x0FMDtYWTHKtDMgkPmKBLw" : [ ]
    }
  }
}

从响应结果中可以看出,该集群有一个节点,其中有一个索引 logs-2018-01-01,该索引有 5 个分片。

3. _cat/indices

该 API 可以用来获取索引的基本信息,例如索引大小、文档数、分片数和副本数等。以下是一个例子:

GET /_cat/indices?v

响应结果:

health status index               uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   logs-2018-01-01     A3eAdOw5Q5e-eZvov5W2FA   5   1          0            0      2.1kb          2.1kb

从响应结果中可以看出,该集群的健康状态为黄色,logs-2018-01-01 索引有 5 个分片和 1 个副本。

通过命令行工具检查 Elasticsearch 状态

除了通过 Elasticsearch API 来查询集群状态外,我们还可以使用命令行工具来执行该操作。以下是对应的命令:

1. curl

使用 curl 命令来执行 HTTP 请求,例如:

curl localhost:9200/_cluster/health?pretty
2. httpie

使用 httpie 命令来执行 HTTP 请求,例如:

http localhost:9200/_cluster/health?pretty
3. escli

使用 escli 命令行工具来查询 Elasticsearch 状态信息,例如:

escli cluster_health
总结

在本文中,我们介绍了如何使用 Elasticsearch API 和命令行工具来查询 Elasticsearch 集群的状态信息。开发人员可以根据自己的需求来选择合适的方式来检查 Elasticsearch 集群的状态。