【准备】

主备heartbeat设置主机名

vim /etc/sysctl.conf

net.ipv4.ip_forward = 1 开启路由转发

sysctl –p

【安装】

配置好yum源(CentOS系统不许要配置)

yum install heartbeat-*

【配置】

配置文件都在/etc/ha.d/里面,主要包括下面四个配置文件

authkeys                    [认证文件]

haresources              [资源文件]

ldirectord.cf              [ldirectord配置文件]

ha.cf                                     [主配置文件]

可以从目录/usr/share/doc/heartbeat-2.1.3/ /usr/share/doc/heartbeat-ldirectord-2.1.3/ 复制到/etc/ha.d/里面,也可以直接手动创建

文件内容

[authkeys文件]

auth 2
2 sha1 HI!

[ha.cf文件]

logfile         /var/log/ha-log.log                         
#日志文件位置
logfacility     local0                                                    
ucast          eth0 192.168.1.100                     
#指定为单播方式,如果为多个节点则启用mcast或bcast,此处为对端IP
keepalive       2                                                        
#发送心跳报文的间隔
warntime        8                                                      #超过此间隔未收到报文,日志里报警,但不切换
deadtime        10                                                    #超过此间隔未收到报文,则认为对方已经宕掉,进行切换
initdead        120                                                    #heartbeat重启后,启动其所控制的资源的时间
udpport         694                                                 
#心跳检查的端口
auto_failback   off                                                       
#主节点重启成功后,on为再切回主节点工作,off为不切换
watchdog /dev/watchdog                                            
#启动内核看门狗功能,能让系统出现故障后重启该机器
node            ha.server01                               
#主节点名称
node            ha.server02                                
#备节点名称
ping            192.168.1.1                        
#伪集群节点,测试本机是否还能连通网络
respawn hacluster/usr/lib/heartbeat/ipfail           
#指定当heartbeat启动时执行的程序,ipfail会判断伪集群节点是否正常
apiauth         ipfail uid=hacluster gid=haclient 
#指定ipfail的用户和组

[haresources文件]

ha.server01\                                                                                       
#主节点主机名,主备都一样
IPaddr:: 192.168.1.100/24/eth0/192.168.1.255\          
#资源1:在eth0上启用IP别名
ldirectord::ldirectord.cf\                                                                 #资源2:启动ldirectord服务
LVSSyncDaemonSwap::master                                                       #资源3:启动ipvsadm服务

[ldirectord.cf文件]

checktimeout=3                                                                                  #ldirectord等待健康检查执行完毕的等待时间,单位秒。如果因为某些原因检查失败或在设置的时间周期内没有完成检查,ldirectord将会从IPVS表中移除真实服务器
checkinterval=3                                                                                  #检查间隔
autoreload=yes                                                                                   #启用这个选项,会自动应用ldirectord.cf文件的改动
logfile="/var/log/ldirectord.log"                                                    #指定日志文件
quiescent=no                                                                                       #当检查检查失败或者超时后执行的策略(no:会将realserver移除,yes会将realserver权重置为0)
virtual=192.168.1.100:80                                                                #vip及其端口
        real=192.168.1.101:80 gate   1                                   #realserver:gate表示路由模式,ipip表示隧道模式,masp为nat模式后面是权重值。(此处必须缩进)
        real=192.168.1.102:80 gate   2
        real=192.168.1.103:80 gate   2
        service=http                                                                      #这一行指出测试真实服务器的健康时ldirectord使用的服务
        scheduler=wlc                                                                  #lvs算法
        persistent=240                                                                 #会话保持时间
        protocol=tcp                                                                     #这一行指出该服务使用的协议
        checktype=negotiate                                                      #这个选项指出ldirectord守护进程使用什么方法监视真实服务器
        checkport=80                                                                    #指定健康检查使用的端口是80。
virtual=192.168.1.100:443
        real=192.168.1.100:443 gate
        real=192.168.1.100:443 gate
        real=192.168.1.100:443 gate
        service=https
        scheduler=wlc
        persistent=240
        protocol=tcp
        checktype=negotiate
        checkport=443

PS:详细参数看官方文档

http://www.linux-ha.org/ha.cf

http://www.linux-ha.org/authkeys

http://linux-ha.org/GettingStarted#configuringharesources


将相关服务设为开机自启动

chkconfig heartbeat on
chkconfig ldirectord on

【管理】

/etc/init.d/heartbeat start 启动heartbeat服务
/usr/lib/heartbeat/hb_standby 将服务切换至对方上
/usr/lib/heartbeat/hb_takeover 将服务从备上切换到本地上
ipvsadm -ln    查看lvs状态
ipvsadm -ln -rate 查看lvs连接速率信息
ipvsadm -ln -stats 查看lvs统计信息


--rate选项是显示速率信息
1. CPS      (current connection rate)   每秒连接数
2. InPPS    (current in packet rate)    每秒的入包个数
3. OutPPS   (current out packet rate)   每秒的出包个数
4. InBPS    (current in byte rate)      每秒入流量(字节)
5. OutBPS   (current out byte rate)      每秒入流量(字节)


--stats选项是统计自该条转发规则生效以来的

1. Conns    (connectionsscheduled)  已经转发过的连接数
2. InPkts   (incoming packets)       入包个数
3. OutPkts  (outgoing packets)       出包个数
4. InBytes  (incoming bytes)         入流量(字节)
5. OutBytes (outgoing bytes)         出流量(字节)



【客户端】

路由模式需要在web上执行realserver_dr 脚本,并设为开机自动执行

隧道模式需要在web上执行realserver_ipip 脚本,并设为开机自动执行

【防火墙】

需开启694端口,允许udp协议


备注:

设置apache不记录hearbeat检测的配置(httpd.conf

174938792.jpg