我是靠谱客的博主 单身凉面,最近开发中收集的这篇文章主要介绍Haproxy 2.3.0平滑发布解决方案实现(附带shell脚本),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

编译安装

我只有两台服务器 就在两台服务器上体验
47.110.136.12
47.114.138.34

平滑发布脚本




 #!/bin/sh
getHealthy(){

result=`ssh  root@$haproxy_node "echo 'show servers state $backend_name' | socat stdio /var/lib/haproxy/haproxy.sock"`
sum=0
online_array=()
status=()
sumby=28
temp=26
# 记录实例名称 
online_hostname=()
for val in $result
do
     let "sum += 1"
   # echo $sum
     if [ $temp == $sum ];then
         online_hostname[${#online_hostname[*]}]=${val}
   #      echo $val
	 let "temp+=20"		
     fi
     if [ $sumby == $sum ];then
    #	echo $val
        status[${#status[*]}]=${val}	
        let "sumby+=20"
     fi
done

#echo 数组变量arr的值为:"${online_hostname[*]}"
#echo 数组变量arr的值为:"${status[*]}"
length=${#status[*]}

result=()


#下标遍历
for((i=0;i<${#status[*]};i++))
do

        if [ 2 = ${status[i]} ];then
#		echo ${online_hostname[i]}
		result[${#result[*]}]=${online_hostname[i]}
        fi

done


# 通过直接遍历数组内容
#for item in ${result[*]}
#do 
#	echo  result:"${item}"
#done
echo ${result[*]}
}

getPublishStatus(){
result=`ssh   root@$haproxy_node "echo 'show servers state $backend_name' | socat stdio /var/lib/haproxy/haproxy.sock"`
sum=0
online_array=()
status=()
sumby=28
temp=26
#记录实例名称
online_hostname=()
for val in $result
do
     let "sum += 1"
     if [ $temp == $sum ];then
         online_array[${#online_array[*]}]=${val}
         let "temp+=20"
     fi
     if [ $sumby == $sum ];then
        status[${#status[*]}]=${val}
        let "sumby+=20"
     fi
done

echo getPublishStatus数组变量arr的值为:"${online_array[*]}"
echo getPublishStatus数组变量state的值为:"${status[*]}"


#下标遍历
for((i=0;i<${#online_array[*]};i++))
do

        if [ $cur_name==${online_array[i]} ];then
		  if [ 2 == ${status[i]} ];then	
                	return 1
		  else
			return 0
		  fi
        fi

done

}

getCurSessions(){

result=`ssh  root@$haproxy_node "echo 'show servers conn $backend_name' | socat stdio /var/lib/haproxy/haproxy.sock"`

init=22
addrinit=16
sum=0
connects=()
servers=()
for val in $result
do
     let "sum += 1"
     if [ $addrinit == $sum ];then
         servers[${#servers[*]}]=${val}
         let "addrinit+=14"
     fi

     if [ $init == $sum ];then
         connects[${#connects[*]}]=${val}
         let "init+=14"
     fi
done

echo [getCurSessions]connects:"${connects[*]}"
echo [getCurSessions]servers:"${servers[*]}"
server_name=$backend_name/$cur_node

for((i=0;i<${#servers[*]};i++))
do

        if [ $server_name == ${servers[i]} ];then
                return ${connects[i]}
        fi

done

return 1


}


DATE=`date +'%Y-%m-%d %H:%M:%S'`

echo $DATE

DIR=/root
JARFILE=clkj-gateway.jar
PROGRAM=clkj
cur_node=real_server1
haproxy_node=haproxy_node
backend_name=backend_forward
PORT=8081

echo "cur_node:$cur_node"

echo gethealth start

pods=($(getHealthy)) 


for item in ${pods[*]}
do
        echo  pods:"${item}"
done
podsize=${#pods[*]}

echo ${pods[0]}
echo size:$podsize
if [ $podsize == 0 ];then
echo failed,cause no availabe pod
exit 1
fi 
if [ $podsize == 1 -a ${pods[0]} == $cur_node ];then
echo reject publish current available:$cur_node
exit 1
fi

echo start maint pod
 
#echo debug 111
#ssh root@node3 "echo 'disable server backend_forward/$cur_node' |socat stdio /var/lib/haproxy/haproxy.sock"

ssh root@$haproxy_node "echo 'set server  $backend_name/$cur_node state drain' |socat stdio /var/lib/haproxy/haproxy.sock"


sleep 1

getCurSessions

connects=$?
echo getCurSessions:$connects

while (($connects != 0))
do
getCurSessions
connects=$?
echo [retry getCurSessions]:$connects
done

echo 软下线成功
#java -jar
ps -ef | grep $JARFILE |grep $PORT |grep -v grep | awk '{print $2}' | xargs kill -9
source /etc/profile

getPublishStatus
#publishres1=($(getPublishStatus))
publishres1=$?

while (($publishres1 != 0))
do
	echo 摘除服务retry get publish status
	getPublishStatus
	publishres1=$?
	#publishres=($(getPublishStatus))
	#endtime=`date +'%Y-%m-%d %H:%M:%S'`
	#end_seconds=$(date --date="$endtime" +%s)
	#echo 持续:$((end_seconds-start_seconds))
	if [ $publishres1 = 0 ];then
        echo 摘除服务成功	
	fi
        #if [ $((end_seconds-start_seconds)) -gt 30 ];then
	#echo "timeout publish failed "
    	#exit 1
        #fi
done


echo publishres1:$publishres1

echo 开始上线服务
nohup java  -jar  $JARFILE --server.port=$PORT > out.log &

sleep 1
echo start获取发布状态

#ssh root@node3 "echo 'set server backend_forward/$cur_node weight 0' |socat stdio /var/lib/haproxy/haproxy.sock"

#ssh root@node3 "echo 'enable server backend_forward/$cur_node' |socat stdio /var/lib/haproxy/haproxy.sock"
getPublishStatus
publishres=$?
#publishres=($(getPublishStatus))
echo publishres:$publishres

starttime=`date +'%Y-%m-%d %H:%M:%S'`
start_seconds=$(date --date="$starttime" +%s)

while (($publishres != 1))
do
	echo hty:retry get publish status
	getPublishStatus
	publishres=$?
	#publishres=($(getPublishStatus))
	endtime=`date +'%Y-%m-%d %H:%M:%S'`
	end_seconds=$(date --date="$endtime" +%s)
	echo 持续:$((end_seconds-start_seconds))
	if [ $publishres = 1 ];then
			echo publishres success
            break	
	fi
    if [ $((end_seconds-start_seconds)) -gt 120 ];then
			echo "timeout publish failed "
    	exit 1
    fi
done

ssh root@$haproxy_node "echo 'enable server $backend_name/$cur_node' |socat stdio /var/lib/haproxy/haproxy.sock"







最后

以上就是单身凉面为你收集整理的Haproxy 2.3.0平滑发布解决方案实现(附带shell脚本)的全部内容,希望文章能够帮你解决Haproxy 2.3.0平滑发布解决方案实现(附带shell脚本)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(40)

评论列表共有 0 条评论

立即
投稿
返回
顶部