SERVER端:
#!/bin/bash
#create by littelbear 17:59 2017/12/4
VIP=(
192.168.202.29
192.168.202.30
) ##定义VIP
RS=(
192.168.202.103
192.168.202.104
) ##RS端的IP
i=0
start() {
for ip in ${VIP[@]}
do
echo $ip
ifconfig eno16777736:$i $ip up ##给网卡添加VIP
((i++)) ##i加一
ipvsadm -A -t $ip:80 -s rr ##给LVS添加VIP并指定端口和算法(rr轮询)
route add -host $ip dev eno16777736 ##添加VIP的网关
for rs in ${RS[@]}
do
ipvsadm -a -t $ip:80 -r $rs:80 -g ##将RIP与VIP连接起来,并指定LVS的模式
echo $rs
done
ipvsadm --set 30 5 60 ##指定连接时间
done
}
stop() {
for ip in ${VIP[@]}
do
route del -host $ip dev eno16777736 ##关闭网关
done
ipvsadm -C
}
case "$1" in
start)
start
;;
stop)
stop
;;
*)
echo "USAGE:$0 {start|stop}"
;;
esac
CLIENT端:
#!/bin/bash
VIP=(
192.168.202.29
192.168.202.30
)
i=29
for vip in ${VIP[@]}
do
ifconfig lo:$i ${vip}/32 netmask 255.255.255.255 up ##给lo网卡添加VIP,注意子网掩码
((i++))
route add -host $vip eno16777736 ##添加网关
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
##抑制ARP
done