nginx负载均衡平滑迁移shell

7年前 (2017-07-20) gtj linux, lnmp 0评论 已收录 680℃

因公司业务需求,每次发布都需要手动去切换负载均衡的负载IP,手动操作有点负载。

  1. #!/bin/bash  
  2. #*****************************************************  
  3. #         Author: suixiaofeng  
  4. #           blog:https://blog.cool360.org  
  5. #          Email: 258818040@qq.com   
  6. #  Last modified: 2017-07-12 21:44  
  7. #       Filename: nginx_upstream.sh  
  8. #    Description:   
  9. #****************************************************  
  10. [ -f /etc/init.d/functions ] && . /etc/init.d/functions  
  11. nginx_conf=/usr/local/nginx/conf/nginx.conf  
  12. IP1="192.168.1.2"  
  13. IP2="192.168.1.23"  
  14. IP3="192.168.1.33"  
  15. HOST=`ifconfig eth0|sed -n '2p'|sed 's#^.*dr:##g'|sed 's#Bc.*$##g'`  
  16. IPTOTALL=($IP1 $IP2 $IP3)  
  17. [ $# -ne 1 ] && {  
  18.    echo $"Usage:$0 {start|stop}"  
  19.    exit  
  20. }  
  21.   
  22.   
  23. function startup (){  
  24.      
  25.      if [ "$IP1" == ${HOST} ] ; then   
  26.           sed -i '/'$IP1'/s/#//g' $nginx_conf  
  27.           sed -i  '/'$IP2'/s/^/#/' $nginx_conf  
  28.           sed -i  '/'$IP3'/s/^/#/' $nginx_conf  
  29.      elif [ "$IP2" == ${HOST} ] ; then   
  30.           sed -i '/'$IP2'/s/#//g' $nginx_conf  
  31.           sed -i  '/'$IP1'/s/^/#/' $nginx_conf  
  32.           sed -i  '/'$IP3'/s/^/#/' $nginx_conf  
  33.             
  34.       elif [ "$IP3"  == ${HOST} ] ; then  
  35.           sed -i '/'$IP3'/s/#//g' $nginx_conf  
  36.           sed -i  '/'$IP1'/s/^/#/' $nginx_conf  
  37.           sed -i  '/'$IP2'/s/^/#/' $nginx_conf  
  38.     
  39.                 
  40.      else   
  41.   
  42.             echo "IP is error!"  
  43.              exit 1  
  44.      fi  
  45.   
  46. /usr/local/nginx/sbin/nginx -t  >/dev/null 2>&1  
  47. [ $? -eq 0 ] && {  
  48.    /usr/local/nginx/sbin/nginx -s  reload  >/dev/null 2>&1  
  49. if [ $? -eq 0 ] ; then   
  50.    action "nginx is reload"  /bin/true  
  51. fi  
  52.  }  
  53.   
  54. }  
  55.   
  56.   
  57. function stop () {  
  58.   
  59.   
  60.        if [ "$IP1" == ${HOST} ] ; then   
  61.           sed -i '/'$IP2'/s/#//g' $nginx_conf  
  62.           sed -i '/'$IP3'/s/#//g' $nginx_conf  
  63.           sed -i  '/'$IP1'/s/^/#/' $nginx_conf  
  64.      elif [ "$IP2" == ${HOST} ] ; then  
  65.           sed -i '/'$IP1'/s/#//g' $nginx_conf  
  66.           sed -i '/'$IP3'/s/#//g' $nginx_conf  
  67.           sed -i  '/'$IP2'/s/^/#/' $nginx_conf  
  68.            
  69.             
  70.       elif [ "$IP3"  == ${HOST} ] ; then  
  71.           sed -i '/'$IP2'/s/#//g' $nginx_conf  
  72.           sed -i '/'$IP1'/s/#//g' $nginx_conf  
  73.           sed -i  '/'$IP3'/s/^/#/' $nginx_conf  
  74.         
  75.     
  76.                 
  77.      else  
  78.   
  79.             echo "IP is error!"  
  80.              exit 1  
  81.      fi  
  82.   
  83. /usr/local/nginx/sbin/nginx -t >/dev/null 2>&1  
  84. [ $? -eq 0 ] && {  
  85.    /usr/local/nginx/sbin/nginx -s  reload  >/dev/null 2>&1  
  86. if [ $? -eq 0 ] ;then  
  87.    action "nginx is reload"  /bin/true  
  88. fi  
  89.   
  90.  }  
  91.   
  92.   
  93. }  
  94.   
  95. case $1 in   
  96.   
  97.       "start")  
  98.           startup  
  99.             ;;  
  100.        "stop")  
  101.            stop  
  102.             ;;  
  103.        "*" )  
  104.           echo $"Usage:$0 {start|stop}"  
  105.            exit 1  
  106. esac  

感觉不够简洁,不够完善,如果有好的优化,可以提出。

博主

这货来去如风,什么鬼都没留下!!!

相关推荐

嗨、骚年、快来消灭0回复。

×
订阅图标按钮
Less is more!!!