使用HeartBeat实现高可用HA的配置。HA即(high available)高可用,又被叫做双机热备,用于关键性业务。简单理解就是,有2台机器 A 和 B,正常是 A提供服务,B待命闲置,当 A 宕机或服务宕掉,会切换至B机器继续提供服务。常见的实现高可用的开源软件有 heartbeat 和 keepalived。
一、准备实验环境
服务器A:
主机名:Master01
操作系统:CentOS6.8 64位
eth0网卡地址:10.60.196.178
服务器B:
主机名:Slave01
操作系统:CentOS6.8 64位
eth0网卡地址:10.60.196.179
虚拟VIP:
VIP:10.60.196.180
二、设置主机名
master节点设置hostname
hostname Master01 vim /etc/sysconfig/network 编辑配置文件:HOSTNAME=Master01
vim /etc/hosts
编辑配置文件:
10.60.196.178 Master01
10.60.196.179 Slave01
# vim /etc/sysconfig/network 编辑配置文件:HOSTNAME=Master01
slave节点设置hostname
# hostname Slave01
vim /etc/hosts
编辑配置文件:
10.60.196.178 Master01
10.60.196.179 Slave01# vim /etc/sysconfig/network 编辑配置文件:HOSTNAME=Slave01
三、关闭2台服务器的防火墙
关闭iptables
# iptables -F # service iptables save # service iptables stop
关闭selinux:
# setenforce 0 # sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
四、安装heartbeat (2台都操作)
# yum install -y epel-release
# yum install -y heartbeat* libnet
五、主Master01节点配置
1、拷贝配置文件:
# cd /usr/share/doc/heartbeat-3.0.4/ # cp authkeys ha.cf haresources /etc/ha.d/ # cd /etc/ha.d
2、修改authkeys:
# vim authkeys 更改或增加如下内容: auth 33 md5 Hello!然后修改其权限 # chmod 600 authkeys
3、编辑haresources文件:
# vim haresources加入下面一行:Master01 10.60.196.180/24/eth0:0 raysync
说明:master为主节点hostname,10.60.196.180为vip,/24为掩码为24的网段,eth0:0为vip的设备名,httpd为heartbeat监控的服务,也是两台机器对外提供的核心服务。改行指定在启动时,节点得到vip,并启动httpd,在停止时heartbeat首先停止httpd,然后释放vip。
4、编辑ha.cf:
# vim ha.cf 修改为如下内容:
logfile /var/log/ha-log
logfacility local0
keepalive 2
deadtime 30
warntime 10
initdead 120
udpport 694
ucast eth0 10.60.196.179
auto_failback on
node Master01
node Slave01
ping 10.60.196.254
respawn hacluster /usr/lib64/heartbeat/ipfail
5、配置说明:
logfile /var/log/ha-log:heartbeat的日志文件。
keepalive 2:心跳的时间间隔,默认时间单位为秒s。
deadtime 30:超出该时间间隔未收到对方节点的心跳,则认为对方已经死亡。
warntime 10:超出该时间间隔未收到对方节点的心跳,则发出警告并记录到日志中。
initdead 120:在某系统上,系统启动或重启之后需要经过一段时间网络才能正常工作,该选项用于解决这种情况产生的时间间隔,取值至少为deadtime的2倍。
udpport 694:设置广播通信使用的端口,694为默认使用的端口号。
ucast eth0 10.60.196.179:设置对方机器心跳检测的网卡和IP。
auto_failback on:heartbeat的两台主机分别为主节点和从节点。主节点在正常情况下占用资源并运行所有的服务,遇到故障时把资源交给从节点由从节点运行服务。在该选项设为on的情况下,一旦主节点恢复运行,则自动获取资源并取代从节点,否则不取代从节点。
respawn heartbeat /usr/lib/heartbeat/ipfail:指定与heartbeat一同启动和关闭的进程,该进程被自动监视,遇到故障则重新启动。最常用的进程是ipfail,该进程用于检测和处理网络故障,需要配合ping语句指定的ping node来检测网络连接。如果你的系统是64bit,请注意该文件的路径。
六、把主节点上的三个配置文件拷贝到从节点
# cd /etc/ha.d # scp authkeys ha.cf haresources root@Slave01:/etc/ha.d
七、从节点slave编辑ha.cf
# vim /etc/ha.d/ha.cf
只需要更改一个地方如下:ucast eth0 10.60.196.179改为ucast eth0 10.60.196.180
八、启动heartbeat服务
配置完毕后,先Master01启动,后Slave01启动。
# service heartbeat start
九、更改vip路由脚本
1、创建routecheck.sh脚本
vim /root/bin/routecheck.sh
#!/bin/bash
source /etc/profile
for((i=1;i<=30;i++))
do
ifconfig|grep eth0:0
a=$?
ip route show |grep 10.60.196.180
b=$?
ip route show|grep 10.60.196.254
c=$?
#vip存在,且vip没有路由就更改路由
if [[ $a == 0 ]] && [[ $b != 0 ]];then
route del default gw 10.60.196.254 && ip route add 0.0.0.0/0 via 10.60.196.254 dev eth0 src 10.60.196.180
fi
#vip不存在,默认网关不存在就重启网卡
if [[ $a != 0 ]] && [[ $c != 0 ]];then
ip route add 0.0.0.0/0 via 10.60.196.254 dev eth0
fi
sleep 2
done
2、定时运行
crontab -e
*/1 * * * * bash /root/bin/routecheck.sh > /dev/null
十、RaySync自启动脚本
cat /etc/init.d/raysync
#!/bin/bash
#
# description: setup RaySync service
# Version: 0.1.0
# Date: 2018-02-06
# Author: lirou
#
# chkconfig: - 87 17
source /etc/profile
../etc/rc.d/init.d/functions
set -e
#set -x
export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8
if [ $# -ne 1 ];then
echo "Error Usage: service $(basename $0) [ start | stop | status | restart ]"
exit 3
fi
BASE_DIR=
PID_DIR=
if [[ "x$BASE_DIR" == "x" ]];then
BASE_DIR=/opt/RaySync
fi
RAYSYNC_CONF=$BASE_DIR/config/config.ini
if ! [ -d $(dirname $RAYSYNC_CONF) ];then
install -d -m 755 $(dirname $RAYSYNC_CONF)
fi
if [[ "x$PID_DIR" == "x" ]];then
PID_DIR=/var/run/raysync
mkdir -pv $PID_DIR &>/dev/null
fi
#MONITOR_PID=$PID_DIR/monitor.pid
MONITOR_LOCKFILE=$PID_DIR/monitor.lock
RAYSYNC_SERVER_LOCKFILE=$PID_DIR/raysync-server.lock
RAYSYNC_PROXY_SERVER_LOCKFILE=$PID_DIR/raysync-proxy.lock
MONITOR_BIN_NAME=
RAYSYNC_SERVER_BIN_NAME=
RAYSYNC_PROXY_SERVER_BIN_NAME=
if [[ "x$MONITOR_BIN_NAME" == "x" ]];then
MONITOR_BIN_NAME=monitor
fi
if [[ "x$RAYSYNC_SERVER" == "x" ]];then
RAYSYNC_SERVER_BIN_NAME=RaySyncServer
fi
if [[ "x$RAYSYNC_PROXY_SERVER" == "x" ]];then
RAYSYNC_PROXY_SERVER_BIN_NAME=RaySync-Proxy-Server
fi
start() {
if ! [ -s $RAYSYNC_CONF ];then
echo "Error: don't have configure file in $RAYSYNC_CONF path."
fi
if ! [ -f $MONITOR_LOCKFILE ]; then
# nohup ${BASE_DIR}/${MONITOR_BIN_NAME} &>/dev/null &
nohup ${BASE_DIR}/${MONITOR_BIN_NAME} >/dev/null 2>&1 &
sleep 1
if [[ "x$(ps aux | grep -i monitor | grep -v -i grep )" == "x" ]];then
echo -e "monitor setup fail" && failure
else
touch $MONITOR_LOCKFILE
fi
fi
echo -e " monitor is running: " && success
if ! [ -f $RAYSYNC_SERVER_LOCKFILE ];then
nohup $BASE_DIR/RaySyncServer >/dev/null 2&>1 &
sleep 1
if [[ "x$(ps aux | grep RaySyncServer | grep -v -i 'grep' )" == "x" ]];then
echo -e "RaySyncServer setup fail" && failure
else
touch $RAYSYNC_SERVER_LOCKFILE
fi
fi
echo -e " RaySyncServer is running" && success
if ! [ -f $RAYSYNC_PROXY_SERVER_LOCKFILE ];then
#nohup $BASE_DIR/proxy/$RAYSYNC_PROXY_SERVER_BIN_NAME >/dev/null 2&>1
#echo "nohup $BASE_DIR/proxy/$RAYSYNC_PROXY_SERVER_BIN_NAME >/dev/null 2&>1"
sleep 1
if [[ "x$(ps aux | grep RaySync-Proxy-Server )" == "x" ]];then
echo -e "RaySync-Proxy-Server setup fail" && failure
else
touch $RAYSYNC_PROXY_SERVER_LOCKFILE
fi
fi
echo -e " RaySync-Proxy-Server is running" && success
}
stop() {
if [ -f $MONITOR_LOCKFILE ];then
# killall $MONITOR_BIN_NAME
ps -ef|grep Ray|grep -v grep|awk '{print $2}'|xargs kill -9 >/dev/null 2&>1
rm -f $MONITOR_LOCKFILE
fi
echo -e "monitor stopped" && success
if [ -f $RAYSYNC_SERVER_LOCKFILE ];then
#kill $(echo $RAYSYNC_SERVER_PID)
# killall $RAYSYNC_SERVER_BIN_NAME
ps -ef|grep Ray|grep -v grep|awk '{print $2}'|xargs kill -9 >/dev/null 2&>1
rm -f $RAYSYNC_SERVER_LOCKFILE
fi
echo -e "RaySyncServer stopped" && success
# result_Proxy=`ps -ef|grep ${RAYSYNC_PROXY_SERVER_BIN_NAME}|grep -v grep`
# if [ -f $RAYSYNC_PROXY_SERVER_LOCKFILE ] || [ $result_Proxy != "" ];then
if [ -f $RAYSYNC_PROXY_SERVER_LOCKFILE ];then
ps -ef|grep Ray|grep -v grep|awk '{print $2}'|xargs kill -2 >/dev/null 2&>1
echo ${RAYSYNC_PROXY_SERVER_BIN_NAME}
rm -f $RAYSYNC_PROXY_SERVER_LOCKFILE
fi
echo -e "RaySync-Proxy-Server stopped" && success
}
status() {
if [ -f $MONITOR_LOCKFILE ];then
echo " monitor is running"
else
echo " monitor stopped"
fi
if [ -f $RAYSYNC_SERVER_LOCKFILE ];then
echo " RaySyncServer is running"
else
echo " RaySyncServer stopped"
fi
if [ -f $RAYSYNC_PROXY_SERVER_LOCKFILE ];then
echo " RaySync-Proxy-Server is running"
else
echo " RaySync-Proxy-Server stopped"
fi
}
case $1 in
start)
start
;;
stop)
stop
;;
status)
status
;;
restart)
stop
sleep 2
start
;;
*)
echo "Error Usage: service $(basename $0) [ start | stop | status | restart ]"
exit 3
esac