分类: 系统运维
2008-11-04 14:42:32
Heartbeat+lvs负载均衡
测试环境:
硬件 两台dell1950 (单硬盘)
系统 FC7
软件 ipvsadm-1.24 heartbeat-
说明 一台172.16.3.75 (lvsdr)作为主director服务器
172.16.3.73 (lvsdrbak)作为热备director服务器
虚拟ip为172.16.3.76
采用基于DR的模式进行负载均衡
算法采用的是轮叫调度(Round-Robin Scheduling) 即ipvsadm –rr模式
注1:(本次测试为节约机器,将备份节点也作为一台realserver)
注2:算法模式一共有7种分别是
加权轮叫调度(Weighted Round-Robin Scheduling)
最小连接调度(Least-Connection Scheduling)
加权最小连接调度(Weighted Least-Connection Scheduling)
基于局部性的最少链接(Locality-Based Least Connections Scheduling)
带复制的基于局部性最少链接(Locality-Based Least Connections with Replication Scheduling)
目标地址散列调度(Destination Hashing Scheduling)
源地址散列调度(Source Hashing Scheduling)
只需要改变ipvsadm后面的参数
ipvsadm -rr 论叫调度
rr|wrr|lc|wlc|lblc|lblcr|dh|sh|sed|nq
一,配置基于隧道(DR)模式的LVS集群
所需软件 ipvsadm-1.24
需要建立一个软链接把内核指向/usr/src/linux
ln -s /usr/src/kernels/
tar zxvf ipvsadm-1.24.tar.gz
cd ipvsadm-
make;make install
1)在lvsdr上部署(备份节点做完全一样的部署)
$ vi /etc/rc.d/init.d/lvsDR
#!/bin/bash
VIP=172.16.3.76
RIP1=172.16.3.73
RIP2=172.16.3.235
. /etc/rc.d/init.d/functions
case "$1" in
start)
echo "start LVS of DirectorServer"
#Set the Virtual IP Address
/sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up
/sbin/route add -host $VIP dev lo:0
#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
#end
;;
stop)
echo "close LVS Directorserver"
/sbin/ipvsadm -C
;;
*)
echo "Usage: $0 {start|stop}"
exit 1
esac
2)在realserver上部署
$ vi /etc/rc.d/init.d/realserverDR
#!/bin/sh
VIP=172.16.3.76
/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
~
二:安装heartbeat
所需软件
e2fsprogs-
libnet
heartbeat-
安装软件
安装e2fsprogs
rpm -ivh e2fsprogs-1.35-7.1.i386.rpm
安装libnet
tar zxvf libnet.tar.gz
cd libnet
./configure
make;make install
安装heartbeat
tar zxvf heartbeat-
cd heartbeat-
./Configureme configure
make;make install
cp doc/ha.cf doc/haresources doc/authkeys /etc/ha.d/
cp ldirectord/ldirectord.cf /etc/ha.d/
groupadd -g 694 haclient
useradd -u 694 -g haclient hacluster
修改配置
重点三个配置文件
ha.cf haresources authkeys
1, ha.cf
[root@lvsdrbak src]# awk '/^[^$]/&&/^[^#]/' /etc/ha.d/ha.cf
debugfile /var/log/ha-debug
logfile /var/log/ha-log
logfacility local0
keepalive 2
deadtime 30
warntime 10
initdead 120
udpport 694
bcast eth0 # Linux
auto_failback on
node lvsdr
node lvsdrbak
hopfudge 1
2, haresources
这个文件其实就只有一行
lvsdr IPaddr::172.16.3.76 ldirectord lvsDR
3.authkeys
auth 1
1 sha1 key-for-sha1-any-text-you-want
chmod -R 600 autykeys 权限必须是600
以上三个文件 主从服务器上一致
修改hosts
$ vi /etc/hosts
127.0.0.1 localhost.localdomain localhost
172.16.3.75 lvsdr
172.16.3.73 lvsdrbak
其中
三:配置ldirectord
Ldirectord的作用是监测Real Server,当Real Server失效时,把它从Load Balancer列表中删除,恢复时重新添加,在安装heartbeat时已经安装了Ldirectord。
但是在fc7中启动ldirectord会报缺少Mail/Send.pm或者LWP/useragant模块,需要安装perl模块
所需模块
lwp/useagant
mailsend
生成makefile:
perl Makefile.PL
建立模块
make
测试模块
make test
如果测试结果报告“all test ok”,您就可以放心地安装编译好的模块了。安装模块前,先要
make install
配置 ldirectord 配置文件在/etc/ha.d/ldirectord.cf
[root@lvsdr lvs]# awk '/^[^$]/&&/^[^#]/' /etc/ha.d/ldirectord.cf
checktimeout=3
checkinterval=1
autoreload=yes
logfile="/var/log/ldirectord.log"
quiescent=yes
virtual=172.16.3.76:80
real=172.16.3.73:80 gate
real=172.16.3.235:80 gate
service=http
request="test.html"
receive="Test Page"
scheduler=rr
protocol=tcp
#checktype=negotiate
#checkport=80
#request="index.html"
#receive="Test Page"
#virtualhost=测试 ldirectord是否可以起来
现在可以在主节点172.16.3.75启动heartbeat
/etc/init.d/heartbeat start
起来正常的结果应该是这样
[root@lvsdr src]# netstat -npl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 :::22 :::* LISTEN 2095/sshd
udp 0 0
udp 0 0
[root@lvsdr src]# ifconfig
eth0 Link encap:Ethernet HWaddr 00:15:C5:EF:E0:D9
inet addr:172.16.3.75 Bcast:172.16.3.255 Mask:255.255.255.0
inet6 addr: fe80::215:c5ff:feef:e0d9/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:58138 errors:0 dropped:0 overruns:0 frame:0
TX packets:17891 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:8955674 (8.5 MiB) TX bytes:3217775 (3.0 MiB)
Interrupt:16 Memory:f8000000-f8012100
eth0:0 Link encap:Ethernet HWaddr 00:15:C5:EF:E0:D9
inet addr:172.16.3.76 Bcast:172.16.3.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
Interrupt:16 Memory:f8000000-f8012100
eth0:1 Link encap:Ethernet HWaddr 00:15:C5:EF:E0:D9
inet addr:
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
Interrupt:16 Memory:f8000000-f8012100
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:2493 errors:0 dropped:0 overruns:0 frame:0
TX packets:2493 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:1339237 (1.2 MiB) TX bytes:1339237 (1.2 MiB)
lo:0 Link encap:Local Loopback
inet addr:172.16.3.76 Mask:255.255.255.255
UP LOOPBACK RUNNING MTU:16436 Metric:1
测试:关闭主节点,备份节点将自动接管directorserver服务。
四,把备份节点也作为realserver
1. 在上述DR节点做的以外,备份节点还需要也添加realserver脚本
另外需要添加关闭备用节点的脚本
[root@lvsdr lvs]# vi /etc/rc.d/init.d/closeDR
VIP=172.16.3.76
. /etc/rc.d/init.d/functions
case "$1" in
start)
echo "start director server and close dr"
ifconfig lo:0 down
echo 1 > /proc/sys/net/ipv4/ip_forward
echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce
;;
stop)
echo "start Real Server"
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: lvs {start|stop}"
exit 1
esac
这个一个关闭备份节点的脚本,可以放到heartbeat里面去,在启动备用节点的时候自动关闭备份节点脚本
2, 修改备份节点的haresources
只需添加一项,
[root@lvsdrbak lvs]# awk '/^[^$]/&&/^[^#]/' /etc/ha.d/haresources
lvsdr closedr IPaddr::172.16.3.76 ldirectord lvsDR
确定closedr脚本在 启动lvsDR之前就可以了。
五,测试
director服务器是 172.16.3.75
热备机是172.16.3.73
虚拟ip是172.16.3.76
realserver为 172.16.3.73 172.16.3.235
在主DR上启动服务
/etc/rc.d/init.d/heartbeat start
在ie里面输入可以得到这两个页面
172.16.3.235下面有个index1.html 显示172.16.3.235
172.16.3.73下面有个index2.html 显示172.16.3.73
可以通过/index1.html
/index2.html
访问
拔掉172.16.3.75的网线
服务器切到172.16.3.73上。也正常显示