Elasticsearch关于unassigned shards修复

本文介绍了一种使用Python脚本来修复Elasticsearch集群中未分配Shard的方法,并提供了监测修复进程的方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ES 版本: 5.2.1

步骤:

  • curl localhost:9200/_cat/shards > shards
  • 跑脚本:nohup python recovery.py &
    ### 注意:跑脚本过程会返回大量json,时间较长,请注意放入后台
  • 查看修复shard进度:curl 127.0.0.1:9200/_cat/recovery/你修复shard对应的索引
  • 结果: 找到索引对应的shard,看到existing_store done说明已经从本地修复
     index 19 268ms existing_store done n/a        n/a                    10.0.58.67 node_name
#!/usr/bin/env python
#name: recovery.py

import requests
import json
host = "http://localhost:9200/_cluster/allocation/explain"
s= requests.Session()
def reroute_shard(index,shard,node):
    data = {
    "commands" : [
        {
          "allocate_stale_primary" : {
              "index" : index, "shard" : shard, "node" : node, "accept_data_loss": True
          }
        }
    ]
   }
    print data
    url = "http://localhost:9200/_cluster/reroute"
    res = s.post(url,json=data)
    print res

def get_node(line):
    if "UNASSIGNED" in line:
        line = line.split()
        index = line[0]
        shard = line[1]
        if line[2] != "p":
            return
        body = {
           "index": index,
           "shard": shard,
           "primary": True
               }
        res = s.get(host, json = body)
        for store in res.json().get("node_allocation_decisions"):
            if store.get("store").get("allocation_id"):
               node_name = store.get("node_name")
        reroute_shard(index,shard,node_name)
    else:
        return

with open("shards", 'rb') as f:
    map(get_node,f)

相关文档:
https://www.elastic.co/guide/en/elasticsearch/reference/5.2/cluster-reroute.html
https://www.elastic.co/guide/en/elasticsearch/reference/5.2/cluster-allocation-explain.html

### 解决 Elasticsearch 集群中分片未分配 (unassigned) 的方法 当遇到Elasticsearch集群中的分片处于未分配状态时,可以采取多种措施来解决问题。通常情况下,这可能是由于节点间的通信问题、磁盘空间不足或是配置不当等原因造成的。 #### 检查集群健康状况 为了诊断具体原因,可以通过API请求获取当前集群的状态信息: ```bash curl -X GET "http://localhost:9200/_cluster/health?pretty" ``` 这条命令会返回有关整个集群健康的详情,包括活动的主分片数、副本数量以及是否有任何未分配的分片等重要指标[^2]。 #### 查看详细的分片分配情况 进一步了解哪些具体的索引或分片存在问题,可执行如下查询: ```bash curl -X GET "http://localhost:9200/_cat/shards?v=true&h=index,shard,prirep,state,node" ``` 此操作能够展示每一个分片的位置及其状态(例如`STARTED`, `UNASSIGNED`),从而帮助定位确切的问题所在。 #### 常见解决方案 - **重启相关节点**:如果某个数据节点突然离线,则可能导致其上的某些分片无法被重新分配给其他存活的数据节点;尝试重启该节点并观察恢复效果。 - **调整集群设置参数**: - 修改`cluster.routing.allocation.disk.watermark.low` 和 `high` 参数以适应实际可用存储容量; - 设置合理的`index.number_of_replicas`值确保有足够的副本来维持高可用性; - 调整`cluster.info.update.interval`使元数据更新更加频繁以便更快响应变化。 - **手动强制分配分片**:对于那些因为策略限制而未能自动迁移的情况,管理员可以选择通过特定指令来进行干预,比如使用以下RESTful API调用来指定目标节点完成分片的手动重置工作: ```json POST /_cluster/reroute { "commands": [ { "allocate_stale_primary": { "index": "your_index_name", "shard": 0, "node": "target_node_id_or_name", "accept_data_loss": true } } ] } ``` 注意,在这里设置了`accept_data_loss:true`意味着接受可能存在的数据丢失风险,请谨慎评估后再做决定[^1]。 #### 日志分析 最后但同样重要的一步是审查日志文件,特别是位于`logs/`目录下的`.log`记录,它们往往包含了最直接有价值的线索用于排查故障根源。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值