Cluster health API
集群運行狀況API返回關于集群運行狀況的簡單狀態(tài)。您還可以使用API僅獲取指定數(shù)據(jù)流和索引的健康狀態(tài)。對于數(shù)據(jù)流,API 檢索流的支持索引的運行狀況。
集群健康狀態(tài)為:綠色、黃色和紅色。在shard級別,紅色狀態(tài)表示集群中沒有分配特定的shard,黃色狀態(tài)表示主shard已分配,但副本未分配,綠色狀態(tài)表示所有shard都已分配。索引級別狀態(tài)由最差分片狀態(tài)控制。集群狀態(tài)由最差索引狀態(tài)控制。
GET /_cluster/health/<target>
<target>: (可選,string)以逗號分隔的數(shù)據(jù)流、索引和索引別名列表,用于限制請求。支持通配符表達式()。如果要針對集群中的所有數(shù)據(jù)流和索引,可以省略該參數(shù)或使用_all或。
- 查詢參數(shù) (舉例)
level : (可選,字符串)可以是cluster、indices或 之一shards??刂品祷氐慕】敌畔⒌脑敿毤墑e。默認為cluster。
timeout: (可選,時間單位)等待響應的時間。如果超時之前未收到響應,則請求失敗并返回錯誤。默認為30s。
wait_for_status:(可選,字符串)綠色、黃色或紅色中的一種。將等待(直到提供的超時),直到集群狀態(tài)變?yōu)樘峁┑臓顟B(tài)或更好,即綠色>黃色>紅色。缺省情況下,不等待任何狀態(tài)。
該API的主要優(yōu)點之一是能夠等待,直到集群達到某個較高的健康水平。例如,下面的代碼將等待60秒使集群達到綠色狀態(tài)(如果在60秒之前達到綠色狀態(tài),它將在該點返回):
# 當前狀態(tài)為黃色
root@ubuntu-x64_02:/opt# curl -X GET "http://192.168.88.12:9201/_cluster/health?pretty"
{
"cluster_name" : "my-cluster",
"status" : "yellow", # <----- 狀態(tài)為 yellow
"timed_out" : false,
"number_of_nodes" : 2,
"number_of_data_nodes" : 2,
"active_primary_shards" : 19,
"active_shards" : 38,
"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" : 92.6829268292683
}
# 默認 level 為cluster, 上面查詢等同于
root@ubuntu-x64_02:/opt# curl -X GET "http://192.168.88.12:9201/_cluster/health/?level=cluster&pretty"
# 等待60秒后返回
root@ubuntu-x64_02:/opt# time curl -X GET "http://192.168.88.12:9201/_cluster/health?wait_for_status=green&timeout=60s&pretty"
{
"cluster_name" : "my-cluster",
"status" : "yellow",
"timed_out" : true,
"number_of_nodes" : 2,
"number_of_data_nodes" : 2,
"active_primary_shards" : 19,
"active_shards" : 38,
"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" : 92.6829268292683
}
real 1m0.014s
user 0m0.000s
sys 0m0.012s
# 如果在60秒之前達到綠色狀態(tài),它將在該點返回(如:30.120s )
root@ubuntu-x64_02:/opt# time curl -X GET "http://192.168.88.12:9201/_cluster/health?wait_for_status=green&timeout=60s&pretty"
{
"cluster_name" : "my-cluster",
"status" : "green",
"timed_out" : false,
"number_of_nodes" : 3,
"number_of_data_nodes" : 3,
"active_primary_shards" : 19,
"active_shards" : 41,
"relocating_shards" : 0,
"initializing_shards" : 0,
"unassigned_shards" : 0,
"delayed_unassigned_shards" : 0,
"number_of_pending_tasks" : 1,
"number_of_in_flight_fetch" : 0,
"task_max_waiting_in_queue_millis" : 0,
"active_shards_percent_as_number" : 100.0
}
real 0m30.120s
user 0m0.000s
sys 0m0.004s
以下是獲取 shards 級別 集群健康狀況的示例 :
root@ubuntu-x64_02:/opt# curl -X GET "http://192.168.88.12:9201/_cluster/health/_all?level=shards&pretty"
{
"cluster_name" : "my-cluster",
"status" : "green",
"timed_out" : false,
"number_of_nodes" : 3,
"number_of_data_nodes" : 3,
"active_primary_shards" : 19,
"active_shards" : 41,
"relocating_shards" : 0,
"initializing_shards" : 0,
"unassigned_shards" : 0,
"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" : 100.0,
"indices" : {
".ds-ilm-history-5-2023.05.04-000004" : {
"status" : "green",
"number_of_shards" : 1,
"number_of_replicas" : 1,
"active_primary_shards" : 1,
"active_shards" : 2,
"relocating_shards" : 0,
"initializing_shards" : 0,
"unassigned_shards" : 0,
"shards" : {
"0" : {
"status" : "green",
"primary_active" : true,
"active_shards" : 2,
"relocating_shards" : 0,
"initializing_shards" : 0,
"unassigned_shards" : 0
}
}
},
..........
Cluster state API
返回有關集群狀態(tài)的元數(shù)據(jù)。
GET /_cluster/state/<metrics>/<target>
- 如果啟用了Elasticsearch安全功能,您必須具有monitor或 manage 集群權(quán)限才能使用此API。
- 集群狀態(tài) API 允許訪問代表整個集群狀態(tài)的元數(shù)據(jù)。這包括以下信息:
- 集群中的節(jié)點集
- 所有集群級別設置
- 有關集群中索引的信息,包括它們的映射和設置
- 集群中所有分片的位置。
路徑參數(shù)
集群狀態(tài)包含關于集群中所有索引的信息,包括它們的映射,以及模板和其他元數(shù)據(jù)。這意味著它有時會非常大。為了避免需要處理所有這些信息,你可以只請求你需要的部分集群狀態(tài):
<metrics>: (可選,字符串)以下選項的逗號分隔列表:
- _all: 顯示所有指標。
- blocks:顯示blocks響應的部分。
- metadata: 顯示metadata響應的部分。如果您提供逗號分隔的索引列表,則返回的輸出將僅包含這些索引的元數(shù)據(jù)。
- routing_table:顯示routing_table響應的部分。如果您提供逗號分隔的索引列表,則返回的輸出將僅包含這些索引的路由表。
<target>:(可選,string)以逗號分隔的數(shù)據(jù)流、索引和別名列表,用于限制請求。支持通配符()。要針對所有數(shù)據(jù)流和索引,省略該參數(shù)或使用或_all。
下面的例子只返回 my-index-000001 的數(shù)據(jù)流或索引的 metadata 和 routing_table 數(shù)據(jù):
root@ubuntu-x64_02:/opt# curl -X GET "http://192.168.88.12:9201/_cluster/state/metadata,routing_table/my-index-000001?pretty"
{
"cluster_name" : "my-cluster",
"cluster_uuid" : "hhRd0sm5SXqDi3oceLw3oQ",
"metadata" : {
......
"cluster_coordination" : {
......
},
"templates" : { },
"indices" : {
"my-index-000001" : {
"version" : 17,
......
"routing_num_shards" : 1024,
"state" : "open",
"settings" : {
"index" : {
......
}
},
"mappings" : {
"_doc" : {
"properties" : {
"employee-id" : {
"index" : false,
"type" : "keyword"
},
......
}
}
},
......
}
},
"index-graveyard" : {
"tombstones" : [ ]
}
},
"routing_table" : {
"indices" : {
"my-index-000001" : {
"shards" : {
"0" : [
{
"state" : "STARTED",
"primary" : true,
"node" : "vPD3eY-FSP6PcX1xY3SEyg",
"relocating_node" : null,
"shard" : 0,
"index" : "my-index-000001",
"allocation_id" : {
"id" : "qOrfDS_aQCqf4pxR8xP3pA"
}
},
{
"state" : "STARTED",
"primary" : false,
"node" : "QPS7tRDBRnCbpv0Dfx_0Lg",
"relocating_node" : null,
"shard" : 0,
"index" : "my-index-000001",
"allocation_id" : {
"id" : "lEzlrVv7SNKvqZasj_hRcA"
}
}
]
}
}
}
}
}
此示例僅返回 blocks 元數(shù)據(jù):
root@ubuntu-x64_02:/opt# curl -X GET "http://192.168.88.12:9201/_cluster/state/blocks/my-index-000001?pretty"
{
"cluster_name" : "my-cluster",
"cluster_uuid" : "hhRd0sm5SXqDi3oceLw3oQ",
"blocks" : { }
}
Cluster allocation explain API
提供對分片當前分配的解釋。
GET _cluster/allocation/explain
POST _cluster/allocation/explain
- 如果啟用了Elasticsearch安全功能,您必須具有monitor或 manage 集群權(quán)限才能使用此API。
集群分配解釋API的目的是為集群中的分片分配提供解釋。
- 對于未分配的分片,explain API提供了對分片未分配原因的解釋。
- 對于已分配的分片,explain API提供了一個解釋,說明為什么分片保留在當前節(jié)點上,而沒有移動或重新平衡到另一個節(jié)點。
當試圖診斷為什么碎片未分配或為什么碎片繼續(xù)保留在當前節(jié)點上時,這個API非常有用。
未分配的主分片, 下面的請求獲取未分配主分片的分配解釋。
# 創(chuàng)建索引 my-index-000002, 并讓其分配到 "name" : "nonexistent_node" 的節(jié)點上
root@ubuntu-x64_02:/opt# curl -X PUT "http://192.168.88.12:9201/my-index-000002/?pretty" -H 'Content-Type: application/json' -d'
> {
> "settings" : {
> "index" : {
> "routing" : {
> "allocation" : {
> "include" : {
> "name" : "nonexistent_node"
> }
> }
> },
> "number_of_shards" : "1",
> "number_of_replicas" : "1"
> }
> }
> }
> '
{
"acknowledged" : true,
"shards_acknowledged" : false,
"index" : "my-index-000002"
}
查看解釋索引 my-index-000002 未分配主分片的原因: 無法分配,因為任何節(jié)點都不允許分配
root@ubuntu-x64_02:/opt# curl -X GET "http://192.168.88.12:9201/_cluster/allocation/explain?pretty" -H 'Content-Type: application/json' -d'
> {
> "index": "my-index-000002",
> "shard": 0,
> "primary": true
> }
> '
{
"index" : "my-index-000002",
"shard" : 0,
"primary" : true,
"current_state" : "unassigned", # <------ 分片的當前狀態(tài)。
"unassigned_info" : {
"reason" : "INDEX_CREATED", # <------------ 分片最初變得未分配的原因。
"at" : "2023-12-26T05:54:44.326Z",
"last_allocation_status" : "no"
},
"can_allocate" : "no", # <---------------- 是否分配分片
"allocate_explanation" : "cannot allocate because allocation is not permitted to any of the nodes",
"node_allocation_decisions" : [
{
"node_id" : "QPS7tRDBRnCbpv0Dfx_0Lg",
"node_name" : "node-3",
"transport_address" : "192.168.88.12:9303",
"node_attributes" : {
"ml.machine_memory" : "6248390656",
"ml.max_open_jobs" : "512",
"xpack.installed" : "true",
"ml.max_jvm_size" : "1073741824",
"transform.node" : "true"
},
"node_decision" : "no", # <------------ 是否將分片分配給特定節(jié)點。
"weight_ranking" : 1,
"deciders" : [
{
"decider" : "filter", # <------------ 導致no節(jié)點決策的決策者。
"decision" : "NO",
# 解釋決策者返回決策的原因no,并提供有用的提示,指出導致決策的設置。
"explanation" : "node does not match index setting [index.routing.allocation.include] filters [name:\"nonexistent_node\"]"
}
]
},
{
"node_id" : "vPD3eY-FSP6PcX1xY3SEyg",
"node_name" : "node-1",
"transport_address" : "192.168.88.12:9301",
"node_attributes" : {
"ml.machine_memory" : "6248390656",
"ml.max_open_jobs" : "512",
"xpack.installed" : "true",
"ml.max_jvm_size" : "1073741824",
"transform.node" : "true"
},
"node_decision" : "no",
"weight_ranking" : 2,
"deciders" : [
{
"decider" : "filter",
"decision" : "NO",
"explanation" : "node does not match index setting [index.routing.allocation.include] filters [name:\"nonexistent_node\"]"
}
]
},
{
"node_id" : "32zPm9fvTPGxPMvNSsCZrw",
"node_name" : "node-2",
"transport_address" : "192.168.88.12:9302",
"node_attributes" : {
"ml.machine_memory" : "6248390656",
"ml.max_open_jobs" : "512",
"xpack.installed" : "true",
"ml.max_jvm_size" : "1073741824",
"transform.node" : "true"
},
"node_decision" : "no",
"weight_ranking" : 3,
"deciders" : [
{
"decider" : "filter",
"decision" : "NO",
"explanation" : "node does not match index setting [index.routing.allocation.include] filters [name:\"nonexistent_node\"]"
}
]
}
]
}
修改 name 為 節(jié)點的 已存在的,則 主分片 可以分配成功:文章來源:http://www.zghlxwxcb.cn/news/detail-781738.html
root@ubuntu-x64_02:/opt# curl -X PUT "http://192.168.88.12:9201/my-index-000002/_settings" -H 'Content-Type: application/json' -d'
> {
> "index" : {
> "routing" : {
> "allocation" : {
> "include" : {
> "name" : "node-1"
> }
> }
> }
> }
> }
> '
{"acknowledged":true}
# 此時狀態(tài)為 yellow , 黃色狀態(tài)表示主shard已分配,但副本未分配
root@ubuntu-x64_02:/opt# curl -X GET "http://192.168.88.12:9201/_cluster/health/my-index-000002?level=shards&pretty"
{
"cluster_name" : "my-cluster",
"status" : "yellow",
"timed_out" : false,
"number_of_nodes" : 3,
"number_of_data_nodes" : 3,
"active_primary_shards" : 1,
"active_shards" : 1,
"relocating_shards" : 0,
"initializing_shards" : 0,
"unassigned_shards" : 1,
"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" : 97.67441860465115,
"indices" : {
"my-index-000002" : {
"status" : "yellow",
"number_of_shards" : 1,
"number_of_replicas" : 1,
"active_primary_shards" : 1,
"active_shards" : 1,
"relocating_shards" : 0,
"initializing_shards" : 0,
"unassigned_shards" : 1,
"shards" : {
"0" : {
"status" : "yellow",
"primary_active" : true,
"active_shards" : 1,
"relocating_shards" : 0,
"initializing_shards" : 0,
"unassigned_shards" : 1
}
}
}
}
}
此時,集群狀態(tài)為黃色, 集群狀態(tài)由最差索引狀態(tài)控制。文章來源地址http://www.zghlxwxcb.cn/news/detail-781738.html
root@ubuntu-x64_02:/opt# curl -X GET "http://192.168.88.12:9201/_cluster/health?pretty"
{
"cluster_name" : "my-cluster",
"status" : "yellow", # <----------- 集群狀態(tài)由最差索引狀態(tài)控制
"timed_out" : false,
"number_of_nodes" : 3,
"number_of_data_nodes" : 3,
"active_primary_shards" : 20,
"active_shards" : 42,
"relocating_shards" : 0,
"initializing_shards" : 0,
"unassigned_shards" : 1,
"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" : 97.67441860465115
}
到了這里,關于Elasticsearch 常用 REST API 之集群APIs的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!