LVS+Keepalived高可用部署

1、LVS+Keepalived高可用部署

  1、keepalived節點部署

  一、安裝keepalived

yum install keepalived ipvsadm -y
mkdir -p /opt/ytd_scripts/keepalived
cat>/opt/ytd_scripts/keepalived/UDP_CHECK.sh<<EOF
#!/bin/bash
/usr/bin/nc -uz -w1 $1 $2 | grep succeeded >/dev/null
exit $?
EOF
cp -rp  /etc/keepalived/keepalived.conf{,.bak}

  二、更改keepalived.conf

  注意:加權平均時,2個節點不要設置成1html

    一、master節點配置
vrrp_instance VI_1 {
    state BACKUP
    interface ens160
    virtual_router_id 51
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 20180126
    }
    virtual_ipaddress {
        10.100.62.66
    }
}

###zuul-8080####
virtual_server 10.100.62.66 8080 {
    delay_loop 6
    lb_algo wrr
    lb_kind DR
    persistence_timeout 5
    protocol TCP

    real_server 10.100.62.43 8080 {
        weight 20
        TCP_CHECK {
            connect_timeout 10
            nb_get_retry 3
            delay_before_retry 3
            connect_port 8080
        }
    }

    real_server 10.100.62.44 8080 {
        weight 20
        TCP_CHECK {
            connect_timeout 10
            nb_get_retry 3
            delay_before_retry 3
            connect_port 8080
        }
    }
}

###zuul-8080-udp####
virtual_server 10.100.62.66 8080 {
    delay_loop 6
    lb_algo wrr
    lb_kind DR
    persistence_timeout 5
    protocol UDP
    
    real_server 10.100.62.43 8080 {
        weight 20        MISC_CHECK {
        misc_path "/opt/ytd_scripts/keepalived/UDP_CHECK.sh 10.100.62.43 8080"
        misc_timeout 3
        }
    }   
    
    real_server 10.100.62.44 8080 {
        weight 20
        MISC_CHECK {
        misc_path "/opt/ytd_scripts/keepalived/UDP_CHECK.sh 10.100.62.44 8080"
        misc_timeout 3
        }
    }   
}
/etc/keepalived/keepalived.conf
    二、slave節點配置
state MASTER -> state BACKUP
priority 100 -> priority 90

  三、節點開啓轉發功能

echo 1 > /proc/sys/net/ipv4/ip_forward

  2、驗證keepalived

  一、啓動keepalived並設置自啓

systemctl enable keepalived
systemctl start keepalived
systemctl status keepalived

  二、關掉master節點,驗證VIP是否到slave上

#在master上執行
systemctl stop keepalived
ip a|grep 'VIP'

#在slave上執行
ip a|gep 'VIP'

  3、兩臺RS上爲ens160:0綁定VIP地址、抑制ARP廣播

  若不綁定循環網卡,將lo網卡換成須要綁定網卡(例如:ens160)後端

  一、在RS上寫相應腳本

#!/bin/bash
#description: Config realserver

VIP=10.100.62.66

/etc/rc.d/init.d/functions

case "$1" in
start)
       /sbin/ifconfig ens160:0 $VIP netmask 255.255.255.255 broadcast $VIP
       /sbin/route add -host $VIP dev ens160:0
       echo "1" >/proc/sys/net/ipv4/conf/ens160/arp_ignore
       echo "2" >/proc/sys/net/ipv4/conf/ens160/arp_announce
       echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
       echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
       sysctl -p >/dev/null 2>&1
       echo "RealServer Start OK"
       ;;
stop)
       /sbin/ifconfig ens160:0 down
       /sbin/route del $VIP >/dev/null 2>&1
       echo "0" >/proc/sys/net/ipv4/conf/ens160/arp_ignore
       echo "0" >/proc/sys/net/ipv4/conf/ens160/arp_announce
       echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore
       echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce
       echo "RealServer Stoped"
       ;;
*)
       echo "Usage: $0 {start|stop}"
       exit 1
esac

exit 0
realserver.sh

  二、在RS上執行腳本

sh realserver.sh start

2、LVS+Keepalived學習連接

  1、使用LVS實現負載均衡原理及安裝配置詳解

  https://www.cnblogs.com/liwei0526vip/p/6370103.html數組

  2、LVS自動化添加及刪除ipvsadm和後端服務器健康狀態檢測腳本

  一、LVS director 負載均衡器增長IPVSADM腳本

#!/bin/bash
#chkconfig: - 88 66
#description: this script to add lvs IP

VIP=192.168.0.254
DIP=192.168.0.100
RIP1=192.168.0.101
RIP2=192.168.0.102
PORT=80
SCHELE=wrr
LOCKFILE=/var/lock/subsys/ipvsadm

case $1 in
start)
#增長vip地址
 /sbin/ifconfig eth0:0 $VIP broadcast $VIP netmask 255.255.255.255 up
 /sbin/route add -host $VIP dev eth0:0
#清除防火牆規則
 /sbin/iptables -F
 /sbin/iptables -X
 /sbin/iptables -Z
#開啓ip轉發功能
 echo 1 > /proc/sys/net/ipv4/ip_forward
#清除ipvsadm 規則
 /sbin/ipvsadm -C
#增長ipvsadm direcotor規則
 /sbin/ipvsadm -A -t $VIP:$PORT -s $SCHELE
#增長realserver 規則
 /sbin/ipvsadm -a -t $VIP:$PORT -r $RIP1 -g
 /sbin/ipvsadm -a -t $VIP:$PORT -r $RIP2 -g
#增長ipvsadm 鎖文件
 /bin/touch  $LOCKFILE
;;
stop)
 if [ ! -e $LOCKFILE ];then
  echo "the ipvsadm is stopped..."
 else
 #刪除vip地址
  /sbin/ifconfig eth0:0 down
 #關閉ip轉發
  echo 0 > /proc/sys/net/ipv4/ip_forward
 #清除ipvsadm 規則
  /sbin/ipvsadm -C
 #刪除鎖文件
  /bin/touch $LOCKFILE
 fi
;;
status)
 if [ ! -e $LOCKFILE ];then
  echo "the ipvsadm is stopped..."
 else
  echo "the ipvsadm is running..."
 fi
;;
*)
 echo "Usage;$0:{start|stop|status}"
;;
esac
director.sh

  二、LVS 增長 real server腳本

#!/bin/bash
#chkconfig: - 77 66
#description: this script to add real server 
#
VIP=192.168.0.254
case $1 in
start)
#arp_ignore: 定義接收到ARP請求時的響應級別;1表示僅在請求的目標地址配置請求到達的接口上的時候,纔給予響應
#arp_announce:定義將本身地址向外通告時的通告級別:2表示僅向與本地接口上地址匹配的網絡進行通告;
 echo 1 >/proc/sys/net/ipv4/conf/lo/arp_ignore
 echo 1 >/proc/sys/net/ipv4/conf/all/arp_ignore
 echo 2 >/proc/sys/net/ipv4/conf/lo/arp_announce
 echo 2 >/proc/sys/net/ipv4/conf/all/arp_announce
#增長VIP地址到lo:0接口,增長路由條目:目的地址爲VIP,由lo:0接口響應(即:源地址爲VIP做爲響應報文給客戶端)
 
 /sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up && /sbin/route add -host $VIP dev lo:0
 
#新建一個鎖文件,前面執行成功則創建鎖文件
 if [  $? -eq 0 ];then
  /bin/touch /var/lock/subsys/ipvsreal
 else
  echo "fail to add vip address and route."
 fi
;;
stop)
#恢復arp響應級別
 echo 0 >/proc/sys/net/ipv4/conf/lo/arp_ignore
 echo 0 >/proc/sys/net/ipv4/conf/all/arp_ignore
 echo 0 >/proc/sys/net/ipv4/conf/lo/arp_announce
 echo 0 >/proc/sys/net/ipv4/conf/all/arp_announce
#剔除VIP地址(路由地址自動刪掉)
  loip=`/sbin/ifconfig lo:0 |grep $VIP`
  if [ ‘$loip‘  == ‘‘ ];then
   echo "VIP address not found."
  else
   /sbin/ifconfig lo:0 down && rm -rf /var/lock/subsys/ipvsreal
   if [ $? -eq 0 ] ;then
    echo "VIP address had been deled."
   else
    echo "VIP address del failly."
    exit 1
   fi
  fi
;;
status)
 if [ ! -e /var/lock/subsys/ipvsreal ];then
  echo "LVS-DR real server stoped."
 else
  echo "LVS-DR real server is running."
 fi
;;
*)
 echo "Usage: $0 {start | stop |status}"
 exit 1
;;
esac
Realserver.sh

  三、RS健康狀態檢查腳本

#!/bin/bash
#chkconfig: - 88 77
#description: check health real server or not
#設置變量:VIP, director端口, 本地地址, real sever IP數組,狀態數組(1表示正常,0表示異常),權重數組,real server 服務地址,LVS 類型,狀態檢測次數,日誌
VIP=192.168.0.254
CPORT=80
FAIL_BACK=127.0.0.1
RS=("192.168.0.101" "192.168.0.102")
declare -a RSSTATUS
RW=("2" "1")
PPORT=80
TYPE=g
CHKLOOP=3
LOG=/var/log/ipvsmonitor.log
#當real server 恢復時,增長 real server 到 director 中
addrs(){
        ipvsadm -a -t $VIP:$CPORT -r $1:$PPORT -$TYPE -w $2
        [ $? -eq 0 ] && return 0 || return 1
}
#當real server 不可用時,刪除director中ipvsadm 條目
delrs(){
                ipvsadm -d -t $VIP:$CPORT -r $1:$PPORT
                [ $? -eq 0 ] && return 0 || return 1
}
#real server 狀態檢測 
checkrs(){
                local I=1
                while [ $I -le $CHKLOOP ];do
                        if curl --connect-timeout 1 http://$1 &>/dev/null;then
                                return 0
                        fi
                let I++
                done
                return 1
}
#初始化狀態數組,當ipvsadm 中條目有real server IP規則時 狀態爲1;當realserver 在ipvsadm 條目中缺失時 狀態爲0
initstatus(){
        local I
        local COUNT=0
         for I in ${RS[*]};do
                if ipvsadm -L -n |grep "$I:$PPORT" &>/dev/null;then
                        RSSTATUS[$COUNT]=1
                else
                        RSSTATUS[$COUNT]=0
                fi
        let COUNT++
        done
}
#調用初始化函數
initstatus
#定義死循環
while :;do
        let COUNT=0
        #逐個遍歷 real server
        for I in ${RS[*]};do
         #檢測real server 正常與否
                if checkrs $I ;then
                #當檢測到real 正常但ipvsadm中沒有real server 條目時增長 real server 條目
                        if [ ${RSSTATUS[$COUNT]} -eq 0 ];then
                                addrs $I ${RW[$COUNT]}
                                [ $? -eq 0 ]&& RSSTATUS[$COUNT]=1 && echo "`date +%F%H:%M:%S`, $I is back." >>$LOG
                        fi
                else
                 #當檢測到real server 異常時,刪除在 ipvsadm 中異常的realserver 條目
                        if [ ${RSSTATUS[$COUNT]} -eq 1 ];then
                                delrs $I
                                [ $? -eq 0 ]&& RSSTATUS[$COUNT]=0 && echo "`date +%F%H:%M:%S`, $I is gone." >>$LOG
                        fi
                fi       
        let COUNT++
        done
        
#每一個五秒鐘遍歷一次        
sleep 5
done
check_health.sh

  3、LVS + Keepalived 實現高可用、負載均衡 Web 集羣

  http://www.javashuo.com/article/p-qrzerrsv-m.htmlbash

相關文章
相關標籤/搜索