1、Dr 安装 ipvsadm
# yum -y install ipvsadm
# lsmod | grep ip_vs #检查ipvs模块是否加载进系统。把ipvs模块加载进系统,需要我们执行ipvsadm命令才会加载进去,或者modprobe ip_vs。
# modprobe ip_vs #加载ipvs模块
# lsmod | grep ip_vs
ip_vs 125220 0
# echo 1 > /proc/sys/net/ipv4/ip_forward #开启路由转发
2、Dr 配置VIP地址
# ifconfig eth0:0 VIP/24
# route add -host VIP dev eth0
3、配置LVS、添加Real server节点
# ipvsadm -C #清空原有配置
# ipvsadm -A -t VIP:80 -s rr -p 20 #指定VIP
# ipvsadm -a -t VIP:80 -r RS1:80 -g -w 1 #rs1节点
# ipvsadm -a -t VIP:80 -r RS2:80 -g -w 1 #rs2节点
# ipvsadm -L -n #查看lvs 配置
脚本
# vim /usr/local/sbin/ipvs
#!/bin/bash
PORT=80
VIP=192.168.119.150
RIP=(192.168.119.134
192.168.119.135
)
start(){
ifconfig eth0:0 $VIP/24 up
route add -host $VIP dev eth0
ipvsadm -C
ipvsadm -A -t $VIP:$PORT -s rr -p 20for ((i=0;i<${#RIP[*]};i++))do
ipvsadm -a -t $VIP:$PORT -r ${RIP[$i]}:$PORT -g -w 1
done
}
stop(){
ipvsadm -C
ifconfig eth0:0 down
route del -host $VIP dev eth0
}case "$1" in
start)
start
echo "ipvs is start"
;;
stop)
stop
echo "ipvs is stop"
;;
restart)
stop
echo "ipvs is stop"
start
echo "ipvs is start"
;;
*)
echo "usage:$0 {start|stop|restart}"
esac
4、Rs 配置
添加lo端口的VIP
# ifconfig lo:0 192.168.119.150/32 up
# route add -host 192.168.119.150 dev lo
ARP抑制
# 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_announce
# echo "2">/proc/sys/net/ipv4/conf/all/arp_announce
脚本
#!/bin/bash
VIP=192.168.119.150
PORT=80
case "$1" in
start)
ifconfig eth0:0 $VIP/32 up
route add -host $VIP dev eth0
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_announce
echo "2">/proc/sys/net/ipv4/conf/all/arp_announce
echo "start LVS of REALServer IP"
;;
stop)
ifconfig eth0:0 $VIP/32 down
route del -host $VIP dev eth0
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_announce
echo "0">/proc/sys/net/ipv4/conf/all/arp_announce
echo "stop LVS OF REALServer"
;;*)
echo "Usage: $0 {start|stop}"
esac