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

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

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

虚拟VIP: VIP:192.168.1.2

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

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

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

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

六、设置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'

七、安装epel扩展源 (2台都操作) [root@master ~]yum install -y epel-release 八、安装heartbeat (2台都操作) [root@master ~]# yum -y install heartbeat* httpd [root@slave ~]# yum -y install heartbeat* httpd

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

九、配置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

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

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

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

4、配置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网卡进行向外广播

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

十二、启动heartbeat服务 [root@master ha.d]# service heartbeat start

httpd的80端口已经打开

网页访问VIP地址

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

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

如果想切换至主机则 [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' 验证是否可以访问

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

查看slave服务器 [root@slave ~]# mount 显示挂载成功