天天看点

HA高可用集群

HA 即 (high available)高可用,又被叫做双机热备, 常用实现高可用的开源软件有heartbeat和keepalived,其中keepalived有负载均衡的功能。

<a href="http://s1.51cto.com/wyfs02/M02/87/29/wKioL1fWZ2-x1FKBAAHn-ygfo50221.png" target="_blank"></a>

下面我们使用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

<a href="http://s5.51cto.com/wyfs02/M01/87/29/wKioL1fWZ7XzJBtUAANsgp-lXn8942.png" target="_blank"></a>

下面操作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'

yum install -y epel-release

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!

<a href="http://s1.51cto.com/wyfs02/M02/87/2C/wKiom1fWaCqDe2lGAABMUfX3Phs502.png" target="_blank"></a>

chmod 600 authkeys

vi  haresources  //加入

aming 192.168.31.110/24/eth0:0 nginx   

aming  是主服务器的名字,   192。168。31。110是虚拟出来的IP

<a href="http://s5.51cto.com/wyfs02/M00/87/2C/wKiom1fWaIfAhJp9AAB2bAhrGh4662.png" target="_blank"></a>

nginx因为在etc/init.d下 所以可以这么写

虚拟IP要写对外提供服务的IP段

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

<a href="http://s2.51cto.com/wyfs02/M02/87/2A/wKioL1fWawjjo5fYAAAG7s-xYqk047.png" target="_blank"></a>

主节点,从节点

<a href="http://s3.51cto.com/wyfs02/M01/87/2A/wKioL1fWagmRZ7HJAABgcQvKOrU634.png" target="_blank"></a>

<a href="http://s1.51cto.com/wyfs02/M00/87/2A/wKioL1fWak3DpvE-AAAvuJ8iJZY804.png" target="_blank"></a>

<a href="http://s2.51cto.com/wyfs02/M02/87/2C/wKiom1fWapTTn3tKAADVqoRPvmI046.png" target="_blank"></a>

<a href="http://s4.51cto.com/wyfs02/M02/87/2C/wKiom1fWas2BqsHSAACWnOYM6Fo961.png" target="_blank"></a>

从上:

<a href="http://s2.51cto.com/wyfs02/M01/87/2A/wKioL1fWa12RghT5AAAkANq8ee4306.png" target="_blank"></a>

这里写IP也可以写网卡的名字(对方的)不是业务口

less /var/log/ha-log 日志文件

7.  把主上的三个配置拷贝到从上:

cd /etc/ha.d/

scp  authkeys  ha.cf haresources   aming1:/etc/ha.d/

<a href="http://s4.51cto.com/wyfs02/M00/87/2C/wKiom1fWa9eSvivYAAAc3EJoKPs688.png" target="_blank"></a>

8. 到从上(centos2) 编辑ha.cf

vi  /etc/ha.d/ha.cf   //只需要更改一个地方

ucast eth1 192.168.21.100 改为   ucast eth1 192.168.21.166  

9.  启动heartbeat : 

先主,后从

service heartbeat start

/etc/init.d/heartbeat start

10. 检查测试 

ifconfig 看是否有 eth0:0

ps aux |grep nginx  看是否有nginx进程

测试2

主上停止heartbeat服务

service heartbeat stop 

<a href="http://s2.51cto.com/wyfs02/M02/87/2C/wKiom1fWbTjiWIdWAAHYzB39nuU972.png" target="_blank"></a>

<a href="http://s5.51cto.com/wyfs02/M00/87/2C/wKiom1fWbXTQsUCkAACMhOdbTlc009.png" target="_blank"></a>

<a href="http://s2.51cto.com/wyfs02/M01/87/2C/wKiom1fWbJLh88VyAABhfOuJNYs303.png" target="_blank"></a>

<a href="http://s1.51cto.com/wyfs02/M00/87/2A/wKioL1fWbcjB_3-oAAFi2YACRyg451.png" target="_blank"></a>

本文转自 15816815732 51CTO博客,原文链接:http://blog.51cto.com/68686789/1852043

继续阅读