elasticsearch. guide

集群健康状态 API

可以通过这个 API 获取非常简单的集群健康状态信息.

$ curl -XGET 'http://localhost:9200/_cluster/health?pretty=true'
{                                                                                            
  "cluster_name" : "testcluster",                                                              
  "status" : "green",                                                                        
  "timed_out" : false,                                                                       
  "number_of_nodes" : 2,                                                                     
  "number_of_data_nodes" : 2,                                                                
  "active_primary_shards" : 5,                                                               
  "active_shards" : 10,                                                                      
  "relocating_shards" : 0,                                                                   
  "initializing_shards" : 0,                                                                 
  "unassigned_shards" : 0                                                                    
}

也可以对一个或多个索引执行这个 API 来获取其健康状态 :

$ curl -XGET 'http://localhost:9200/_cluster/health/test1,test2'

集群的健康状态包括: green, yellowred. 在区块(shard)层, 状态 red 表明该区块还未分配, yellow 表明主区块已经分配,但复制区块还未分配, green 表明所有区块都分配了. 索引层的状态是由最差的区块状态决定的. 集群的状态由最差的索引状态决定.

这个API提供了等待集群状态达到指定健康水平的能力,这非常有用。比如下面的命令会等到集群状态持续 green 50秒后(如果在命令发起之前就已经是 green 状态了,他会立即返回):

$ curl -XGET 'http://localhost:9200/_cluster/health?wait_for_status=green&timeout=50s'

请求参数

API 请求接受如下参数:

参数名 说明
level 可选值为 cluster, indicesshards. 指定返回的健康状态的详细程度. 默认为 cluster.
wait_for_status green, yellowred 之一. 在集群健康状态变为指定状态 (直到超出 timeout 时限) 之前一直等待. 默认不等待任何状态.
wait_for_relocating_shards 设定等待的区块迁移数量. 通常为 0 ,表明等待所有区块迁移完成. 默认不等待.
wait_for_nodes 请求等到可访问节点数达到 N. 该参数的取值还可以是 >=N, <=N, >N<N. 或者, 也可以用 ge(N), le(N), gt(N)lt(N).
timeout 配合 wait_for_XXX 参数指定等待时长. 默认 30s.

下面的例子获取 shards 水平的健康状态:

$ curl -XGET 'http://localhost:9200/_cluster/health/twitter?level=shards'
 
Fork me on GitHub