二、环境描述
负载均衡器:eth0 192.168.152.139
VIP:192.168.152.250
web节点IP:192.168.152.151 192.168.152.152
三、负载均衡器安装配置
1)安装
#yum -y install ipvsadm
#lsmod | grep ip_vs 查看没有输出
#modprobe ip_vs 安装即可
2)配置,这里也是个写脚本为了方便vim /etc/init.d/lvsdr
#lvs of DR
VIP=192.168.152.250
RIP1=192.168.152.151
RIP2=192.168.152.152
case "$1" in
start)
echo "start lvs of DR"
/sbin/ifconfig eth0:0 $VIP broadcast $VIP netmask 255.255.255.0 up
echo "1" > /proc/sys/net/ipv4/ip_forward
/sbin/iptables -C
/sbin/ipvsadm -A -t $VIP:80 -s rr
/sbin/ipvsadm -a -t $VIP:80 -r $RIP1:80 -g
/sbin/ipvsadm -a -t $VIP:80 -r $RIP2:80 -g
/sbin/ipvsadm
;;
stop)
echo "stop lvs of DR"
echo "0" > /proc/sys/net/ipv4/ip_forward
/sbin/ipvsadm -C
/sbin/ifconfig eth0:0 down
;;
*)
echo "Usage :$0 {start|stop}"
exit1
esac
#chmod o+x /etc/init.d/lvsdr
#service lvsdr start
三、节点服务器配置
这里我们也都可以写成脚本,这里我就举web1了,web2也是一样的。
#vim /etc/init.d/lvsdrrip
#!/bin/bash
#DR server
VIP=192.168.152.250
case "$1" in
start)
echo "start LVS of DR"
/sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up
/sbin/route add -host $VIP dev lo:0
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
;;
stop)
echo "stop LVS of DR"
/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
;;
*)
echo "Usage:$0 {start|stop}"
exit 1
注:DR模式VIP必须跟服务器同一网段
echo"1";echo"2" 是为了抑制arp广播作用
#chmod o+x /etc/init.d/lvsdr
#service lvsdrrip start
四、验证
在负载均衡器上我们查看下
# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.152.250:80 rr
-> 192.168.152.152:80 Route 1 0 1
-> 192.168.152.151:80 Route 1 0 1
下面我们访问一下看看:
这里我们可以看到试验成功了。
我们最后在看下节点服务器的信息,看多出了什么:
# ifconfig
eth0 Link encap:Ethernet HWaddr 00:0C:29:6E:B2:0F
inet addr:192.168.152.151 Bcast:192.168.152.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe6e:b20f/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:4696 errors:0 dropped:0 overruns:0 frame:0
TX packets:3535 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:447778 (437.2 KiB) TX bytes:470257 (459.2 KiB)
Interrupt:177 Base address:0x1424
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:3746 errors:0 dropped:0 overruns:0 frame:0
TX packets:3746 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:868823 (848.4 KiB) TX bytes:868823 (848.4 KiB)
lo:0 Link encap:Local Loopback
inet addr:192.168.152.250 Mask:255.255.255.255
UP LOOPBACK RUNNING MTU:16436 Metric:1