nginx负载均衡平滑迁移shell
因公司业务需求,每次发布都需要手动去切换负载均衡的负载IP,手动操作有点负载。
- #!/bin/bash
- #*****************************************************
- # Author: suixiaofeng
- # blog:https://blog.cool360.org
- # Email: 258818040@qq.com
- # Last modified: 2017-07-12 21:44
- # Filename: nginx_upstream.sh
- # Description:
- #****************************************************
- [ -f /etc/init.d/functions ] && . /etc/init.d/functions
- nginx_conf=/usr/local/nginx/conf/nginx.conf
- IP1="192.168.1.2"
- IP2="192.168.1.23"
- IP3="192.168.1.33"
- HOST=`ifconfig eth0|sed -n '2p'|sed 's#^.*dr:##g'|sed 's#Bc.*$##g'`
- IPTOTALL=($IP1 $IP2 $IP3)
- [ $# -ne 1 ] && {
- echo $"Usage:$0 {start|stop}"
- exit
- }
- function startup (){
- if [ "$IP1" == ${HOST} ] ; then
- sed -i '/'$IP1'/s/#//g' $nginx_conf
- sed -i '/'$IP2'/s/^/#/' $nginx_conf
- sed -i '/'$IP3'/s/^/#/' $nginx_conf
- elif [ "$IP2" == ${HOST} ] ; then
- sed -i '/'$IP2'/s/#//g' $nginx_conf
- sed -i '/'$IP1'/s/^/#/' $nginx_conf
- sed -i '/'$IP3'/s/^/#/' $nginx_conf
- elif [ "$IP3" == ${HOST} ] ; then
- sed -i '/'$IP3'/s/#//g' $nginx_conf
- sed -i '/'$IP1'/s/^/#/' $nginx_conf
- sed -i '/'$IP2'/s/^/#/' $nginx_conf
- else
- echo "IP is error!"
- exit 1
- fi
- /usr/local/nginx/sbin/nginx -t >/dev/null 2>&1
- [ $? -eq 0 ] && {
- /usr/local/nginx/sbin/nginx -s reload >/dev/null 2>&1
- if [ $? -eq 0 ] ; then
- action "nginx is reload" /bin/true
- fi
- }
- }
- function stop () {
- if [ "$IP1" == ${HOST} ] ; then
- sed -i '/'$IP2'/s/#//g' $nginx_conf
- sed -i '/'$IP3'/s/#//g' $nginx_conf
- sed -i '/'$IP1'/s/^/#/' $nginx_conf
- elif [ "$IP2" == ${HOST} ] ; then
- sed -i '/'$IP1'/s/#//g' $nginx_conf
- sed -i '/'$IP3'/s/#//g' $nginx_conf
- sed -i '/'$IP2'/s/^/#/' $nginx_conf
- elif [ "$IP3" == ${HOST} ] ; then
- sed -i '/'$IP2'/s/#//g' $nginx_conf
- sed -i '/'$IP1'/s/#//g' $nginx_conf
- sed -i '/'$IP3'/s/^/#/' $nginx_conf
- else
- echo "IP is error!"
- exit 1
- fi
- /usr/local/nginx/sbin/nginx -t >/dev/null 2>&1
- [ $? -eq 0 ] && {
- /usr/local/nginx/sbin/nginx -s reload >/dev/null 2>&1
- if [ $? -eq 0 ] ;then
- action "nginx is reload" /bin/true
- fi
- }
- }
- case $1 in
- "start")
- startup
- ;;
- "stop")
- stop
- ;;
- "*" )
- echo $"Usage:$0 {start|stop}"
- exit 1
- esac
感觉不够简洁,不够完善,如果有好的优化,可以提出。
嗨、骚年、快来消灭0回复。