Redis+Keeplived实现高可用

本文涉及的产品
Redis 开源版,标准版 2GB
推荐场景:
搭建游戏排行榜
云数据库 Tair(兼容Redis),内存型 2GB
简介:

博文说明【前言】:

    本文将通过个人口吻介绍Redis+Keeplived实现高可用的相关知识,在目前时间点【2017年6月23号】下,所掌握的技术水平有限,可能会存在不少知识理解不够深入或全面,望大家指出问题共同交流,在后续工作及学习中如发现本文内容与实际情况有所偏差,将会完善该博文内容。


    本文为我编写的部署文档(word格式)中关于redis部分的内容复制粘贴而来,因此会存在一些格式问题,因不影响阅读和理解,也没有改的必要了,这样反而便于大家慢下来理解。发表此处,一来是有方便自己查看,二来是给广大网友参考,谢谢大家阅读。




正文:


Redis配置信息

Redis部署使用两台服务器,实现Redis+keepalived,提供redis服务高可用,当主redis进程或服务器宕机之后,备redis进程或服务器继续提供服务。

服务器配置信息如下所示:

主机名/IP

端口

用途

JZSHPT-APP5/

10.133.214.15

6379

Redis主服务器-master

JZSHPT-APP6/

10.133.214.16

6379

Redis从服务器-salve

 

一:Redis主服务器搭建-redis-master

1、安装redis

JZSHPT-APP5:/soft # tar -zxf redis-3.2.1.tar.gz  -C /usr/local

JZSHPT-APP5:/soft # cd /usr/local/redis-3.2.1/

JZSHPT-APP5:/usr/local/redis-3.2.1 # make

JZSHPT-APP5:/usr/local/redis-3.2.1 # cd src &&make install

JZSHPT-APP5:/usr/local # mv /usr/local/redis-3.2.1/ /usr/local/redis

1redis配置文件修改

JZSHPT-APP5:/usr/local# vim /usr/local/redis/redis.conf

修改文件相关内容为:

#bind 127.0.0.1

protected-mode no

port 6379

daemonizeyes

pidfile/usr/local/redis/redis.pid

logfile"/usr/local/redis/redis.log"

dbfilename dump6379.rdb

dir /usr/local/redis/

2redis启动脚本编写

JZSHPT-APP5:/usr/local/redis# vim /etc/init.d/redisd

#!/bin/sh

#chkconfig345 86 14

#descriptionStartup and Shutdown script for Redis-3.2.1

progdir=/usr/local/redis/src

progname=redis-server

daemon=$progdir/$progname

config=/usr/local/redis/redis.conf

pidfile=/usr/local/redis/redis.pid

desc="redisdaemon"

scriptname=/etc/init.d/redisd

start()

{

    if test -x $daemon;

    then

    echo -e "Starting$desc:$progname"

        if $daemon $config

        then

            echo -e "Start OK!!!"

        else

             echo -e "Startfailed!!!"

        fi 

    else

    echo -e "Couldn't find RedisServer($daemon)"

    fi 

}

 

stop()

{

    if test -e $pidfile;

    then

        echo -e "Stopping$desc:$progname"

        if kill `cat $pidfile`

        then

            echo -e "stop OK!!!"

        else

            echo -e "Stop failed!!!"

        fi

    else

        echo -e "No Redis Server($daemoon)running"

    fi

}

 

restart()

{

    echo -e "Restarting$desc:$progname"

    stop

        start

 

}

 

status()

{

    ps aux | grep $progname

}

 

case$1 in

    start)

        start

    ;;

    stop)

        stop

    ;;

    restart)

restart

    ;;

    status)

    status

    ;;

    *)

    echo"Usage:$scriptnme{start|stop|restart|status}" >&2

    exit 1

    ;;

esac

exit0

 

JZSHPT-APP5:/usr/local/redis #chmod +x /etc/init.d/redisd      

JZSHPT-APP5:/usr/local/redis #chkconfig --add redisd

2、安装Keepalived

安装keepalived前请自行安装解决依赖关系(可能会涉及的软件包为libopenssl-develzlib-develipvsadm内核模块等)

JZSHPT-APP5:/soft # tar -zxf keepalived-1.2.23.tar.gz 

JZSHPT-APP5:/soft # cd keepalived-1.2.23/

JZSHPT-APP5:/soft/keepalived-1.2.23 # ./configure--prefix=/usr/local/keepalived/

JZSHPT-APP5:/soft/keepalived-1.2.23 # make &&make install

1keepalived配置文件编辑

JZSHPT-APP5:~# mkdir -p /etc/keepalived/{scripts,log}

JZSHPT-APP5:~ # vim /usr/local/keepalived/etc/keepalived/

! Configuration File for keepalived

 

 global_defs {

    router_idRedis1_DEVEL

}

 

vrrp_script chk_redis {

         script"/etc/keepalived/scripts/redis_check.sh"

         interval2

         timeout2

         fall 3

}

 

vrrp_instance VI_1 {

     stateMASTER

     interfaceeth0                                         

    virtual_router_id 51

     nopreempt

     priority200

      advert_int5

     authentication {

              auth_typePASS

       auth_pass redis

     }

    virtual_ipaddress {

         10.133.214.50

     }

    track_script {

        chk_redis

    }

       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

}

2keepalived启动脚本配置

JZSHPT-APP5:~ # cp /soft/keepalived-1.2.23/keepalived/etc/init.d/keepalived.suse.init/etc/init.d/keepalived.suse

JZSHPT-APP5:~ # cp/usr/local/keepalived/sbin/keepalived /usr/local/sbin/

JZSHPT-APP5:~ # cp /usr/local/keepalived/etc/keepalived/keepalived.conf/etc/keepalived/

3)定义监控脚本

JZSHPT-APP5:~ # vim/etc/keepalived/scripts/redis_check.sh

#!/bin/sh

###/etc/keepalived/scripts/redis_check.sh

ALIVE=`/usr/local/bin/redis-cli PING`

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

  echo $ALIVE

  exit 0

else

  echo $ALIVE

  exit 1

fi

4)定义状态切换为master时执行的脚本

JZSHPT-APP5:~ # vim/etc/keepalived/scripts/redis_master.sh

#!/bin/sh

###/etc/keepalived/scripts/redis_master.sh

REDISCLI="redis-cli"

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

pid=$$

echo "`date +'%Y-%m-%d:%H:%M:%S'`|$pid|state:[slaver]">> $LOGFILE

echo "`date +'%Y-%m-%d:%H:%M:%S'`|$pid|state:[slaver]Run 'SLAVEOF 10.133.214.16 6379'" >> $LOGFILE

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

echo "`date+'%Y-%m-%d:%H:%M:%S'`|$pid|state:[slaver] wait 10 sec for data sync from oldmaster" >> $LOGFILE

sleep 10

echo "`date+'%Y-%m-%d:%H:%M:%S'`|$pid|state:[slaver] data rsync from old mater ok...">> $LOGFILE

echo "`date+'%Y-%m-%d:%H:%M:%S'`|$pid|state:[master] Run slaveof no one,close master/slave">> $LOGFILE

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

echo "`date+'%Y-%m-%d:%H:%M:%S'`|$pid|state:[master] wait other slave connect....">> $LOGFILE

5)定义状态切换为backups时执行的脚本

JZSHPT-APP5:~ # vim/etc/keepalived/scripts/redis_backup.sh

#!/bin/bash

###/etc/keepalived/scripts/redis_backup.sh

REDISCLI="redis-cli"

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

pid=$$

 

echo "`date+'%Y-%m-%d:%H:%M:%S'`|$pid|state:[master]" >> $LOGFILE

echo "`date+'%Y-%m-%d:%H:%M:%S'`|$pid|state:[master] Being slave state..." >> $LOGFILE2>&1

echo "`date+'%Y-%m-%d:%H:%M:%S'`|$pid|state:[master] wait 10 sec for data sync from oldmaster" >> $LOGFILE

sleep 10

echo "`date+'%Y-%m-%d:%H:%M:%S'`|$pid|state:[master] data rsync from old mater ok...">> $LOGFILE

echo "`date +'%Y-%m-%d:%H:%M:%S'`|$pid|state:[slaver]Run 'SLAVEOF 10.133.214.16 6379'" >> $LOGFILE

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

echo "`date+'%Y-%m-%d:%H:%M:%S'`|$pid|state:[slaver] slave connect to 192.18.3.21ok..." >> $LOGFILE

6) 定义状态切换为stop时执行的脚本

JZSHPT-APP5:~ # cp/etc/keepalived/scripts/redis_backup.sh /etc/keepalived/scripts/redis_stop.sh

7) 定义状态切换为fault时执行的脚本

JZSHPT-APP5:~ # cp/etc/keepalived/scripts/redis_backup.sh /etc/keepalived/scripts/redis_fault.sh

3、安装rsyslog实现keepalived日志分离

1)安装rsyslog软件包

JZSHPT-APP5:/soft # rpm -ivhrsyslog-5.10.1-0.7.49.x86_64.rpm

2)修改rsyslog配置文件

JZSHPT-APP5:/soft # vim /etc/rsyslog.conf

140 #*.*;mail.none;news.none                        -/var/log/messages

141 *.*;mail.none;news.none;local0.none                     -/var/log/messages

153 #local0,local1.*                               -/var/log/localmessages

154 local0.*                               -/var/log/keepalived.log

155 local1.*                               -/var/log/localmessages

156 local2,local3.*                         -/var/log/localmessages

157 local4,local5.*                        -/var/log/localmessages

158  cal6,local7.*                        -/var/log/localmessages

3)修改rsyslog启动脚本配置文件

根据/etc/init.d/syslog中的三个启动选项,修改该文件内的”SYSLOG_DAEMON”选项

JZSHPT-APP5:/soft # vim /etc/sysconfig/syslog

40 #SYSLOG_DAEMON="syslog-ng"

41 SYSLOG_DAEMON="rsyslogd"                               

4)修改Keepalived启动脚本选项

JZSHPT-APP5:/soft #  vim /etc/init.d/keepalived.suse

17 DAEMON_OPT="-d "

18 DAEMON_OPT="-d -D -S 0"

5)启动rsyslog服务

JZSHPT-APP5:/soft # /etc/init.d/syslog restart

4、启动RedisKeepalived

JZSHPT-APP5:~ # /etc/init.d/redisd start

JZSHPT-APP5:~ # /etc/init.d/keepalived.suse start

二:Redis从服务器搭建-redis-slave

1、安装redis

JZSHPT-APP6:/soft # tar -zxf redis-3.2.1.tar.gz  -C /usr/local

JZSHPT-APP6:/soft # cd /usr/local/redis-3.2.1/

JZSHPT-APP6:/usr/local/redis-3.2.1 # make

JZSHPT-APP6:/usr/local/redis-3.2.1 # cd src &&make install

JZSHPT-APP6:/usr/local # mv /usr/local/redis-3.2.1/ /usr/local/redis

1redis配置文件修改

JZSHPT-APP6:/usr/local# vim /usr/local/redis/redis.conf

修改文件相关内容为:

#bind 127.0.0.1

protected-mode no

port 6379

daemonizeyes

pidfile/usr/local/redis/redis.pid

logfile"/usr/local/redis/redis.log"

dbfilename dump6379.rdb

dir /usr/local/redis/

2redis启动脚本编写

JZSHPT-APP6:/usr/local/redis# vim /etc/init.d/redisd

#!/bin/sh

#chkconfig345 86 14

#descriptionStartup and Shutdown script for Redis-3.2.1

progdir=/usr/local/redis/src

progname=redis-server

daemon=$progdir/$progname

config=/usr/local/redis/redis.conf

pidfile=/usr/local/redis/redis.pid

desc="redisdaemon"

scriptname=/etc/init.d/redisd

start()

{

    if test -x $daemon;

    then

    echo -e "Starting$desc:$progname"

        if $daemon $config

        then

            echo -e "Start OK!!!"

       else

             echo -e "Startfailed!!!"

        fi 

    else

    echo -e "Couldn't find RedisServer($daemon)"

    fi 

}

 

stop()

{

    if test -e $pidfile;

    then

        echo -e "Stopping$desc:$progname"

        if kill `cat $pidfile`

        then

            echo -e "stop OK!!!"

        else

            echo -e "Stop failed!!!"

        fi

    else

        echo -e "No Redis Server($daemoon)running"

    fi

}

 

restart()

{

    echo -e "Restarting$desc:$progname"

    stop

        start

 

}

 

status()

{

    ps aux | grep $progname

}

 

case$1 in

    start)

        start

    ;;

    stop)

        stop

    ;;

    restart)

restart

    ;;

    status)

    status

    ;;

    *)

    echo"Usage:$scriptnme{start|stop|restart|status}" >&2

    exit 1

    ;;

esac

exit0

 

JZSHPT-APP6:/usr/local/redis #chmod +x /etc/init.d/redisd      

JZSHPT-APP6:/usr/local/redis #chkconfig --add redisd

2、安装Keepalived

安装keepalived前请自行安装解决依赖关系(可能会涉及的软件包为libopenssl-develzlib-develipvsadm内核模块等)

JZSHPT-APP6:/soft # tar -zxf keepalived-1.2.23.tar.gz 

JZSHPT-APP6:/soft # cd keepalived-1.2.23/

JZSHPT-APP6:/soft/keepalived-1.2.23 # ./configure--prefix=/usr/local/keepalived/

JZSHPT-APP6:/soft/keepalived-1.2.23 # make &&make install

1keepalived配置文件编辑

JZSHPT-APP6:~ # mkdir -p /etc/keepalived/{scripts,log}

JZSHPT-APP6:~ # vim /usr/local/keepalived/etc/keepalived/

! Configuration File for keepalived

 

 global_defs {

    router_idRedis2_DEVEL

}

 

vrrp_script chk_redis {

         script"/etc/keepalived/scripts/redis_check.sh"

         interval 2

         timeout2

         fall 3

}

 

vrrp_instance VI_1 {

     state BACKUP

     interfaceeth0                                         

    virtual_router_id 51

     priority200

      advert_int5

     authentication {

              auth_typePASS

       auth_pass redis

     }

     virtual_ipaddress{

         10.133.214.50

     }

    track_script {

        chk_redis

    }

       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

}

2keepalived启动脚本配置

JZSHPT-APP6:~ # cp /soft/keepalived-1.2.23/keepalived/etc/init.d/keepalived.suse.init/etc/init.d/keepalived.suse

JZSHPT-APP6:~ # cp /usr/local/keepalived/sbin/keepalived/usr/local/sbin/

JZSHPT-APP6:~ # cp/usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/

3)定义监控脚本

JZSHPT-APP6:~ # vim/etc/keepalived/scripts/redis_check.sh

#!/bin/sh

###/etc/keepalived/scripts/redis_check.sh

ALIVE=`/usr/local/bin/redis-cli PING`

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

  echo $ALIVE

  exit 0

else

  echo $ALIVE

  exit 1

fi

4)定义状态切换为master时执行的脚本

JZSHPT-APP6:~ # vim/etc/keepalived/scripts/redis_master.sh

#!/bin/sh

###/etc/keepalived/scripts/redis_master.sh

REDISCLI="redis-cli"

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

pid=$$

echo "`date+'%Y-%m-%d:%H:%M:%S'`|$pid|state:[slaver]" >> $LOGFILE

echo "`date +'%Y-%m-%d:%H:%M:%S'`|$pid|state:[slaver]Run 'SLAVEOF 10.133.214.15 6379'" >> $LOGFILE

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

echo "`date+'%Y-%m-%d:%H:%M:%S'`|$pid|state:[slaver] wait 10 sec for data sync from oldmaster" >> $LOGFILE

sleep 10

echo "`date+'%Y-%m-%d:%H:%M:%S'`|$pid|state:[slaver] data rsync from old mater ok...">> $LOGFILE

echo "`date+'%Y-%m-%d:%H:%M:%S'`|$pid|state:[master] Run slaveof no one,closemaster/slave" >> $LOGFILE

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

echo "`date+'%Y-%m-%d:%H:%M:%S'`|$pid|state:[master] wait other slave connect....">> $LOGFILE

5)定义状态切换为backups时执行的脚本

JZSHPT-APP6:~ # vim/etc/keepalived/scripts/redis_backup.sh

#!/bin/bash

###/etc/keepalived/scripts/redis_backup.sh

REDISCLI="redis-cli"

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

pid=$$

 

echo "`date+'%Y-%m-%d:%H:%M:%S'`|$pid|state:[master]" >> $LOGFILE

echo "`date+'%Y-%m-%d:%H:%M:%S'`|$pid|state:[master] Being slave state..." >>$LOGFILE 2>&1

echo "`date+'%Y-%m-%d:%H:%M:%S'`|$pid|state:[master] wait 10 sec for data sync from oldmaster" >> $LOGFILE

sleep 10

echo "`date +'%Y-%m-%d:%H:%M:%S'`|$pid|state:[master]data rsync from old mater ok..." >> $LOGFILE

echo "`date +'%Y-%m-%d:%H:%M:%S'`|$pid|state:[slaver]Run 'SLAVEOF 10.133.214.15 6379'" >> $LOGFILE

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

echo "`date +'%Y-%m-%d:%H:%M:%S'`|$pid|state:[slaver]slave connect to 10.133.214.15 ok..." >> $LOGFILE

6) 定义状态切换为stop时执行的脚本

JZSHPT-APP6:~ # cp/etc/keepalived/scripts/redis_backup.sh /etc/keepalived/scripts/redis_stop.sh

7) 定义状态切换为fault时执行的脚本

JZSHPT-APP6:~ # cp /etc/keepalived/scripts/redis_backup.sh/etc/keepalived/scripts/redis_fault.sh

3、安装rsyslog实现keepalived日志分离

1)安装rsyslog软件包

JZSHPT-APP5:/soft # rpm -ivhrsyslog-5.10.1-0.7.49.x86_64.rpm

2)修改rsyslog配置文件

JZSHPT-APP5:/soft # vim /etc/rsyslog.conf

140 #*.*;mail.none;news.none                        -/var/log/messages

141 *.*;mail.none;news.none;local0.none                     -/var/log/messages

153 #local0,local1.*                               -/var/log/localmessages

154 local0.*                               -/var/log/keepalived.log

155 local1.*                               -/var/log/localmessages

156 local2,local3.*                        -/var/log/localmessages

157 local4,local5.*                        -/var/log/localmessages

159  cal6,local7.*                         -/var/log/localmessages

3)修改rsyslog启动脚本配置文件

根据/etc/init.d/syslog中的三个启动选项,修改该文件内的”SYSLOG_DAEMON”选项

JZSHPT-APP5:/soft # vim /etc/sysconfig/syslog

40 #SYSLOG_DAEMON="syslog-ng"

41 SYSLOG_DAEMON="rsyslogd"                               

4)修改Keepalived启动脚本选项

JZSHPT-APP5:/soft #  vim /etc/init.d/keepalived.suse

17 DAEMON_OPT="-d "

18 DAEMON_OPT="-d -D -S 0"

5)启动rsyslog服务

JZSHPT-APP5:/soft # /etc/init.d/syslog restart

4、启动RedisKeepalived

JZSHPT-APP6:~ # /etc/init.d/redisd start

JZSHPT-APP6:~ # /etc/init.d/keepalived.suse start




结尾:


     感谢阅读,祝有收获的一天,谢谢!









      本文转自1清风揽月1  51CTO博客,原文链接:http://blog.51cto.com/watchmen/1941410,如需转载请自行联系原作者





相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore     ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库 ECS 实例和一台目标数据库 RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
相关文章
|
7月前
|
存储 监控 NoSQL
Redis 高可用之主从模式
上一节RDB和AOF持久化机制提到了 Redis 的持久性,也就是在服务器实例宕机或故障时,拥有再恢复的能力。但是在这个服务器实例宕机恢复期间,是无法接受新的数据请求。对于整体服务而言这是无法容忍的,因此我们可以使用多个服务器实例,在一个实例宕机中断时,另外的服务器实例可以继续对外提供服务,从而不中断业务。Redis 是如何做的呢?Redis 做法是**增加冗余副本**,**将一份数据同时保存在多个实例**上。那么如何保存各个实例之间的数据一致性呢?
83 0
Redis 高可用之主从模式
|
7月前
|
NoSQL 关系型数据库 MySQL
Redis高可用之主从复制架构(第一部分)
Redis高可用之主从复制架构(第一部分)
|
7月前
|
机器学习/深度学习 NoSQL Redis
Redis高可用之集群架构(第三部分)
Redis高可用之集群架构(第三部分)
|
7月前
|
消息中间件 NoSQL Redis
Redis高可用之哨兵模式(第二部分)
Redis高可用之哨兵模式(第二部分)
|
7月前
|
存储 监控 NoSQL
|
4月前
|
NoSQL Redis
基于Redis的高可用分布式锁——RedLock
这篇文章介绍了基于Redis的高可用分布式锁RedLock的概念、工作流程、获取和释放锁的方法,以及RedLock相比单机锁在高可用性上的优势,同时指出了其在某些特殊场景下的不足,并提到了ZooKeeper作为另一种实现分布式锁的方案。
131 2
基于Redis的高可用分布式锁——RedLock
|
5月前
|
监控 NoSQL Redis
Redis 哨兵模式高可用
Redis 哨兵模式高可用
91 4
|
2月前
|
存储 NoSQL 大数据
大数据-51 Redis 高可用方案CAP-AP 主从复制 一主一从 全量和增量同步 哨兵模式 docker-compose测试
大数据-51 Redis 高可用方案CAP-AP 主从复制 一主一从 全量和增量同步 哨兵模式 docker-compose测试
45 3
|
5月前
|
负载均衡 NoSQL 应用服务中间件
搭建高可用及负载均衡的Redis
【7月更文挑战第10天】
202 1
|
6月前
|
存储 运维 NoSQL
Redis 分区:构建高性能、高可用的大规模数据存储解决方案
Redis 分区:构建高性能、高可用的大规模数据存储解决方案