lvs简介可以参考:http://blog.csdn.net/harbor1981/article/details/1358925
架构图:
所需软件:
ipvsadm-1.24-10.x86_64.rpm
heartbeat-2.1.3-3.el5.centos.x86_64.rpm
heartbeat-pils-2.1.3-3.el5.centos.x86_64.rpm
heartbeat-stonith-2.1.3-3.el5.centos.x86_64.rpm
pyxml-0.8.4-4.x86_64.rpm
系统环境:centos 5.9-64bit
192.168.137.14 虚拟ip
192.168.137.134 lvsmaster lvs主
192.168.137.135 lvsbackup lvs从
192.168.137.130 realserver1 web服务器
192.168.137.131 realserver2 web服务器
1、lvs安装
yum -y install ipvsadmyum -y install libnetyum -y install e2fsprogsyum -y install heartbeat
yum -y install heartbeat-devel
yum -y install heartbeat-ldirectord
echo "192.168.137.135 lvsmaster">>/etc/hosts
echo "192.168.137.135 lvsbackup">>/etc/hosts
2、配置步骤主
lvs配置
(1)lvsdr-basic脚本配置
[root@lvsmaster ~]#cat /etc/init.d/lvsdr-basic#!/bin/sh
vip=192.168.137.14
rip1=192.168.137.130
rip2=192.168.137.131
/etc/rc.d/init.d/functions
case "$1" in
start)
echo "start [ok]"
/sbin/ifconfig eth0:1 $vip broadcast $vip netmask 255.255.255.255 up
/sbin/route add -host $vip dev eth0:1
echo "1" > /proc/sys/net/ipv4/ip_forward
/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 "stop [ok]"
/sbin/ipvsadm -c
;;
*)
echo "usage:$0 {start|stop}"
exit 1
esac
[root@lvsmaster ~]#chmod u x /etc/init.d/lvsdr-basic
(2)ha.cf配置
[root@lvsmaster ~]#cat /etc/ha.d/ha.cf
debugfile /var/log/ha-debu
logfile /var/log/ha-log
logfacility local0
keepalive 2
warntime 10
deadtime 30
initdead 120
hopfudge 1
udpport 694
bcast eth0
ucast eth0 192.168.137.135
ipauto_failback on
node lvsmaster
node lvsbackup
ping 192.168.137.1
respawn root /usr/lib/heartbeat/ipfail
apiauth ipfail gid=root uid=root
(3)authkeys认证文件的配置
[root@lvsmaster ~]# cat /etc/ha.d/authkeys
auth 3
3 md5 9bf2f23aae3a63c16ea681295ffd7666[root@lvsmaster ~]#chmod u x 600 authkeys
(4)ldirectord.cf文件配置
[root@lvsmaster ~]# cat /etc/ha.d/ldirectord.cf
checktimeout=3
checkinterval=1
autoreload=no
logfile="/var/log/ldirectord.log"
quiescent=no
virtual=192.168.137.14:80
real=192.168.137.130:80 gate
real=192.168.137.131:80 gate
service=http
request="test.html"
receive="test"
scheduler=sh
protocol=tcp
(5)haresources文件配置[root@lvsmaster ~]# cat /etc/ha.d/haresources
lvsmaster ipaddr::192.168.137.14/24/eth0 ldirectord lvsdr-basic
(6)关闭ldirectord服务开启heartbeat服务。
[root@lvsmaster ~]# chkconfig --del ldirectord[root@lvsmaster ~]#
chkconfig --level 2345 ldirectord off[root@lvsmaster ~]# chkconfig
--level 2345 heartbeat on
备份lvs配置(1)switchdr配置
[root@lvsbackup ~]#cat /etc/init.d/switchdr#!/bin/sh
# description: close lo0 and arp_ignore
vip=192.168.137.14
/etc/rc.d/init.d/functions
case "$1" in
start)
echo "************* start director server and close tunl ***********"
/sbin/ifconfig lo:0 down
echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce
;;
stop)
echo "start real server"
/sbin/ifconfig eth0:0 down
/sbin/ifconfig lo:0 $vip netmask 255.255.255.255 broadcast $vip 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
;;
*)
echo "usage: switchdr {start|stop}"
exit 1
esac
[root@lvsbackup ~]#chmod u x /etc/init.d/switchdr
(2)ha.cf配置
[root@lvsbackup ~]#cat /etc/ha.d/ha.cf
debugfile /var/log/ha-debu
logfile /var/log/ha-log
logfacility local0
keepalive 2
warntime 10
deadtime 30
initdead 120
hopfudge 1
udpport 694
bcast eth0
ucast eth0 192.168.137.134
auto_failback on
node lvsmaster
node lvsbackup
ping 192.168.137.1
respawn root /usr/lib/heartbeat/ipfail
apiauth ipfail gid=root uid=root
(3)authkeys 配置
[root@lvsbackup ~]# cat /etc/ha.d/authkeys
auth 3
3 md5 9bf2f23aae3a63c16ea681295ffd7666[root@lvsbackup ~]#chmod 600 authkeys
(4) ldirectord.cf配置
[root@lvsbackup ~]# cat /etc/ha.d/ldirectord.cf
checktimeout=3
checkinterval=1
autoreload=no
logfile="/var/log/ldirectord.log"
quiescent=no
virtual=192.168.137.14:80
real=192.168.137.130:80 gate
real=192.168.137.131:80 gate
service=http
request="test.html"
receive="test"
scheduler=sh
protocol=tcp
(5)配置haresources
[root@lvsbackup ~]# cat /etc/ha.d/haresources
lvsmaster ipaddr::192.168.137.14/24/eth0 ldirectord switchdr
(6)关闭ldirectord服务开启heartbeat服务。
[root@lvsbackup ~]# chkconfig --del ldirectord[root@lvsbackup ~]#
chkconfig --level 2345 ldirectord off[root@lvsbackup ~]# chkconfig
--level 2345 heartbeat on
3、客户端配置ip 192.168.137.131 192.168.137.130
#cat /etc/init.d/realserver#!/bin/bash
#description : start realserver
vip=192.168.137.14
/sbin/ifconfig lo:0 $vip broadcast $vip netmask 255.255.255.255 up
/sbin/route add -host $vip dev lo:0echo "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#chmod u x /etc/init.d/realserver执行
# /etc/init.d/realserver
查看vip虚拟ip是否已经绑定:
# ifconfig lo:0
lo:0 link encap:local loopback
inet addr:192.168.137.14 mask:255.255.255.255
up loopback running mtu:16436 metric:1
4、启动服务
lvs主
/etc/init.d/heartbeat start
lvs备份
/etc/ini.d/switchdr stop
/etc/init.d/heartbeat start
测试(1)停掉主备份会接替主,当主恢复时备份自动转为备份状态。
(2)停止一台apache真实服务器。ipvsadm将会自动把这一台机器剔除。
得出结论:不断刷新192.168.137.14,会轮流显示realserver1 web和realserver2 web页面