- 论坛徽章:
- 0
|
基于DR方式的LVS高可用负载均衡(with HeartBeat)
2008-11-22 TsengYia#126.com
################################################################
系统环境:RHEL5 [ 2.6.18-8.el5xen ]
软件环境:
◇ RHEL5系统光盘自带的rpm包(多数默认已有,带“”标记的可能需要额外安装):
ipvsadm-1.24-8.1.i386
perl-5.8.8-10
mod-perl-2.0.2-6.1
perl-libwww-perl-5.805-1.1.1
perl-Compress-Zlib-1.42-1.fc6
perl-HTML-Parser-3.55-1.fc6
perl-HTML-Tagset-3.10-2.1.1
perl-URI-1.35-3
perl-Net-SSLeay-1.30-4.fc6
perl-Net-DNS-0.59-1.fc6
perl-Net-IP-1.25-2.fc6
◇ 下载的软件包(保存到/dl_pkgs/目录):
HeartBeat(负载分配及心跳热备管理)
http://hg.linux-ha.org/lha-2.1/archive/STABLE-2.1.4.tar.bz2
Perl-MailTools模块
http://www.sfr-fresh.com/cgi-bin/warex?unix/misc/MailTools-2.04.tar.bz2
libnet库
ftp://195.220.108.108/linux/dag/redhat/el5/en/i386/dag/RPMS/libnet-1.1.2.1-2.2.el5.rf.i386.rpm
##########################################################################
![]()
使用heartbeat的心跳设置监测服务器双方的状态。
心跳的监测线路可以使用直连网线(如:H1-eth0 192.168.168.1, H2-eth0 192.168.168.2),也可以直接使用现有网络线路(eth0 ucast单播)。
一、配置主节点H1
1、修改IP地址、主机名映射及proc参数
1) 修改IP地址
eth0的IP地址为173.17.17.11,——配置过程略。
2) 修改主机名及hosts文件
shell> vi /etc/sysconfig/network
NETWORKING=yes
NETWORKING_IPV6=no
HOSTNAME=h1.example.com
shell> vi /etc/hosts
173.17.17.11 h1.example.com h1
shell> hostname h1.example.com
3) 修改proc参数
shell> vi /etc/sysctl.conf
net.ipv4.ip_forward = 1
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.eth0.send_redirects = 0
net.ipv4.conf.eth1.send_redirects = 0
shell> sysctl -p
2、安装ipvsadm及libnet、MailTools工具
shell> mount /dev/cdrom /media/cdrom #//挂载RHEL5安装光盘
shell> cd /media/cdrom/Server/
shell> rpm -ivh ../Cluster/ipvsadm-1.24-8.1.i386.rpm
shell> rpm -ivh perl-libwww-perl-5.805-1.1.1.noarch.rpm perl-Net-DNS-0.59-1.fc6.i386.rpm perl-Net-IP-1.25-2.fc6.noarch.rpm
shell> cd /dl_pkgs/
shell> rpm -ivh libnet-1.1.2.1-2.2.el5.rf.i386.rpm
shell> tar jxvf MailTools-2.04.tar.bz2
shell> cd MailTools-2.04
shell> perl Makefile.PL
shell> make
shell> make install
3、安装HeartBeat工具(源码包已包含ldirectord)
shell> groupadd haclient
shell> useradd -M -s /sbin/nologin -g haclient hacluster
shell> cd /dl_pkgs/
shell> tar jxvf Heartbeat-STABLE-2-1-STABLE-2.1.4.tar.bz2
shell> cd Heartbeat-STABLE-2-1-STABLE-2.1.4/
shell> ./ConfigureMe configure
shell> make && make install
shell> cp doc/authkeys doc/ha.cf doc/haresources /etc/ha.d/ #//复制相关配置文件
shell> cp ldirectord/ldirectord.cf /etc/ha.d/
4、配置heartbeat
1) 修改authkeys文件
shell> vi /etc/ha.d/authkeys
auth 2
2 sha1 HI!
shell> chmod 600 /etc/ha.d/authkeys
2) 配置ha.cf
shell> vi /etc/ha.d/ha.cf
logfile /var/log/ha-log #//ha日志文件位置
logfacility local0
keepalive 5 #//心跳频率间隔(秒)
deadtime 30 #//确认对方已当机的超时间隔
warntime 10
initdead 120 #//主机初始化(重启)时的当机超时间隔
udpport 694
#bcast eth1
ucast eth0 173.17.17.12
auto_failback on
watchdog /dev/watchdog #//启用该行可以使当heartbeat服务当掉以后重新启动系统,调试阶段可以先注释掉该行。
node h1.example.com h2.example.com #//热备双方的完整主机名,要与“uname -n”命令的结果保持一致
ping 173.17.17.254 #//通过ping网关检视自身状态
respawn hacluster /usr.lib/heartbeat/ipfail
apiauth ipfail gid=haclient uid=hacluster
use_logd no
3) 配置haresources
shell> vi /etc/ha.d/haresources
h1.example.com 173.17.17.1 ldirectord::ldirectord.cf #//使用主节点的主机名、VirtIP地址,加载ldirectord脚本(带配置文件)
4) 配置ldirectord.conf
shell> vi /etc/ha.d/ldirectord.cf
checktimeout=3
checkinterval=1
autoreload=yes
logfile="/var/log/ldirectord.log"
quiescent=ues
virtual=173.17.17.1:80 #//每个服务可以对应一个virtual段配置,如21、25等
real=173.17.17.17:80 gate
real=173.17.17.18:80 gate
real=173.17.17.19:80 gate
real=173.17.17.20:80 gate
fallback=127.0.0.1:80 gate
service=http
virtualhost=hw1.example.com
scheduler=rr #//指定使用哪种负载均衡分配算法,可以使用“ipvsadm --help”,了解“schedule”下的各种算法方案信息。
# persistent=60 #//连接保持超时
protocol=tcp
checktype=negotiate
checkport=80
#//配置完毕可以执行service ldirectord start,看是否能正常运行,根据提示进行故障排除。
#//跟踪日志文件/var/log/ldirectord.lpg 排除错误
7、启动heartbeat
shell> chkconfig --level 35 heartbeat on
shell> service heartbeat start
#//跟踪日志文件/var/log/ha-log 排除错误
二、配置备用节点H2
1、修改IP地址、主机名映射及proc参数
eth0的IP地址为173.17.17.12,主机名为 h2.example.com 。
参考主节点H1中的安装过程。
2、安装ipvsadm及libnet、MailTools工具
参考主节点H1中的安装过程。
3、安装HeartBeat工具(源码包已包含ldirectord)
参考主节点H1中的安装过程。
为了节省再次编译的时间,可以直接使用H1主机中编译的代码,例如:
shell>
shell> scp -r h1.example.com:/dl_pkgs/Heartbeat-STABLE-2-1-STABLE-2.1.4/ /dl_pkgs/
shell> cd /dl_pkgs/Heartbeat-STABLE-2-1-STABLE-2.1.4/
shell> make install
4、配置heartbeat
shell> groupadd haclient
shell> useradd -M -s /sbin/nologin -g haclient hacluster
配置文件和H1完全一致,可以直接使用H1主机中的配置文件。
shell> scp -r h1.example.com:/etc/ha.d/ /etc/
shell> service heartbeat start
shell> chkconfig --level 2345
三、配置各Real Server节点(以Web服务为例)
配置多台主机提供httpd服务,各服务器保持镜像同步(过程略)。
----测试阶段可以使用不同内容的web服务,以查看效果。
1、修改proc参数
shell> vi /etc/sysctl.conf
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
shell> sysctl -p
2、添加虚拟地址及路由记录
shell> vi /opt/add_vip.sh
#!/bin/bash
ifconfig lo:0 173.17.17.1 netmask 255.255.255.255 up
route add -host 173.17.17.1 dev lo:0
shell> chmod +x /opt/add_vip.sh
shell> /opt/add_vip.sh
shell> echo "/opt/add_vip.sh" >> /etc/rc.local
四、验证LVS及Heartbeat
1、在H1服务器上执行“ipvsadm -L -n”命令,
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 173.17.17.1:http rr
-> 173.17.17.17:80 Masq 1 0 0
-> 173.17.17.18:80 Masq 1 0 0
-> 173.17.17.19:80 Masq 1 0 0
-> 173.17.17.20:80 Masq 1 0 0
2、在H1服务器上执行“ifconfig eth0:0”查看是否有IP地址为173.17.17.1的eth0:0接口
3、使用外部客户端浏览器多次访问
http://173.17.17.1
查看各台真实web服务器的访问日志
查看H1主机中的/var/log/ldirectord.log日志
4、拔掉H1主机eth0的网线,等待一段时间,观察H1 eth0:0是否停掉,H2 eth0:0启用,对比H1、H2主机的ha日志,访问
http://173.17.17.1
看是否仍然可用。
……
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/40349/showart_1657702.html |
|