elasticsearch集群更换节点操作

76 阅读2分钟

携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第30天,点击查看活动详情

@[toc] 在elasticsearch集群中,由于在原有服务器集群上,每台服务器开了3个实例,导致部分节点压力过大,因此,新增一台服务器,将原有节点迁移到新服务器。 elasticsearch集群中增加节点步骤:

1.关闭集群分配reblance配置

PUT _cluster/settings 
{
  "transient": {
    "cluster.routing.rebalance.enable":"none"
  }
}

结果:

{
  "acknowledged": true,
  "persistent": {},
  "transient": {
    "cluster": {
      "routing": {
        "rebalance": {
          "enable": "none"
        }
      }
    }
  }
}

此时,索引的变更不再会导致分片在节点中的迁移。 如果需要更合理的管理es集群,建议将集群reblance关闭,完全手动管理,这样效率会更高,但是需要更加复杂的脚本配合。

2.增加节点

vim /etc/security/limits.conf 增加如下内容:

elastic soft memlock unlimited
elastic hard memlock unlimited
elastic soft nofile  655350
elastic hard nofile  655350
elastic soft nproc   655350
elastic soft nproc   655350

vim /etc/sysctl.conf 增加

vm.max_map_count = 262144
sysctl -p

创建用户

useradd elasticsearch -d  /opt/elasticsearch

修改hosts文件,确保本机hostname在 hosts文件中有描述

切换到elastic用户

unzip elasticsearch-5.0.1.zip 
mv ./elasticsearch-5.0.1 ./elasticsearch-node5-1
ln -s /opt/elasticsearch/elasticsearch-node5-1 /opt/elasticsearch/node5-1

修改 /opt/elasticsearch/node5/config 下的jvm.options 将内存改为20G -Xms20g -Xmx20g

安装xpack ./bin/elasticsearch-plugin install file:///opt/elasticsearch/x-pack-5.0.1.zip 增加node,group配置 修改 elasticsearch.yml cluster.routing.allocation.awareness.attributes: rack node.attr.rack: r3 这样可以避免统一索引的分片备份到相同节点

启动节点:

 /opt/elasticsearch/node5-1/bin/elasticsearch -d

此时通过elk监控界面可以看到新增加的节点 image.png

3.reroute 节点数据

采用如下命令即可

POST _cluster/reroute
{
  "commands":[
    {
     "move" : {
                "index" : "activemq-2017.11.01", "shard" : 2,
                "from_node" : "node4-3", "to_node" : "node5-1"
      }
    },
       {
         "move" : {
                "index" : "activemq-2017.11.01", "shard" : 3,
                "from_node" : "node4-3", "to_node" : "node5-1"
         }
       },
       {
         "move" : {
                "index" : "activemq-2017.11.01", "shard" : 4,
                "from_node" : "node4-3", "to_node" : "node5-1"
         }
       }
    ]
}

image.png

重复上述操作,将需要迁移的节点上的shard全部迁移到新节点,之后关闭节点,并重新打开自动平衡即可。