环境描述:本文在配置LVS时使用三台linux,虚拟VIP:192.168.0.254
一台做Directorserver(192.168.0.1)
两台做realserver(192.168.0.2,192.168.0.3)
Load Balancer: 192.168.0.1
Real Server 1: 192.168.0.2
Real Server 2: 192.168.0.3
软件
ipvsadm-1.24.tar.gz:
tar zxvf ipvsadm-1.24.tar.gz
cd ipvsadm-1.24
make && make install
注意在make时可能会出现很多错误的信息,请按照如下操作就可以心编译正常
ln -s /usr/src/kernels/2.6.11-1.1369_FC4-i686/ /usr/src/linux
在进行编译就不会出错了
或者yum安装
yum -y install ipvsadm*
======================================
配置基于DR模式Lvs集群
192.168.0.1
[[email protected] lvs]# cat /usr/local/lvs/LvsDR
#!/bin/sh
VIP=192.168.0.254
RIP1=192.168.0.2
RIP2=192.168.0.3
/etc/rc.d/init.d/functions
case "$1" in
start)
echo "start LVS of DirectorServer"
#Set the Virtual IP Address
/sbin/ifconfig eth0:1 $VIP broadcast $VIP netmask 255.255.255.255 up
/sbin/route add -host $VIP dev eth0:1
#Clear IPVS Table
/sbin/ipvsadm -C
#Set Lvs
/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
#Run Lvs
/sbin/ipvsadm
;;
stop)
echo "close LVS Directorserver"
/sbin/ipvsadm -C
;;
*)
echo "Usage: $0 {start|stop}"
exit 1
esac
=======================================
192.168.0.2
[[email protected] lvs]# cat /usr/local/lvs/realserver
#!/bin/sh
VIP=192.168.0.254
/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
================================================
192.168.0.3
[[email protected] lvs]# cat /usr/local/lvs/realserver
#!/bin/sh
VIP=192.168.0.254
/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
==================================
[[email protected] lvs]# cat /usr/local/apache/htdocs/index.html
This is realserver1
[[email protected] lvs]# cat /usr/local/apache/htdocs/index.html
This is realserver2
分别启动服务
./LvsDR start
./realserver
./realserver
启动apache
访问:
http://192.168.0.254
验证的时候可以人为造成负载过高,这样就从一台跳转到另一台
find / -name aaaaaaaaa
不断刷新会出现This is realserver1 This is realserver2
xf后记 :
如果启动后未产生/etc/sysconfig/ipvsadm文件
可执行
ipvsadm --save > /etc/sysconfig/ipvsadm 保存配置,这样每次重启lvs服务就不需要执行/usr/local/lvs/LvsDR 脚本了。(因为/etc/init.d/ipvsadm脚本中默认读取/etc/sysconfig/ipvsadm 配置)
查看负载状态
ipvsadm -L -n
重启
service ipvsadm restart