ElasticSearch 实战: ES 集群管理 API

Elasticsearch 集群管理 API 旨在帮助用户监控集群状态、调整配置、维护节点健康以及进行其他与集群层面相关的操作。以下是实战中常用的 Elasticsearch 集群管理 API 及其示例:

集群健康状态检查

获取集群当前的健康状况(green、yellow、red),以及节点、分片等基本信息。

请求格式:

GET /_cluster/health

可选参数:

  • level: 指定返回的详细程度,可选值有 cluster(默认)、indicesshards
  • wait_for_status: 等待集群达到指定状态后返回,可选值为 greenyellowrednone(默认)。
  • timeout: 设置等待超时时间,如 30s1m

示例:

GET /_cluster/health?wait_for_status=green&timeout=30s

集群统计信息

获取集群级别的统计信息,包括节点、索引、分片、内存、CPU 使用情况等。

请求格式:

GET /_cluster/stats

示例:

GET /_cluster/stats

节点列表

列出集群中的所有节点及其详细信息。

请求格式:

GET /_nodes

可选参数:

  • metric: 指定返回哪些度量指标,如 jvmfsosnetworkthread_pooltransporthttpbreakeringestprocessindicesadaptive_selectiontransformscript
  • node_id: 查询特定节点的信息。

示例:

GET /_nodes/stats?metric=jvm,os,process

节点信息

获取指定节点的详细信息。

请求格式:

GET /_nodes/<node_id>

示例:

GET /_nodes/node-1

集群状态

获取集群整体状态,包括节点、分片分配、路由表等详细信息。

请求格式:

GET /_cluster/state

可选参数:

  • metric: 指定返回哪些度量指标,如 blocksmetadatanodesrouting_tablerouting_nodesmaster_nodeversion
  • 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 和参数。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值