RedHat下直接路由模式的负载均衡
首先:直接路由模式的负载均衡的工作原理:客户机访问director机,director机将客户端的请求发送到真实服务器,然后真实服务器将请求应回应的结果直接回应给客户机。这种方式减轻director机的压力,效率高,一般适用于大中型企业。
实验环境: 两台主机服务器:192.168.0.11 和 192.168.0.12
中转机: 192.168.0.13 和 192.168.0.19
客户机:192.168.0.10
(主机名要跟IP保存一致。)
实验步骤:
A.服务器:
1。192.168.0.11
# yum install -y httpd
# service httpd start
# echo "192.168.0.11" >/var/www/html/index.html
# route add default gw 192.168.0.13
# vim /bin/shutarp
-------------------------------
#!/bin/bash
VIP=192.168.0.19
/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
sysctl -p
-------------------------------
# shutarp
2。192.168.0.12
# yum install -y httpd
# service httpd start
# echo "192.168.0.12" >/var/www/html/index.html
# route add default gw 192.168.0.13
# vim /bin/shutarp
-------------------------------
#!/bin/bash
VIP=192.168.0.19
/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
sysctl -p
-------------------------------
# shutarp
B.中转机:
1。安装 ipvs服务:
# yum clean all
# yum install -y ipvsadm
2。打开ip转发功能,关闭其他服务,设置中转
# vim /etc/init.d/lvsDR
---------------------------------
#/bin/bash
VIP=192.168.0.19
RIP1=192.168.0.11
RIP2=192.168.0.12
GW=192.168.0.254
. /etc/rc.d/init.d/functions
case "$1" in
start)
echo "start LVS of DS"
/sbin/ifconfig eth0:0 $VIP broadcast $VIP netmask 255.255.255.0 up
/sbin/route add -host $VIP dev eth0:0
/sbin/ipvsadm -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 "close LVS Directorserver"
/sbin/ipvsadm -C
;;
*)
echo "Usage: $0 {start|stop}"
exit 1
esac
---------------------------------
# service lvsDR start (执行脚本)
# vim /bin/shutarp
-------------------------------
#!/bin/bash
VIP=192.168.0.19
/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
sysctl -p
-------------------------------
# shutarp
C。客户端:
在浏览器中打开: http://192.168.0.19/ (重复刷新,看显示的页面:如果是不同页面说明实验成功!)