Elasticsearch系列-集群API(一)

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/projim_tao/article/details/102644646

引言

Elasticsearch提供了大量了的Rest API用以操作Elastic search的相关功能,提供了极大的便利,掌握这些API是熟练地使用Elasticsearch的前提。

Elastic search提供的API有集群API、搜索API、索引API、文档API、cat API、聚合API等,本文主要讲解Elasticsearch API,使用集群API(下文称为cluster API)可以查看集群简况状况、集群状态和统计信息、集群节点信息等内容。

一、查看集群健康情况(health)

使用helath API可以查看集群的整体健康情况,当有异常时及时告警,例如:

$ curl -X GET "localhost:9200/_cluster/health?pretty"
{
  "cluster_name": "elasticsearch",
  "status": "yellow",
  "timed_out": false,
  "number_of_nodes": 1,
  "number_of_data_nodes": 1,
  "active_primary_shards": 3,
  "active_shards": 3,
  "relocating_shards": 0,
  "initializing_shards": 0,
  "unassigned_shards": 3,
  "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
}

甚至还可以指定索引,查看索引级别的健康情况,更加方便问题定位。

$ curl -X GET "localhost:9200/_cluster/health/test1,test2?pretty"

1.1 API请求参数

  • level

    值可以为cluster、indices、shards,用于返回不同层次的健康状况信息,默认为cluster

  • wait_for_status

    值可以为green、yellow、red,此参数代表等待集群的状态变为指定的状态或者超时返回。

  • wait_for_no_relocating_shards

    true/fasle,代表是否等待不存在relocating的分片时返回,默认false。

  • wait_for_no_initializing_shards

    true/fasle,代表是否等待不存在initializing的分片时返回,默认false。

  • wait_for_active_shards

    数值,代表等待多少个分片活跃时存活,默认为0,all代表等待所有的分片都为活跃的情况下返回。

  • wait_for_nodes

    数值,等待指定个数(记为N)的节点可达后返回,可以为**>=N,<=N,>N,<N**,也可以对应使用ge(N),le(N),gt(N),lt(N)

  • wait_for_events

    给定一个优先级,等待指定优先级的所有队列事件被处理后返回。指定的优先级可为immediate,urgent,high,normal,low,languid

  • timeout

    当使用wait_for_XXX参数时,指定的超时时间,默认为30s

  • master_timeout

    连接master节点的超时时间,如果没有设置,则与timeout保持一致。

  • local

    true/false,true代表从本地节点获取相应的信息,false代表从master节点获取信息,默认为false。

1.2 API响应参数

结果如上所示,下面对返回值的重要字段进行详细解释:

  • cluster_name

    集群的名称,每个集群拥有一个唯一的名称。

  • status

    集群的状态,分为green,yellow,red三种,green代表集群所有的主分片和副本分片都正常运行,yellow代表所有的主分片都正常运行,但是存在未正常分配的副本分片,red代表有主分片未能正常分配。

  • timed_out

    本次查询是否超时,值为true/false。

  • number_of_nodes

    集群拥有的节点数量。

  • number_of_data_nodes

    集群拥有的数据节点的数量。

  • active_primary_shards

    活动的主分片

  • active_shards

    活动的分片

  • relocating_shards

    显示当前正在从一个节点迁往其他节点的分片的数量。通常来说应该是 0,不过在 Elasticsearch 发现集群不太均衡时,该值会上涨。比如说:添加了一个新节点,或者下线了一个节点。

  • initializing_shards

    是刚刚创建的分片的个数。比如,当你刚创建第一个索引,分片都会短暂的处于 initializing 状态。这通常会是一个临时事件,分片不应该长期停留在 initializing 状态。你还可能在节点刚重启的时候看到 initializing 分片:当分片从磁盘上加载后,它们会从 initializing 状态开始。

  • unassigined_shards

    是已经在集群状态中存在的分片,但是实际在集群里又找不着。通常未分配分片的来源是未分配的副本。比如,一个有 5 分片和 1 副本的索引,在单节点集群上,就会有 5 个未分配副本分片。如果你的集群是 red 状态,也会长期保有未分配分片(因为缺少主分片)

  • number_of_pending_tasks

    等待中的任务数量

  • number_of_in_flight_fetch

    进行中的任务数量

  • task_max_waiting_in_queue_millis

    任务在队列中等待的最长时间。

  • active_shards_percent_as_number

    活动的分片占所有分片的比例。

二、查看集群状态

集群状态API提供了一个全面查看集群状态信息的方法,返回的响应内容很多,包括映射、节点、文档等元数据信息,Elasticsearch提供了过滤器,可以根据需要获取相应的内容。

$ curl -X GET "localhost:9200/_cluster/state?pretty"

注意:当集群还在构建过程中,响应的cluster_uuid 字段的值可能为_na_,集群的状态版本可能为-1

默认情况下,集群状态请求被路由到主节点,以确保返回最新的集群状态。出于调试目的,您可以通过向查询字符串添加local=true来检索特定节点的本地集群状态。

$ curl -X GET "localhost:9200/_cluster/state/{metrics}/{indices}?pretty"
  • indices指的是索引,使用逗号分隔

  • metrics的值有以下选择,可以使用多个值,用逗号分隔

    • version

      集群状态版本

    • master_node

      master_node段的信息,关于集群主节点

    • nodes

      nodes段的信息,关于集群中的节点

    • routing_table

      routing_table段的信息

    • metadata

      metadata段的信息,关于配置、映射等元数据信息

    • blocks

      blocks段的信息

    • _all

      包括所有的信息

三、查看集群统计信息

集群统计信息API用于查看集群的各种统计汇总信息,包括CPU、内存、存储、文档统计等信息。

$ curl -X GET "localhost:9200/_cluster/stats?human&pretty"
{
  "_nodes" : {
    "total" : 1,
    "successful" : 1,
    "failed" : 0
  },
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "nhG2-6K6RU2kDreqsUZy6A",
  "timestamp" : 1570199646793,
  "status" : "yellow",
  "indices" : {
    "count" : 1,
    "shards" : {
      "total" : 3,
      "primaries" : 3,
      "replication" : 0.0,
      "index" : {
        "shards" : {
          "min" : 3,
          "max" : 3,
          "avg" : 3.0
        },
        "primaries" : {
          "min" : 3,
          "max" : 3,
          "avg" : 3.0
        },
        "replication" : {
          "min" : 0.0,
          "max" : 0.0,
          "avg" : 0.0
        }
      }
    },
    "docs" : {
      "count" : 0,
      "deleted" : 0
    },
    "store" : {
      "size" : "783b",
      "size_in_bytes" : 783
    },
    "fielddata" : {
      "memory_size" : "0b",
      "memory_size_in_bytes" : 0,
      "evictions" : 0
    },
    "query_cache" : {
      "memory_size" : "0b",
      "memory_size_in_bytes" : 0,
      "total_count" : 0,
      "hit_count" : 0,
      "miss_count" : 0,
      "cache_size" : 0,
      "cache_count" : 0,
      "evictions" : 0
    },
    "completion" : {
      "size" : "0b",
      "size_in_bytes" : 0
    },
    "segments" : {
      "count" : 0,
      "memory" : "0b",
      "memory_in_bytes" : 0,
      "terms_memory" : "0b",
      "terms_memory_in_bytes" : 0,
      "stored_fields_memory" : "0b",
      "stored_fields_memory_in_bytes" : 0,
      "term_vectors_memory" : "0b",
      "term_vectors_memory_in_bytes" : 0,
      "norms_memory" : "0b",
      "norms_memory_in_bytes" : 0,
      "points_memory" : "0b",
      "points_memory_in_bytes" : 0,
      "doc_values_memory" : "0b",
      "doc_values_memory_in_bytes" : 0,
      "index_writer_memory" : "0b",
      "index_writer_memory_in_bytes" : 0,
      "version_map_memory" : "0b",
      "version_map_memory_in_bytes" : 0,
      "fixed_bit_set" : "0b",
      "fixed_bit_set_memory_in_bytes" : 0,
      "max_unsafe_auto_id_timestamp" : -1,
      "file_sizes" : { }
    }
  },
  "nodes" : {
    "count" : {
      "total" : 1,
      "data" : 1,
      "coordinating_only" : 0,
      "master" : 1,
      "ingest" : 1
    },
    "versions" : [
      "6.5.4"
    ],
    "os" : {
      "available_processors" : 4,
      "allocated_processors" : 4,
      "names" : [
        {
          "name" : "Windows 10",
          "count" : 1
        }
      ],
      "mem" : {
        "total" : "7.8gb",
        "total_in_bytes" : 8482287616,
        "free" : "2.5gb",
        "free_in_bytes" : 2716463104,
        "used" : "5.3gb",
        "used_in_bytes" : 5765824512,
        "free_percent" : 32,
        "used_percent" : 68
      }
    },
    "process" : {
      "cpu" : {
        "percent" : 0
      },
      "open_file_descriptors" : {
        "min" : -1,
        "max" : -1,
        "avg" : 0
      }
    },
    "jvm" : {
      "max_uptime" : "3.4d",
      "max_uptime_in_millis" : 294220612,
      "versions" : [
        {
          "version" : "1.8.0_221",
          "vm_name" : "Java HotSpot(TM) 64-Bit Server VM",
          "vm_version" : "25.221-b11",
          "vm_vendor" : "Oracle Corporation",
          "count" : 1
        }
      ],
      "mem" : {
        "heap_used" : "238.8mb",
        "heap_used_in_bytes" : 250430752,
        "heap_max" : "1.9gb",
        "heap_max_in_bytes" : 2112618496
      },
      "threads" : 46
    },
    "fs" : {
      "total" : "88.1gb",
      "total_in_bytes" : 94684311552,
      "free" : "18.4gb",
      "free_in_bytes" : 19831750656,
      "available" : "18.4gb",
      "available_in_bytes" : 19831750656
    },
    "plugins" : [ ],
    "network_types" : {
      "transport_types" : {
        "netty4" : 1
      },
      "http_types" : {
        "netty4" : 1
      }
    }
  }
}

还可以使用Node Filter过滤器进行内容过滤。关于Node Filter的内容请参考后面章节。

四、等待集群任务

此API用于返回集群层面的已经提交但是未被执行的变更,例如创建索引、更新映射、分配分片等,一般情况下应该是空的。

$ curl -X GET "localhost:9200/_cluster/pending_tasks?pretty"

五、集群重新路由

reroute命令允许手动更改集群中各个碎片的分配。例如,可以显式地将碎片从一个节点移动到另一个节点,可以取消分配,可以显式地将未分配的碎片分配给特定的节点。

curl -X POST "localhost:9200/_cluster/reroute?pretty" -H 'Content-Type: application/json' -d'
{
    "commands" : [
        {
            "move" : {
                "index" : "test", "shard" : 0,
                "from_node" : "node1", "to_node" : "node2"
            }
        },
        {
          "allocate_replica" : {
                "index" : "test", "shard" : 1,
                "node" : "node3"
          }
        }
    ]
}
'
  • 当routing.rebalance.enable设置为true,那么reroute之后,Elasticsearch会重新进行集群的平衡。
  • 当cluster.routing.allocation设置为false,集群将禁用分片分配,只能使用reroute进行手工分配。
command参数有:
  • move

    用于将一个活动的分片从一个节点移动到另外一个节点。支持的请求体参数有:

    • index

      索引的名称

    • shard

      分片的编号

    • from_node

      源节点

    • to_node

      目标节点

  • cancel

    取消一个分片的分配,默认只能取消副本分片的分片,如果想要取消主分片的分配,需要指定allow_primary参数为true。支持的请求体参数有:

    • index

      索引的名称

    • shard

      分片的编号

    • allow_primary

      true/false,代表是否允许取消主分片的分配。

  • allocate_replica

    分配一个未被分配的分片到某一个节点。支持的参数如下:

    • index

      索引的名称

    • shard

      分片的编号

    • node

      节点的名称

六、更新集群配置

6.1 获取集群配置

$ curl -X GET "localhost:9200/_cluster/settings?pretty"

6.2 更新集群配置

集群配置的更新分为持久化的、临时的更新,持久化的配置更新后集群重启仍然生效,临时的更新在集群重启后不生效。

更新持久化配置

$ curl -X PUT "localhost:9200/_cluster/settings?pretty" -H 'Content-Type: application/json' -d'
{
    "persistent" : {
        "indices.recovery.max_bytes_per_sec" : "50mb"
    }
}
'

更新临时配置

$ curl -X PUT "localhost:9200/_cluster/settings?flat_settings=true&pretty" -H 'Content-Type: application/json' -d'
{
    "transient" : {
        "indices.recovery.max_bytes_per_sec" : "20mb"
    }
}
'

6.3 重置集群配置

重置集群配置的方式只需要将配置项设置为null

$ curl -X PUT "localhost:9200/_cluster/settings?pretty" -H 'Content-Type: application/json' -d'
{
    "transient" : {
        "indices.recovery.max_bytes_per_sec" : null
    }
}
'

6.4 使用通配符重置集群配置

$ curl -X PUT "localhost:9200/_cluster/settings?pretty" -H 'Content-Type: application/json' -d'
{
    "transient" : {
        "indices.recovery.*" : null
    }
}
'

6.5 集群配置顺序

当同时使用配置文件、配置API进行配置更新时,有效性遵循以下顺序:

  1. 临时集群配置
  2. 持久化集群配置
  3. elasticsearch.yml配置文件配置

一般建议本地配置放到elasticsearch.yml配置文件,集群相关的配置使用settings API。

七、获取集群配置

7.1 获取显式设置的配置

$ curl -X GET "localhost:9200/_cluster/settings?pretty"

7.2 获取默认的配置

$ curl -X GET "localhost:9200/_cluster/settings?include_defaults=true&pretty"

猜你喜欢

转载自blog.csdn.net/projim_tao/article/details/102644646