Elasticsearch 集群管理 API 旨在帮助用户监控集群状态、调整配置、维护节点健康以及进行其他与集群层面相关的操作。以下是实战中常用的 Elasticsearch 集群管理 API 及其示例:
集群健康状态检查
获取集群当前的健康状况(green、yellow、red),以及节点、分片等基本信息。
请求格式:
GET /_cluster/health
可选参数:
level
: 指定返回的详细程度,可选值有cluster
(默认)、indices
、shards
。wait_for_status
: 等待集群达到指定状态后返回,可选值为green
、yellow
、red
或none
(默认)。timeout
: 设置等待超时时间,如30s
、1m
。
示例:
GET /_cluster/health?wait_for_status=green&timeout=30s
集群统计信息
获取集群级别的统计信息,包括节点、索引、分片、内存、CPU 使用情况等。
请求格式:
GET /_cluster/stats
示例:
GET /_cluster/stats
节点列表
列出集群中的所有节点及其详细信息。
请求格式:
GET /_nodes
可选参数:
metric
: 指定返回哪些度量指标,如jvm
、fs
、os
、network
、thread_pool
、transport
、http
、breaker
、ingest
、process
、indices
、adaptive_selection
、transform
、script
。node_id
: 查询特定节点的信息。
示例:
GET /_nodes/stats?metric=jvm,os,process
节点信息
获取指定节点的详细信息。
请求格式:
GET /_nodes/<node_id>
示例:
GET /_nodes/node-1
集群状态
获取集群整体状态,包括节点、分片分配、路由表等详细信息。
请求格式:
GET /_cluster/state
可选参数:
metric
: 指定返回哪些度量指标,如blocks
、metadata
、nodes
、routing_table
、routing_nodes
、master_node
、version
。index
: 指定检查特定索引的状态。
示例:
GET /_cluster/state/metadata,routing_table?pretty
节点关闭
安全地关闭指定节点,使其停止接收请求并开始清理资源。
请求格式:
POST /_cluster/nodes/<node_id>/_shutdown
示例:
POST /_cluster/nodes/node-1/_shutdown
更新集群设置
修改集群范围内的全局配置。
请求格式:
PUT /_cluster/settings
{
"<setting_key>": "<new_value>",
...其它设置项...
}
示例:更改集群最大索引数量限制:
PUT /_cluster/settings
{
"cluster.max_indices_per_shard": ¼
}
集群快照
创建、恢复、监控集群快照,用于备份和灾难恢复。
创建快照:
PUT /_snapshot/<repository>/<snapshot_name>?wait_for_completion=true
{
"indices": "<comma-separated_index_list>",
"ignore_unavailable": true,
"include_global_state": false
}
恢复快照:
POST /_snapshot/<repository>/<snapshot_name>/_restore
{
"indices": "<comma-separated_index_list>",
"ignore_unavailable": true,
"include_aliases": false,
"include_global_state": false
}
使用 Cat API 监控集群
Cat API 提供了一系列简洁、易于阅读的命令,用于快速监控集群状态。例如:
-
节点信息:
GET /_cat/nodes?v
-
索引状态:
GET /_cat/indices?v
-
分片分配:
GET /_cat/shards?v
这些 API 调用返回的结果通常以表格形式展示,便于在终端中快速浏览。
使用 Java Client 管理集群
Java 开发者可以使用官方提供的 Java High Level REST Client 或 Java Low Level REST Client 来执行上述集群管理操作。以下是一个使用 High Level REST Client 获取集群健康状态的示例:
import org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest;
import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
public class ClusterHealthExample {
public static void main(String[] args) throws Exception {
RestHighLevelClient client = // 初始化客户端实例...
ClusterHealthRequest request = new ClusterHealthRequest();
request.waitForStatus(ClusterHealthStatus.GREEN);
request.timeout("30s");
ClusterHealthResponse response = client.cluster().health(request, RequestOptions.DEFAULT);
System.out.println("Cluster status: " + response.getStatus().name());
System.out.println("Active primary shards: " + response.getActivePrimaryShards());
// ...其他响应字段的处理...
}
}
通过上述实战指南,您可以了解如何使用 Elasticsearch 集群管理 API 进行日常监控、故障排查、配置调整等工作。实际使用时,请结合具体的 Elasticsearch 版本和业务需求来选择合适的 API 和参数。