kaiser_cn 发表于 2019-1-7 12:43:54

RHEL5搭建apache服务器全过程(四)配置heartbeat和ipvsadm

  首先配置公网yum源
  导入key:
   #rpm --import http://ftp.sjtu.edu.cn/centos/5/os/i386/RPM-GPG-KEY-CentOS-5
   #vim /etc/yum.repos.d/yum.repo(注意此目录中只允许一个repo文件生效)

name=CentOS-5 - Base
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever5&arch=$basearch&

repo=os
#baseurl=http://mirror.centos.org/centos/$releasever/os/$basearch/

baseurl=http://ftp.sjtu.edu.cn/centos/5/os/$basearch/
gpgcheck=1
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-centos5

#released updates


name=CentOS-5 - Updates
#mirrorlist=http://mirrorlist.centos.org/?release=4&arch=$basearch&repo=updates

baseurl=http://ftp.sjtu.edu.cn/centos/5/updates/$basearch/
gpgcheck=1
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-centos5

#packages used/produced in the build but not released


name=CentOS-5 - Addons
#mirrorlist=http://mirrorlist.centos.org/?release=4&arch=$basearch&repo=addons


baseurl=http://ftp.sjtu.edu.cn/centos/5/addons/$basearch/
gpgcheck=1
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-centos5

#additional packages that may be useful


name=CentOS-5 - Extras
#mirrorlist=http://mirrorlist.centos.org/?release=4&arch=$basearch&repo=extras


baseurl=http://ftp.sjtu.edu.cn/centos/5/extras/$basearch/
gpgcheck=1
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-centos5

#additional packages that extend functionality of existing packages


name=CentOS-5 - Plus
#mirrorlist=http://mirrorlist.centos.org/?release=4&arch=$basearch&repo=centosplus

baseurl=http://ftp.sjtu.edu.cn/centos/5/centosplus/$basearch/
gpgcheck=1
enabled=0
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-centos5

#contrib - packages by Centos Users


name=CentOS-5 - Contrib
#mirrorlist=http://mirrorlist.centos.org/?release=4&arch=$basearch&repo=contrib

baseurl=http://ftp.sjtu.edu.cn/centos/5/contrib/$basearch/
gpgcheck=1
enabled=0
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-centos5
按顺序安装下列包:
rpm –ivh /mnt/Server/Cluster/ipvsadm-1.24-10.i386.rpm(需挂载光盘)
yum –y install libnet
yum –y install perl-MailTools
yum –y install heartbeat
yum –y install heartbeat-devel
yum –y install heartbeat-ldirectord
复制配置文件:
cp /usr/share/doc/heartbeat-ldirectord-2.1.3/ldirectord.cf   /etc/ha.d/
cp /usr/share/doc/heartbeat-2.1.3/ha.cf   /etc/ha.d
cp /usr/share/doc/heartbeat-2.1.3/authkeys   /etc/ha.d
cp /usr/share/doc/heartbeat-2.1.3/haresources   /etc/ha.d
分别编辑上面4个文件:
ldirecord.cf文件:
      checktimeout=3               #检测超时3s
      checkinterval=1               #检查时间间隔1s
      autoreload=yes               #配置文件改变时自动加载配置文件
      logfile="/var/log/ldirectord.log" #定义日志文件
      quiescent=yes   
      virtual=192.168.0.225:80      #虚拟IP
      real=192.168.0.223:80 gate      #realserver web1.eyo.com
      real=192.168.0.224:80 gate      #realserver web2.eyo.com
      allback=127.0.0.1:80         #所有服务器发生故障时访问该服务器
      service=http                  #服务名称
      scheduler=rr                  #这里使用的直连路由调度算法
      protocol=tcp       #如果上面的virtual配置项最后的:80没有,则这里必须使用fwm协议
      checktype=negotiate
ha.cf文件:
28      debugfile /var/log/ha-debug #定义日志文件
33      logfile /var/log/ha-log       #同上
38      logfacility   local0       #默认
52      keepalive 2   #设定heartbeat之间的时间间隔为2秒.
60      deadtime 30    #在30秒后宣布节点死亡。
65      warntime 10    #在日志中发出“late heartbeat“警告之前等待的时间,单位为秒。
75      initdead 120   #在某些配置下,重启后网络需要一些时间才能正常工作。这个单独的”deadtime”选项可以处理这种情况。它的取值至少应该为通常deadtime的两倍。
80      udpport 694   #使用端口694进行bcast和ucast通信。这是默认的,并且在IANA官方注册的端口号。
95      bcast   eth1    # Linux 表示在eth1接口上使用广播heartbeat(将eth1替换为eth0,eth2),或者您使用的任何接口。
117   mcast eth0 225.0.0.1 694 1 0   #默认即可
125   ucast eth0 192.168.0.223      #改为你eth0的地址。
161   auto_failback on             #该选项是必须配置的。值为on or off
215   node    www.eyo.com    #负载均衡服务器名,必须与uname -n的输出一致。
216   node    mail.eyo.com      #同上
224   ping 192.168.0.15         #最好选择一个任何时候都能ping通的真实IP
257   respawn hamail /usr/lib/heartbeat/ipfail
263   apiauth ipfail gid=haclient uid=hamail
haresources文件:
web1.eyo.com ldirectord::ldirectord.cf LVSSyncDaemonSwap::master IPaddr::192.168.0.225/24/eth0/192.168.0.255(必须写成一行)

authkeys文件:(这里使用的是md5方式,注意,该文件的权限必须是600)
            auth 3
            3 md5 test


然后编辑/etc/sysctl.conf文件,加入以下行:
net.ipv4.ip_forward = 1
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.eth0.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.eth0.arp_announce = 2
再执行sysctl -p命令,使其立即生效

以上是web1的配置。切记,web2上的/etc/ha.d/目录下的authkeys、haresources、ha.cf、ldirectord.cf四个文件必须和web1上的相同。
把web1上的文件拷贝到web2上:
#scp /etc/ha.d/* root@192.168.0.224:/etc/ha.d/

另外在web2上的/etc/sysctl.conf文件中也加入像web1上的内容




验证Heartbeat:
将Web2上的默认主页稍微修改,
# vim /usr/local/apache2/htdocs/index.html

这里修改为“web2 It works!”,然后分别启动web1和web2上的heartbeat服务:
# service heartbeat start

首先在web1上使用ifconfig查看网络接口,会多出一个eth0:0的接口,并且IP地址为192.168.0.225/24,

eth0:0    Link encap:EthernetHWaddr 00:0C:29:F2:AC:B3
          inet addr:192.168.0.225Bcast:192.168.0.255Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICASTMTU:1500Metric:1
          Interrupt:67 Base address:0x2024

在/etc/ha.d/resource.d/目录中执行下面的命令,会出现相应的结果:

# ./LVSSyncDaemonSwap master status
master running
(ipvs_syncmaster pid: 13114)
# ./LVSSyncDaemonSwap backup status
backup stopped
(ipvs_syncmaster pid: 13114)

# ./LVSSyncDaemonSwap master status
master stopped
# ./LVSSyncDaemonSwap backup status
backup stopped

接下来,通过windows下的IE浏览器,访问http://192.168.0.225,应该看到web1上的默认主页:It works!
若停止web1上的heartbeat服务,则web2会接管,并产生一个eth0:0接口

eth0:0    Link encap:EthernetHWaddr 00:0C:29:3A:59:5F
          inet addr:192.168.0.225Bcast:192.168.0.255Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICASTMTU:1500Metric:1
          Interrupt:67 Base address:0x2024


# ./LVSSyncDaemonSwap master status
master running
(ipvs_syncmaster pid: 3980)
# ./LVSSyncDaemonSwap backup status
backup stopped
(ipvs_syncmaster pid: 3980)

你再通过windows下的IE浏览器,访问http://192.168.0.225,应该看到web2上的默认主页:web 2 It works!
如果把web1上的heartbeat重新启动,那么web1会重新接管。


  验证lvs:
安装完ipvsadm后,默认是没有/etc/sysconfig/ipvsadm文件生成的,而启动ipvsadm服务时没有这个配置文件,会报错“Applying IPVS configuration: /etc/init.d/ipvsadm: line 62: /etc/sysconfig/ipvsadm: No such file or directory”, 首先应该执行service ipvsadm save,再执行service ipvsadm start

web1和web2上都执行以下命令:
  # service ipvsadm save
Saving IPVS table to /etc/sysconfig/ipvsadm:               [确定]
# service ipvsadm start
Clearing the current IPVS table:                           [确定]
Applying IPVS configuration:                               [确定]

  接下来通过在当前正处于接管状态的结点上运行ipvsadm –Ln,应该能看到如下现象:
  # ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port         Forward Weight ActiveConn InActConn
TCP192.168.0.225:80 rr
-> 192.168.0.223:80             Route   1      0          0         
-> 192.168.0.224:80             Local   1      0
上面只是实现了冗余备份的功能,下面的步骤将实现负载均衡的功能:
修改/etc/sysctl.conf,再添加以下两条:
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2

到现在为止,我们这个实验中总共应该修改和添加了7条:
net.ipv4.ip_forward = 1
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.eth0.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.eth0.arp_announce = 2
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2



然后编写如下脚本,这里命名为lvs.sh,并将脚本的绝对路径写入rc.local文件
  # vim /var/scripts/lvs.sh
  #!/bin/bash
  ifconfig lo:0 192.168.0.225 broadcast 192.168.0.225 netmask 255.255.255.255 up
route add -host 192.168.0.225 dev lo:0
sysctl –p

# chmod a+x /var/scripts/lvs.sh
# echo "/var/scripts/lvs.sh" >> /etc/rc.local
# /var/scripts/lvs.sh

以上步骤在两台服务器上都必须完成,然后就可以验证了,访问http://192.168.0.225 刷新应该能看到不同的页面(一直按住F5刷新,效果会比较明显)



页: [1]
查看完整版本: RHEL5搭建apache服务器全过程(四)配置heartbeat和ipvsadm