Load Balancing Lvs--shell script configuration LVs

Source: Internet
Author: User

This article mainly uses three scripts to implement the LVS configuration. Script 1:lvs.sh is configured on the load Balancer side, and script 2:realserver.sh is configured on the backend real server server. Script 3:check_lvs.sh is a script used to test the health of the backend server on the load Balancer side, if a real server outage is automatically cleared, if the recovery is added, the backend all servers are down, the load balancer will provide the error page to the cluster.


Planning:

Load Balancing:

dip:192.168.1.11

vip:192.168.1.10

Background real server has two of the following:

rs1:192.168.1.9

rs2:192.168.1.8


Install the IPVSADM and httpd services on 192.168.1.11 and provide the error page when the backend server is down.

Yum Install Ipvsadm-y

Install the HTTPD service separately on the Rs1,rs2, and start the service to provide the webpage.

Yum Install Httpd-y


Write scripts on the 192.168.1.11 server lvs.sh

Cd/opt/scripts

Vim lvs.sh

#!/bin/bash## lvs script for vs/dr# chkconfig: - 90 10#. /etc/ rc.d/init.d/functions# vip=192.168.1.10dip=192.168.1.11rip1=192.168.1.9rip2=192.168.1.8port=80rsweight1= 2rsweight2=5 case  "$"  in    start)          /sbin/ifconfig eth0:0  $VIP  broadcast  $VIP  netmask 255.255.255.255  up          #启用eth0: vip      configuration    /sbin/route add -host  $VIP  dev eth0:0          #添加VIP路由信息          echo 1 > / proc/sys/net/ipv4/ip_forward         #打开ip转发功能           /sbin/iptables -f        /sbin/ iptables -z         #清空iptables规则                  /sbin/ipvsadm -c        /sbin /ipvsadm -a -t  $VIP: 80 -s wlc        /sbin/ ipvsadm -a -t  $VIP: $PORT  -r  $RIP 1: $PORT  -g -w  $RSWEIGHT 1         /sbin/ipvsadm -a -t  $VIP: $PORT  -r  $RIP 2: $PORT  -g -w  $RSWEIGHT 2                  #添加ipvs规则                  /bin/touch /var/lock/subsys/ipvsadm &> /dev/null          #创建锁文件     ;;      stop)         echo 0 > /proc/sys/net/ipv4/ip_forward          /sbin/ipvsadm -C        /sbin/ifconfig eth0:0  down        /sbin/route del  $VIP           /bin/rm -f /var/lock/subsys/ipvsadm          echo  "ipvs is stopped ..."     ;;      status)         if [ ! -e  /var/lock/subsys/ipvsadm ]; then             echo  "ipvsadm is stopped&nbsp ..."          else            echo  "ipvs is  RUNNING&NBSP: "            ipvsadm -l -n         fi    ;;      *)         echo  "usge: $0  {Start|stop|status} "    ;; Esac

This script can be added to the list of system services and can be set to boot automatically.


Scripting realserver.sh in Back-end servers Rs1 and RS2 are as follows:

#!/bin/bash## script to start lvs dr real server.# chkconfig: -  90 10# description: lvs dr real server#.  /etc/rc.d/init.d/ functionsvip=192.168.1.10host= '/bin/hostname ' case  "$"  in    start)          /sbin/ifconfig lo down         /sbin/ifconfig lo up        echo 1 >  /proc/sys/net/ipv4/conf/lo/arp_ignore        echo 2  > /proc/sys/net/ipv4/conf/lo/arp_announce        echo 1  > /proc/sys/net/ipv4/conf/all/arp_ignore        echo  2 > /proc/sys/net/ipv4/conf/all/arp_announce         /sbin/ifconfig lo:0 $VIP  broadcast  $VIP  netmask 255.255.255.255 up         /sbin/route add -host  $VIP  dev lo:0    ;;     stop)         /sbin/ifconfig lo:0  Down        echo 0 > /proc/sys/net/ipv4/conf/lo/arp_ ignore        echo 0 > /proc/sys/net/ipv4/conf/lo/ Arp_announce        echo 0 > /proc/sys/net/ipv4/conf /all/arp_ignore        echo 0 > /proc/sys/net/ipv4/ conf/all/arp_announce    ;;     status)         islothere= '/sbin/ifconfig  lo:0 | grep  $VIP '         isrothere= ' netstat -rn | grep  "lo:0"  | grep  $VIP '          if [ !  "$islothere"  -o !  "Isrothere"  ];then             # Either the route or the  Lo:0 device            # not found.             echo  "lvs-dr real  server stopped. "         else             echo  "lvs-dr real server running."         fi    ;;     *)         echo  "$0: usage: $0  {start|status|stop} "        exit 1    ;; Esac


You can start the script when you are done.


LVS itself does not have a health status detection function on the backend server, the following on the 192.168.1.11 server to use the script every 5s detect the background server health status, and realize the automatic removal of downtime server, after recovery can be automatically added.


Vim check_lvs.sh

#!/bin/bash# #VIP =192.168.19.211cport=80fail_back=127.0.0.1rs= ("192.168.19.245"   "192.168.19.219")  # Define an array and assign a value declare -a rsstatus  #定义一个空数组RW = ("2"   "1") rport=80type=g  #定义为DR模型CHKLOOP =3log= /var/log/ipvsmonitor.log  #定义一个添加规则的函数  addrs ()  {    if ipvsadm -l  -n | grep  "$: $RPORT"  &> /dev/null;then         return 0    else         ipvsadm -a -t  $VIP: $CPORT  -r $1: $RPORT  -$TYPE  -w $2         [ $? -eq 0 ] && return 0 | |  return 1    fi}  #定义删除规则的函数  delrs ()  {    if  ipvsadm -L -n | grep  "$: $RPORT"  &> /dev/null;then         ipvsadm -d -t  $VIP: $CPORT  -r $1: $RPORT          [ $? -eq 0 ] && return 0 | |  return 1    else        return 0     fi}  #定义是否要添加错误页面的规则的函数  ifaddls ()  {    if [  ${rsstatus[0]} -eq 0 ];then        if [  ${RSSTATUS[1]} -eq 0 ];then             if ipvsadm -L -n | grep  "127.0.0.1:80"  &> /dev/null ; Then                echo   "' date  ' +%f %t '  All RS is Down and Local web is  Up " >>  $LOG     &Nbsp;       else                 ipvsadm -a -t  $VIP: $CPORT  -r 127.0.0.1:80 -$ Type                [ $?  -eq 0 ]; echo  "' date  ' +%f %t '  All RS is Down!  Local 127.0.0.1:80 is up!!! "  >>  $LOG             fi         else             if ipvsadm -L -n | grep  "127.0.0.1:80"  &> /dev/null;then                 ipvsadm - d -t  $VIP: $CPORT  -r 127.0.0.1:80            fi        fi     else        if ipvsadm -l -n | grep   "127.0.0.1:80"  &> /dev/null;then             ipvsadm -d -t  $VIP: $CPORT  -r 127.0.0.1:80         fi    fi}  #定义检测后端服务器服务健康状态的函数   checkrs ()  {     local I=1    while [  $I  -le  $CHKLOOP  ];d o        if curl --connect-timeout 1 http ://$1 &> /dev/null; then             return 0        fi         let I++    DONE    RETURN 1} #检测脚本初始化函数   initstatus ()  {     local i    local count=0    for i  in ${rs[*]};d o        if ipvsadm -l -n  | grep  "$I: $RPORT"  && > /dev/null; then             rsstatus[$COUNT]=1         else            rsstatus[$COUNT]=0         fi        let COUNT++     done} #脚本开始执行: initstatusifaddls while :; do  #无限循环      let count=0    for i in ${rs[*]};d o         if checkrs  $I;  then            if [  ${rsstatus[$COUNT]} -eq 0 ];then                 addrs  $I  ${rw[$COUNT]}                 [ $? -eq 0 ] &&  rsstatus[$COUNT]=1 && echo  "' date  ' +%f %t ',  $I  is back."  >>  $LOG             fi         else             if [ ${rsstatus[$COUNT]} -eq 1 ]; then                 delrs  $I                  [ $? -eq 0 ] && rsstatus[$COUNT] =0 && echo  "' date  ' +%f %t ',  $I  is gone."  >>  $LOG             fi         fi        let COUNT++     done    ifaddls    sleep 5  #睡眠5s再循环done







This article from the "Linux Learning" blog, declined reprint!

Load Balancing Lvs--shell script configuration LVs

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.