linux高可用集群heartbeat实现http的高可用

linux高可用集群的种类很多,比如常见的heartbeat,corosync,rhcs,keepalived,这些集群软件的出现为我们的业务生 产环境提供了高可用的保证,本文将简单介绍一下用heartbeat的v2版本来处理一个简单的http高可用集群的搭建。

在实现http高可用集群之前,首先至少需要2台主机,并且需要做3点基本的准备工作:1.设置节点名称,并且集群中的所有节点都能通过节点名称去解析集群中的所有主机。为了集群服务的高可用性,这里选择使用/etc/hosts,而且要保证uname -n的值必须和hostname中的值一致。2.使用ssh让双机互信。3.时间同步。

在安装heartbeat软件之前,首先把上面的基本工作完成。这里采用2台主机(192.168.1.201,192.168.1.202)来做我们的高可用集群服务。1.首先登陆192.168.1.201修改hostname=test1.qiguo.com,在/etc/sysconfig/network中修改 HOSTNAME=test1.qiguo.com保证服务器下次启动的时候主机名不变。在/etc/hosts中添加192.168.1.201 test1.qiguo.com test1; 192.168.1.20 test2.qiguo.com test2两行,再在192.168.1.202的这台主机上执行同样的操作。2.在192.168.1.201中执行ssh-keygen -t rsa,然后使用ssh-copy-id -i root@test2实现ssh互信。这个步骤在两个机器上都要执行。3.在2台服务器上使用时间同步命令ntpdate 133.100.11.8(可用的ntp服务器ip地址)

上面三步完成以后,就可以开始安装heartbeat了。可以去epel下载heartbeat的安装包,默认需要下heartbeat- 2.1.4-11.el5.i386,heartbeat-gui-2.1.4-11.el5.i386,heartbeat-pils- 2.1.4-11.el5.i386,heartbeat-stonith-2.1.4-11.el5.i386 这4个软件包。但是这4个软件包依赖于其它两个软件包perl-MailTools-1.77-1.el5.noarch,libnet- 1.1.6-7.el5.i386,所以首先得把这2个软件包给装出来。使用rpm -ivh perl-MailTools-1.77-1.el5.noarch的时候会报依赖关系的错误,所以用yum –nogpgcheck localinstall perl-MailTools-1.77-1.el5.noarch来安装。然后用同样的方式来一起安装剩余的几个包。注意:这些软件要在2个服务器上都 要安装

安装完成heartbeat中,heartbeat默认的配置文件在/etc/ha.d中。ha.d中的rc.d都是资源管理相关的脚本,而 resource.d中都是资源代理脚本,服务脚本在/etc/ha.d/heartbeat中。默认装起后的heartbeat没有配置文件,不过可以 从/usr/share/doc/heartbeat-2.1.4/中把ha.cf,authkeys和haresources三个文件放在/etc /ha.d中。这3个配置文件的作用是:authkeys:密钥文件,这个文件的权限必须为600,否则不能启动heartbeat服务ha.cf:heartbeat服务自身的配置文件haresources:资源代理配置文件下面只需要对这3个文件做下配置就可以实现我们的http高可用集群了。先来看authkeys文件:#auth 1 ? 提供密钥的认证方式#1 crc ? 循环冗余校验码认证#2 sha1 HI! ? sha1算法认证#3 md5 Hello! ? md5认证这里最好采用sha或者md5认证,crc的性能偏低。如果使用md5认证的配置文件如下:auth 1 # 1代码使用下面以1开头的行来作为密钥认证的条件1 md5 9adc3f50d9bb9e9c795fce0a839aa766生成md5的方式只需要在shell命令行中,输入echo “qiguo” | md5sum即可

第二个配置文件ha.cf里面的内容很多,简单介绍如下:

    #debugfile   /var/log/ha-debug #是否启用debug的日志    logfile   /var/log/ha-log  #日志文件的存放位置    #logfacility   local0  #日志的设施,如果启用了logfile,就不要启动这个选项    keepalive   2   #每隔多少时间进行心跳检测一次    #deadtime   30   #服务器经过多少时间后,还没有检测到其存在,就认为其已经掉线    #warntime   10   #警告时长    #initdead   120  #一个集群起来多久,第二个集群还没启动,则认为集群不成功    #udpport    694  #监听的端口    #baud     19200  #串行线的发送速率    bcast    eth0   #以广播的方式发送心跳检测(这里我们使用广播的方式,直接启动bcast eth0即可,这种方式在局域网中机子多的情况下,很耗费资源)    #mcast    eth0 255.0.0.1 694 1 0  #以多播的方式发送心跳检测    #ucast    eth0 192.168.1.2 #以单播的方式发送心跳检测    #auto_failback on  #主节点挂了以后,又恢复了,是否从新跳转到主节点上,on表示从新跳转。    #stonith baytech /etc/ha.d/conf/stonith.baytech  #定义stonith,怎么隔绝不在线的节点    #node ken3  #集群内的节点名称,每一个节点需要使用一个node,并且值必须与uname -n的值相同    node test1.qiguo.com    node test2.qiguo.com    #ping 10.10.10.254  #指定ping的地址    ping 192.168.1.1    #网管地址

第三个配置文件haresources文件是集群资源配置文件。上面提供了很多的配置样例,拿其中一个的样例配置文件来说明: #node1 ?10.0.0.170 Filesystem::/dev/sda1::/data1::ext2node1就是主节点的名称,10.0.0.170就是vip,Filesystem是资源代理(资源代理可以从/etc/ha.d /resource.d和/etc/init.d/从查找,”::”代表该资源代理的参数)。这里我们做http高可用,所以配置如下:test1.qiguo.com IPaddr::192.168.1.210/24/eth0 httpd即可上述三个配置文件成功后,就把他们复制到192.168.1.202这个主机上。复制完成以后,分别在两台主机上装上httpd服务。装上的httpd服 务一定不能让他们开机自动启动。如果全部配置成功以后,可以关闭httpd服务开始启动heartbeat服务了。

heartbeat[4825]: 2014/05/11_23:54:35 info: Version 2 support: falseheartbeat[4825]: 2014/05/11_23:54:35 WARN: Logging daemon is disabled --enabling logging daemon is recommendedheartbeat[4825]: 2014/05/11_23:54:35 info: **************************heartbeat[4825]: 2014/05/11_23:54:35 info: Configuration validated. Starting heartbeat 2.1.4heartbeat[4826]: 2014/05/11_23:54:35 info: heartbeat: version 2.1.4heartbeat[4826]: 2014/05/11_23:54:35 info: Heartbeat generation: 1399811242heartbeat[4826]: 2014/05/11_23:54:35 info: glib: UDP Broadcast heartbeat started on port 694 (694) interface eth0heartbeat[4826]: 2014/05/11_23:54:35 info: glib: UDP Broadcast heartbeat closed on port 694 interface eth0 - Status: 1heartbeat[4826]: 2014/05/11_23:54:35 info: glib: ping heartbeat started.heartbeat[4826]: 2014/05/11_23:54:35 info: G_main_add_TriggerHandler: Added signal manual handlerheartbeat[4826]: 2014/05/11_23:54:35 info: G_main_add_TriggerHandler: Added signal manual handlerheartbeat[4826]: 2014/05/11_23:54:35 info: G_main_add_SignalHandler: Added signal handler for signal 17heartbeat[4826]: 2014/05/11_23:54:35 info: Local status now set to: 'up'heartbeat[4826]: 2014/05/11_23:54:36 info: Link test1.qiguo.com:eth0 up.heartbeat[4826]: 2014/05/11_23:54:36 info: Link 192.168.1.1:192.168.1.1 up.heartbeat[4826]: 2014/05/11_23:54:36 info: Status update for node 192.168.1.1: status pingheartbeat[4826]: 2014/05/11_23:54:41 info: Link test2.qiguo.com:eth0 up.heartbeat[4826]: 2014/05/11_23:54:41 info: Status update for node test2.qiguo.com: status upharc[4835]:     2014/05/11_23:54:41 info: Running /etc/ha.d/rc.d/status statusheartbeat[4826]: 2014/05/11_23:54:42 info: Comm_now_up(): updating status to activeheartbeat[4826]: 2014/05/11_23:54:42 info: Local status now set to: 'active'heartbeat[4826]: 2014/05/11_23:54:42 info: Status update for node test2.qiguo.com: status activeharc[4853]:     2014/05/11_23:54:42 info: Running /etc/ha.d/rc.d/status statusheartbeat[4826]: 2014/05/11_23:54:53 info: remote resource transition completed.heartbeat[4826]: 2014/05/11_23:54:53 info: remote resource transition completed.heartbeat[4826]: 2014/05/11_23:54:53 info: Initial resource acquisition complete (T_RESOURCES(us))IPaddr[4907]:   2014/05/11_23:54:53 INFO:  Resource is stoppedheartbeat[4871]: 2014/05/11_23:54:53 info: Local Resource acquisition completed.harc[4957]:     2014/05/11_23:54:53 info: Running /etc/ha.d/rc.d/ip-request-resp ip-request-respip-request-resp[4957]:  2014/05/11_23:54:53 received ip-request-resp IPaddr::192.168.1.210/24/eth0 OK yesResourceManager[4976]:  2014/05/11_23:54:53 info: Acquiring resource group: test1.qiguo.com IPaddr::192.168.1.210/24/eth0 httpdIPaddr[5002]:   2014/05/11_23:54:53 INFO:  Resource is stoppedResourceManager[4976]:  2014/05/11_23:54:53 info: Running /etc/ha.d/resource.d/IPaddr 192.168.1.210/24/eth0 startIPaddr[5097]:   2014/05/11_23:54:53 INFO: Using calculated netmask for 192.168.1.210: 255.255.255.0IPaddr[5097]:   2014/05/11_23:54:53 INFO: eval ifconfig eth0:0 192.168.1.210 netmask 255.255.255.0 broadcast 192.168.1.255IPaddr[5068]:   2014/05/11_23:54:53 INFO:  SuccessResourceManager[4976]:  2014/05/11_23:54:53 info: Running /etc/init.d/httpd  start

观察日志,可以发现高可用的http集群已经启动起来了。现在人为的在test1这台机子上执行shutdown -h now后观察日志的变化。(也可以使用heartbeat自带的hb_standby脚本来切换,默认在/usr/lib/heartbeat目录下)

heartbeat[11796]: 2014/05/11_20:56:46 info: Received shutdown notice from 'test1.qiguo.com'.heartbeat[11796]: 2014/05/11_20:56:46 info: Resources being acquired from test1.qiguo.com.heartbeat[11862]: 2014/05/11_20:56:46 info: acquire local HA resources (standby).heartbeat[11863]: 2014/05/11_20:56:46 info: No local resources [/usr/share/heartbeat/ResourceManager listkeys test2.qiguo.com] to acquire.heartbeat[11862]: 2014/05/11_20:56:46 info: local HA resource acquisition completed (standby).heartbeat[11796]: 2014/05/11_20:56:46 info: Standby resource acquisition done [all].harc[11888]:    2014/05/11_20:56:46 info: Running /etc/ha.d/rc.d/status statusmach_down[11903]:       2014/05/11_20:56:46 info: Taking over resource group IPaddr::192.168.1.210/24/eth0ResourceManager[11928]: 2014/05/11_20:56:46 info: Acquiring resource group: test1.qiguo.com IPaddr::192.168.1.210/24/eth0 httpdIPaddr[11954]:  2014/05/11_20:56:46 INFO:  Resource is stoppedResourceManager[11928]: 2014/05/11_20:56:46 info: Running /etc/ha.d/resource.d/IPaddr 192.168.1.210/24/eth0 startIPaddr[12049]:  2014/05/11_20:56:46 INFO: Using calculated netmask for 192.168.1.210: 255.255.255.0IPaddr[12049]:  2014/05/11_20:56:46 INFO: eval ifconfig eth0:0 192.168.1.210 netmask 255.255.255.0 broadcast 192.168.1.255IPaddr[12020]:  2014/05/11_20:56:46 INFO:  SuccessResourceManager[11928]: 2014/05/11_20:56:46 info: Running /etc/init.d/httpd  startmach_down[11903]:       2014/05/11_20:56:46 info: /usr/share/heartbeat/mach_down: nice_failback: foreign resources acquiredmach_down[11903]:       2014/05/11_20:56:46 info: mach_down takeover complete for node test1.qiguo.com.heartbeat[11796]: 2014/05/11_20:56:46 info: mach_down takeover complete.

打开备服务器上的日志,注意观察,在备服务器上,已经将资源全部拿了过去,现在继续访问192.168.1.210可以看到显示的就是test2这台主 机上的内容,当test1从新上线以后,由于我们上面设置了auto_failback的值为on,所以会再次把资源拿回来,这里就不再放日志文件了。到 这里一个简单的高可用httpd服务就已经建立起来了。

由于很多情况下httpd高可用服务还会用到共享文件的服务,所以有时候需要共享文件系统。只需要在haresources中多定义一个文件系统的资源。test1.qiguo.com IPaddr::192.168.1.210/24/eth0 Filesystem::192.168.1.230:/html::/var/www/html::nfs httpd。这里采用nfs文件系统来挂载。

linux高可用集群heartbeat实现http的高可用

相关文章:

你感兴趣的文章:

标签云: