HA集群配置

1HAHigh Availability)指的是通过尽量缩短因日常维护操作(计划)和突发的系统崩溃(非计划)所导致的停机时间,以提高系统和应用的可用性。它与被认为是不间断操作的容错技术有所不同。HA系统是目前企业防止核心计算机系统因故障停机的最有效手段。常使用"heartbeat"开源软件实现HA集群部署。  

2HA架构示意图

3、实验环境

[[email protected] ~]# ifconfig

eth0 Link encap:Ethernet HWaddr 00:0C:29:DF:2B:CD

inet addr:192.168.1.66 Bcast:192.168.1.255 Mask:255.255.255.0

   

[[email protected] ~]# ifconfig

eth0 Link encap:Ethernet HWaddr 00:0C:29:75:7D:E9

inet addr:192.168.1.67 Bcast:192.168.1.255 Mask:255.255.255.0

添加hosts

[[email protected] ~]# cat /etc/hosts

127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4

::1 localhost localhost.localdomain localhost6 localhost6.localdomain6

192.168.1.66 master

192.168.1.67 slave

添加扩展源

32位系统选择:

rpm -ivh http://dl.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm

rpm -ivh http://dl.fedoraproject.org/pub/epel/5/i386/epel-release-5-4.noarch.rpm

 

64位系统选择:

rpm -ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm

rpm -ivh http://dl.fedoraproject.org/pub/epel/5/x86_64/epel-release-5-4.noarch.rpm

 

导入key

rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6

rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-5

4、安装heartbeat(主从机器上都要安装)

[[email protected] ~]# yum install -y heartbeat    #安装heartbeat

[[email protected] ~]# yum install -y libnet    #安装依赖包libnet

5、配置HA

MASTER:编辑配置文件

[[email protected] ~]# cd /usr/share/

[[email protected] ~]# cd /usr/share/doc/heartbeat-3.0.4/

[[email protected] heartbeat-3.0.4]# cp authkeys ha.cf haresources /etc/ha.d/    #拷贝authkeys ha.cf haresources这三个文件到/etc/ha.d/目录下

[[email protected] ~]# vim /etc/ha.d/authkeys     #编辑authkeys配置文件,认证相关

auth 3 #加密方式选择,选项为1/2/3,只选其一即可,选定后,打开下面对应选项即可。

#1 crc

#2 sha1 HI!

3 md5 Hello!

   

[[email protected] ~]# chmod 600 /etc/ha.d/authkeys

[[email protected] ~]# vim /etc/ha.d/haresources     #编辑haresources配置文件,主要配置同步参数

master 192.168.1.65/24/eth0 nginx #主要配置这一行,格式为:hostname Virtual IP/掩码/网卡同步的资源

[[email protected] ~]# vim /etc/ha.d/ha.cf     #编辑ha.cf配置文件

   

debugfile /var/log/ha-debug    #debug日志

logfile /var/log/ha-log    #ha日志

logfacility local0    #日志级别

keepalive 2    #探测间隔时间,单位是秒

deadtime 30    #死亡时间,单位是秒

warntime 10    #告警时间,记录到logfile中,单位是秒

initdead 60    #等待重启的时间,单位是秒,一般配置为deadtime的两倍

udpport 694    #心跳线通信端口

ucast eth0:1 10.0.0.2    #配置对方IP

auto_failback on    #启动抢占模式

node master    #节点名称

node slave    #节点名称

ping 192.168.112.1    #仲裁检测地址

respawn hacluster /usr/lib64/heartbeat/ipfail    #使得Heartbeathacluster的身份来执行ipfail进程并监视该进程的执行情况,如果其死亡便重启之。ipfail脚本用于检测网络连通性。

SLAVE:编辑配置文件

[[email protected] ~]# vim /etc/ha.d/authkeys     #slave上的authkeymaster上的保持一致

auth 3 #加密方式选择,选项为1/2/3,只选其一即可,选定后,打开下面对应选项即可。

#1 crc

#2 sha1 HI!

3 md5 Hello!

   

[[email protected] ~]# chmod 600 /etc/ha.d/authkeys

[[email protected] ~]# vim /etc/ha.d/haresources     #slave上的haresourcesmaster上的保持一致

master 192.168.1.65/24/eth0:1 nginx #主要配置这一行,格式为:hostname Virtual IP/掩码/网卡同步的资源

[[email protected] ~]# vim /etc/ha.d/ha.cf     #编辑ha.cf配置文件

   

debugfile /var/log/ha-debug    #debug日志

logfile /var/log/ha-log    #ha日志

logfacility local0    #日志级别

keepalive 2    #探测间隔时间,单位是秒

deadtime 30    #死亡时间,单位是秒

warntime 10    #告警时间,记录到logfile中,单位是秒

initdead 60    #等待重启的时间,单位是秒,一般配置为deadtime的两倍

udpport 694    #心跳线通信端口

ucast eth0:1 10.0.0.1    #配置对方IP

auto_failback on    #启动抢占模式

node master    #节点名称

node slave    #节点名称

ping 192.168.112.1    #仲裁检测地址

respawn hacluster /usr/lib64/heartbeat/ipfail    #使得Heartbeathacluster的身份来执行ipfail进程并监视该进程的执行情况,如果其死亡便重启之。ipfail脚本用于检测网络连通性。

6、测试HA一定要先将主机和备机的时间同步

启动heartbeat,先启动master,然后启动slave

[[email protected] ha.d]# service heartbeat start

Starting High-Availability services: INFO: Resource is stopped

Done.

   

[[email protected] ~]# service heartbeat start

Starting High-Availability services: INFO: Resource is stopped

Done.

查看heartbeat运行状态

[[email protected] ~]# service heartbeat status

heartbeat OK [pid 1937 et al] is running on master [master]...

 

[[email protected] ~]# service heartbeat status

heartbeat OK [pid 2298 et al] is running on slave [slave]...