之前写了一篇文章如何安全重启ES集群的节点,这又一个前提,就是需要停止写入业务。但是,有些时候业务是不能停的,又需要重启某一个节点(例如补丁修复,服务器更换等),这就需要用到本篇文章提到的不停业务重启ES节点。
总体思路
将需要重启的节点从集群中删除,然后重新加入,也能起到重启的效果,但是不需要停业务
具体步骤
Step1 开启集群shard自动平衡
一般情况下,集群的自动平衡都是开启的,可以用以下指令查看
$ curl "localhost:9200/_cluster/settings?pretty"
{
"persistent" : {
"cluster" : {
"routing" : {
"allocation" : {
"enable" : "all"
}
}
}
}
}
如果 “enable” : “all”,则表示自动平衡已经开启,如果没有开启,用以下方法开启自动平衡。
curl -X PUT "localhost:9200/_cluster/settings?pretty" -H 'Content-Type: application/json' -d'
{
"persistent": {
"cluster.routing.allocation.enable": "all"
}
}
'
Step2 从ES集群中删除指定节点
curl -X PUT "localhost:9200/_cluster/settings?pretty" -H 'Content-Type: application/json' -d'
{
"persistent" : {
"cluster.routing.allocation.exclude._ip" : "10.0.0.1"
}
}
'
这里可以通过ip,nodename等方法指定需要删除的节点
_name Match nodes by node name
_host_ip Match nodes by host IP address (IP associated with hostname)
_publish_ip Match nodes by publish IP address
_ip Match either _host_ip or _publish_ip
_host Match nodes by hostname
_id Match nodes by node id
_tier Match nodes by the node’s data tier role
Step3 等待集群自动重新平衡Shard
在同步期间,可以通过以下方法查看集群状态
$ curl 'localhost:9200/_cluster/health?pretty'
{
"cluster_name" : "natanalyzer",
"status" : "green",
"timed_out" : false,
"number_of_nodes" : 6,
"number_of_data_nodes" : 6,
"active_primary_shards" : 6694,
"active_shards" : 6844,
"relocating_shards" : 0,
"initializing_shards" : 0,
"unassigned_shards" : 0,
"delayed_unassigned_shards" : 0,
"number_of_pending_tasks" : 0,
"number_of_in_flight_fetch" : 0,
"task_max_waiting_in_queue_millis" : 0,
"active_shards_percent_as_number" : 100.0
}
当relocating_shards=0的时候,再次确认需要删除的节点上已经没有shard,才执行下一步的操作
curl http://localhost:9200/_cat/shards?v|grep esnode5
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 561k 100 561k 0 0 1029k 0 --:--:-- --:--:-- --:--:-- 1028k
Step4,重新启动节点
Step5,节点重新加入集群
由于重新启动的节点已经被列入集群的exclude列表,需要对其进行清除,以触发shard重新平衡
curl -X PUT "localhost:9200/_cluster/settings?pretty" -H 'Content-Type: application/json' -d'
{
"persistent" : {
"cluster.routing.allocation.exclude._name" : null
}
}
'
综上,就可以在带业务的情况下完成集群节点的重启。