Elasticsearch 常用运维命令大全

Elasticsearch 常用运维命令大全

概述

Elasticsearch 的日常运维离不开各种 API 调用。本文整理了 ES 集群管理中最高频使用的命令,涵盖集群健康、索引管理、分片迁移、磁盘水位和熔断器配置五大方面,适合作为速查手册收藏。

示例使用 curl + 基础认证方式。请将 your_password 替换为实际密码。

一、集群健康状态

查看集群健康

bash 复制代码
curl -u elastic:your_password http://localhost:9200/_cluster/health?pretty=true

返回的关键字段:

  • status:green(正常)/ yellow(副本未分配)/ red(主分片未分配)
  • number_of_nodes / number_of_data_nodes:节点数
  • active_shards / unassigned_shards:分片状态

查看存储状态

bash 复制代码
curl -u elastic:your_password http://localhost:9200/_cat/allocation?v

查看索引列表

复制代码
GET _cat/indices?v

查看节点列表

bash 复制代码
curl -u elastic:your_password http://localhost:9200/_cat/nodes?v

查看 Master 节点

复制代码
GET _cat/master

查看未分配分片的原因

复制代码
GET _cluster/allocation/explain

未分配分片的常见原因:

原因 含义
INDEX_CREATED 索引创建后尚未分配
NODE_LEFT 节点离开集群,其上分片未分配
ALLOCATION_FAILED 分片分配失败
REPLICA_ADDED 副本刚添加,尚未分配
CLUSTER_RECOVERED 集群恢复后重新分配中
NEW_INDEX_RESTORED 从快照恢复的新索引
REALLOCATED_REPLICA 找到更优副本位置,旧副本被取消

重新尝试分配失败的分片

复制代码
POST _cluster/reroute?retry_failed=true

查看等待中的任务

复制代码
GET _cat/pending_tasks

查看正在搬迁的索引

复制代码
GET _cat/recovery?v&h=index,shard,time,type,stage,source_node,target_node,files_percent,bytes_percent,translog_ops_percent&s=type:desc,stage,shard&active_only

可以实时观察分片搬迁的进度和速度。

二、索引管理

查看索引配置

复制代码
GET index_name/_settings?flat_settings

加上 include_defaults 可查看包含默认值的完整配置:

复制代码
GET index_name/_settings?flat_settings&include_defaults

查看索引 Mapping

复制代码
GET index_name/_mapping

查看索引模板

复制代码
GET _template/<template-name>

查看索引分片分布

复制代码
GET _cat/shards/<index-name>

查看索引 Checkpoint(用于排查数据同步问题)

复制代码
GET /index_name/_stats?level=shards

Reindex(重建索引)

当需要修改 Mapping 或调整分片数时,通过 Reindex 将旧索引数据复制到新索引:

json 复制代码
POST _reindex
{
  "source": { "index": "old_index" },
  "dest": { "index": "new_index" }
}

三、分片搬迁与故障处理

手动迁移分片

指定将某个分片从节点 A 搬迁到节点 B:

json 复制代码
POST _cluster/reroute
{
  "commands": [{
    "move": {
      "index": "index_name",
      "shard": 1,
      "from_node": "node-A",
      "to_node": "node-B"
    }
  }]
}

节点名可通过 GET _cat/nodes?v 获取。

取消搬迁

json 复制代码
POST _cluster/reroute
{
  "commands": [{
    "cancel": {
      "index": "index_name",
      "shard": 0,
      "node": "target-node",
      "allow_primary": true
    }
  }]
}

强制分配空主分片(丢弃数据)

当主分片数据丢失且无法恢复时,强制分配空主分片(需要 accept_data_loss: true,谨慎使用):

json 复制代码
POST /_cluster/reroute
{
  "commands": [{
    "allocate_empty_primary": {
      "index": "index_name",
      "shard": 1,
      "node": "surviving-node",
      "accept_data_loss": true
    }
  }]
}

修改索引分配延迟

节点离开集群后,ES 默认等待 1 分钟再重新分配其分片。这段时间内如果节点恢复,分片无需重新同步。可在索引级别修改等待时间:

json 复制代码
PUT /_all/_settings
{
  "settings": {
    "index.unassigned.node_left.delayed_timeout": "5m"
  }
}

设为 0 则立即分配,不等待。

按 IP 排除节点

集群级别排除指定节点(被排除的节点上的分片会自动迁移):

bash 复制代码
curl -s -u elastic:your_password -X PUT -H 'Content-Type: application/json' \
  localhost:9200/_cluster/settings \
  -d '{"transient":{"cluster.routing.allocation.exclude._ip":"10.0.1.10,10.0.1.11"}}'

也可以按索引级别排除:

json 复制代码
PUT index_name/_settings
{
  "index.routing.allocation.exclude._name": "node-A"
}

四、集群配置与均衡

查看集群参数

复制代码
GET /_cluster/settings

分片分配开关

cluster.routing.allocation.enable 控制分片分配行为:

含义
all 允许所有分片分配(默认)
primaries 仅允许主分片分配
new_primaries 仅允许新索引的主分片分配
none 禁止所有分配

使用场景:重启节点前设为 none,避免无谓的分片搬迁;重启完成后恢复为 all

分片均衡开关

cluster.routing.rebalance.enable

含义
all 允许所有分片均衡
primaries 仅平衡主分片
replicas 仅平衡副本分片
none 不进行均衡

并发恢复控制

bash 复制代码
# 修改节点恢复并发数(默认 2)
curl -s -u elastic:your_password -X PUT -H 'Content-Type: application/json' \
  localhost:9200/_cluster/settings \
  -d '{"transient":{"cluster.routing.allocation.node_concurrent_recoveries": "5"}}'

关键并发参数:

  • node_concurrent_recoveries:节点级并发恢复数(默认 2)
  • node_concurrent_incoming_recoveries:传入恢复并发数(默认 2)
  • node_concurrent_outgoing_recoveries:传出恢复并发数(默认 2)
  • cluster_concurrent_rebalance:集群级并发均衡数(默认 2)

节点线程池查看

复制代码
GET _cat/thread_pool/write?v&h=node_name,size

日志级别动态调整

json 复制代码
PUT _cluster/settings
{
  "transient": { "logger._root": "DEBUG" }
}

针对特定模块:

json 复制代码
PUT _cluster/settings
{
  "transient": {
    "logger.org.elasticsearch.action.bulk": "DEBUG"
  }
}

五、磁盘水位线配置

ES 有三道磁盘水位线来保护集群:

参数 默认值 行为
cluster.routing.allocation.disk.watermark.low 85% 达到后不再向该节点分配新副本
cluster.routing.allocation.disk.watermark.high 90% 达到后将该节点分片迁移到其他节点
cluster.routing.allocation.disk.watermark.flood_stage 95% 达到后对该节点上的索引设只读锁

水位线可以是百分比或绝对值(如 500mb),但所有值必须统一用百分比或统一用绝对值,不可混用。

bash 复制代码
# 示例:调整低水位为 80%
curl -s -u elastic:your_password -X PUT -H 'Content-Type: application/json' \
  localhost:9200/_cluster/settings \
  -d '{"transient":{"cluster.routing.allocation.disk.watermark.low":"80%"}}'

相关参数:

  • cluster.routing.allocation.disk.threshold_enabled:是否启用磁盘决策器(默认 true)
  • cluster.info.update.interval:磁盘使用率检查间隔(默认 30s)
  • cluster.routing.allocation.disk.include_relocations:计算磁盘使用率时是否包含正在搬迁的分片(默认 true)

六、熔断器(Circuit Breaker)

熔断器防止 ES 因内存不足而 OOM。

父级熔断器

  • indices.breaker.total.limit:总内存限制,默认为 JVM Heap 的 95%(按实际内存算)或 70%(按子熔断器预留量算)
  • indices.breaker.total.use_real_memory:是否按实际内存计算(默认 true)

单索引分片数限制

json 复制代码
PUT index_name/_settings
{
  "index.routing.allocation.total_shards_per_node": 2
}

此参数帮助索引分片在各节点均匀分布。

总结

ES 运维核心就三件事:看状态 (health/allocation)、管分片 (reroute/move)、调参数(settings/watermark)。本文覆盖的命令基本能满足 90% 的日常运维需求。

几个关键习惯:

  • 节点维护前先关分片分配(enable: none),完事再开
  • 多关注磁盘水位,flood_stage 触发后索引变只读,影响业务
  • Reindex 是修改 Mapping / 调整分片最安全的方式
  • _cat 系列 API 比 _cluster 系列输出更简洁,适合命令行快速查看
相关推荐
Acrellea1 小时前
固态变压器(SST)热管理破局:安科瑞交直流专属测温方案护航无人值守运维
运维·网络
寒冰碧海1 小时前
大模型部署从0到1(一):通用环境全流程准备|NVIDIA驱动+Docker+NVIDIA Container Toolkit
运维·docker·容器
风曦Kisaki2 小时前
# Linux 系统资源查看命令总结(附命令快查表)
linux·运维·服务器
Elasticsearch2 小时前
Elastic: 将来一年中 AI 从活动转向成果
elasticsearch
蝶恋舞者2 小时前
DNS 服务器(后续持续更新)
运维·服务器
Mortalbreeze3 小时前
深入理解 Linux 线程机制(四):线程同步——条件变量与信号量
linux·运维·服务器·开发语言·c++
Chockmans4 小时前
春秋云境CVE-2023-51385(保姆级教学)
大数据·web安全·elasticsearch·搜索引擎·网络安全·春秋云境·cve-2023-51385
汤愈韬4 小时前
KYCA运维认证
运维·服务器·网络
ZENERGY-众壹4 小时前
50MW分布式VPP接入实战:调度中心要求的1分钟频率如何不卡死API
运维·服务器·分布式·光伏运维
运维大师4 小时前
【Linux运维极简教程】10-日志管理与故障排查
linux·运维·服务器