elasticsearch集群索引自动化双副本设置脚本

文档描述:

        本文主要说明es集群索引单福本设置双副本操作,分为手动执行命令及自动化脚本执行。自动化脚本运行主要涉及:自动检查集群索引副本状态,检查集群健康状态,只有当前集群为green状态才可以执行索引双副本操作,等集群恢复状态green后才执行下一个副本操作。自动脚本需要添加到计划任务可以设置分钟或小时等定时执行自动检测副本执行。

手动执行双副本命令

##副本修改0但副本1双副本
curl -H 'Content-Type:application/json' -XPUT 'http://172.168.xx.xx:9201/索引名称/_settings?pretty' -d '{"number_of_replicas":1,"refresh_interval": "120s" }'

自动化设置ES集群索引双副本脚本:

cat   es_fuben_4.sh
#!/bin/bash
#es集群副本检查及自动设置副本
#es集群IP地址配置
IP_ADD=192.168.0.4
ip=`/sbin/ip add |grep inet |grep -v inet6 |grep -v host |awk '{print $2}' |awk -F"/" '{print $1}'|head -1`
#日志
LOGS_TIME=`date "+%Y-%m"`
#查看集群副本状况
#curl -H 'Content-Type:application/json' -XGET http://$IP_ADD:9201/_cat/indices/?v -s |grep -v close|awk -v OFS=" number_of_replicas:" '{print $3,$6}'|grep "number_of_replicas:0"
curl -H 'Content-Type:application/json' -XGET http://$IP_ADD:9201/_cat/indices/?v -s |grep -v close|awk -v OFS=" number_of_replicas:" '{print $3,$6}'|grep "number_of_replicas:0"  >/opt/shell/provided_name_list_fuben_"$IP_ADD".txt
index_list=`cat /opt/shell/provided_name_list_fuben_"$IP_ADD".txt|awk '{print $1}'`
for index_name  in $index_list
do
#检查集群状态
ES_status=`curl -s http://$IP_ADD:9201/_cluster/health?pretty |grep status |awk -F "\"" '{print $4}'`
if [ $ES_status == "green" ];then
	number=`curl -H 'Content-Type:application/json' -XGET http://$IP_ADD:9201/$index_name/_settings?pretty -s |grep '"number_of_replicas" : "0"' |awk -F'"' '{print $4}'`
	if [ $number -eq 0 ];then
		echo "`date "+%Y-%m-%d %H:%M:%S"` 开始执行副本操作 $index_name" >>/opt/shell/number_of_replicas_"$IP_ADD"_"$LOGS_TIME".log
		curl -H 'Content-Type:application/json' -XPUT "http://$IP_ADD:9201/$index_name/_settings?pretty" -d '{"number_of_replicas":1,"refresh_interval": "60s" }'
	 sleep 300
	fi
	number1=`curl -H 'Content-Type:application/json' -XGET http://$IP_ADD:9201/$index_name/_settings?pretty -s |grep '"number_of_replicas" : "1"' |awk -F'"' '{print $4}'`
        if [ $number1 -eq 1 ];then
	number_check=`curl -H 'Content-Type:application/json' -XGET http://$IP_ADD:9201/$index_name/_settings?pretty -s |grep 'number_of_replicas'`
                echo "`date "+%Y-%m-%d %H:%M:%S"` 完成执行副本操作 $index_name $number_check" >>/opt/shell/number_of_replicas_"$IP_ADD"_"$LOGS_TIME".log
        fi
else
	echo "`date "+%Y-%m-%d %H:%M:%S"` 暂不执行副本操作 $index_name 集群状态: $ES_status" >>/opt/shell/number_of_replicas_"$IP_ADD"_"$LOGS_TIME".log
	break
fi
done

注意:

1、自动化脚本需要配置定时任务定期自动检测执行双副本操作

2、es IP地址及端口,日志文件目录路径这些根据实际情况修改

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值