使用HeartBeat实现高可用HA的配置过程详解

1、前言
HA即(high available)高可用,又被叫作双机热备,用于关键性业务。简单理解就是,有2台机器 A 和 B,正常是 A 提供服务,B 待命闲置,当 A 宕机或服务宕掉,会切换至B机器继续提供服务。常见的实现高可用的开源软件有 heartbeat 和 keepalivedhtml

2、准备实验环境
服务器A:
主机名:master
操做系统:CentOS6.5 64位
eth0网卡地址:192.168.1..3node

服务器B:
主机名:slave
操做系统:CentOS6.5 64位
eth0网卡地址:192.168.1.4linux

虚拟VIP:
VIP:192.168.1.2web

3、设置主机名
A服务器
[root@localhost ~]# hostname master
[root@localhost ~]# vim /etc/sysconfig/network
HOSTNAME=mastervim

B服务器
[root@localhost ~]# hostname slave
[root@localhost ~]# vim /etc/sysconfig/network
HOSTNAME=slave服务器

4、关闭防火墙和Selinux(2台节点都要操做)
[root@localhost ~]# setenforce 0
[root@localhost ~]# service iptables stop
[root@localhost ~]# chkconfig iptables off网络

5、配置hosts文件(2台节点都操做)
[root@localhost ~]# vim /etc/hosts
192.168.1.3 master
192.168.1.4 slavessh

6、设置ssh互信(2台节点都操做)
[root@master ~]# ssh-keygen -t rsa -f ~/.ssh/id_rsa -P ''
[root@master ~]# ssh-copy-id -i .ssh/id_rsa.pub root@192.168.1.4
测试
[root@master ~]# ssh 192.168.1.4 'ifconfig'ide

7、安装epel扩展源 (2台都操做)
[root@master ~]yum install -y epel-release
8、安装heartbeat (2台都操做)
[root@master ~]# yum -y install heartbeat httpd
[root@slave ~]# yum -y install heartbeat
httpd测试

8、配置时间同步(2台都操做)
[root@master ~]# date -s "2019-02-27“
2019年 02月 27日 星期三 00:00:00 CST
[root@master ~]# date -s "13:50:30"

9、配置httpd
[root@master ~]# echo "Master" > /var/www/html/index.html
[root@master ~]# service httpd start
[root@master ~]# service httpd stop
[root@master ~]# chkconfig httpd off

10、配置Heartbeat
一、拷贝三个配置文件
[root@master ~]# cd /etc/ha.d/
[root@master ha.d]# cp -r /usr/share/doc/heartbeat-3.0.4/{authkeys,ha.cf,haresources} ./

二、修改authkeys
[root@master ha.d]# vim authkeys
auth 1
1 md5 5t2dhlk6sdrf8dshnm3
更改authkeys的权限
[root@master ha.d]# chmod 600 authkeys

三、编辑haresources文件
[root@master ha.d]# vim haresources
master IPaddr::192.168.1.2/24/eth0:0 httpd

四、配置ha.cf文件
[root@master ha.d]# vim ha.cf
bcast eth0
node master
node slave

配置说明
debugfile /var/log/ha-debug:该文件保存heartbeat的调试信息。
logfile /var/log/ha-log:heartbeat的日志文件。
keepalive 2:心跳的时间间隔,默认时间单位为秒s。
deadtime 30:超出该时间间隔未收到对方节点的心跳,则认为对方已经死亡。
warntime 10:超出该时间间隔未收到对方节点的心跳,则发出警告并记录到日志中。
initdead 60:在某系统上,系统启动或重启以后须要通过一段时间网络才能正常工做,该选项用于解决这种状况产生的时间间隔,取值至少为deadtime的2倍。
udpport 694:设置广播通讯使用的端口,694为默认使用的端口号。
bcast eth0 :经过eth0网卡进行向外广播

11、把主节点上的三个配置文件拷贝到从节点
[root@master ~]#cd /etc/ha.d
[root@master ha.d]#scp authkeys ha.cf haresources slave:/etc/ha.d

12、启动heartbeat服务
[root@master ha.d]# service heartbeat start
使用HeartBeat实现高可用HA的配置过程详解

httpd的80端口已经打开
使用HeartBeat实现高可用HA的配置过程详解

网页访问VIP地址
使用HeartBeat实现高可用HA的配置过程详解

十3、切换至备机
必须远程启动备机的heartbeat
[root@master heartbeat]# ssh slave 'service heartbeat start'

[root@master ha.d]# cd /usr/share/heartbeat/
[root@master heartbeat]# ./hb_standby

使用HeartBeat实现高可用HA的配置过程详解

若是想切换至主机则
[root@slave ~]# cd /usr/share/heartbeat
[root@slave heartbeat]# ./hb_standby

HA挂载NFS系统配置

准备一台NFS服务器
操做系统:CentOS7.5 64位
eth0网卡地址:192.168.1.5
主机名:localhost
[root@localhost ~]#mkdir /web/htdocs -pv
[root@localhost ~]#vim /etc/exports
/web/htdocs 192.168.1.0/24(ro)
[root@localhost ~]#yum -y install nfs* --skip-broken
[root@localhost ~]#systemctl start nfs
[root@localhost ~]#cd /web/htdocs/
[[root@localhost htdocs]#vim index.html
NFS server
[root@localhost ~]#systemctl start rpcbind
[root@localhost ~]#systemctl stop firewalld
[root@localhost ~]#setenforce 0

服务器(2台都操做)
[root@master ~]# mount 192.168.1.5:/web/htdocs /mnt
[root@master ~]# ls /mnt/
index.html
[root@master ~]# cat /mnt/index.html
NFS server
[root@master ~]# umount /mnt/
[root@master ~]# cd /etc/ha.d/
[root@master ha.d]# vim haresources
master 192.168.1.2/24/eth0:0 Filesystem::192.168.1.5:/web/htdocs::/var/www/html::nfs httpd
[root@master ha.d]# scp haresources slave:/etc/ha.d/
[root@master ha.d]# service heartbeat start
[root@master ha.d]# ssh slave 'service heartbeat start'
验证是否能够访问
使用HeartBeat实现高可用HA的配置过程详解

设置master为备机
[root@master ha.d]# cd /usr/share/heartbeat/
[root@master heartbeat]# ./hb_standby

查看slave服务器
[root@slave ~]# mount
使用HeartBeat实现高可用HA的配置过程详解显示挂载成功