Elasticsearch 常用运维命令大全
概述
Elasticsearch 的日常运维离不开各种 API 调用。本文整理了 ES 集群管理中最高频使用的命令,涵盖集群健康、索引管理、分片迁移、磁盘水位和熔断器配置五大方面,适合作为速查手册收藏。
示例使用
curl+ 基础认证方式。请将your_password替换为实际密码。
一、集群健康状态
查看集群健康
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:分片状态
查看存储状态
curl -u elastic:your_password http://localhost:9200/_cat/allocation?v
查看索引列表
GET _cat/indices?v
查看节点列表
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 将旧索引数据复制到新索引:
POST _reindex
{
"source": { "index": "old_index" },
"dest": { "index": "new_index" }
}
三、分片搬迁与故障处理
手动迁移分片
指定将某个分片从节点 A 搬迁到节点 B:
POST _cluster/reroute
{
"commands": [{
"move": {
"index": "index_name",
"shard": 1,
"from_node": "node-A",
"to_node": "node-B"
}
}]
}
节点名可通过
GET _cat/nodes?v获取。
取消搬迁
POST _cluster/reroute
{
"commands": [{
"cancel": {
"index": "index_name",
"shard": 0,
"node": "target-node",
"allow_primary": true
}
}]
}
强制分配空主分片(丢弃数据)
当主分片数据丢失且无法恢复时,强制分配空主分片(需要 accept_data_loss: true,谨慎使用):
POST /_cluster/reroute
{
"commands": [{
"allocate_empty_primary": {
"index": "index_name",
"shard": 1,
"node": "surviving-node",
"accept_data_loss": true
}
}]
}
修改索引分配延迟
节点离开集群后,ES 默认等待 1 分钟再重新分配其分片。这段时间内如果节点恢复,分片无需重新同步。可在索引级别修改等待时间:
PUT /_all/_settings
{
"settings": {
"index.unassigned.node_left.delayed_timeout": "5m"
}
}
设为 0 则立即分配,不等待。
按 IP 排除节点
集群级别排除指定节点(被排除的节点上的分片会自动迁移):
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"}}'
也可以按索引级别排除:
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 | 不进行均衡 |
并发恢复控制
# 修改节点恢复并发数(默认 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
日志级别动态调整
PUT _cluster/settings
{
"transient": { "logger._root": "DEBUG" }
}
针对特定模块:
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),但所有值必须统一用百分比或统一用绝对值,不可混用。
# 示例:调整低水位为 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)
单索引分片数限制
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系列输出更简洁,适合命令行快速查看

1276

被折叠的 条评论
为什么被折叠?



