Redis双机热备方案参考资料:://deidara.blog.51cto.com/400447/302402://shiguanghui.iteye.com/blog/2001499背景目前,Redis集群的官方方案还处在开发测试中,未集成到稳定版中。且目前官方开发中的RedisCluster提供的功能尚不完善(可参考官方网站或),在生产环境中不推荐使用。通过调研发现市面上要实现采用单一的IP来访问,大多采用keepalived实现redis的双机热备作为过渡方案。环境部署环境介绍:Master:192.168.1.218redis,keepalivedSlave:192.168.1.219redis,keepalivedVirturalIPAddress(VIP):192.168.1.220设计思路:两个redisserver主从备份。提供redis服务高可用;两个keepalived服务主从备份,提供VIP服务的高可用。1)每台redisserver分别有主,从两个配置文件(redis_master.conf,redis_slave.conf),通过启动脚本启动服务,启动脚本会检测这个redis集群中的其他服务器的角色,如果有master服务存在,则以slave角色启动,否则将自己以master服务器启动;2)keepalived监控脚本,定时(频率:每秒一次)检测当前服务器是否获取集群VIP,如果获取集群VIP,则将本服务器上的redis服务器设置为master。同时将远端的其他redis服务器设置为slave;保证获取集群VIP的redis服务器角色为master,其他的设置为slave。3)keepalived监控脚本,还会自动检测当前redis服务器是否正常,如果连续两次检测异常,则停掉本本机的keepalived服务,释放集群VIP,让去漂移到其他可以提供redis服务的服务器上;4)当Master与Slave均运作正常时,Master负责服务,Slave负责同步数据;当Master挂掉,Slave正常时,Slave接管服务,同时关闭主从复制功能;当Master恢复正常,则从Slave同步数据,同步数据之后关闭主从复制功能,恢复Master身份,于此同时Slave等待Master同步数据完成之后,恢复Slave身份。然后依次循环。实施步骤:----创建专用用户useradd-gdevelopredisadminechoHisun@1125|passwd--stdinredisadmin说明:以下部署过程都是在root(或具备sudo权限的账号)账户下进行。----安装配置redis1.下载redis源码cdwget安装redistar-zxvfredis-2.8.3.tar.gzcdredis-2.8.3#reds的安装可以不用执行configuremake#测试maketest####在速度较慢的机器上执行maketest可能出现下列错误,无影响#***[err]:Testreplicationpartialresync:nobacklogintests/integration/replication-psync.tcl3.配置redis#创建redis主目录mkdir-p/usr/local/redis-2.8.3/{bin,conf,logs}cp-a-R-psrc/redis-server/usr/local/redis-2.8.3/bin/cp-a-R-psrc/redis-cli/usr/local/redis-2.8.3/bin/cp-a-R-psrc/redis-benchmark/usr/local/redis-2.8.3/bin/cp-a-R-psrc/redis-sentinel/usr/local/redis-2.8.3/bin/cp-a-R-psrc/redis-check-dump/usr/local/redis-2.8.3/bin/cp-a-R-psrc/redis-check-aof/usr/local/redis-2.8.3/bin/#创建redis启动脚本vi/usr/local/redis-2.8.3/redis-start.sh####以下为master上的配置,slave上的配置只需要修改对应的LOCALIP和REMOTEIP即可。#!/bin/bashREDISPATH=/usr/local/redis-2.8.3REDISCLI=$REDISPATH/bin/redis-cliLOGFILE=$REDISPATH/logs/redis-state.logLOCALIP=192.168.1.218REMOTEIP=192.168.1.219REMOTEREDISROLE=`$REDISCLI-h$REMOTEIPinfo|greprole`ifgreprole:master<<<$REMOTEREDISROLE;then#startasslave$REDISPATH/bin/redis-server$REDISPATH/conf/redis_slave.confif[$?==0];thenecho[INFO]`date+%F/%H:%M:%S`:$LOCALIPstartasslavesuccessful.>>$LOGFILEelseecho[ERROR]`date+%F/%H:%M:%S`:$LOCALIPstartasslaveerror.>>$LOGFILEfielse#startasmaster$REDISPATH/bin/redis-server$REDISPATH/conf/redis_master.confif[$?==0];thenecho[INFO]`date+%F/%H:%M:%S`:$LOCALIPstartasmastersuccessful.>>$LOGFILEelseecho[ERROR]`date+%F/%H:%M:%S`:$LOCALIPstartasmastererror.>>$LOGFILEfifi#创建redis关闭脚本vi/usr/local/redis-2.8.3/redis-stop.sh####以下为master上的配置,slave上的配置相同。#!/bin/bashREDISPATH=/usr/local/redis-2.8.3LOGFILE=$REDISPATH/logs/redis-state.logkill-9`ps-ef|grep'/bin/redis-server'|grep-vgrep|awk'{print$2}'`if[$?==0];thenecho[INFO]`date+%F/%H:%M:%S`:redisshutdowncompleted!>>$LOGFILEelseecho[ERROR]`date+%F/%H:%M:%S`:redisisnotstarted.>>$LOGFILEfi#创建redis配置文件cp-a-R-predis.conf/usr/local/redis-2.8.3/conf/redis_master.confcp-a-R-predis.conf/usr/local/redis-2.8.3/conf/redis_slave.conf#修改redis_master.conf对应配置项:####192.168.1.218主服务器redis_master.conf对应配置项#######daemonizenodaemonizeyes#bind127.0.0.1bind192.168.1.218logfile/usr/local/redis-2.8.3/logs/redis.log#其他配置依据实际生产环境修改############################################################192.168.1.219从服务器redis_master.conf对应配置项#######daemonizenodaemonizeyes#bind127.0.0.1bind192.168.1.219logfile/usr/local/redis-2.8.3/logs/redis.log#其他配置依据实际生产环境修改#########################################################修改redis_slave.conf对应配置项:####192.168.1.218主服务器redis_slave.conf对应配置项########daemonizenodaemonizeyes#bind127.0.0.1bind192.168.1.218logfile/usr/local/redis-2.8.3/logs/redis.log#slaveof<masterip><masterport>slaveof192.168.1.2196379#其他配置依据实际生产环境修改############################################################192.168.1.219从服务器redis_slave.conf对应配置项########daemonizenodaemonizeyes#bind127.0.0.1bind192.168.1.219logfile/usr/local/redis-2.8.3/logs/redis.log#slaveof<masterip><masterport>slaveof192.168.1.2186379#其他配置依据实际生产环境修改#########################################################修改redis的属主和权限chmod–R750/usr/local/redis-2.8.3/chown–Rredisadmin:develop/usr/local/redis-2.8.3/----安装配置keepalived1.下载keepalived源码Release1.2.9注意:最新版为1.2.10测试过程中出错.wget安装keepalived需要安装以下依赖包:makegcclibpopt-devlibnl-devlibcurl4-openssl-devpoptopensslcdtar–zxvfkeepalived-1.2.9.tar.gzcdkeepalived-1.2.9./configure--prefix=/usr/local/keepalivedmake&&makeinstall3.配置keepalived#在Master和Slave上创建如下配置文件(可根据实际情况调整):mv/usr/local/keepalived/etc/keepalived/keepalived.conf/usr/local/keepalived/etc/keepalived/keepaliv