使用heartbeat来做HA集群

HA 即 (high available)高可用,又被叫做双机热备。


用于关键性业务。 简单理解就是,有两台机器A和B,正常是A提供服务,B待命闲置,当A宕机或服务宕掉,会切换至B机器继续提供服务。



两个机器, 都是centos6.5,网卡eth1 ip如下:

bier   192.168.1.166

bier1  192.168.1.165


1. hostname 设置好,分别为bier  和 bier1 


2. 关闭防火墙 iptables -F;  

        getenforce

    关闭selinux: setenforce 0

3. vi /etc/hosts   // 增加内容如下

  

192.168.1.166 bier   

192.168.1.165 bier1  



4. 安装epel扩展源:

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

5. 两个机器都安装heartbeat / libnet


yum  install -y heartbeat*   libnet  nginx



6.  主上(bier)配置


cd /usr/share/doc/heartbeat-3.0.4/


cp  authkeys  ha.cf haresources   /etc/ha.d/


cd /etc/ha.d


vi  authkeys  //加入,口令,首位呼应

3

3 md5 Hello!


chmod 600 authkeys



vi  haresources  //加入


bier 192.168.1.10/24/eth1:0 nginx   



vi  ha.cf   //改为如下内容:

debugfile /var/log/ha-debug

logfile /var/log/ha-log

logfacility     local0

keepalive 2  #2秒检测一次是不是宕机

deadtime 30  #30秒检测一次是不是可以ping通

warntime 10   #记录日志,发生一个警告

initdead 60  #机器有时候需要重启预留60秒的时间出来

udpport 694    #启动一个端口通信

ucast eth1 192.168.1.165   #通信的ip

auto_failback on  #从接手了

node    bier

node    bier1

ping 192.168.1.1   #谁都去ping网关

respawn hacluster /usr/lib/heartbeat/ipfail   #监听服务,检测网络状况,宕机让他启动



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

cd /etc/ha.d/

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

==========从这个机器复制到别的机器


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


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


ucast eth1 192.168.1.165 改为   ucast eth1 192.168.1.166  #改为对方的ip



9.  启动heartbeat : 


先主,后从


/etc/init.d/heartbeat start 或者


service heartbeat start


日志 less /var/log/ha-log


10. 检查测试 

ifconfig 看是否有 eth1:0

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


11.  测试1


主上禁ping


iptables -I INPUT -p icmp -j DROP


12. 测试2


主上停止heartbeat服务

service heartbeat stop 






1、安装依赖包(node1和node2同步安装)


[root@node2 ~]# yum -y install autoconf automake gcc-c++ asciidoc libxslt-devel libtool libtool-ltdl-devel libxml2 libxml2-devel bzip2-devel glib2-devel mercurial *openssl* net-snmp OpenIPMI flex bison e2fsprogs-devel


2、源码编译安装


下载地址http://linux-ha.org/wiki/Download

#下载heartbeat

[root@node2 ~]# wget http://hg.linux-ha.org/heartbeat-STABLE_3_0/archive/958e11be8686.tar.bz2

#下载cluster glue

[root@node2 ~]# wget http://hg.linux-ha.org/glue/archive/0a7add1d9996.tar.bz2

#下载cluster resource agents    

[root@node1 ~]# wget https://github.com/ClusterLabs/resource-agents/archive/v3.9.6.tar.gz

3、创建用户与组


[root@node1 ~]# groupadd haclient

[root@node1 ~]# useradd -g haclient hacluster -M -s /sbin/nologin

4、编译


cluster glue

[root@node1 ~]# tar xf 0a7add1d9996.tar.bz2 

[root@node1 ~]# cd Reusable-Cluster-Components-glue--0a7add1d9996/

[root@node1 Reusable-Cluster-Components-glue--0a7add1d9996]# ./autogen.sh

]# ./configure --prefix=/usr/local/heartbeat --sysconfdir=/etc/heartbeat libdir=/usr/local/heartbeat/lib64 LIBS='/lib64/libuuid.so.1' --with-daemon-user=hacluster --with-daemon-group=haclient

#LIBS如果是32位系统自行更改

]# make && make install

 

resource agents

[root@node1 ~]# tar xf resource-agents-3.9.6.tar.gz

[root@node1 ~]# cd resource-agents-3.9.6

[root@node1 resource-agents-3.9.6]# ./autogen.sh 

[root@node1 resource-agents-3.9.6]# ./configure --prefix=/usr/local/heartbeat --sysconfdir=/etc/heartbeat libdir=/usr/local/heartbeat/lib64 CFLAGS=-I/usr/local/heartbeat/include LDFLAGS=-L/usr/local/heartbeat/lib64 LIBS='/lib64/libuuid.so.1' --with-daemon-user=hacluster --with-daemon-group=haclient

[root@node1 resource-agents-3.9.6]# make && make install

 

heartbeat

[root@node1 ~]# tar xf 958e11be8686.tar.bz2 

[root@node1 ~]# cd Heartbeat-3-0-958e11be8686/

[root@node1 Heartbeat-3-0-958e11be8686]# ./bootstrap 

]# ./configure --prefix=/usr/local/heartbeat --sysconfdir=/etc/heartbeat CFLAGS=-I/usr/local/heartbeat/include LDFLAGS=-L/usr/local/heartbeat/lib64 LIBS='/lib64/libuuid.so.1' --with-daemon-user=hacluster --with-daemon-group=haclient

 

#编译报错,路径重复,google后得出删除 glue_config.h 中配置文件路径即可

[root@node1 Heartbeat-3-0-958e11be8686]# make && make install

../include/config.h:390:1: error: this is the location of the previous definition

gmake[1]: *** [strlcpy.lo] Error 1

gmake[1]: Leaving directory `/root/Heartbeat-3-0-958e11be8686/replace'

make: *** [all-recursive] Error 1

 

[root@node1 Heartbeat-3-0-958e11be8686]# vim /usr/local/heartbeat/include/heartbeat/glue_config.h

define HA_HBCONF_DIR "/usr/local/heartbeat/etc/ha.d/"

#删除最后一行即上行内容

5、复制配置文件至/etc//heartbeat/ha.d中


[root@node1 Heartbeat-3-0-958e11be8686]# cp doc/ha.cf /etc/heartbeat/ha.d/

[root@node1 Heartbeat-3-0-958e11be8686]# cp doc/haresources /etc/heartbeat/ha.d/

[root@node1 Heartbeat-3-0-958e11be8686]# cp doc/authkeys /etc/heartbeat/ha.d/

6、将heartbeat加入系统服务,并开机启动


[root@node1 ~]# chkconfig --add heartbeat


#之后可以用service来进行start|stop操作了


[root@node1 ~]# chkconfig heartbeat on


7、修改认证文件权限为600,不然heartbeat无法工作


[root@node1 ~]# chmod 600  /etc/heartbeat/ha.d/authkeys

8、为resource-agents建立脚本软连接


[root@node1 ~]# ln -s /usr/local/heartbeat/usr/lib/ocf /usr/lib/ocf



1、配置authkeys文件,指明启用何种算法,使用何种密钥,本文件须更改权限为400

auth 2

#1 crc

2 sha1 2SIEok+gXAvB6G4seA8mhw

#3 md5 Hello!

  

生成随机字符串作为密钥

[root@node2 ~]# openssl rand -base64 16

2SIEok+gXAvB6G4seA8mhw==

  

  

2、配置ha.cf文件,定义高可用集群的基本工作方式

  

定义日志文件位置(二选一) logfacility为将日志交由syslog管理     

logfile        /var/log/ha-log

#logfacility     local0

多长时间发送一次心跳信息,默认为2秒

#keepalive 2

多长时间宣布某节点死亡,默认30秒

#deadtime 30

多长时间警告对方心跳信息延迟了,默认10秒

#warntime 10

第一次死去时间,避免因网络问题导致宣布死亡

#initdead 120

使用udp694端口传递心跳,并选择哪种方式传递心跳

#udpport        694

串行线缆传递心跳

#serial /dev/ttyS0      # Linux

#serial /dev/cuaa0      # FreeBSD

#serial /dev/cuad0      # FreeBSD 6.x

#serial /dev/cua/a      # Solaris

串行线缆的工作频率

#baud   19200

广播传递心跳

#bcast  eth0            # Linux

#bcast  eth1 eth2       # Linux

#bcast  le0             # Solaris

#bcast  le1 le2         # Solaris

多播传递心跳,网卡必须支持多播,ifconfig | grep MULTICAST

mcast eth0 225.0.0.1 694 1 0          #端口694,TTL为1,不允许回传为0

#启用网卡支持多播

[root@node1 ha.d]# ip link set eth0 multicast on

  

单薄传递心跳

#ucast eth0 192.168.1.2

自动故障转回

auto_failback on

指明节点

#node   ken3

#node   kathy

node    node1

node    node2

指明网关为ping node设备(仲裁设备)

#ping 10.10.10.254

ping 192.168.0.1

指明一个组为ping node设备(仲裁设备)

#ping_group group1 10.10.10.254 10.10.10.253

指明节点间传送的压缩算法

compression     bz2

指明节点间传送数据压缩的最小数据为2KB

compression_threshold 2

 

3、配置haresources文件,定义集群资源直接加入资源

node1 192.168.0.17/24/eth0/192.168.0.255 httpd

 

4、将httpd设置为开机不启动

[root@node1 ha.d]# chkconfig httpd off

 

5、启动服务

[root@node1 ~]# service heartbeat start











本文转自 chengxuyonghu 51CTO博客,原文链接:http://blog.51cto.com/6226001001/1912830,如需转载请自行联系原作者
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值