HA介绍

HA 即 (high available)高可用,又被叫做双机热备,用于关键性业务。 简单理解就是,有两台机器A和B,正常是A提供服务,B待命闲置,当A宕机或服务宕掉,会切换至B机器继续提供服务。常用实现高可用的开源软件有heartbeat和keepalived,其中keepalived有负载均衡的功能。

下面我们使用heartbeat来做HA集群,并且把nginx服务作为HA对应的服务。

试验准备: 两个机器, 都是centos6.5,网卡eth0 ip如下: aming 192.168.31.166 aming1 192.168.31.100 两个eth1 ip如下: aming 192.168.21.166 aming1 192.168.21.100

下面操作1-5都是在两个机器上操作

  1. hostname 设置好,分别为aming 和 aming1

  2. 关闭防火墙 iptables -F;
    关闭selinux: setenforce 0

  3. vi /etc/hosts // 增加内容如下:
    192.168.31.166 aming
    192.168.31.100 aming1

  4. 安装epel扩展源: rpm -ivh 'http://www.lishiming.net/data/attachment/forum/epel-release-6-8_32.noarch.rpm'

  5. 两个机器都安装heartbeat / libnet yum install -y heartbeat* libnet nginx

  6. 主上(aming)配置 cd /usr/share/doc/heartbeat-3.0.4/ cp authkeys ha.cf haresources /etc/ha.d/ cd /etc/ha.d vi authkeys //加入或更改为auth 3 3 md5 Hello!

chmod 600 authkeys

vi haresources //加入 aming 192.168.31.110/24/eth0:0 nginx

vi ha.cf //改为如下内容: debugfile /var/log/ha-debug logfile /var/log/ha-log logfacility local0 keepalive 2 deadtime 30 warntime 10 initdead 60 udpport 694 ucast eth1 192.168.21.100 auto_failback on node aming node aming1 ping 192.168.21.1 respawn hacluster /usr/lib/heartbeat/ipfail

  1. 把主上的三个配置拷贝到从上: cd /etc/ha.d/ scp authkeys ha.cf haresources aming1:/etc/ha.d/

  2. 到从上(aming1) 编辑ha.cf vi /etc/ha.d/ha.cf //只需要更改一个地方 ucast eth1 192.168.21.100 改为 ucast eth1 192.168.21.166

  3. 启动heartbeat : 先主,后从 service heartbeat start

  4. 检查测试 ifconfig 看是否有 eth0:0 ps aux |grep nginx 看是否有nginx进程

  5. 测试1 主上故意禁ping iptables -I INPUT -p icmp -j DROP

  6. 测试2 主上停止heartbeat服务 service heartbeat stop

  7. 测试脑裂 主和从上都down掉eth1网卡 ifdown eth1

扩展学习

关于heartbeat配置文件参考文档: http://blog.chinaunix.net/uid-20749043-id-1878328.html heartbeat和keepalived比较 http://blog.csdn.net/yunhua_lee/article/details/9788433 http://zhengdl126.iteye.com/blog/1738012 DRBD工作原理和配置 http://502245466.blog.51cto.com/7559397/1298945 视频地址: 链接: http://pan.baidu.com/s/1jG7YnaA 密码: y4t4 链接: http://pan.baidu.com/s/1o6wuJlo 密码: 4mse