设为首页 收藏本站
查看: 1172|回复: 0

[经验分享] RHEL5搭建apache服务器全过程(四)配置heartbeat和ipvsadm

[复制链接]

尚未签到

发表于 2019-1-7 12:43:54 | 显示全部楼层 |阅读模式
  首先配置公网yum源
  导入key:
   [root@web1 ~]#rpm --import http://ftp.sjtu.edu.cn/centos/5/os/i386/RPM-GPG-KEY-CentOS-5
   [root@web1 ~]#vim /etc/yum.repos.d/yum.repo(注意此目录中只允许一个repo文件生效)
[base]
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

[update]
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

[addons]
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

[extras]
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

[centosplus]
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

[contrib]
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上的默认主页稍微修改,
[root@web2 ~]# vim /usr/local/apache2/htdocs/index.html

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

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

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

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

[root@web1 resource.d]# ./LVSSyncDaemonSwap master status
master running
(ipvs_syncmaster pid: 13114)
[root@web1 resource.d]# ./LVSSyncDaemonSwap backup status
backup stopped
(ipvs_syncmaster pid: 13114)

[root@web2 resource.d]# ./LVSSyncDaemonSwap master status
master stopped
[root@web2 resource.d]# ./LVSSyncDaemonSwap backup status
backup stopped

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

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


[root@web2 resource.d]# ./LVSSyncDaemonSwap master status
master running
(ipvs_syncmaster pid: 3980)
[root@web2 resource.d]# ./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上都执行以下命令:
  [root@web1 ~]# service ipvsadm save
Saving IPVS table to /etc/sysconfig/ipvsadm:               [确定]
[root@web1 ~]# service ipvsadm start
Clearing the current IPVS table:                           [确定]
Applying IPVS configuration:                               [确定]

  接下来通过在当前正处于接管状态的结点上运行ipvsadm –Ln,应该能看到如下现象:
  [root@web2 ~]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.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文件
  [root@web1 ~]# 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

[root@web1 ~]# chmod a+x /var/scripts/lvs.sh
[root@web1 ~]# echo "/var/scripts/lvs.sh" >> /etc/rc.local
[root@web1 ~]# /var/scripts/lvs.sh


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




运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-660354-1-1.html 上篇帖子: 教你如何用heartbeat搭建一个高可用的web集群 下篇帖子: heartbeat v2配置高可用web集群和基于nfs搭建MySQL高可用集群
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表