HA-高可用集群

原理:两台web服务器,通过心跳线进行通信,当主节点出现服务异常,备用节点通过探测判断主节点是否存活,若是不存活,就把服务接管过来。

Web1和Web2中间有一根心跳线,检查对方的存活状态。流动IP:也叫vip是对外提供服务的ip,正常情况下是配置在Web1上的,当Web1宕机后,Web2会自动配置该vip,对外提供服务。
 
需求:在线上环境有一台nginx服务器提供对外web访问,为了防止服务器突然宕机造成业务损失,则需要两台服务器提供高可用集群一主一备服务,主的服务器宕机,备的服务器立即接管主的继续对外提供访问,让业务不受影响。

实验环境所需要工具:heartbeat、libnet、nginx

默认是没有heartbeat包,需要安装扩展源:epel
epel下载地址:wget  www.lishiming.net/data/attachment/forum/epel-release-6-8_32.noarch.rpm
 
安装工具:yum -y install heartbeat
安装工具:yum -y install libnet
 
master IP: 192.168.2.254
slave IP:192.168.2.253
 
一、master服务器配置
 
1:修改hosts文件
[root@master ~]# vim /etc/hosts
说明:将主和备份的服务器地址和主机写入hosts文件,将红色的字段复制到备份的文件
 
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.2.254 master
192.168.2.253 slave
 
 
 
2:切换到hearteat工具文件目录
[root@master ~]cd /usr/share/doc/heartbeat-3.0.4/
[root@master heartbeat-3.0.4]# ls
apphbd.cf  authkeys  AUTHORS  ChangeLog  COPYING  COPYING.LGPL  ha.cf  haresources  README
 
  
3:拷贝authkeys、ha.cf、haresources三个配置文件
[root@master heartbeat-3.0.4] cp authkeys ha.cf haresources /etc/ha.d/
[root@master heartbeat-3.0.4] cd /etc/ha.d/
 
4:编辑配置文件
[root@master ha.d ] vim authkeys
auth 3  ##这里定义的是你下面所开启的级别
#1 crc
#2 sha1 HI!
3 md5 Hello!
 
5:aurhkeys文件权限需要设置600
[root@master ha.d ] chmod 600 authkeys
 
6:配置虚接口和编辑vip的文件
[root@master ha.d]# cd /etc/sysconfig/network-scripts/
[root@master network-scripts]# cp ifcfg-eth0 ifcfg-eth0:1
[root@master network-scripts]# vim ifcfg-eth0:1
加入下面的内容
DEVICE=eth0:1
TYPE=Ethernet
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=static
IPADDR=192.168.2.110
NETMASK=255.255.255.0
 
7:重启网络服务
[root@master network-scripts]# service network  restart
 
正在关闭接口 eth0:                                     [确定]
关闭环回接口:                                             [确定]
弹出环回接口:                                             [确定]
弹出界面 eth0: Determining if ip address 192.168.2.254 is already in use for device eth0...
Determining if ip address 192.168.2.110 is already in use for device eth0...
                                                           [确定]
 
8:重启服务和编辑配置文件
[root@master network-scripts]# vim haresources
说明:
master ##表示是主的主机名
192.168.2.110/24 ##表示流动vip也就是虚接口地址
eth0:0 ##表示你所用的网卡
nginx ##表示你用什么服务做的集群,这里用的是Nginx,根据自己使用情况设置
 
加入下面内容
master  192.168.2.110/24/eth0:1 nginx
 
9:编辑ha.cf文件
[root@master ha.d]# vim ha.cf
debugfile /var/log/ha-debug
logfile /var/log/ha-log ##设置系统日志的信息范围为 local0
logfacility     local0 ##这是日志的级别
keepalive 2  ##设置心跳的检测时间,默认单位为秒
deadtime 30 ##宣布节点死亡的时间,单位为秒
warntime 10 ##通过这个来调整判断节点的死亡时间
initdead 60 ##Heartbeat 启动资源的等待时间,单位为秒
udpport 694 ##设定用于bcast和ucast通信的端口,默认为 UDP 694
ucast eth0 192.168.2.253 ##心跳网卡和对方的ip地址
auto_failback on ##这个设定会使节点具备优先级。当服务运行于备机上时,如果主节点已恢复,会自动取回运行在备机上的服务。
node    master ##设置主的主机名
node    slave ##设置备份的主机名
ping 192.168.2.1 ##指定ping节点,检测网络状态,通常是网关
respawn hacluster /usr/lib/heartbeat/ipfail ##设定 Heartbeat 监控的服务,该服务会随着 Heartbeat 服务启动时启动,停止时则一起停止。当出现意外导致该服务停止时,Heartbeat 将自动重启该服务
 
10:复制三个配置文件到备份服务器
[root@master ha.d]# scp authkeys ha.cf haresources slave:/etc/ha.d/
The authenticity of host 'slave (192.168.2.253)' can't be established.
RSA key fingerprint is 74:0a:ee:44:ae:1a:9f:86:47:43:92:4b:c0:02:29:0b.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'slave,192.168.2.253' (RSA) to the list of known hosts.
root@slave's password:
authkeys                                                                           100%  643     0.6KB/s   00:00    
ha.cf                                                                                  100%  331     0.3KB/s   00:00    
haresources                                                                      100% 5888     5.8KB/s   00:00
 
二、slave服务器配置
 
1:修改hosts文件
[root@slave ~]# vim /etc/hosts
 
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.2.254 master
192.168.2.253 slave
 
2:在备份服务器slave里只需要编辑ha.cf配置文件,其它两个文件默认
[root@slave src]# cd /etc/ha.d/
[root@slave ha.d]# ls
authkeys  ha.cf  harc  haresources  rc.d  README.config  resource.d  shellfuncs
[root@slave ha.d]# vim 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 eth0 192.168.2.254 ##改成master的地址
auto_failback on
node    master
node    slave
ping 192.168.2.2
respawn hacluster /usr/lib/heartbeat/ipfail
 
三、启动heartbeat、测试结果
说明:在高可用集群中启动heartbeat是按照先启动主,再启动备份的顺序
 
1:启动master,关闭iptables和selinux
[root@master ha.d]# iptables -F
[root@master ha.d]# setenforce 0
[root@master ha.d]# /etc/init.d/heartbeat start
Starting High-Availability services: INFO:  Resource is stopped
Done.
 
2:启动slave,关闭iptables和selinux
[root@slave ha.d]# iptables -F
[root@slave ha.d]# setenforce 0
[root@slave ha.d]# /etc/init.d/heartbeat start
Starting High-Availability services: INFO:  Resource is stopped
Done.
 
3:测试,使master服务器不能ping通slave的服务器,让master死掉,slave的继承
更改下主页文件
[root@master ~]# echo "1111111111111111111master" > /usr/share/nginx/html/index.html
 
 
4:用curl测试
[root@master ~]# curl -x 127.0.0.1:80 192.168.2.110
1111111111111111111master
 
5:使用命令禁掉主Ping工具,让其死掉
[root@master ha.d]# iptables -A INPUT -p icmp -j DROP
 
6:在备份服务查看有没有虚接口过来
[root@slave ha.d]# ifconfig
eth0:1    Link encap:Ethernet  HWaddr 00:0C:29:4D:F5:D8  
          inet addr:192.168.1.110  Bcast:192.168.1.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          Interrupt:19 Base address:0x2000
 
7:curl访问测试
[root@slave ha.d]# curl -x127.0.0.1:80 192.168.2.110
222222222222slave
##备用的服务器成功继承,高可用集群实验成功
原文地址:https://www.cnblogs.com/douyi/p/11631519.html