资源描述
SUSE Linux搭建企业服务器的双机配置过程
随着信息化建设的不断推进,各大企事业单位的活动越来越多的依赖于其关键的业务信息系统,这些业务信息系统对整个机构的运营和发展起着至关重要的作用,一旦发生宕机故障或应用停机,将给机构带来巨大的经济损失。Suse企业版里提供的HA软件是一款专业的高可用集群软件产品,它不仅仅是一款双机热备软件,它还为您提供Linux平台上完整的高可用性解决方案。
当集群中的某个节点由于软件或硬件原因发生故障时,集群系统可以把资源切换到其他健康的节点上,使整个系统能连续不间断的对外提供服务,从而为机构24x365的关键业务提供了可靠的保障,达到了系统99.999%的高可用性和可靠性。SuSE Enterprise Linux 10.0是内置Linux 2.6.16内核的企业级服务器,较之SuSE Enterprise Linux 9.0,它在性能、可扩展性、易管理性和安全等方面都予以加强,并有众多硬件和应用软件支持。
最近,笔者使用SuSE Enterprise Linux 10.0作为系统平台(使用其它Linux平台在安装部署时可能有小差异),在其上采用HeartBeat、Mon和Rsync等开源软件打造了一个高可用系统,挖掘了SuSE Linux的高可用性。下面我为大家详细介绍如何在Suse Linux搭建双机的配置过程。
一、 HeartBeat、Mon、Rsync简介
HeartBeat是一个高可用性解决方案,其官方网站是www.linux- ha.org。HeartBeat目前被广泛地应用,是很多商业高可用性软件的重要组成部分。大多数Linux厂商已经把它很好地集成在自己的系统中,例如SuSE Linux、RedHat和Debian Linux等。此外,HeartBeat也能很好地部署在Solaris和FreeBSD系统上。
Mon是一个后台服务运行情况的监控和告警软件,能够对大多数标准服务进行监控和告警,其中包括SMTP服务、Telnet服务、FTP服务、NNTP 服务、HTTP服务、POP3服务、Samba服务和NFS服务等,还可以自己编写告警事件和自定义服务。在本系统中,将使用Mon对两台主机的网络连接情况和服务运行情况进行监控,目的是及时告警,并且自动恢复服务。 Rsync是一个用于数据同步的软件,除了数据拷贝外,还有增量备份、同步owner、group和文件权限等重要信息的功能。在本系统中,使用 Rsync来同步主服务器和备用服务器的数据。
二、安装环境
首先,需要准备两台PC服务器,每台服务器有两块网卡,其物理网络图如图1
安装环境
图1 网络拓扑图
虽然在 YaST2控制中心里有"高可用性"图形化的配置向导,我们这里不介绍一个过程,中在讲述原理和方法,主要是基于命令行的配置方式。首先在PC服务器上安装SuSE Enterprise Linux 10.0系统,并将eth0配置为192.168.8.*网段,eth1配置为10.1.1.*网段,eth0链接对外的交换机,eth1用于两台机器的对连。
修改/etc /hosts文件,修改内容如下:
1. 10.1.1.1 linux1
2. 10.1.1.2 linux2
3. 192.168.8.92 svr1
4. 192.168.8.93 svr2
5. 192.168.8.112 svr
注意,10.1.1.*为心跳线IP地址,192.168.8.*为对外IP,192.168.8.112为浮动IP。并且,拷贝hosts文件到 linux2上:
1. #scp /etc/hosts 10.1.1.2:/etc
同时,关闭不需要的服务如下:
1. chkconfig -s alsasound off
2. chkconfig -s nfs off
3. chkconfig -s nfsboot off
4. chkconfig -s portmap off
5. chkconfig -s slpd off
6. chkconfig -s smbfs off
7. chkconfig -s setserial off
8. chkconfig -s splash off
9. chkconfig -s splash_early off
10. chkconfig -s splash_late off
11. chkconfig -s xdm off
12. chkconfig -s fbset off
13. chkconfig -s cups off
14. chkconfig -s nscd off
15. chkconfig -s random off
最后,修改启动级别为3。找到/etc/inittab文件的如下几行:
1. #The default runlevel is defined here id:5:initdefault:
将其改为如下:
1. #The default runlevel is defined here id:3:initdefault:
三、安装HeartBeat
通过yast2工具添加heartbeat,如图片2
安装HeartBeat
图2 heartbeat安装
1)安装HeartBeat软件包。可以通过SuSE的光盘进行安装,安装命令如下:
1. #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 三个文件考入/etc/ha.d/目录下,再将主配置文件/etc/ha.d/ha.cf修改如下:
1. node linux1
2. node linux2
3. keepalive 2
4. deadtime 30
5. warntime 10
6. initdead 120
7. auto_failback on
8. bcast eth1
9. ping 10.1.1.1
10. ping 10.1.1.2
11. respawn hacluster /usr/lib/heartbeat/ipfail
② 认证方式文件/etc/ha.d/authkeys
修改认证方式文件/etc/ha.d/authkeys如下:
1. auth 2
2. 2 crc
③ 资源配置文件/etc/ha.d heresources
假设现在正在进行Apache服务的HA系统,这里设置的服务必须在/etc /ha.d/resource.d和/etc/rc.d/init.d下有响应的脚本。修改资源配置文件/etc/ha.d/haresources如下:
1. linux1 192.168.8.112 apache2
④ 拷贝配置文件到linux2
1. #scp /etc/ha.d/ha.cf 10.1.1.2:/etc/ha.d
2. #scp /etc/ha.d/authkeys 10.1.1.2:/etc/ha.d
3. #scp /etc/ha.d/haresources 10.1.1.2:/etc/ha.d
3)启动HeartBeat服务:
1. #chkconfig -s heartbeat on
2. #chmod 600 /etc/ha.d/authkeys
3. #/etc/init.d/heartbeat start
系统显示信息如下:
1. Starting High-Availability servicesheartbeat:2010/4/01_23:05:19 info: Neither logfile nor logfacility found.
2. heartbeat: 2010/4/01_23:05:19 info: Logging defaulting to /var/log/ha-log
3. heartbeat: 2010/4/01_23:05:19 info:**************************
4. heartbeat: 2010/4/01_23:05:19 info:Configuration validated.Starting heartbeat 1.2.2
5. done
通过运行"ps -ef|grep heartbeat"命令来查看进程:
1. root 4240 1 0 23:25 ? 00:00:01 heartbeat:heartbeat:master control process
2. nobody 4242 4240 0 23:05 ? 00:00:00 heartbeat:heartbeat:FIFO reader
3. nobody 4243 4240 0 23:05 ? 00:00:00 heartbeat:heartbeat:write:bcast eth1
4. nobody 4244 4240 0 23:05 ? 00:00:00 heartbeat:heartbeat:read:bcast eth1
5. nobody 4245 4240 0 23:05 ?00:00:00 heartbeat:heartbeat:write:ping 10.1.1.1
6. nobody 4246 4240 0 23:05 ? 00:00:00 heartbeat:heartbeat:read:ping 10.1.1.1
7. nobody 4247 4240 0 23:05 ? 00:00:00 heartbeat:heartbeat:write:ping 10.1.1.2
8. nobody 4248 4240 0 23:05 ? 00:00:00 heartbeat:heartbeat:read:ping 10.1.1.2
9. haclust 4254 4240 0 23:07 ? 00:00:00 /usr/lib/heartbeat/ipfail
1 安装Apache
1)安装Apache和相关软件包如下:
1. #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
2. #SuSEconfig --module apache2
2)启动Apache:
1. /etc/init.d/apache2 start
并运行"ps -ef|grep apache"查看进程:
1. root 4387 1 10 23:33 ? 00:00:00 /usr/sbin/httpd2-prefork -f /etc/apache2/httpd.conf
2. wwwrun 4388 4387 0 23:33 ? 00:00:00 /usr/sbin/httpd2-prefork -f /etc/apache2/httpd.conf
3. wwwrun 4389 4387 0 23:33 ? 00:00:00 /usr/sbin/httpd2-prefork -f /etc/apache2/httpd.conf
4. wwwrun 4390 4387 0 23:33 ? 00:00:00 /usr/sbin/httpd2-prefork -f /etc/apache2/httpd.conf
5. wwwrun 4391 4387 0 23:33 ? 00:00:00 /usr/sbin/httpd2-prefork -f /etc/apache2/httpd.conf
6. wwwrun 4392 4387 0 23:33 ? 00:00:00 /usr/sbin/httpd2-prefork -f /etc/apache2/httpd.conf
3)创建并编辑一个index.html文件:
1. #vi /srv/www/htdocs/index.html
在其中输入"linux ha"的字样,并保存退出。通过Client端访问浮动IP:192.168.8.112,在linux1上成功安装Apache服务后,在linux2 上执行以上操作,并且同样测试其结果如图3。
安装HeartBeat
图3 linux上的测试结果
四、测试HA系统
首先,关闭linux1的网络服务(也可以使用shutdown):
#/etc/init.d/network stop
然后,查看linux2的log文件。正确结果是HA系统发现linux1已经 dead,由linux2接替其工作。
1. #tail -f /var/log/ha-log
2. heartbeat: 2010/4/02_14:18:16 WARN: node 10.1.1.1:is dead
3. heartbeat: 2010/4/02_14:18:16 info: Link 10.1.1.1:10.1.1.1 dead................
4. heartbeat: 2010/4/02_14:18:27 info: mach_down takeover complete.
5. heartbeat: 2010/4/02_14:18:27 info: mach_down takeober complete for node linux1.
6. heartbeat: 2010/4/02_14:18:27 ERROR: Both machines own our resources!
同时,在linux2上面检查浮动IP,此时浮动IP应该绑定在linux2上。用"ifconfig"查看如图3示。
这时,通过Client端访问浮动IP:192.168.8.112,HTTP服务正常。
1. Eth0:0 Link encap:Ethernet HWaddr 00:0C:29:35:E6:63
2. Inet addr:192.168.0.112 bcast:192.168.9.255 Mask:255.255.254.0
3. Interrupt:9 Base address:0x1000
重新启动linux1的网络服务,浮动IP将又重新绑定到 linux1。此时通过Client端访问浮动IP:192.168.8.112,HTTP服务同样正常。
五、Mon服务监控
使用Mon可以实现监控网络、监控服务、E-mail告警和自动重启服务等。
1) 安装Mon软件及其相关软件包。
从光盘安装Time-period、Convert-BER、perl-Mon和fping:
1. #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:
1. #tar xvzf Time-HiRes-1.65.tar.gz
2. #3cd Time-HiRes-1.65/perl Makefile.PL && make &&make test && make install
从www.kernel.org/software/mon/下载并安装软件包mon-0.99.2.tar.bz2:
1. #tar xvjf mon-0.99.2.tar.bz2
2. #cp -r mon-0.99.2 /usr/lib/mon
3. #mkdir /etc/mon
4. #cp /usr/lib/mon/etc/auth.cf /etc/mon
2) 编辑/etc/mon/mon.cf文件:
1. cfbasedir = /etc/mon
2. alertdir = /usr/lib/mon/alert.d
3. mondir = /usr/lib/mon/mon.d
4. statedir = /usr/lib/mon/state.d
5. logdir = /usr/lib/mon/log.d
6. dtlogfile = /usr/lib/mon/log.d/dt.log
7. maxprocs = 20
8. histlength = 100
9. randstart = 60s
10. dtlogging = yes
11. authtype = getpwnam
12. hostgroup servers 10.1.1.1 10.1.1.2
13. hostgroup www 10.1.1.1
14. watch servers
15. service ping
16. interval 1m
17. monitor fping.moniter
18. period wd {Sun-Sat}
19. alert mail.alert 97140@ (请指定邮件地址)
20. watch www
21. service http
22. interval 1m
23. monitor http.monitor
24. period wd {Sun-Sat}
25. alert apacherestart.alert
26. alert mail.alert 97140@ (请指定邮件地址)
然后编辑/ust/lib/mon/alert.d /apacherestart.alert文件:
1. #!/bin/sh
2. /etc/init.d/apache2 restart
3. chmod +x /usr/lib/mon/alert.d/apacherestart.alert
同样地,在linux2完成以上步骤。但注意,在配置/etc/mon/mon.cf时,"hostgroup www 10.1.1.1"应设置为"hostgroup www 10.1.1.2"。
在两台服务器上分别安装和配置完成后,启动Mon服务:
1. #/usr/lib/mon/mon -c /etc/mon/mon.cf &
查看log文件,检查服务状态:
1. #tail /var/log/messages
2. Nov 2 17:15:20 linux mon[14079]: mon server started
运行"ps -ef|grep mon"查看进程,可以看到Mon服务成功启动。
1. root 14079 1 0 17:15 ? 00:00:00 /usr/bin/perl/usr/lib/mon/mon -c/etc/mon/mon.cf
3)对Mon服务进行测试。
① 测试Apache服务的邮件告警和服务重启功能
先关闭linux1的Apache服务:
1. #/etc/init.d/apache2 stop
查看log文件,检查Mon服务的处理步骤。Mon首先发现WWW服务不能访问,然后重启Apache服务,并且发Email到指定的邮箱。
log文件内容如下:
1. Nov 2 17:15:59 linux mon[14079]: failure for www http
1099386959 10.1.1.1
2. 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
3. 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
邮件内容则如下:
1. ALERT www/http: 10.1.1.1(Tue Nov 2 17:16:03)(标题)
2. Sunmmary output : 10.1.1.1
3. Group : www
4. Service : http
5. Time noticed : Tue Nov 2 17:16:03 2004
6. Secs until next alert :
7. Members : 10.1.1.1
8. Detailed text (if any) follows:
9. HOST 10.1.1.1: connect: Connection refused
② 测试网络监控和警告
关闭linux1的网络服务:
1. #/etc/init.d/network stop
Mon会发送告警邮件到指定邮箱,邮件内容略。
六、数据同步
假设这里只更新linux1服务器上的数据,由linux1定时向linux2复制数据,这就需要进行数据同步设置。
首先,设置linux1到linux2的SSH无密码登录:
1. #ssh-keygen -t dsa
2. #scp /root/.ssh/id_dsa.pub 10.1.1.2:/root/.ssh/authorized_keys
3. #ssh 10.1.1.2 (应该不用输入密码就能登录)
然后,在linux1上运行Rsync进行数据同步:
1. #/usr/bin/rsync -avzoge ssh /srv/www/htdocs 10.1.1.2:/srv/www/htdocs
这时会显示如下内容:
1. building file list ... done
将数据同步服务加入到Crontab,设置30分钟(时间可以根据具体需要决定)定时数据同步一次。命令如下:
1. #crontab -e
2. */30 * * * * /usr/sbin/rsync -avzoge ssh 10.1.1.2:/srv/www/html
至此,具有数据同步、服务监控的高可用性服务系统搭建完成。
展开阅读全文