#!/bin/bash # Author:Lin yao hong #LVS scripts . /etc/init.d/functions Netdev=ens33 Gw=10.10.10.1 VIP=10.10.10.12 SUBNET="${Netdev}:`echo $VIP | awk -F "." '{print $4}'`" PORT=80 RIP=( 10.10.10.20 10.10.10.21 ) function start(){ if [ `ifconfig | grep $VIP | wc -l` -ne 0 ];then stop fi #ifconfig $SUBNET $VIP broadcast $VIP netmask 255.255.255.0 up ifconfig $SUBNET $VIP/24 up route add -host ${VIP} dev ${Netdev} ipvsadm -C ipvsadm --set 30 5 60 ipvsadm -A -t $VIP:$PORT -s wrr -p 20 for ((i=0;i<${#RIP[*]};i++)) do ipvsadm -a -t $VIP:$PORT -r ${RIP[$i]} -m -w 1 done } function stop(){ arping -c 1 -I ${Netdev} -s ${VIP} ${Gw} ipvsadm -C if [ `ifconfig | grep $VIP | wc -l` -ne 0 ];then ifconfig $SUBNET down fi route del -host $VIP dev ${Netdev} &>/dev/null } case "$1" in start) start action "ipvs is started" /bin/true ;; stop) stop action "ipvs is stopped" /bin/true ;; restart) stop action "ipvs is stopped" /bin/true start action "ipvs is started" /bin/true ;; *) echo "USAGE:$0 {start | stop | restart}" esac