RHEL5搭建apache服务器全过程(四)配置heartbeat和ipvsadm
首先配置公網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文件: 28debugfile /var/log/ha-debug?#定義日志文件 33logfile /var/log/ha-log?????? #同上 38logfacility???? local0?????? #默認 52keepalive 2???? #設定heartbeat之間的時間間隔為2秒. 60deadtime 30??? #在30秒后宣布節點死亡。 65warntime 10??? #在日志中發出“late heartbeat“警告之前等待的時間,單位為秒。 75initdead 120???? #在某些配置下,重啟后網絡需要一些時間才能正常工作。這個單獨的”deadtime”選項可以處理這種情況。它的取值至少應該為通常deadtime的兩倍。 80udpport 694???? #使用端口694進行bcast和ucast通信。這是默認的,并且在IANA官方注冊的端口號。 95bcast?? eth1??? # Linux 表示在eth1接口上使用廣播heartbeat(將eth1替換為eth0,eth2),或者您使用的任何接口。 117mcast eth0 225.0.0.1 694 1 0?? #默認即可 125ucast eth0 192.168.0.223????? #改為你eth0的地址。 161auto_failback on???????????? #該選項是必須配置的。值為on or off 215node??? www.eyo.com??? #負載均衡服務器名,必須與uname -n的輸出一致。 216node??? mail.eyo.com??????? #同上 224ping 192.168.0.15?????????? #最好選擇一個任何時候都能ping通的真實IP 257respawn hamail /usr/lib/heartbeat/ipfail 263apiauth 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刷新,效果會比較明顯)
?
?
?
轉載于:https://blog.51cto.com/linuxclub/607956
總結
以上是生活随笔為你收集整理的RHEL5搭建apache服务器全过程(四)配置heartbeat和ipvsadm的全部內容,希望文章能夠幫你解決所遇到的問題。