Redis+keepalived实现双机热备-阿里云开发者社区

开发者社区> 开发与运维> 正文
登录阅读全文

Redis+keepalived实现双机热备

简介:

一. 简介

安装使用centos 5.6(64)

Master 192.168.2.235

Slave 192.168.2.236

Vip 192.168.2.200

编译环境 yum -y install gcc gcc+ gcc-c++ openssl openssl-devel pcre pcre-devel

当 Master 与 Slave 均运作正常时, Master负责服务,Slave负责Standby;

当 Master 挂掉,Slave 正常时, Slave接管服务,同时关闭主从复制功能;

当 Master 恢复正常,则从Slave同步数据,同步数据之后关闭主从复制功能,恢复Master身份,于此同时Slave等待Master同步数据完成之后,恢复Slave身份。

然后依次循环。

需要注意的是,这样做需要在Master与Slave上都开启本地化策略,否则在互相自动切换的过程中,未开启本地化的一方会将另一方的数据清空,造成数据完全丢失

二. 安装

Gperftools安装

 tar xzvf gperftools-2.1.tar.gz

cd gperftools-2.1

. ./configure --enable-frame-pointers

make

make install

mv /usr/local/lib/libtcmalloc.so.4 /usr/lib64/

Keepalived安装

tar xf keepalived-1.2.12.tar.gz

cd keepalived-1.2.12

./configure

make && make install

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

cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/

chmod +x /etc/init.d/keepalived

chkconfig –add keepalived

mkdir /etc/keepalived

ln –s /usr/local/sbin/keepalived /usr/sbin

Redis安装

tar xf redis-2.8.10.tar.gz

cd redis2.8.10

mkdir –p /opt/redis

make PREFIX=/opt/redis USE_TCMALLOC=yes FORCE_LIBC_MALLOC=yes install

 三 .配置

创建配置文件夹以及文件

 mkdir -p /opt/redis/etc

mkdir -p /opt/redis/run

mkdir -p /opt/redis/data/6379

mkdir -p /opt/redis/log

cp /root/redis-2.8.10/redis.conf  /opt/redis/etc/redis.conf

mv /opt/redis/etc/redis.conf /opt/redis/etc/redis_6379.conf

修改配置文件:

vim /opt/redis/etc/redis_6379.conf

daemonize yes

pidfile /opt/redis/run/redis_6379.pid

dir /opt/redis/data/6379

logfile /opt/redis/log/redis_6379.log

创建服务管理脚本

vim /etc/init.d/redis

#!/bin/sh

PATH="/opt/redis/bin:$PATH"

EXEC="/opt/redis/bin/redis-server"

CLIEXEC="/opt/redis/bin/redis-cli"

PIDFILE="/opt/redis/run/redis_6379.pid"

CONF="/opt/redis/etc/redis_6379.conf"

REDISPORT="6379"

case "$1" in

    start)

        if [ -f $PIDFILE ]

        then

                echo "$PIDFILE exists, process is already running or crashed."

        else

                echo "Starting Redis server..."

                $EXEC $CONF

        fi

        ;;

    stop)

        if [ ! -f $PIDFILE ]

        then

                echo "$PIDFILE does not exist, process is not running."

        else

                PID=$(cat $PIDFILE)

                echo "Stopping ..."

                $CLIEXEC -p $REDISPORT shutdown

                while [ -x /proc/${PID} ]

                do

                    echo "Waiting for Redis to shutdown ..."

                    sleep 1

                done

                echo "Redis stopped."

        fi

        ;;

    *)   

        echo "Usage: $0 {start|stop}" >&2

        exit 1

        ;;

esac

授权 chmod +x /etc/init.d/redis

vim /etc/sysctl.conf

在最后添加以下节点:

vm.overcommit_memory = 1

#sysctl –p

启动redis

/etc/init.d/redis start

验证


 ps aux | grep redis

 首先在master上创建如下配置

mkdir –p /etc/keepalived

vim /etc/keepalived/keepalived.conf

vrrp_script chk_redis {

                script "/etc/keepalived/scripts/redis_check.sh"   ###监控脚本

                interval 2                                        ###监控时间

}

vrrp_instance VI_1 {

        state MASTER                            ###设置为MASTER

        interface eth0                          ###监控网卡   

        virtual_router_id 51

        priority 101                            ###权重值

        authentication {

                     auth_type PASS             ###加密

                     auth_pass redis            ###密码

        }

        track_script {

                chk_redis                       ###执行上面定义的chk_redis

        }

        virtual_ipaddress {

             192.168.2.200                        ###VIP

        }

        notify_master /etc/keepalived/scripts/redis_master.sh

        notify_backup /etc/keepalived/scripts/redis_backup.sh

        notify_fault  /etc/keepalived/scripts/redis_fault.sh

        notify_stop   /etc/keepalived/scripts/redis_stop.sh

}

 

vim /etc/keepalived/scripts/redis_master.sh

#!/bin/bash

REDISCLI="/opt/redis/bin/redis-cli"

LOGFILE="/var/log/keepalived-redis-state.log"

echo "[master]" >> $LOGFILE

date >> $LOGFILE

echo "Being master...." >> $LOGFILE 2>&1

echo "Run SLAVEOF cmd ..." >> $LOGFILE

$REDISCLI SLAVEOF 192.168.2.236(slave) 6379 >> $LOGFILE  2>&1

sleep 10 #延迟10秒以后待数据同步完成后再取消同步状态

echo "Run SLAVEOF NO ONE cmd ..." >> $LOGFILE

$REDISCLI SLAVEOF NO ONE >> $LOGFILE 2>&1

 

vim /etc/keepalived/scripts/redis_backup.sh

#!/bin/bash

REDISCLI="/opt/redis/bin/redis-cli"

LOGFILE="/var/log/keepalived-redis-state.log"

echo "[backup]" >> $LOGFILE

date >> $LOGFILE

echo "Being slave...." >> $LOGFILE 2>&1

sleep 15 #延迟15秒待数据被对方同步完成之后再切换主从角色

echo "Run SLAVEOF cmd ..." >> $LOGFILE

$REDISCLI SLAVEOF 192.168.2.236(slave)  6379 >> $LOGFILE  2>&1

在slave上创建如下文件:

vim /etc/keepalived/keepalived.conf

vrrp_script chk_redis {

                script "/etc/keepalived/scripts/redis_check.sh"   ###监控脚本

                interval 2                                        ###监控时间

}

vrrp_instance VI_1 {

        state BACKUP                                ###设置为BACKUP

        interface eth0                              ###监控网卡

        virtual_router_id 51

        priority 100                                ###比MASTRE权重值低

        authentication {

                     auth_type PASS

                     auth_pass redis                ###密码与MASTRE相同

        }

        track_script {

                chk_redis                       ###执行上面定义的chk_redis

        }

        virtual_ipaddress {

             192.168.2.200                         ###VIP

        }

        notify_master /etc/keepalived/scripts/redis_master.sh

        notify_backup /etc/keepalived/scripts/redis_backup.sh

        notify_fault  /etc/keepalived/scripts/redis_fault.sh

        notify_stop   /etc/keepalived/scripts/redis_stop.sh

}

vim /etc/keepalived/scripts/redis_master.sh

#!/bin/bash

REDISCLI="/opt/redis/bin/redis-cli"

LOGFILE="/var/log/keepalived-redis-state.log"

echo "[master]" >> $LOGFILE

date >> $LOGFILE

echo "Being master...." >> $LOGFILE 2>&1

echo "Run SLAVEOF cmd ..." >> $LOGFILE

$REDISCLI SLAVEOF 192.168.2.235(master) 6379 >> $LOGFILE  2>&1

sleep 10 #延迟10秒以后待数据同步完成后再取消同步状态

echo "Run SLAVEOF NO ONE cmd ..." >> $LOGFILE

$REDISCLI SLAVEOF NO ONE >> $LOGFILE 2>&1

vim /etc/keepalived/scripts/redis_backup.sh

#!/bin/bash

REDISCLI="/opt/redis/bin/redis-cli"

LOGFILE="/var/log/keepalived-redis-state.log"

echo "[backup]" >> $LOGFILE

date >> $LOGFILE

echo "Being slave...." >> $LOGFILE 2>&1

sleep 15 #延迟15秒待数据被对方同步完成之后再切换主从角色

echo "Run SLAVEOF cmd ..." >> $LOGFILE

$REDISCLI SLAVEOF 192.168.2.235 6379 >> $LOGFILE  2>&1

以下脚本分别在master和slave上创建

mkdir /etc/keepalived/scripts

vim /etc/keepalived/scripts/redis_check.sh

#!/bin/bash

ALIVE=`/opt/redis/bin/redis-cli PING`

if [ "$ALIVE" == "PONG" ]; then

  echo $ALIVE

  exit 0

else

  echo $ALIVE

  exit 1

fi

 vim /etc/keepalived/scripts/redis_fault.sh

#!/bin/bash

LOGFILE=/var/log/keepalived-redis-state.log

echo "[fault]" >> $LOGFILE

date >> $LOGFILE

 vim /etc/keepalived/scripts/redis_stop.sh

#!/bin/bash

LOGFILE=/var/log/keepalived-redis-state.log

echo "[stop]" >> $LOGFILE

date >> $LOGFILE

授执行权  chmod  +x  /etc/keepalived/scripts/*.sh

四 .测试

在msater和slave上分别启动redis;

/etc/init.d/redis start

分别启动keepalived

/etc/init.d/keepalived start


在master上执行 ip a

虚拟IP已经绑定了

在master上执行/etc/init.d/redis stop 查看slave

看到了吧秒级切换。

接下来再吧master上的redis启动起来又切换回来了。

测试redis同步

redis教程:http://www.runoob.com/redis/redis-tutorial.html



本文转自 Linux_woniu 51CTO博客,原文链接:http://blog.51cto.com/linuxcgi/1965311

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

分享:
开发与运维
使用钉钉扫一扫加入圈子
+ 订阅

集结各类场景实战经验,助你开发运维畅行无忧

其他文章