ES 节点平滑下线

2021-12-29 Views Elasticsearch339字2 min read

1. 排除节点

通过以下方式将节点从集群排除

url: http://localhost:9200/_cluster/settings  
method: PUT
body: 
{
    "transient":{
        "cluster.routing.allocation.exclude._name":"{node.name}"
    }
}

上面其实会触发分片的 Allocation 机制,涉及的参数为 cluster.routing.allocation.exclude.{attribute},其中 {attribute} 表示节点的匹配方式,支持三种:

  • _name:匹配 node 名称,多个 node 名称用逗号隔开;
  • _ip:匹配 node ip 地址,多个地址用逗号隔开;
  • _host:匹配 node 主机名,多个主机名用逗号隔开;

2. 等待所有分片迁移完成

上一步执行后,不能马上停服务,必须要等待所有分片迁移到其他节点,若安装了 ES head 插件,建议通过界面查看分区迁移情况与集群健康状态。也可以通过以下命令查看

url: http://localhost:9200/_cat/shards 
method: GET
查看是否存在 RELOCATING 状态的分片

所有分片迁移完成后,可再通过以下命令查看节点数据是否已迁移,都是 0 表示数据也已经迁移

url: http://localhost:9200/_nodes/{node.name}/stats/indices?pretty
method: GET
response:
{
  ...
  "indices" : {
        "docs" : {
          "count" : 0,
          "deleted" : 0
        },
        "store" : {
          "size_in_bytes" : 0
        },
  ...
}

3. 停掉节点

4. 恢复集群路由策略

url: http://localhost:9200/_cluster/settings  
method: PUT
body: 
{
    "transient":{
        "cluster.routing.allocation.exclude._name":null
    }
}
EOF