一个正确的ES集群重启流程(附串行重启脚本)

编程入门 行业动态 更新时间:2024-10-23 12:26:51

一个正确的ES集群<a href=https://www.elefans.com/category/jswz/34/1770429.html style=重启流程(附串行重启脚本)"/>

一个正确的ES集群重启流程(附串行重启脚本)

目录

1、关闭集群自动均衡、禁止集群写入

2、重启es集群

3、打开集群自动均衡、开启集群写入

4、补充一个串行重启es集群的shell脚本


注释:本集群所有操作都在跳板机或者堡垒机进行,运维日常一般不需要我们登录具体的机器。

1、关闭集群自动均衡、禁止集群写入

#关闭集群自动均衡
curl -XPUT "http://集群任意IP:9200/_cluster/settings?pretty" -H 'Content-Type:application/json' -d 
'{"persistent" :{"cluster.routing.rebalance.enable": "none"},"transient" :{"cluster.routing.rebalance.enable": "none"}
}'
#curl命令可以在kibana平台替换为如下命令:
PUT /_cluster/settings  启动时候,禁自动均衡
{"persistent": {"cluster.routing.rebalance.enable": "none"},"transient": {"cluster.routing.rebalance.enable": "none"}
}参数取值:
all:默认选项,允许所有种类的分片的分片平衡
primaries:仅允许主分片的分片平衡。
replicas:仅允许副本分片的分片平衡。
none:任何索引都不允许任何形式的分片平衡。#检查集群自动均衡是否关闭
curl -XGET "http://集群任意IP:9200/_cluster/settings?pretty"
#curl命令可以在kibana平台替换为如下命令:
get/_cluster/settings?pretty#禁止集群写入
curl -XPUT "http://集群任意IP:9200/_cluster/settings?pretty" -H 'Content-Type:application/json' -d '
{"persistent" :{"cluster.blocks.read_only" : "true"},"transient" :{"cluster.blocks.read_only" : "true"}
}'
#curl命令可以在kibana平台替换为如下命令:
PUT /_cluster/settings  //启动时候,禁写
{"persistent": {"cluster.blocks.read_only": true},"transient": {"cluster.blocks.read_only": true}
}#检查集群写入是否已经关闭
curl -XGET "http://集群任意IP:9200/_cluster/settings?pretty"
#curl命令可以在kibana平台替换为如下命令:
get /_cluster/settings?pretty

2、重启es集群

#停es服务
ssh ip -C 'ps -ef|grep  org.elasticsearch.bootstrap.Elasticsearch|grep -v grep|awk '{print \$2}'|xargs kill -9'#启动es服务
ssh ip -C 'su - es -c "cd /home/es/software/elasticsearch/bin;sh elasticsearch -d"'

3、打开集群自动均衡、开启集群写入

--先打开写入,再打开自动均衡
#打开集群写入
curl -XPUT "http://集群任意IP:9200/_cluster/settings?pretty" -H 'Content-Type:application/json' -d '{"persistent" :{"cluster.blocks.read_only" : "false"}}'
curl -XPUT "http://集群任意IP:9200/_cluster/settings?pretty" -H 'Content-Type:application/json' -d '{"transient" :{"cluster.blocks.read_only" : "false"}}'
#curl命令可以在kibana平台替换为如下命令:
PUT /_cluster/settings
{"persistent": {"cluster.blocks.read_only": false}
}
PUT /_cluster/settings
{"transient": {"cluster.blocks.read_only": false}
}#检查集群写入是否已经打开
curl -XGET "http://集群任意IP:9200/_cluster/settings?pretty"
#curl命令可以在kibana平台替换为如下命令:
get /_cat/shards?v&pretty&s=ip:desc#打开集群自动均衡
curl -XPUT "http://集群任意IP:9200/_cluster/settings?pretty" -H 'Content-Type:application/json' -d '{"persistent" :{"cluster.routing.rebalance.enable": "all"}}'
curl -XPUT "http://集群任意IP:9200/_cluster/settings?pretty" -H 'Content-Type:application/json' -d '{"transient" :{"cluster.routing.rebalance.enable": "all"}}'
#curl命令可以在kibana平台替换为如下命令:
PUT /_cluster/settings
{"persistent": {"cluster.routing.rebalance.enable": "ALL"},"transient": {"cluster.routing.rebalance.enable": "ALL"}
}#检查集群自动均衡是否打开
curl -XGET "http://集群任意IP:9200/_cluster/settings?pretty"
#curl命令可以在kibana平台替换为如下命令:
get /_cluster/settings?pretty

4、补充一个串行重启es集群的shell脚本

写脚本的背景:最近接了一个业务的需求,需要添加一个配置,然后在不影响集群的情况下重启生效(业务可以接受短暂的yellow和red)。这样我就需要在集群恢复green之后,才能重启下一台机器。

脚本使用方法:作为一个大数据运维人员,个人习惯所有操作都在跳板机执行。脚本可以参考背景和里面的注释。使用方法就是在跳板机准备以下脚本esrestart.sh和一个listIP列表放置es集群ip或域名。执行如下命令:

sh esrestart.sh listIP
#!/bin/bash
#0、本脚本用于重启es集群,其中active master必须手动启动,脚本无法重启此mater
#1、判断参数
if [ $# -lt 1 ]
thenecho -e "\033[1;31m 参数不足,请重新执行... \033[0m"echo -e "\033[1;32m 此脚本传参使用方法:$0 第一个参数为待重启的es集群ip列表文件名 \033[0m"exit ;
fi
workdir=`pwd`#2、建立es相关的配合文件
list=$1
es_home="/home/es/software/elasticsearch"
es_cluster_name="es-mimi6"
list_ip=`cd ${workdir};cat $list`sum=`cat $list|sed '/^$/d'|wc -l`
num=1#4、人工判断脚本是否可以执行,防止误操作
while true ; doecho -ne "\033[33m本脚本会重启es集群,请在 120 秒内确认是否执行: (Y/N) \033[0m" ; read -t120 result[ -z "${result}" ] && breakcase ${result} inY|y)    break ;;N|n)    echo -e "\033[1;31m配置有误 ,退出es集群重启 !\033[0m" ; exit 1 ;;*)      echo -e "\033[1;31m输入有误请重新输入 !\033[0m" ;;esac
done#5、开始重启操作
for ip in ${list_ip[@]} ; doecho -e "\033[1;31m$num\033[1;32m/$sum	 =============== $ip 节点 $user 用户下 现有java进程 =============== \033[0m"es_active_master=`curl -s http://$ip:9200/_cat/master|awk '{print $3}'`if [ "${ip}" != "${es_active_master}" ] ; thenes_num=1while [ `curl -s http://${ip}:9200/_cat/health|awk '{print $4}'` != "green" ] ; doecho -e "\033[1;33m 0.${es_num}、集群未恢复green状态,${ip} 节点本次总计划等待sleep ${es_num}0s \033[0m"sleep 10let es_num++done#上次es服务启动时间echo -e "\033[1;35m 1、集群状态为:`curl -s http://${ip}:9200/_cat/health|awk '{print $4}'`,${ip} 节点上次启动时间如下: \033[0m"sudo ssh $ip -C "su - es -c 'jps|grep -v Jps'|awk '{print \$1}'|xargs ps -o lstart|grep -v STARTED"echo -e "\033[1;35m 2、开始执行重启命令 \033[0m"sudo ssh $ip -C "su - es -c 'cd ${es_home}/bin && ps -ef|grep  org.elasticsearch.bootstrap.Elasticsearch|grep -v grep|awk '{print \$2}'|xargs kill -9 && sleep 5 && sh elasticsearch -d'"#重启后es服务启动时间echo -e "\033[1;35m 3、集群状态为:`curl -s http://${es_active_master}:9200/_cat/health|awk '{print $4}'`,${ip} 节点本次启动时间如下: \033[0m"sudo ssh $ip -C "su - es -c 'jps|grep -v Jps'|awk '{print \$1}'|xargs ps -o lstart|grep -v STARTED"elseecho -e "\033[1;31m ${ip} 为 active master,重启跳过此节点,需要手动重启 \033[0m"filet num++
done

更多推荐

一个正确的ES集群重启流程(附串行重启脚本)

本文发布于:2023-07-28 21:52:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1329672.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重启   集群   脚本   流程   正确

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!