redis主备部署方案Redis部署方式采用主备的方式,通过keepalived来对外提供虚IP,并实现主备自动切换功能。主实例A:192.168.20.30备实例B:192.168.20.232虚IP:192.168.20.110正常工作时,虚IP在主实例A上,主实例A上的数据自动同步到备实例B上,当主实例A挂掉之后,备实例B将自动接管虚IP,并将redis转换为主模式,待原主实例A恢复后,A将自动切换成备模式,从B上同步数据,主备角色互换,实现融灾备份。安装部署步骤如下:1.安装keepalivedwget如果报错configure:error:!!!OpenSSLisnotproperlyinstalledonyoursystem.!!!!!!CannotincludeOpenSSLheadersfiles.解决办法:yum-yinstallopenssl-develyum-yinstallpopt-develln-s/usr/src/kernels/2.6.32-220.el6.x86_64//usr/src/linux./configuremakemakeinstallcp/usr/local/etc/rc.d/init.d/keepalived/etc/rc.d/init.d/cp/usr/local/etc/sysconfig/keepalived/etc/sysconfig/cp/usr/local/sbin/keepalived/usr/sbin/mkdir/etc/keepalived添加keepalived的配置项:vi/etc/keepalived/keepalived.confglobal_defs{router_idLVS_DEVEL}vrrp_scriptMonitor_Redis{script/home/bbcv/redis/redis_keepalive.shinterval3#每3秒执行一次weight2}#VIP1vrrp_instanceVI_1{stateBACKUP#主备服务器都设置成BACKUPinterfaceeth0virtual_router_id55priority100#备份服务上将100改为90advert_int1#检查间隔nopreempt#设置为不抢占,注意这个配置只能设置在state为BACKUP的主机上,而且这个主机的priority必须比另外一台高authentication{auth_typePASSauth_pass1111}notify_master/home/bbcv/redis/redis_master.shnotify_backup/home/bbcv/redis/redis_backup.shtrack_script{Monitor_Redis#(调用redis进程检测脚本)}virtual_ipaddress{192.168.20.110#(如果有多个VIP,继续换行填写.)}}启动keepalivedservicekeepalivedstart2.redis_keepalive.sh脚本该脚本主要实现对redis进程进行监控,当检测到redis进程挂掉时,自动停止keepalived进程,使虚IP进行漂移注意添加该脚本的可执行权限!chmod+xredis_keepalive.sh#!/bin/bashtime=$(date'+%Y-%m-%d%H:%M:%S')#redis部署路径redispath=/home/bbcv/redis#redis-cli部署路径rediscli=$redispath/redis-2.0.0-rc4/redis-cliif[!-dlogs];thenmkdir$redispath/logsfilogfile=$redispath/logs/redis-state.logoldfile=$logfile'.'$(date+%Y-%m-%d--date='30daysago')yesterdayfile=$logfile'.'$(date+%Y-%m-%d--date='1daysago')#把昨天的日志重命名if[-f$yesterdayfile]thenechoOKelsemv$logfile$yesterdayfile$logfile2&1echo$time[$yesterdayfile]MoveyesterdayfileSuccess!$logfilefi#删除老的日志文件if[-f$oldfile]thenrm-f$oldfile$logfile2&1echo$time[$oldfile]DeleteOldFileSuccess!$logfileelseechonooldfilefi#检测redis端口是否正常status=`$rediscli-h127.0.0.1-p6379info|greprole|awk-F:'{print$2}'`$rediscli-h127.0.0.1-p6379info/dev/nullif[$?-eq0];thenecho$timeredisserverisOK$logfileecho目前状态为:$status$logfileelseecho$timenoredisservicefound!$logfilesleep2#trytofounditagain$rediscli-h127.0.0.1-p6379info/dev/nullif[$?-eq0];thenecho$timeredisserverisOK$logfileecho目前状态为:$status$logfile#exit0elseecho$timeredisservererror$logfile#stopkeepalived/etc/init.d/keepalivedstopecho$timestopkeepalived$logfilefifi3.redis_master.sh脚本该脚本主要实现当keepalived切换到master时,执行slaveofnoone语句把redis的端口转换为主模式注意添加该脚本的可执行权限!chmod+xredis_master.sh#!/bin/sh#本机IP地址localip=127.0.0.1#另一台服务器的地址backip=192.168.20.232#redis端口号port=637999981111114948time=$(date'+%Y-%m-%d%H:%M:%S')#redis部署路径redispath=/home/bbcv/redis#redis-cli部署路径rediscli=$redispath/redis-2.0.0-rc4/redis-cli#日志文件目录logfile=$redispath/logs/redis-state.logforpin$portdo{echo$timeredis$p端口切换成主端口$logfile$rediscli-h$localip-p$pslaveofnoone$logfile2&1sleep1}done4.redis_backup.sh该脚本主要实现当keepalived切换到master时,执行slaveof主redisIP端口语句,把redis的端口转换为备模式,并同步主redis的数据注意添加该脚本的可执行权限!chmod+xredis_backup.sh#!/bin/sh#本机IP地址localip=127.0.0.1#另一台服务器的地址backip=192.168.20.232#redis端口号port=637999981111114948time=$(date'+%Y-%m-%d%H:%M:%S')#redis部署路径redispath=/home/bbcv/redis#redis-cli部署路径rediscli=$redispath/redis-2.0.0-rc4/redis-cli#日志文件目录logfile=$redispath/logs/redis-state.logforpin$portdo{echo$timeredis$p端口切换成$backip的备端口$logfile$rediscli-h$localip-p$pslaveof$backip$p$logfile2&1sleep1}done5.安装redis1.新建文件夹,如mkdirredis2.将redis-2.2.13.tar.gz拷入其中3.解压并进入主目录4.make5.makeinstall(Ubuntu上需要sudo执行)6.修改配置文件redis.conf修改如下:daemonizeno改为daemonizeyes取消注释:syslog-enabledno(改为syslog-enabledyes)syslog-facilitylocal0已将修改后的redis.conf文件放入附件,不同需求还需再修改配置文件(如修改端口等)7.在执行redis-serverredis.conf启动时,可能会报如下错误:Can'tchdirto'/var/db/redis':Nosuchfileordirectory需要手动创建'/var/db/redis'文件夹#WARNINGovercommit_memoryissetto0!Backgroundsavemayfailunderlowmemorycondition需要执行sysctlvm.overcommit_memory=1修改内核参数8.再次执行redis-serverredis.conf可成功启动redis6.测试1.查看当前redis是主还是备/home/bbcv/redis/redis-2.0.0-rc4/redis-cli-h127.0.0.1-p6379info2.在主redisA上新增一条数据,查看备redisB是否同步该数据/home/bbcv/redis/redis-2.0.0-rc4/redis-cli-h127.0.0.1-p63793.停止主redisA,查看虚IP是否切换到备redisB服务器,并查看备redisB服务器是否切换成主状态ipa查看虚IP是否在本机上4.恢复redisA,查看redisA是否从redisB中同步数据