LVS 自开发脚本

lvs启动脚本服务端

 

chkconfig --add lvs

chmod +x /etc/init.d/lvs

 

[root@lvs ~]# cat /etc/init.d/lvs

#!/bin/bash

# chkconfig: 2345 90 60

# description: lvs server start and stopscripts

# hexudong

 

 

. /etc/init.d/functions

 

VIP=(

 192.168.1.16

)

 

RS=(

 192.168.1.20

 192.168.1.21

)

 

dev=eth0

arp_ip=192.168.1.253

 

lvs_start(){

 ipvsadm -C

 ipvsadm --set 30 5 60

  for((i=0;i<${#VIP[*]};i++))

  do

   count=`ip addr|grep ${VIP[i]}|wc -l`

    [$count -eq 0 ]&&ip addr add ${VIP[i]}/24 dev $dev

   RETVAL1=$?

   /sbin/arping -I $dev -c 3 -s ${VIP[i]} $arp_ip >/dev/null 2>&1

   ipvsadm -A -t ${VIP[i]}:80 -s rr -p20

   for ((n=0;n<${#RS[*]};n++))

   do

     ipvsadm -a -t ${VIP[i]}:80 -r ${RS[n]} -g -w 1

   done 

 done

 ((RETVAL=$RETVAL1+$?))

 sleep 2

  if[ $RETVAL -eq 0 ]

   then

     action "The lvs_server is start" /bin/true

 else

     action "The lvs_server is start" /bin/false

  fi

}

 

lvs_stop(){

 ipvsadm -C

  for((i=0;i<${#VIP[*]};i++))

  do

   count=`ip addr|grep ${VIP[i]}|wc -l`

    [$count -ne 0 ]&&ip addr del ${VIP[i]}/24 dev $dev

   RETVAL1=$?

 done

 ((RETVAL=$RETVAL1+$?))

  sleep2

  if[ $RETVAL -eq 0 ]

   then

     action "The lvs_server is stop" /bin/true

 else

     action "The lvs_server is stop" /bin/false

  fi

}

 

USAGE(){

 echo "USAGE:$0{start|stop|restart}"

}

 

[ $# -ne 1 ]&&{

 USAGE

 exit 2

}

 

case "$1" in

 start)

      lvs_start

      exit $RETVAL

      ;;

 stop)

      lvs_stop

      exit $RETVAL

      ;;

 restart)

      lvs_stop

      lvs_start

      exit $RETVAL

      ;;

  *)

      USAGE

      exit 3

esac

lvs客户端

[root@web1 ~]# cat /etc/init.d/lvs_client

#!/bin/bash

# chkconfig: 2345 90 60

# description: lvs client start and stopscripts

# hexudong

 

. /etc/init.d/functions

 

VIP=(

 192.168.1.16

)

 

lvs_start(){

  for((i=0;i<${#VIP[*]};i++))

  do

   count=`ip addr|grep ${VIP[i]}|wc -l`

    [$count -eq 0 ]&&ip addr add ${VIP[i]}/32 dev lo

 done

 RETVAL=$?

 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

 sleep 1

  if[ $RETVAL -eq 0 ]

   then

     action "The lvs_client is start" /bin/true

 else

     action "The lvs_client is start" /bin/false

  fi

}

 

lvs_stop(){

  for((i=0;i<${#VIP[*]};i++))

  do

   count=`ip addr|grep ${VIP[i]}|wc -l`

    [$count -ne 0 ]&&ip addr del ${VIP[i]}/32 dev lo

 done

 RETVAL=$?

 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

 sleep 1

  if[ $RETVAL -eq 0 ]

   then

     action "The lvs_client is stop" /bin/true

 else

     action "The lvs_client is stop" /bin/false

  fi

}

 

USAGE(){

 echo "USAGE:$0{start|stop|restart}"

}

 

[ $# -ne 1 ]&&{

 USAGE

  exit2

}

 

case "$1" in

 start)

      lvs_start

      exit $RETVAL

      ;;

 stop)

      lvs_stop

      exit $RETVAL

      ;;

 restart)

      lvs_stop

      lvs_start

      exit $RETVAL

      ;;

  *)

      USAGE

      exit 3

esac

web RS的健康检查

[root@web2 scripts]# cat lvs_rs_check.sh

#!/bin/bash

# hexudong

 

VIP=(

 192.168.1.16

)

 

RS=(

 192.168.1.20

 192.168.1.21

)

 

web_file=/data0/check/index.html

 

html(){

  cat>$web_file<<EOF

<html>

<head>

<title>rs nodedetection</title>

<meta http-equiv="refresh"content="1">

</head>

<body >

<center><atarget=_blank><font size=8 color="0000ff"><b>RSCheck</b></font></a><br></center>

<hr>

<div><p>rs checklist</p></div>

<table width="400"height="200"  border=1align=center>

  <tr size=300>

        <td bgcolor="Cyan"width="200">RS</td>

       <td bgcolor="Cyan" width="150">STAT</td>

  </tr>

EOF

 

  for((i=0;i<${#RS[*]};i++))

  do

   echo "   <trsize=300>" >>$web_file

   echo "        <tdbgcolor=\"#00CC33\" >${RS[i]}</td>" >>$web_file

    echo "        <td id=${RS[i]}bgcolor=\"#00CC33\" >OK</td>" >>$web_file

   echo "   </tr>">>$web_file

 done

 

  cat>>$web_file<<EOF

</table>

</body>

<html>

EOF

}

 

lvs_add(){

  for((n=0;n<${#VIP[*]};n++))

  do

   if [ `ipvsadm -L -n|grep ${RS[i]}|wc -l` -ne ${#VIP[*]} ];then

      ipvsadm -a -t ${VIP[n]}:80 -r ${RS[i]} -g -w 1

   fi

 done

}

 

lvs_del(){

  for((n=0;n<${#VIP[*]};n++))

  do

   if [ `ipvsadm -L -n|grep ${RS[i]}|wc -l` -ne 0 ];then

      ipvsadm -d -t ${VIP[n]}:80 -r ${RS[i]}

   fi

  done

}

 

 

check_rs(){

  for((i=0;i<${#RS[*]};i++))

  do

   num=`curl -I -s -w "%{http_code}" -o /dev/null ${RS[i]}`

   if [ $num -ne 200 ]

     then

       sed -i /'<td id='${RS[i]}' bgcolor'/s@.*@'        <td id='${RS[i]}'bgcolor="#FF0000" >BAD</td>'@g $web_file

       lvs_del

   else

       sed -i /'<td id='${RS[i]}' bgcolor'/s@.*@'        <td id='${RS[i]}'bgcolor="#00CC33" >OK</td>'@g $web_file

       lvs_add

   fi 

 done

}

 

main(){

 html

 while true;do

   check_rs

   sleep 1

 done

}

 

main

lvs 备机-对主机进行健康检查

备机先不要开健康检查,等待主机挂掉,备机增加VIP,并管理RS

 

[root@lvs scripts]# cat lvs_check.sh

#!/bin/bash

# hexudong

 

 

VIP=(

 192.168.1.16

)

RS=(

 192.168.1.21

 192.168.1.20

)

dev=eth0

lvs_server=192.168.1.14

arp_ip=192.168.235.253

 

lvs_add(){

 ipvsadm -C

 ipvsadm --set 30 5 60

  for((i=0;i<${#VIP[*]};i++))

  do

   count=`ip addr|grep ${VIP[i]}|wc -l`

    [$count -eq 0 ]&&ip addr add ${VIP[i]}/24 dev $dev

   /sbin/arping -I $dev -c 3 -s ${VIP[i]} $arp_ip >/dev/null 2>&1

   ipvsadm -A -t ${VIP[i]}:80 -s wrr -p20

   for ((n=0;n<${#RS[*]};n++))

   do

     ipvsadm -a -t ${VIP[i]}:80 -r ${RS[n]} -g -w 1

   done

 done

}

 

lvs_del(){

 ipvsadm -C

  for((i=0;i<${#VIP[*]};i++))

  do

   count=`ip addr|grep ${VIP[i]}|wc -l`

    [$count -ne 0 ]&&ip addr del ${VIP[i]}/24 dev $dev

 done

}

 

check_lvs(){

 ping -c2 $lvs_server >>/dev/null 2>&1

 RETVAL=$?

  if[ $RETVAL -ne 0 ]

   then

     [ `ipvsadm -L -n|grep "TCP"|wc -l` -eq 0 ]&& \

     lvs_add

 else

     [ `ipvsadm -L -n|grep "TCP"|wc -l` -ne 0 ]&& \

     lvs_del

  fi

}

 

main(){

 while true;do

   check_lvs

   sleep 1

 done

}

 

main


本文出自 “晴空” 博客,谢绝转载!

你可能感兴趣的:(LVS)