双机热备技术

redis双机热备的流程:原理是将一个服务器作为主服务器,另一个服务器作为从服务器,做到在主服务器宕机后迅速切换到辅助缓存服务器。以主服务器为例子;
首先是安装redis,需要用到root用户的权限:
然后创建redis的主目录:
mkdir -p /usr/local/redis-master/{bin,conf,logs}

cp -a src/redis-server /usr/local/redis-master/bin/

cp -a src/redis-cli /usr/local/redis-master/bin/

cp -a src/redis-benchmark /usr/local/redis-master/bin/

cp -a src/redis-sentinel /usr/local/redis-master/bin/

cp -a src/redis-check-dump /usr/local/redis-master/bin/

cp -a src/redis-check-aof /usr/local/redis-master/bin/

然后创建redis的启动脚本vi /usr/local/redis-master/redis-start.sh:

!/bin/bash

REDISPATH=/usr/local/redis-2.8.3
REDISCLI=$REDISPATH/bin/redis-cli
LOGFILE=$REDISPATH/logs/redis-state.log
LOCALIP=x #自身IP
REMOTEIP=y #监控服务器的IP

REMOTEREDISROLE=$REDISCLI -h $REMOTEIP info | grep "role"
if grep "role:master" <<< $REMOTEREDISROLE ; then
#start as slave
$REDISPATH/bin/redis-server $REDISPATH/conf/redis_slave.confif [ "$?" == "0" ];then
echo "[INFO]date +%F/%H:%M:%S :$LOCALIP start as slave successful." >> $LOGFILEelseecho "[ERROR]date +%F/%H:%M:%S :$LOCALIP start as slave error." >> $LOGFILE
fi
else#start as master
$REDISPATH/bin/redis-server $REDISPATH/conf/redis_master.confif [ "$?" == "0" ];then
echo "[INFO]date +%F/%H:%M:%S :$LOCALIP start as master successful." >> $LOGFILEelseecho "[ERROR]date +%F/%H:%M:%S :$LOCALIP start as master error." >> $LOGFILE
fi
fi

给脚本赋予权限 chmod 755 /usr/local/redis-master/bin/redis-start.sh;
然后创建redis的关闭脚本 vi /usr/local/redis-master/redis-stop.sh

!/bin/bash

REDISPATH=/usr/local/redis-master
LOGFILE=$REDISPATH/logs/redis-state.log
kill -9 ps -ef|grep '/bin/redis-server'|grep -v grep|awk '{print $2}'
if [ "$?" == "0" ];then
echo "[INFO]date +%F/%H:%M:%S :redis shutdown completed!" >> $LOGFILE
elseecho "[ERROR]date +%F/%H:%M:%S :redis is not started." >> $LOGFILE
fi

chmod 755 /usr/local/redis-master/bin/redis-stop.sh;

然后复制现在redis配置文件到用户目录:
cp -a -R -p redis.conf /usr/local/redis-master/conf/redis_master.conf
cp -a -R -p redis.conf /usr/local/redis-slave/conf/redis_slave.conf

然后修改相应的配置文件:

vi /usr/local/redis-master/conf/redis_master.conf

daemonize no

daemonize yes

bind 127.0.0.1

bind x #相应的IP地址
logfile "/usr/local/redis-master/logs/redis.log"

vi /usr/local/redis-master/conf/redis_slave.conf

daemonize no

daemonize yes

bind 127.0.0.1

bind y
logfile "/usr/local/redis-master/logs/redis.log"

slaveof

slaveof y #相应的IP地址和端口

然后修改这个redis-master的用户组别和权限:
chown -R cpay:cpay /usr/local/redis-master;
chmod -R 755 /usr/local/redis-master;

然后再安装keepalived:
1.下载keepalived源码Release 1.2.9

注意:最新版为1.2.10测试过程中出错.
wget http://www.keepalived.org/software/keepalived-1.2.9.tar.gz
2.安装keepalived

需要安装以下依赖包: make gcc libpopt-dev libnl-dev libcurl4-openssl-dev popt openssl
cd

tar –zxvf keepalived-1.2.9.tar.gz

cd keepalived-1.2.9

./configure --prefix=/usr/local/keepalived

make && make install

然后复制指定文件到系统/etc文件夹:

cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/

chmod 755 /etc/rc.d/init.d/keepalived

cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
chmod 755 /etc/sysconfig/keepalived

cp /usr/local/keepalived/sbin/keepalived /usr/sbin/

chmod 755 /usr/sbin/keepalived
mkdir /etc/keepalived

安装完成后进行配置:
mv /usr/local/keepalived/etc/keepalived/keepalived.conf /usr/local/keepalived/etc/keepalived/keepalived.conf-bak
vi /usr/local/keepalived/etc/keepalived/keepalived.conf

vrrp_script chk_redis {
script "/usr/local/keepalived/etc/keepalived/scripts/chk_redis.sh" ###监控脚本
interval 2 ###监控时间
}
vrrp_instance VI_1 {
state MASTER ###设置为MASTER
interface eth3 ###监控网卡,依据实际情况来定
virtual_router_id 51
priority 101 ###权重值
advert_int 1
authentication {
auth_type PASS ###加密
auth_pass redis ###密码
}
track_script {
chk_redis ###执行上面定义的chk_redis
}
virtual_ipaddress {
z ###VIP
}
}
将配置文件复制到系统目录
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived;
chmod 755 /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived;

然后创建用于监视redis的脚本(主):
mkdir /usr/local/keepalived/etc/keepalived/scripts
vi /usr/local/keepalived/etc/keepalived/scripts/chk_redis.sh

!/bin/bash

REDISPATH=/usr/local/redis_slave
REDISCLI=$REDISPATH/bin/redis-cli
LOGFILE=$REDISPATH/logs/redis-state.log
LOCALIP=1xx
REMOTEIP=xxx
VIP=xxx
REMOTEPASSWORD=art390
LOCALPASSWORD=art390
REMOTEPORT=6378
LOCALPORT=6378
REMOTECLI="$REDISCLI -h $REMOTEIP -p $REMOTEPORT -a $REMOTEPASSWORD"
LOCALCLI="$REDISCLI -h $LOCALIP -p $LOCALPORT -a $LOCALPASSWORD"
VIPALIVE=ip a | grep "$VIP"
if [ "$VIPALIVE" == "" ]; then
echo "[info]:"date" keepalived server is pengding or stop" >> $LOGFILE
else
echo "VIP IS HERE" >> $LOGFILE
#check local service is running
if [ $LOCALCLI ping = "PONG" ]; then
echo "now local is in use" >> $LOGFILE
# check local redis server role.
echo $LOCALCLI info | grep "role" >> $LOGFILE
if grep "role:slave" <<< $REDISROLE; then
#change local redis server as master
echo "[info1]:"date" Run SLAVEOF NO ONE cmd ..." >> $LOGFILE
$LOCALCLI SLAVEOF NO ONE >> $LOGFILE 2>&1

                    #change remoting redis server as slave
                    if [ `$REMOTECLI ping` = "PONG" ];then
                            REMOTEREDISROLE=`$REMOTECLI info | grep "role"`
                            if grep "role:master" <<< $REMOTEREDISROLE; then
                                    echo "[info2]:"`date`" Run remote server SLAVEOF cmd ..." >> $LOGFILE
                                    $REMOTECLI SLAVEOF $LOCALIP 6378 >> $LOGFILE  2>&1
                            fi
                    fi
            fi
    else
            echo "[warn]:"`date`"  redis server($LOCALIP) is not health..." >> $LOGFILE
            sleep 1
            if [ "`$LOCALCLI PING`" != "PONG" ]; then
                    echo "[error]:"`date`"  redis server($LOCALIP) will be stop..." >> $LOGFILE
                    service keepalived stop
            fi
    fi

fi

然后给脚本配置权限:chmod 755 /usr/local/keepalived/etc/keepalived/scripts/chk_redis.sh

从服务器的脚本为:

!/bin/bash

REDISPATH=/usr/local/redis_slave
REDISCLI=$REDISPATH/bin/redis-cli
LOGFILE=$REDISPATH/logs/redis-state.log
LOCALIP=xxx
REMOTEIP=xxx
VIP=xxx
REMOTEPASSWORD=art390
LOCALPASSWORD=art390
REMOTEPORT=6378
LOCALPORT=6378
REMOTECLI="$REDISCLI -h $REMOTEIP -p $REMOTEPORT -a $REMOTEPASSWORD"
LOCALCLI="$REDISCLI -h $LOCALIP -p $LOCALPORT -a $LOCALPASSWORD"
VIPALIVE=ip a | grep "$VIP"
if [ "$VIPALIVE" == "" ]; then
echo "[info]:"date" keepalived server is pengding or stop" >> $LOGFILE
else
echo "VIP IS HERE" >> $LOGFILE
#check local service is running
if [ $LOCALCLI ping = "PONG" ]; then
echo "now local is in use" >> $LOGFILE
# check local redis server role.
echo $LOCALCLI info | grep "role" >> $LOGFILE
if grep "role:slave" <<< $REDISROLE; then
#change local redis server as master
echo "[info1]:"date" Run SLAVEOF NO ONE cmd ..." >> $LOGFILE
$LOCALCLI SLAVEOF NO ONE >> $LOGFILE 2>&1

                    #change remoting redis server as slave
                    if [ `$REMOTECLI ping` = "PONG" ];then
                            REMOTEREDISROLE=`$REMOTECLI info | grep "role"`
                            if grep "role:master" <<< $REMOTEREDISROLE; then
                                    echo "[info2]:"`date`" Run remote server SLAVEOF cmd ..." >> $LOGFILE
                                    $REMOTECLI SLAVEOF $LOCALIP 6378 >> $LOGFILE  2>&1
                            fi
                    fi
            fi
    else
            echo "[warn]:"`date`"  redis server($LOCALIP) is not health..." >> $LOGFILE
            sleep 1
            if [ "`$LOCALCLI PING`" != "PONG" ]; then
                    echo "[error]:"`date`"  redis server($LOCALIP) will be stop..." >> $LOGFILE
                    service keepalived stop
            fi
    fi

fi

注意:一定要先启动redis,再启动keealived,否则redis_check.sh会将keepalived自动关闭。

脚本创建完成以后,我们开始按照如下流程进行测试:
1.启动Master上的Redis
/usr/local/redis-2.8.3/redis-start.sh

关闭时,直接杀死进程或执行以下脚本

/usr/local/redis-2.8.3/redis-stop.sh

2.启动Slave上的Redis
/usr/local/redis-2.8.3/redis-start.sh

关闭时,直接杀死进程或执行以下脚本

/usr/local/redis-2.8.3/redis-stop.sh

3.启动Master上的Keepalived
/etc/rc.d/init.d/keepalived start

关闭方法

/etc/rc.d/init.d/keepalived stop

4.启动Slave上的Keepalived
/etc/rc.d/init.d/keepalived start

关闭方法

/etc/rc.d/init.d/keepalived stop

你可能感兴趣的:(双机热备技术)