1、 ES索引副本自动检测并设置副本,日志记录创建副本过程。
2、加入定时任务监控非副本自动设置。
cat es_fuben.sh
#!/bin/bash
#es集群自动设置副本脚本
#es集群IP地址配置
IP_ADD=192.168.1.1
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.txt
index_list=`cat /opt/shell/provided_name_list_fuben.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_"$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_"$LOGS_TIME".log
fi
else
echo "`date "+%Y-%m-%d %H:%M:%S"` 非健康状态暂不执行副本操作 $index_name 集群状态: $ES_status" >>/opt/shell/number_of_replicas_"$LOGS_TIME".log
break
fi
done
查看集群索引副本状况:
curl -H 'Content-Type:application/json' -XGET http://192.168.1.1:9201/*/_settings?pretty |grep -E "provided_name|number_of_replicas"
配置索引副本操作:
curl -H 'Content-Type:application/json' -XPUT 'http://192.168.1.1:9201/索引名称/_settings?pretty' -d '{"number_of_replicas":1,"refresh_interval": "120s" }'
自动化设置Elasticsearch索引副本

本文介绍了如何通过Linux脚本实现Elasticsearch索引副本的自动化检测和设置,并详细说明了查看集群索引副本状况及配置索引副本操作的方法,同时提及将此过程纳入定时任务以持续监控。
6277

被折叠的 条评论
为什么被折叠?



