LVS+Heartbeat+ldirectord构建高可用负载均衡群集(DR模式)

时间:2021-08-04 22:25:47

 

表1.1IP地址参数设置  

 名称 角色 IP地址
Load Balancer (RHEL5) 均衡器 (LVS-Master) eth0:0(Vip):192.168.5.198 eth0:192.168.5.196 eth1:10.0.0.1 (心跳接口)
                                                                                                
                                                                                                
 Load Balancer (RHEL5) 均衡器 (LVS-Slave) eth0:0(Vip):192.168.5.198 eth0:192.168.5.197 eth1:10.0.0.2 (心跳接口)
                                                                                                
                                                                                                
RealServer1 (RHEL5) 服务器群集节点1 (Web服务器) lo:0(Vip):192.168.5.198 eth0:192.168.5.191
                                                                                  
RealServer2 (RHEL5) 服务器群集节点2 (Web服务器) lo:0(Vip):192.168.5.198 eth0:192.168.5.192
                                          

   实验步骤: 一、Load Balancer1(LVS-Master)配置 1) 设置网卡IP地址  [root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0# Advanced Micro Devices [AMD] 79c970 [PCnet32 LANCE]DEVICE=eth0BOOTPROTO=staticHWADDR=00:0C:29:4C:BD:42ONBOOT=yesIPADDR=192.168.1.196NETMASK=255.255.255.0GATEWAY=192.168.1.1[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth1# Advanced Micro Devices [AMD] 79c970 [PCnet32 LANCE]DEVICE=eth1BOOTPROTO=staticONBOOT=yesHWADDR=00:0c:29:4c:bd:4cIPADDR=192.168.2.1NETMASK=255.255.255.0GATEWAY=192.168.2.1[root@localhost ~]# service network restart关闭环回接口:                                             [确定]弹出环回接口:                                             [确定]弹出界面 eth0:                                            [确定]弹出界面 eth1:                                            [确定] 2) 更改主机名为:LVS-Master [root@localhost ~]# vim /etc/sysconfig/network //永久性修改,需重启机器后生效NETWORKING=yesNETWORKING_IPV6=noHOSTNAME=LVS-Master[root@localhost ~]# hostname LVS-Master //临时性修改,主机名及时生效。这项必须改 3) 更改/etc/hosts文件 [root@localhost ~]# vi /etc/hosts //ip地址与主机名映射添加如下两行192.168.1.196   LVS-Master192.168.1.197   LVS-Slave 4)安装heartbeat所需软件 A、检查perl-libwww-perl-*软件包是否已安装 [root@localhost ~]# rpm -qa |grep perl-libwww-perlperl-libwww-perl-5.805-1.1.1 B、安装ipvsadm管理器 [root@localhost ~]# mount /dev/cdrom /media/mount: block device /dev/cdrom is write-protected, mounting read-only[root@localhost ~]# cd /media/Cluster[root@localhost Cluster]# rpm -ivh ipvsadm-1.24-10.i386.rpm C、编译安装libnet //这里所用的软件被放到了/root目录下了 [root@localhost ~]# lsanaconda-ks.cfg install.log         libnet-0.10.11.tar.gzDesktop          install.log.syslog soft[root@localhost ~]# tar zxvf libnet-0.10.11.tar.gz[root@localhost ~]# ls //解压到/root下anaconda-ks.cfg install.log         libnet                 softDesktop          install.log.syslog libnet-0.10.11.tar.gz[root@localhost ~]# cd libnet[root@localhost libnet]# ./configure ;make ;make install D、编译安装MailTools [root@localhost libnet]# cd -/root[root@localhost ~]# tar zxf MailTools-2.04.tar.gz -C /usr/src/[root@localhost ~]# cd /usr/src/[root@localhost src]# lsdebug kernels MailTools-2.04 redhat vmware-tools-distrib[root@localhost src]# cd MailTools-2.04/[root@localhost MailTools-2.04]# perl Makefile.PL ;make ;make install E、编译安装Hearbeat [root@localhost src]# cd[root@localhost ~]# tar jxvf Heartbeat-STABLE-2-1-STABLE-2.1.4.tar.bz2 -C /usr/src/[root@localhost ~]# cd /usr/src/Heartbeat-STABLE-2-1-STABLE-2.1.4/[root@localhost Heartbeat-STABLE-2-1-STABLE-2.1.4]# groupadd haclient[root@localhost Heartbeat-STABLE-2-1-STABLE-2.1.4]# useradd hacluster -g haclient -s /sbin/nologin –M//添加hearbeat运行所需的程序组账户:haclient组,程序用户:haclusterHeartbeat编译前需要使用其自带的ConfigureMe脚本进行配置[root@localhost Heartbeat-STABLE-2-1-STABLE-2.1.4]# ./ConfigureMe configure[root@localhost Heartbeat-STABLE-2-1-STABLE-2.1.4]# ./ConfigureMe make[root@localhost Heartbeat-STABLE-2-1-STABLE-2.1.4]# ./ConfigureMe installF、将heartbeat设置成开机在2、3、4、5级别下自启动[root@localhost Heartbeat-STABLE-2-1-STABLE-2.1.4]# chkconfig --add heartbeat[root@localhost Heartbeat-STABLE-2-1-STABLE-2.1.4]# chkconfig heartbeat on 4) 配置ldirectord故障检测及管理 ldirectord的配置文件ldirectord.cf的配置文件复制到/etc/ha.d目录下[root@localhost Heartbeat-STABLE-2-1-STABLE-2.1.4]# mkdir /etc/ha.d[root@localhost Heartbeat-STABLE-2-1-STABLE-2.1.4]# cp ./ldirectord/ldirectord.cf /etc/ha.d/ldirectord.cf[root@localhost Heartbeat-STABLE-2-1-STABLE-2.1.4]# cd /etc/ha.d/[root@localhost ha.d]# lsldirectord.cf[root@localhost ha.d]# vim ldirectord.cfchecktimeout=3checkinterval=1fallback=127.0.0.1:80autoreload=yeslogfile="/var/log/ldirectord.log"logfile="local0"quiescent=yesvirtual=192.168.1.198:80real=192.168.1.191:80 gatereal=192.168.1.192:80 gatefallback=127.0.0.1:80 gateservice=httpscheduler=rrprotocol=tcpchecktype=negotiate echeckport=80 5) 设定heartbeat配置文档 Heartbeat的配置文件有三个:ha.cf、haresources、authkeys。这三个配置文件需要放置 /etc/ha.d目录下,但是此目录下默认没有这三个文件,所以要复制样本文件。 [root@localhost ~]# cd Heartbeat-STABLE-2-1-STABLE-2.1.4[root@localhostHeartbeat-STABLE-2-1-STABLE-2.1.4]#cp ./doc/ha.cf ./doc/haresources ./doc/authkeys /etc/ha.d/[root@localhost Heartbeat-STABLE-2-1-STABLE-2.1.4]# cd /etc/ha.d/[root@localhost ha.d]# lsauthkeys ha.cf haresources ldirectord.cf[root@localhost ha.d]# vi ha.cfdebugfile /var/log/ha-debug //日志文件存储位置logfile /var/log/ha-log //其他非调试信息的写入位置logfacility     local0 //写入日志的日志级别,默认为local0keepalive 2 //心跳间隔时间,默认为2sdeadtime 30 //主机死亡时间,表示将在停止响应 30 秒之后启动故障转移,辅助服务器接替主服务器的工作warntime 10 //在日志中发出最后心跳”late heartbeat” 前的警告时间设定initdead 120 //节点重启后所花的时间udpport 694   //upd的默认端口为694cast   eth1            # Linux   //心跳检测所使用的网卡auto_failback on //设置当主服务器从故障恢复后,是否自动从辅助服务器中切换回来,还是手动切换过来node    LVS-Master //节点名称,与uname –n保持一致node    LVS-Slaverespawn root /usr/lib/heartbeat/ipfailapiauth ipfail gid=haclient uid=hacluster 6) 编辑haresources文件,定义heartbeat启动设定VIP,启动ldirectord服务 [root@localhost ha.d]# vim haresources在最后添加如下行LVS-Master IPaddr::192.168.1.198/32 ldirectord::ldirectord.cf 其中,“LVS-Master”是主节点名称,“192.168.5.198/32”是VIP,“ldirectord::ldirectord.cf”指定ldirectord使用ldirectord.cf配置文件。 编辑authkeys文件,指定使用认证的方式。 [root@localhost ha.d]# vim authkeys找到如下行,修改即可auth 11 crc 考虑到安全性,authkeys文件的权限必须为600 [root@localhost ha.d]# chmod 600 authkeys 最后重启heartbeat服务 [root@localhost ~]#service heartbeat restart 二、Load Balancer2LVS-Slave)配置 1)设置网卡IP地址 [root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0# Advanced Micro Devices [AMD] 79c970 [PCnet32 LANCE]DEVICE=eth0BOOTPROTO=staticHWADDR=00:0C:29:AB:B0:02ONBOOT=yesIPADDR=192.168.1.197NETMASK=255.255.255.0GATEWAY=192.168.1.1[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth1# Advanced Micro Devices [AMD] 79c970 [PCnet32 LANCE]DEVICE=eth1BOOTPROTO=staticONBOOT=yesHWADDR=00:0c:29:ab:b0:0cIPADDR=192.168.2.2NETMASK=255.255.255.0GATEWAY=192.168.2.1[root@localhost ~]# service network restart关闭环回接口:                                             [确定]弹出环回接口:                                             [确定]弹出界面 eth0:                                            [确定]弹出界面 eth1:                                            [确定] 2) 更改主机名为:LVS-Slave [root@localhost ~]# vim /etc/sysconfig/network //永久性修改,需重启机器后生效NETWORKING=yesNETWORKING_IPV6=noHOSTNAME=LVS-Slave[root@localhost ~]# hostname LVS-Slave //临时性修改,主机名及时生效 3) 更改/etc/hosts文件 [root@localhost ~]# vi /etc/hosts //ip地址与主机名映射添加如下两行192.168.1.196   LVS-Master192.168.1.197   LVS-Slave 4)安装heartbeat所需软件 A、检查perl-libwww-perl-*软件包是否已安装 [root@localhost ~]# rpm -qa |grep perl-libwww-perlperl-libwww-perl-5.805-1.1.1 B、安装ipvsadm管理器 [root@localhost ~]# mount /dev/cdrom /media/mount: block device /dev/cdrom is write-protected, mounting read-only[root@localhost ~]# cd /media/Cluster[root@localhost Cluster]# rpm -ivh ipvsadm-1.24-10.i386.rpm C、编译安装libnet //这里所用的软件被放到了/root目录下了 [root@localhost ~]# lsanaconda-ks.cfg install.log         libnet-0.10.11.tar.gzDesktop          install.log.syslog soft[root@localhost ~]# tar zxvf libnet-0.10.11.tar.gz[root@localhost ~]# ls //解压到/root下anaconda-ks.cfg install.log         libnet                 softDesktop          install.log.syslog libnet-0.10.11.tar.gz[root@localhost ~]# cd libnet[root@localhost libnet]# ./configure ;make ;make install D、编译安装MailTools [root@localhost libnet]# cd -/root[root@localhost ~]# tar zxf MailTools-2.04.tar.gz -C /usr/src/[root@localhost ~]# cd /usr/src/[root@localhost src]# lsdebug kernels MailTools-2.04 redhat vmware-tools-distrib[root@localhost src]# cd MailTools-2.04/[root@localhost MailTools-2.04]# perl Makefile.PL ;make ;make install E、编译安装Hearbeat [root@localhost src]# cd[root@localhost ~]# tar jxvf Heartbeat-STABLE-2-1-STABLE-2.1.4.tar.bz2 -C /usr/src/[root@localhost ~]# cd /usr/src/Heartbeat-STABLE-2-1-STABLE-2.1.4/[root@localhost Heartbeat-STABLE-2-1-STABLE-2.1.4]# groupadd haclient[root@localhost Heartbeat-STABLE-2-1-STABLE-2.1.4]# useradd hacluster -g haclient -s /sbin/nologin –M//添加hearbeat运行所需的程序组账户:haclient组,程序用户:haclusterHeartbeat编译前需要使用其自带的ConfigureMe脚本进行配置[root@localhost Heartbeat-STABLE-2-1-STABLE-2.1.4]# ./ConfigureMe configure[root@localhost Heartbeat-STABLE-2-1-STABLE-2.1.4]# ./ConfigureMe make[root@localhost Heartbeat-STABLE-2-1-STABLE-2.1.4]# ./ConfigureMe installF、将heartbeat设置成开机在2、3、4、5级别下自启动[root@localhost Heartbeat-STABLE-2-1-STABLE-2.1.4]# chkconfig --add heartbeat[root@localhost Heartbeat-STABLE-2-1-STABLE-2.1.4]# chkconfig heartbeat on 4)备份节点上的配置文件内容要求与主节点服务器中的保持一致,所以可直接从主节点服 务器(LVS-Master)上复制heartbeat的三个配置文件(ha.cf、haresources、authkeys),ldirectord的配置文件(ldirectord.cf) 在LVS-Slave上创建目录ha.d  [root@localhost ~]# mkdir /etc/ha.d 在LVS-Master上scp /etc/ha.d/*root@192.168.1.197:/etc/ha.d [root@localhost ~]# scp /etc/ha.d/* root@192.168.1.197:/etc/ha.dThe authenticity of host '192.168.1.197 (192.168.1.197)' can't be established.RSA key fingerprint is 11:4a:6c:17:64:76:e5:e4:f6:53:c1:79:31:8b:da:10.Are you sure you want to continue connecting (yes/no)? yesWarning: Permanently added '192.168.1.197' (RSA) to the list of known hosts.root@192.168.1.197's password:authkeys                                 100% 643     0.6KB/s   00:00    ha.cf                                    100%   10KB 10.3KB/s   00:00    haresources                              100% 5967     5.8KB/s   00:00    ldirectord.cf                            100% 7541     7.4KB/s   00:00   4) 配置ldirectord故障检测及管理 ldirectord的配置文件ldirectord.cf的配置文件复制到/etc/ha.d目录下[root@localhost Heartbeat-STABLE-2-1-STABLE-2.1.4]# mkdir /etc/ha.d[root@localhost Heartbeat-STABLE-2-1-STABLE-2.1.4]# cp ./ldirectord/ldirectord.cf /etc/ha.d/ldirectord.cf[root@localhost Heartbeat-STABLE-2-1-STABLE-2.1.4]# cd /etc/ha.d/[root@localhost ha.d]# lsldirectord.cf[root@localhost ha.d]# vim ldirectord.cfchecktimeout=3checkinterval=1fallback=127.0.0.1:80autoreload=yeslogfile="/var/log/ldirectord.log"logfile="local0"quiescent=yesvirtual=192.168.1.198:80real=192.168.1.191:80 gatereal=192.168.1.192:80 gatefallback=127.0.0.1:80 gateservice=httpscheduler=rrprotocol=tcpchecktype=negotiate echeckport=80 5) 设定heartbeat配置文档 Heartbeat的配置文件有三个:ha.cf、haresources、authkeys。这三个配置文件需要放置 /etc/ha.d目录下,但是此目录下默认没有这三个文件,所以要复制样本文件。 [root@localhost ~]# cd Heartbeat-STABLE-2-1-STABLE-2.1.4[root@localhostHeartbeat-STABLE-2-1-STABLE-2.1.4]#cp ./doc/ha.cf ./doc/haresources ./doc/authkeys /etc/ha.d/[root@localhost Heartbeat-STABLE-2-1-STABLE-2.1.4]# cd /etc/ha.d/[root@localhost ha.d]# lsauthkeys ha.cf haresources ldirectord.cf[root@localhost ha.d]# vi ha.cfdebugfile /var/log/ha-debug //日志文件存储位置logfile /var/log/ha-log //其他非调试信息的写入位置logfacility     local0 //写入日志的日志级别,默认为local0keepalive 2 //心跳间隔时间,默认为2sdeadtime 30 //主机死亡时间,表示将在停止响应 30 秒之后启动故障转移,辅助服务器接替主服务器的工作warntime 10 //在日志中发出最后心跳”late heartbeat” 前的警告时间设定initdead 120 //节点重启后所花的时间udpport 694   //upd的默认端口为694cast   eth1            # Linux   //心跳检测所使用的网卡auto_failback on //设置当主服务器从故障恢复后,是否自动从辅助服务器中切换回来,还是手动切换过来node    LVS-Master //节点名称,与uname –n保持一致node    LVS-Slaverespawn root /usr/lib/heartbeat/ipfailapiauth ipfail gid=haclient uid=hacluster 6) 编辑haresources文件,定义heartbeat启动设定VIP,启动ldirectord服务 [root@localhost ha.d]# vim haresources在最后添加如下行LVS-Slave IPaddr::192.168.1.198/32 ldirectord::ldirectord.cf 其中,“LVS-Master”是主节点名称,“192.168.5.198/32”是VIP,“ldirectord::ldirectord.cf”指定ldirectord使用ldirectord.cf配置文件。 编辑authkeys文件,指定使用认证的方式。 [root@localhost ha.d]# vim authkeys找到如下行,修改即可auth 11 crc 考虑到安全性,authkeys文件的权限必须为600 [root@localhost ha.d]# chmod 600 authkeys 最后重启heartbeat服务 [root@localhost ~]#service heartbeat restart 三、Realserver服务器上的配置 提供服务的RealServer1和RealServer2不需要安装任何IPVS软件,只需要设置在开机后自动执行以下脚本: 1)在RealServer1上的设置  [root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0# Advanced Micro Devices [AMD] 79c970 [PCnet32 LANCE]DEVICE=eth0BOOTPROTO=staticHWADDR=00:0C:29:26:16:64ONBOOT=yesIPADDR=192.168.1.191NETMASK=255.255.255.0GATEWAY=192.168.1.1 编写lvs-dr脚本 [root@localhost ~]# vi /opt/lvs-dr#!/bin/bashvip="192.168.1.198"/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_ignoreecho "2" > /proc/sys/net/ipv4/conf/lo/arp_announceecho "1" > /proc/sys/net/ipv4/conf/all/arp_ignoreecho "2" > /proc/sys/net/ipv4/conf/all/arp_announce[root@localhost ~]# chmod +x /opt/lvs-dr //给予执行权限[root@localhost ~]# echo "/opt/lvs-dr" >>/etc/rc.local //让脚本lvs-dr随机开机启动[root@localhost ~]# /opt/lvs-dr //执行lvs-dr脚本[root@localhost ~]# mount /dev/cdrom /media/ //挂载光盘,安装YUMmount: block device /dev/cdrom is write-protected, mounting read-only[root@localhost ~]# vim /etc/yum.repos.d/rhel-debuginfo.repo[rhel-debuginfo]name=Red Hat Enterprise Linux $releasever - $basearch - Debugbaseurl=file:///media/Serverenabled=1gpgcheck=0[root@localhost ~]# yum -y install http* //安装apache[root@localhost ~]# echo "111" > /var/www/html/index.html //在RealServer1添加测试页[root@localhost ~]# service httpd restart //重启web服务器 2)在RealServer1上的设置 [root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0# Advanced Micro Devices [AMD] 79c970 [PCnet32 LANCE]DEVICE=eth0BOOTPROTO=staticHWADDR=00:0C:29:E8:EE:83ONBOOT=yesIPADDR=192.168.1.192NETMASK=255.255.255.0GATEWAY=192.168.1.1 编写lvs-dr脚本 [root@localhost ~]# vi /opt/lvs-dr#!/bin/bashvip="192.168.1.198"/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_ignoreecho "2" > /proc/sys/net/ipv4/conf/lo/arp_announceecho "1" > /proc/sys/net/ipv4/conf/all/arp_ignoreecho "2" > /proc/sys/net/ipv4/conf/all/arp_announce[root@localhost ~]# chmod +x /opt/lvs-dr //给予执行权限[root@localhost ~]# echo "/opt/lvs-dr" >>/etc/rc.local //让脚本lvs-dr随机开机启动[root@localhost ~]# /opt/lvs-dr //执行lvs-dr脚本[root@localhost ~]# mount /dev/cdrom /media/ //挂载光盘,安装YUMmount: block device /dev/cdrom is write-protected, mounting read-only[root@localhost ~]# vim /etc/yum.repos.d/rhel-debuginfo.repo[rhel-debuginfo]name=Red Hat Enterprise Linux $releasever - $basearch - Debugbaseurl=file:///media/Serverenabled=1gpgcheck=0[root@localhost ~]# yum -y install http* //安装apache[root@localhost ~]# echo "111" > /var/www/html/index.html //在RealServer1添加测试页[root@localhost ~]# service httpd restart //重启web服务器 四、测试 1)启动heartbeat服务 分别在主、副均衡服务器上启动heartbeat服务,启动命令为: [root@LVS-Slave ~]# service heartbeat start 2)确定realserver1和realserver2服务器的httpd服务启动,页面能正常访问 3)在两台客户机上分别打开浏览器,输入http://192.168.1.198,将分别看到不同的页面