副本分片重建

POST _cluster/reroute
{
  "commands": [
    {
      "cancel": {
        "index": "my_index",
        "shard": 0,
        "node": "node_id_of_bad_replica"
      }
    },
    {
      "allocate_replica": {
        "index": "my_index",
        "shard": 0,
        "node": "node_id_of_target_node"
      }
    }
  ]
}
移动副本分片
// 取消后复制到别的节点

POST _cluster/reroute
{
"commands": [
{
"cancel": {
"index": "my_index",
"shard": 0,
"node": "node_id_of_bad_replica"
}
},
{
"allocate_replica": {
"index": "my_index",
"shard": 0,
"node": "node_id_of_target_node"
}
}
]
}

 
可以——Elasticsearch 的 _cluster/reroute API 支持对单个副本分片执行“删除本地拷贝 → 分配空白副本”操作,核心思路是:
  1. 先把该副本从当前节点取消(cancel)
  2. 再显式allocate_replica到同一节点(或别的节点),ES 发现节点上已无分片文件,就会触发一次完整 peer-recovery,即重新从主分片拷贝一份空白/全新的副本。

 

 

由于副本分片损坏,我们需要重建该副本。通常,我们可以通过几种方式来实现,比如临时减少副本数再恢复,或者将损坏的副本分片重新分配到一个新的节点上。以下步骤将指导您完成重建单个损坏副本分片的过程。

注意:在执行以下操作之前,请确保您有一个可用的主分片,并且集群状态至少为黄色(即所有主分片都正常)。

步骤:

  1. 确认损坏的副本分片信息。

  2. 临时减少副本数(设置为0),这样Elasticsearch会删除所有副本(包括损坏的)。

  3. 然后将副本数恢复为所需的值,Elasticsearch会重新创建副本。

# 1. 首先确认损坏的副本分片
GET _cat/shards?v&h=index,shard,prirep,state,node,unassigned.reason
GET _cluster/allocation/explain
{
  "index": "your_index_name",
  "shard": 0,
  "primary": false
}

# 2. 临时将副本数设置为0(删除所有副本)
PUT your_index_name/_settings
{
  "index.number_of_replicas": 0
}

# 3. 等待副本分片被完全删除
GET _cluster/health?wait_for_no_initializing_shards=true&timeout=5m

# 4. 恢复原来的副本数
PUT your_index_name/_settings
{
  "index.number_of_replicas": 1
}

# 5. 监控重建进度
GET _cat/recovery/your_index_name?v

 我们使用取消分配命令(cancel)不会删除数据。取消分配命令的作用是取消当前正在进行的分片分配过程,并将分片标记为未分配状态。然后,Elasticsearch 会尝试重新分配该分片。如果分片数据损坏,重新分配可能会失败,或者如果主分片是好的,则会从主分片复制数据到新的副本。

posted @ 2025-11-12 13:57  滴滴滴  阅读(9)  评论(0)    收藏  举报