1、redis主备自动切换部署方案文档修改记录修改日期修改人修改说明版本号修改页主实例A:192.168.20.30备实例B:192.168.20.232虚IP:192.168.20.110方案说明:redis部署方式采用主备的方式,通过keepalived来对外提供虚IP,备服务器实时同步主服务器的数据,当主服务器挂掉时,能在5秒内完成主备切换,实现融灾备份,并采用redis数据持久化,确保数据的安全。正常工作时,虚IP在主实例A上,备实例B自动从A上同步数据,当检测到主实例A挂掉之后(检测机制为每3秒自动检测redis的所有端口是否能正常连接,当连续两次检测到其中任意端口无法连接时,将自动停止
2、keepalived程序,使虚IP进行漂移),备实例B将自动接管虚IP,待原主实例A恢复后,A将自动切换成备模式,从B上实时同步数据,主备角色互换,实现融灾备份。安装部署步骤如下:1. 安装keepalivedwget http:/www.keepalived.org/software/keepalived-1.2.6.tar.gztar zxvf keepalived-1.2.6.tar.gzcd keepalived-1.2.6./configure如果报错configure: error: ! OpenSSL is not properly installed on your syste
3、m. ! ! Can not include OpenSSL headers files.解决办法:yum -y install openssl-develyum -y install popt-develln -s /usr/src/kernels/2.6.32-220.el6.x86_64/ /usr/src/linux./configuremakemake installcp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/cp /usr/local/etc/sysconfig/keepalived /etc/sysconfi
4、g/cp /usr/local/sbin/keepalived /usr/sbin/ mkdir /etc/keepalived添加keepalived的配置项:vi /etc/keepalived/keepalived.confglobal_defs router_id LVS_DEVELvrrp_script Monitor_Redis script /home/bbcv/redis/redis_keepalive.sh interval 3 #每3秒执行一次 weight 2# VIP1vrrp_instance VI_1 state BACKUP #主备服务器都设置成BACKUP in
5、terface eth0 virtual_router_id 110 priority 100 # 备份服务上将100改为90 advert_int 1 #检查间隔 nopreempt #设置为不抢占,注意这个配置只能设置在state为BACKUP的主机上,而且这个主机的priority必须比另外一台高 authentication auth_type PASS auth_pass 1111 notify_master /home/bbcv/redis/redis_master.sh notify_backup /home/bbcv/redis/redis_backup.sh track_sc
6、ript Monitor_Redis #(调用redis进程检测脚本) virtual_ipaddress 192.168.20.110 #(如果有多个VIP,继续换行填写.) 启动keepalivedservice keepalived start2. redis_keepalive.sh脚本该脚本主要实现对redis进程进行监控,当检测到redis进程挂掉时,自动停止keepalived进程,使虚IP进行漂移 注意添加该脚本的可执行权限!chmod +x redis_keepalive.sh#!/bin/bashtime=$(date +%Y-%m-%d %H:%M:%S )#redis部
7、署路径redispath=/home/bbcv/redis#redis-cli部署路径rediscli=/usr/local/bin/redis-cliif ! -d $redispath/logs ; then mkdir $redispath/logsfilogfile=$redispath/logs/redis-state.logoldfile=$logfile.$(date +%Y-%m-%d -date=30 days ago)yesterdayfile=$logfile.$(date +%Y-%m-%d -date=1 days ago)#把昨天的日志重命名if -f $yeste
8、rdayfile then echo OKelse mv $logfile $yesterdayfile $logfile 2&1 echo $time $yesterdayfile Move yesterdayfile Success! $logfilefi#删除老的日志文件if -f $oldfile then rm -f $oldfile $logfile 2&1 echo $time $oldfile Delete Old File Success! $logfileelse echo no old filefi#检测redis端口是否正常for line in cat $redisp
9、ath/config.inidoport=echo $line|awk -F | print $1passwd=echo $line|awk -F | print $2$rediscli -h 127.0.0.1 -p $port -a $passwd info /dev/nullif $? -eq 0 ; then status=$rediscli -h 127.0.0.1 -p $port -a $passwd info|grep role|awk -F : print $2 echo $time redis端口$port正常,目前状态为:$status $logfileelse echo
10、 $time ERROR redis端口$port异常 $logfile sleep 2 # try to found it again $rediscli -h 127.0.0.1 -p $port -a $passwd info /dev/null if $? -eq 0 ; then status=$rediscli -h 127.0.0.1 -p $port -a $passwd info|grep role|awk -F : print $2 echo $time redis端口$port正常,目前状态为:$status $logfile #exit 0 else echo $tim
11、e ERROR redis端口$port异常,开始停keepalived $logfile #stop keepalived /etc/init.d/keepalived stop echo $time stop keepalived $logfile fifidone3. redis_master.sh脚本该脚本主要实现当keepalived切换到master时,执行slaveof no one语句把redis的端口转换为主模式 注意添加该脚本的可执行权限!chmod +x redis_master.sh#!/bin/sh#本机IP地址localip=127.0.0.1#另一台服务器的地址b
12、ackip=192.168.20.232time=$(date +%Y-%m-%d %H:%M:%S )#redis部署路径redispath=/home/bbcv/redis#redis-cli部署路径rediscli=/usr/local/bin/redis-cli#日志文件目录logfile=$redispath/logs/redis-state.logfor line in cat $redispath/config.inidoport=echo $line|awk -F | print $1passwd=echo $line|awk -F | print $2echo $time r
13、edis端口$port切换成主端口 $logfile$rediscli -h $localip -p $port -a $passwd slaveof no one $logfile 2&1sleep 1done4. redis_backup.sh该脚本主要实现当keepalived切换到master时,执行slaveof 主redisIP 端口 语句,把redis的端口转换为备模式,并同步主redis的数据 注意添加该脚本的可执行权限!chmod +x redis_backup.sh#!/bin/sh#本机IP地址localip=127.0.0.1#另一台服务器的地址backip=192.1
14、68.20.232time=$(date +%Y-%m-%d %H:%M:%S )#redis部署路径redispath=/home/bbcv/redis#redis-cli部署路径rediscli=/usr/local/bin/redis-cli#日志文件目录logfile=$redispath/logs/redis-state.log#检测主redis服务是否启动port=cat $redispath/config.ini |sed -n 1p| awk -F | print $1passwd=cat $redispath/config.ini |sed -n 1p| awk -F | p
15、rint $2$rediscli -h $backip -p $port -a $passwd info /dev/nullif $? -eq 0 ; then echo $time 主redis服务$backip正常在用 $logfile echo $time 30秒后将从主redis服务$backip同步数据 $logfile sleep 30 time=$(date +%Y-%m-%d %H:%M:%S ) echo $time 开始从redis主服务$backip同步数据 $logfile for line in cat $redispath/config.ini do port=ec
16、ho $line|awk -F | print $1 passwd=echo $line|awk -F | print $2 time=$(date +%Y-%m-%d %H:%M:%S ) echo $time redis $port端口切换成$backip的备端口 $logfile $rediscli -h $localip -p $port -a $passwd slaveof $backip $port $logfile 2&1 sleep 1 doneelse echo $time 启用本机为主redis! $logfilefi5. config.ini该配置文件为每个redis端口
17、对应的密码14948|kuanyun00111172|kuanyun00112172|kuanyun00113172|kuanyun00116172|kuanyun00118000|kuanyun00118001|kuanyun00121172|kuanyun00122172|kuanyun00123172|kuanyun0016. 安装redis1.新建文件夹,如mkdir redis2.将redis-2.2.13.tar.gz拷入其中3.解压并进入主目录4.make5.make install(Ubuntu上需要sudo执行)6.修改配置文件redis.conf修改如下:daemonize
18、 no 改为 daemonize yes取消注释:syslog-enabled no (改为syslog-enabled yes )syslog-facility local0 修改port:port 14948 (改为具体的redis端口)设置redis端口密码:requirepass mypasswd (取消注释,并改为想要设置的密码)设置master的密码:masterauth master-password (该配置为主redis设置的密码)修改dump文件名称:dbfilename aim_dump.rdb (修改成具体模块的dump名称)已将修改后的aim.redis.conf文件
19、放到redis部署目录下,不同需求还需再修改配置文件(如修改端口、增加密码、修改dump文件名称等),可参考如下aim.redis.conf文件7.在执行redis-server redis.conf启动时,可能会报如下错误:Cant chdir to /var/db/redis: No such file or directory 需要手动创建/var/db/redis文件夹# WARNING overcommit_memory is set to 0! Background save may fail under low memory condition 需要执行 sysctl vm.ov
20、ercommit_memory=1 修改内核参数8.再次执行redis-server redis.conf可成功启动redis7. redis启停脚本启动脚本:start.sh#!/bin/shredis-server urm.redis.confredis-server cscs.redis.confredis-server cums.redis.confredis-server aim.redis.confredis-server bgw.redis.confredis-server ggw.redis.confredis-server mgw.redis.confredis-server
21、 vgw.redis.confredis-server ordergw.redis.confredis-server keygw.redis.confPID=ps -ef |grep keepalived|grep -v grep|awk print $2if ! -z $PID ; then echo keepalived is alivedelse echo start keepalived . /etc/init.d/keepalived startfi停止脚本:stop.sh#!/bin/shpidlist=ps -ef |egrep redis|sed /.*grep/d|awk p
22、rint $2for pid in $pidlist; doecho kill pid $pid startkill -9 $pidecho kill pid $pid enddone8. 测试1.查看当前redis是主还是备/usr/local/bin/redis-cli -h 127.0.0.1 -p 6379 info2.在主redisA上新增一条数据,查看备redisB是否同步该数据/home/bbcv/redis/redis-2.0.0-rc4/redis-cli -h 127.0.0.1 -p 63793.停止主redisA,查看虚IP是否切换到备redisB服务器,并查看备redisB服务器是否切换成主状态ip a查看虚IP是否在本机上4.恢复redisA,查看redisA是否从redisB中同步数据