文档描述:
本文主要说明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地址及端口,日志文件目录路径这些根据实际情况修改