服务器集群自动切换网络节点

2024-08-22 16:44:18 浏览数 (2)

代码语言:shell复制
#!/bin/bash
# 集群节点自动切换

# Define the list of IP addresses
ipList=(    10.1.1.8     10.1.1.13 )

failCount=0

failFlag=0
failMaxFlag=0

successCount=0
successMaxFlag=0

failMaxCount=5             # 稳定 5 次pin
successMaxCount=5          # 稳定 5 次pin

i=0


configDIR(){

    if [ `id -un` != "root" ]
    then
        sudoc="sudo" 
    else
        sudoc=""
    fi

    wwwconf=/datadisk/eisc/wwwconf/
    $sudoc mkdir -p $wwwconf ; $sudoc rm -rf $wwwconf/*


    xiangmuName=(
        www   work   sou   enchantment  guzheng  zhoubao  gcc  xibin
    )

    peizhi=" eisc.cn                $ip               62000"   # HOME
}

create_www_conf(){

  echo "案例:   eisc.cn 0.0.0.0 62000"
#  read -p " 请输入三个参数:" peizhi

    buff=( $peizhi )

    yuming=${buff[0]}
    # 前端域名,增加到nginx 消息头部
    ip=${buff[1]}
    # 后端服务器ip
    portmin=${buff[2]}
    portmin=$[portmin-1]
    # 设置最小端口,后面网站按照顺序自动加1

    declare -A port
      port["www"]="62010"
      port["work"]="62011"
      port["sou"]="62012"
      port["enchantment"]="62013"
      port["guzheng"]="62014"
      port["zhoubao"]="62015"
      port["gcc"]="62016"
      port["xibin"]="62100"

    cd $wwwconf; sudo chmod 777 -R $wwwconf $wwwconf/*

    if [ ! -e eiscwwwconf.tar.gz ]
    then
      $sudoc tar -czvf eiscwwwconf.tar.gz *
      $sudoc rm -rf `ls | grep -v eiscwwwconf.tar.gz`
    fi

    $sudoc rm -rf `ls | grep -v eiscwwwconf.tar.gz`

   # file=(`ls $www | grep -vE "ssl|wwwconf|wwwroot|*.sh"`)
   file=${xiangmuName[*]}

    for i in ${file[*]}
        do
            ((a  ))
            b=$[portmin a] ; portlist[$c]=$b ; ((c  ))
            # 打印一次变量,接收一个元素存入数组: portlist
            if [ ! -e $www/$i/cgi-bin ]
            then
            sudo mkdir $www/$i/cgi-bin
            fi

            case $i in
              "www")          b=${port[www]}           ; echo "#------- www 被手动指定固定端口  $b -------#" ;;
              "work")         b=${port[work]}          ; echo "#------- work 被手动指定固定端口  $b -------#" ;;
              "sou")          b=${port[sou]}           ; echo "#------- sou 被手动指定固定端口  $b -------#";;
              "enchantment")  b=${port[enchantment]}   ; echo "#------- enchantment 被手动指定固定端口  $b -------#";;
              "guzheng")      b=${port[guzheng]}       ; echo "#------- guzheng 被手动指定固定端口  $b -------#";;
              "zhoubao")      b=${port[zhoubao]}       ; echo "#------- zhoubao 被手动指定固定端口  $b -------#";;
              "gcc")          b=${port[gcc]}           ; echo "#------- zhoubao 被手动指定固定端口  $b -------#";;
              "xibin")        b=${port[xibin]}         ; echo "#------- xibin 被手动指定固定端口  $b -------#";;
            esac

            echo "  $i 创建web网站 的端口:   $b 前端域名    $i.$yuming    后端ip port   $ip:$b     c   cgi 解析目录: $i/cgi-bin"
            echo "  $i 创建web网站 的端口:   $b 前端域名    $i.$yuming    后端ip port   $ip:$b     c   cgi 解析目录: $i/cgi-bin" >> log_www_conf.txt

#---------  生成前端转发 子站点文件  -----------#
            $sudoc mkdir -p $wwwconf/nginxzhuanfa ; $sudoc chmod 777 $wwwconf/nginxzhuanfa
            echo "
            server{
            listen 80;
            server_name $i.$yuming;
            location / {
                proxy_pass http://$ip:$b;
                #---- 记录 IP 地址 ----#
                proxy_redirect off;
                proxy_set_header Host $http_host;
                proxy_set_header X-Real-IP $remote_addr;     
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
              }
            }
            " > $wwwconf/nginxzhuanfa/$i-$b.conf

            if [ "$i" = "www" ]
            then
                echo "
                  server{
                  listen 80;
                  server_name $i.$yuming $yuming;
                  location / {
                      proxy_pass http://$ip:$b;
                      #---- 记录 IP 地址 ----#
                      proxy_redirect off;
                      proxy_set_header Host $http_host;
                      proxy_set_header X-Real-IP $remote_addr;	    
                      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                    }
                  }
                " > $wwwconf/nginxzhuanfa/$i-$b.conf
            fi


            if [ "$i" = "gcc" ]
            then
                echo "
                  server{
                  listen 80;
                  server_name www.okgcc.cn okgcc.cn;
                  location / {
                      proxy_pass http://$ip:$b;
                      #---- 记录 IP 地址 ----#
                      proxy_redirect off;
                      proxy_set_header Host $http_host;
                      proxy_set_header X-Real-IP $remote_addr;     
                      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                    }
                  }
                " > $wwwconf/nginxzhuanfa/$i-$b.conf
            fi



#---------- 端口对端口 ----------#

            $sudoc mkdir -p $wwwconf/portconf ; $sudoc chmod 777 $wwwconf/portconf
            echo "
            server{
            listen $b;
            location / {
                proxy_pass http://$ip:$b;
                #---- 记录 IP 地址 ----#
                proxy_redirect off;
                proxy_set_header Host $http_host;
                proxy_set_header X-Real-IP $remote_addr;     
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
              }
            }
            " > $wwwconf/portconf/$i-$b.conf

            if [ "$i" = "www" ]
            then
                echo "
                  server{
                  listen $b;
                  location / {
                      proxy_pass http://$ip:$b;

                    }
                  }
                " > $wwwconf/portconf/$i-$b.conf
            fi


            if [ "$i" = "gcc" ]
            then
                echo "
                  server{
                  listen $b;
                  location / {
                      proxy_pass http://$ip:$b;
                    }
                  }
                " > $wwwconf/portconf/$i-$b.conf
            fi



#---------- 后端转发子站点 ----------#

            echo "
      server {
        listen       $b;
        root   $www/$i;
        index index.php index.html index.htm index;

        add_header 'yuming is ' '$i.$yuming';
        add_header 'ipport is ' '$ip:$b';

        location ~ .php {

#                fastcgi_pass   unix:/var/run/php-fpm811.sock;
                fastcgi_pass   127.0.0.1:9000;
                fastcgi_index  index.php;
                fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                include        fastcgi_params;
        }

        location /cgi-bin {
            fastcgi_pass  unix:/var/run/fcgiwrap.socket;
            include fastcgi.conf;
        }
      }
           " > $wwwconf/$b.$i.conf
        done

        echo "[ok]创建网站日志存放在:   $wwwconf/log_www_conf.txt"
        echo "[ok]前端转发 nginx 子文件目录: $wwwconf/nginxzhuanfa"
        sudo nginx -s reload

}


MainNginxconf()
{
    echo "[ok] 通讯正常,正在切换到节点 ip= $ip" ; sleep 10
    configDIR
    create_www_conf
}

# Main function
main() {
    local initFlag=1

    while true; do
        for ip in ${ipList[*]}
        do
            failCount=0
            successCount=0

            for((;;))
            do
            
                ping -c1 -W1 $ip   | grep from  > /dev/null
                if [ $? -ne 0 ]; then               # 0 正常; -ne 不等于0
                    failFlag=1
                else
                    failFlag=0
                    sleep 0.2
                fi  
                echo "Checking network status for $ip failFlag=$failFlag  failCount=$failCount successMaxFlag=$successMaxFlag successCount=$successCount"
                if [ $failFlag -eq 1 ]; then
                    ((failCount  ))
                fi

                if [ $failFlag -eq 0 ]; then
                    ((successCount  ))
                fi

                if [ $failCount -gt $failMaxCount ]; then
                    failMaxFlag=1 
                    successCount=0  
                    successMaxFlag=0

                    break 
                else
                    failMaxFlag=0
                fi 

                if [ $successCount -gt $successMaxCount ]; then
                    successMaxFlag=1;
                    failCount=0
                    failMaxFlag=0

                    break
                else 
                    successMaxFlag=0;
                fi
                
            done

            if [ $successCount -gt $successMaxCount ]; then
                break
            fi
        done
        
        if [ $successMaxFlag -eq 1 ];then
            MainNginxconf
        fi

    done
}
main
# 一键下载: wget ddoss.cn/file/ubuntu/shell/server/grouphost.sh

0 人点赞