SUSE Linux搭建企業(yè)服務(wù)器的雙機(jī)配置過(guò)程
隨著信息化建設(shè)的不斷推進(jìn),各大企事業(yè)單位的活動(dòng)越來(lái)越多的依賴(lài)于其關(guān)鍵的業(yè)務(wù)信息系統(tǒng),這些業(yè)務(wù)信息系統(tǒng)對(duì)整個(gè)機(jī)構(gòu)的運(yùn)營(yíng)和發(fā)展起著至關(guān)重要的作用,一旦發(fā)生宕機(jī)故障或應(yīng)用停機(jī),將給機(jī)構(gòu)帶來(lái)巨大的經(jīng)濟(jì)損失。Suse企業(yè)版里提供的HA軟件是一款專(zhuān)業(yè)的高可用集群軟件產(chǎn)品,它不僅僅是一款雙機(jī)熱備軟件,它還為您提供Linux平臺(tái)上完整的高可用性解決方案。
當(dāng)集群中的某個(gè)節(jié)點(diǎn)由于軟件或硬件原因發(fā)生故障時(shí),集群系統(tǒng)可以把資源切換到其他健康的節(jié)點(diǎn)上,使整個(gè)系統(tǒng)能連續(xù)不間斷的對(duì)外提供服務(wù),從而為機(jī)構(gòu)24x365的關(guān)鍵業(yè)務(wù)提供了可靠的保障,達(dá)到了系統(tǒng)99.999%的高可用性和可靠性。SuSE Enterprise Linux 10.0是內(nèi)置Linux 2.6.16內(nèi)核的企業(yè)級(jí)服務(wù)器,較之SuSE Enterprise Linux 9.0,它在性能、可擴(kuò)展性、易管理性和安全等方面都予以加強(qiáng),并有眾多硬件和應(yīng)用軟件支持。
最近,筆者使用SuSE Enterprise Linux 10.0作為系統(tǒng)平臺(tái)(使用其它Linux平臺(tái)在安裝部署時(shí)可能有小差異),在其上采用HeartBeat、Mon和Rsync等開(kāi)源軟件打造了一個(gè)高可用系統(tǒng),挖掘了SuSE Linux的高可用性。下面我為大家詳細(xì)介紹如何在Suse Linux搭建雙機(jī)的配置過(guò)程。
一、 HeartBeat、Mon、Rsync簡(jiǎn)介
HeartBeat是一個(gè)高可用性解決方案,其官方網(wǎng)站是www.linux- ha.org。HeartBeat目前被廣泛地應(yīng)用,是很多商業(yè)高可用性軟件的重要組成部分。大多數(shù)Linux廠商已經(jīng)把它很好地集成在自己的系統(tǒng)中,例如SuSE Linux、RedHat和Debian Linux等。此外,HeartBeat也能很好地部署在Solaris和FreeBSD系統(tǒng)上。
Mon是一個(gè)后臺(tái)服務(wù)運(yùn)行情況的監(jiān)控和告警軟件,能夠?qū)Υ蠖鄶?shù)標(biāo)準(zhǔn)服務(wù)進(jìn)行監(jiān)控和告警,其中包括SMTP服務(wù)、Telnet服務(wù)、FTP服務(wù)、NNTP 服務(wù)、HTTP服務(wù)、POP3服務(wù)、Samba服務(wù)和NFS服務(wù)等,還可以自己編寫(xiě)告警事件和自定義服務(wù)。在本系統(tǒng)中,將使用Mon對(duì)兩臺(tái)主機(jī)的網(wǎng)絡(luò)連接情況和服務(wù)運(yùn)行情況進(jìn)行監(jiān)控,目的是及時(shí)告警,并且自動(dòng)恢復(fù)服務(wù)。 Rsync是一個(gè)用于數(shù)據(jù)同步的軟件,除了數(shù)據(jù)拷貝外,還有增量備份、同步owner、group和文件權(quán)限等重要信息的功能。在本系統(tǒng)中,使用 Rsync來(lái)同步主服務(wù)器和備用服務(wù)器的數(shù)據(jù)。
二、安裝環(huán)境
首先,需要準(zhǔn)備兩臺(tái)PC服務(wù)器,每臺(tái)服務(wù)器有兩塊網(wǎng)卡,其物理網(wǎng)絡(luò)圖如圖1
安裝環(huán)境
圖1 網(wǎng)絡(luò)拓?fù)鋱D
雖然在 YaST2控制中心里有"高可用性"圖形化的配置向?qū)?,我們這里不介紹一個(gè)過(guò)程,中在講述原理和方法,主要是基于命令行的配置方式。首先在PC服務(wù)器上安裝SuSE Enterprise Linux 10.0系統(tǒng),并將eth0配置為192.168.8.*網(wǎng)段,eth1配置為10.1.1.*網(wǎng)段,eth0鏈接對(duì)外的交換機(jī),eth1用于兩臺(tái)機(jī)器的對(duì)連。
修改/etc /hosts文件,修改內(nèi)容如下:
- 10.1.1.1 linux1
- 10.1.1.2 linux2
- 192.168.8.92 svr1
- 192.168.8.93 svr2
- 192.168.8.112 svr
注意,10.1.1.*為心跳線(xiàn)IP地址,192.168.8.*為對(duì)外IP,192.168.8.112為浮動(dòng)IP。并且,拷貝hosts文件到 linux2上:
- #scp /etc/hosts 10.1.1.2:/etc
同時(shí),關(guān)閉不需要的服務(wù)如下:
- chkconfig -s alsasound off
- chkconfig -s nfs off
- chkconfig -s nfsboot off
- chkconfig -s portmap off
- chkconfig -s slpd off
- chkconfig -s smbfs off
- chkconfig -s setserial off
- chkconfig -s splash off
- chkconfig -s splash_early off
- chkconfig -s splash_late off
- chkconfig -s xdm off
- chkconfig -s fbset off
- chkconfig -s cups off
- chkconfig -s nscd off
- chkconfig -s random off
最后,修改啟動(dòng)級(jí)別為3。找到/etc/inittab文件的如下幾行:
- #The default runlevel is defined here id:5:initdefault:
將其改為如下:
- #The default runlevel is defined here id:3:initdefault:
#p#
三、安裝HeartBeat
通過(guò)yast2工具添加heartbeat,如圖片2
安裝HeartBeat
圖2 heartbeat安裝
1)安裝HeartBeat軟件包??梢酝ㄟ^(guò)SuSE的光盤(pán)進(jìn)行安裝,安裝命令如下:
- #rpm -Uvh heartbeat-1.2.2-0.6.i586.rpm libnet-1.1.1-42.1.i586.rpm
heartbeat-pils-1.2.2-0.6.i586.rpm heartbeat-stonith-1.2.2-0.6.i586.rpm
2)修改HeartBeat的配置文件
① 主配置文件/etc/ha.d/ha.cf
將/usr/share /doc/packages/heartbeat/目錄下ha.cf、authkeys、haresources 三個(gè)文件考入/etc/ha.d/目錄下,再將主配置文件/etc/ha.d/ha.cf修改如下:
- node linux1
- node linux2
- keepalive 2
- deadtime 30
- warntime 10
- initdead 120
- auto_failback on
- bcast eth1
- ping 10.1.1.1
- ping 10.1.1.2
- respawn hacluster /usr/lib/heartbeat/ipfail
② 認(rèn)證方式文件/etc/ha.d/authkeys
修改認(rèn)證方式文件/etc/ha.d/authkeys如下:
- auth 2
- 2 crc
③ 資源配置文件/etc/ha.d heresources
假設(shè)現(xiàn)在正在進(jìn)行Apache服務(wù)的HA系統(tǒng),這里設(shè)置的服務(wù)必須在/etc /ha.d/resource.d和/etc/rc.d/init.d下有響應(yīng)的腳本。修改資源配置文件/etc/ha.d/haresources如下:
- linux1 192.168.8.112 apache2
④ 拷貝配置文件到linux2
- #scp /etc/ha.d/ha.cf 10.1.1.2:/etc/ha.d
- #scp /etc/ha.d/authkeys 10.1.1.2:/etc/ha.d
- #scp /etc/ha.d/haresources 10.1.1.2:/etc/ha.d
3)啟動(dòng)HeartBeat服務(wù):
- #chkconfig -s heartbeat on
- #chmod 600 /etc/ha.d/authkeys
- #/etc/init.d/heartbeat start
系統(tǒng)顯示信息如下:
- Starting High-Availability servicesheartbeat:2010/4/01_23:05:19 info: Neither logfile nor logfacility found.
- heartbeat: 2010/4/01_23:05:19 info: Logging defaulting to /var/log/ha-log
- heartbeat: 2010/4/01_23:05:19 info:**************************
- heartbeat: 2010/4/01_23:05:19 info:Configuration validated.Starting heartbeat 1.2.2
- done
通過(guò)運(yùn)行"ps -ef|grep heartbeat"命令來(lái)查看進(jìn)程:
- root 4240 1 0 23:25 ? 00:00:01 heartbeat:heartbeat:master control process
- nobody 4242 4240 0 23:05 ? 00:00:00 heartbeat:heartbeat:FIFO reader
- nobody 4243 4240 0 23:05 ? 00:00:00 heartbeat:heartbeat:write:bcast eth1
- nobody 4244 4240 0 23:05 ? 00:00:00 heartbeat:heartbeat:read:bcast eth1
- nobody 4245 4240 0 23:05 ?00:00:00 heartbeat:heartbeat:write:ping 10.1.1.1
- nobody 4246 4240 0 23:05 ? 00:00:00 heartbeat:heartbeat:read:ping 10.1.1.1
- nobody 4247 4240 0 23:05 ? 00:00:00 heartbeat:heartbeat:write:ping 10.1.1.2
- nobody 4248 4240 0 23:05 ? 00:00:00 heartbeat:heartbeat:read:ping 10.1.1.2
- haclust 4254 4240 0 23:07 ? 00:00:00 /usr/lib/heartbeat/ipfail
1 安裝Apache
1)安裝Apache和相關(guān)軟件包如下:
- #rpm -Uvh apahe2-2.0.49-27.8.i586.rpm apache2-prefork-2.0.49-27.8.i586.rpm libapr0-2.0.49-27.8.i586.rpm
- #SuSEconfig --module apache2
2)啟動(dòng)Apache:
- /etc/init.d/apache2 start
并運(yùn)行"ps -ef|grep apache"查看進(jìn)程:
- root 4387 1 10 23:33 ? 00:00:00 /usr/sbin/httpd2-prefork -f /etc/apache2/httpd.conf
- wwwrun 4388 4387 0 23:33 ? 00:00:00 /usr/sbin/httpd2-prefork -f /etc/apache2/httpd.conf
- wwwrun 4389 4387 0 23:33 ? 00:00:00 /usr/sbin/httpd2-prefork -f /etc/apache2/httpd.conf
- wwwrun 4390 4387 0 23:33 ? 00:00:00 /usr/sbin/httpd2-prefork -f /etc/apache2/httpd.conf
- wwwrun 4391 4387 0 23:33 ? 00:00:00 /usr/sbin/httpd2-prefork -f /etc/apache2/httpd.conf
- wwwrun 4392 4387 0 23:33 ? 00:00:00 /usr/sbin/httpd2-prefork -f /etc/apache2/httpd.conf
3)創(chuàng)建并編輯一個(gè)index.html文件:
- #vi /srv/www/htdocs/index.html
在其中輸入"linux ha"的字樣,并保存退出。通過(guò)Client端訪(fǎng)問(wèn)浮動(dòng)IP:192.168.8.112,在linux1上成功安裝Apache服務(wù)后,在linux2 上執(zhí)行以上操作,并且同樣測(cè)試其結(jié)果如圖3。
安裝HeartBeat
圖3 linux上的測(cè)試結(jié)果
四、測(cè)試HA系統(tǒng)
首先,關(guān)閉linux1的網(wǎng)絡(luò)服務(wù)(也可以使用shutdown):
#/etc/init.d/network stop
然后,查看linux2的log文件。正確結(jié)果是HA系統(tǒng)發(fā)現(xiàn)linux1已經(jīng) dead,由linux2接替其工作。
- #tail -f /var/log/ha-log
- heartbeat: 2010/4/02_14:18:16 WARN: node 10.1.1.1:is dead
- heartbeat: 2010/4/02_14:18:16 info: Link 10.1.1.1:10.1.1.1 dead................
- heartbeat: 2010/4/02_14:18:27 info: mach_down takeover complete.
- heartbeat: 2010/4/02_14:18:27 info: mach_down takeober complete for node linux1.
- heartbeat: 2010/4/02_14:18:27 ERROR: Both machines own our resources!
同時(shí),在linux2上面檢查浮動(dòng)IP,此時(shí)浮動(dòng)IP應(yīng)該綁定在linux2上。用"ifconfig"查看如圖3示。
這時(shí),通過(guò)Client端訪(fǎng)問(wèn)浮動(dòng)IP:192.168.8.112,HTTP服務(wù)正常。
- Eth0:0 Link encap:Ethernet HWaddr 00:0C:29:35:E6:63
- Inet addr:192.168.0.112 bcast:192.168.9.255 Mask:255.255.254.0
- Interrupt:9 Base address:0x1000
重新啟動(dòng)linux1的網(wǎng)絡(luò)服務(wù),浮動(dòng)IP將又重新綁定到 linux1。此時(shí)通過(guò)Client端訪(fǎng)問(wèn)浮動(dòng)IP:192.168.8.112,HTTP服務(wù)同樣正常。#p#
五、Mon服務(wù)監(jiān)控
使用Mon可以實(shí)現(xiàn)監(jiān)控網(wǎng)絡(luò)、監(jiān)控服務(wù)、E-mail告警和自動(dòng)重啟服務(wù)等。
1) 安裝Mon軟件及其相關(guān)軟件包。
從光盤(pán)安裝Time-period、Convert-BER、perl-Mon和fping:
- #rpm -Uvh perl-Time-period-1.20-301.1.i586.rpm perl-Convert-BER-1.3101-190.1.i586.rpm
perl-Mon-0.11-294.1.i586.rpm fping-2.2b1-819.1.i586.rpm
從www.cpan.org下載并安裝Time-HiRes:
- #tar xvzf Time-HiRes-1.65.tar.gz
- #3cd Time-HiRes-1.65/perl Makefile.PL && make &&make test && make install
從www.kernel.org/software/mon/下載并安裝軟件包mon-0.99.2.tar.bz2:
- #tar xvjf mon-0.99.2.tar.bz2
- #cp -r mon-0.99.2 /usr/lib/mon
- #mkdir /etc/mon
- #cp /usr/lib/mon/etc/auth.cf /etc/mon
2) 編輯/etc/mon/mon.cf文件:
- cfbasedir = /etc/mon
- alertdir = /usr/lib/mon/alert.d
- mondir = /usr/lib/mon/mon.d
- statedir = /usr/lib/mon/state.d
- logdir = /usr/lib/mon/log.d
- dtlogfile = /usr/lib/mon/log.d/dt.log
- maxprocs = 20
- histlength = 100
- randstart = 60s
- dtlogging = yes
- authtype = getpwnam
- hostgroup servers 10.1.1.1 10.1.1.2
- hostgroup www 10.1.1.1
- watch servers
- service ping
- interval 1m
- monitor fping.moniter
- period wd {Sun-Sat}
- alert mail.alert 97140@263.com (請(qǐng)指定郵件地址)
- watch www
- service http
- interval 1m
- monitor http.monitor
- period wd {Sun-Sat}
- alert apacherestart.alert
- alert mail.alert 97140@263.com (請(qǐng)指定郵件地址)
然后編輯/ust/lib/mon/alert.d /apacherestart.alert文件:
- #!/bin/sh
- /etc/init.d/apache2 restart
- chmod +x /usr/lib/mon/alert.d/apacherestart.alert
同樣地,在linux2完成以上步驟。但注意,在配置/etc/mon/mon.cf時(shí),"hostgroup www 10.1.1.1"應(yīng)設(shè)置為"hostgroup www 10.1.1.2"。
在兩臺(tái)服務(wù)器上分別安裝和配置完成后,啟動(dòng)Mon服務(wù):
- #/usr/lib/mon/mon -c /etc/mon/mon.cf &
查看log文件,檢查服務(wù)狀態(tài):
- #tail /var/log/messages
- Nov 2 17:15:20 linux mon[14079]: mon server started
運(yùn)行"ps -ef|grep mon"查看進(jìn)程,可以看到Mon服務(wù)成功啟動(dòng)。
- root 14079 1 0 17:15 ? 00:00:00 /usr/bin/perl/usr/lib/mon/mon -c/etc/mon/mon.cf
3)對(duì)Mon服務(wù)進(jìn)行測(cè)試。
① 測(cè)試Apache服務(wù)的郵件告警和服務(wù)重啟功能
先關(guān)閉linux1的Apache服務(wù):
- #/etc/init.d/apache2 stop
查看log文件,檢查Mon服務(wù)的處理步驟。Mon首先發(fā)現(xiàn)WWW服務(wù)不能訪(fǎng)問(wèn),然后重啟Apache服務(wù),并且發(fā)Email到指定的郵箱。
log文件內(nèi)容如下:
- Nov 2 17:15:59 linux mon[14079]: failure for www http
1099386959 10.1.1.1- Nov 2 17:15:59 linux mon[14079]: calling alert apacherestart.
alert for www/http(/usr/lib/mon/alert.d/apacherestart.alert,) 10.1.1.1- Nov 2 17:16:03 linux mon[14079]: calling alert mail.alert for
www/http(/usr/lib/mon/alert.d/mail.alert,song@heming.org) 10.1.1.1
郵件內(nèi)容則如下:
- ALERT www/http: 10.1.1.1(Tue Nov 2 17:16:03)(標(biāo)題)
- Sunmmary output : 10.1.1.1
- Group : www
- Service : http
- Time noticed : Tue Nov 2 17:16:03 2004
- Secs until next alert :
- Members : 10.1.1.1
- Detailed text (if any) follows:
- HOST 10.1.1.1: connect: Connection refused
② 測(cè)試網(wǎng)絡(luò)監(jiān)控和警告
關(guān)閉linux1的網(wǎng)絡(luò)服務(wù):
- #/etc/init.d/network stop
Mon會(huì)發(fā)送告警郵件到指定郵箱,郵件內(nèi)容略。
六、數(shù)據(jù)同步
假設(shè)這里只更新linux1服務(wù)器上的數(shù)據(jù),由linux1定時(shí)向linux2復(fù)制數(shù)據(jù),這就需要進(jìn)行數(shù)據(jù)同步設(shè)置。
首先,設(shè)置linux1到linux2的SSH無(wú)密碼登錄:
- #ssh-keygen -t dsa
- #scp /root/.ssh/id_dsa.pub 10.1.1.2:/root/.ssh/authorized_keys
- #ssh 10.1.1.2 (應(yīng)該不用輸入密碼就能登錄)
然后,在linux1上運(yùn)行Rsync進(jìn)行數(shù)據(jù)同步:
- #/usr/bin/rsync -avzoge ssh /srv/www/htdocs 10.1.1.2:/srv/www/htdocs
這時(shí)會(huì)顯示如下內(nèi)容:
- building file list ... done
將數(shù)據(jù)同步服務(wù)加入到Crontab,設(shè)置30分鐘(時(shí)間可以根據(jù)具體需要決定)定時(shí)數(shù)據(jù)同步一次。命令如下:
- #crontab -e
- */30 * * * * /usr/sbin/rsync -avzoge ssh 10.1.1.2:/srv/www/html
至此,具有數(shù)據(jù)同步、服務(wù)監(jiān)控的高可用性服務(wù)系統(tǒng)搭建完成。
【編輯推薦】