一、环境说明
操作系统版本:RHEL 5.4_64
redis版本:2.8.17
keepalived版本:1.1.15
master:10.142.130.81
slave:  10.142.130.82
Virtural IP Address (VIP) :10.142.130.83(对外提供服务)
 
redis安装路径:/app/tomcat/redis
redis端口:6379
keepalived安装路径:/etc/keepalived
 
二、设计思路
1、当master与slave均运作正常时,VIP绑定在master上,master负责对外提供服务,slave进行replication,作为备用。
2、当master挂掉,slave正常时,VIP漂移至slave,由slave接管服务,同时关闭主从复制功能。
3、当master恢复正常后,从slave上同步数据,但不继续接管服务,此时master作为备用。
4、当slave挂掉,master正常,master接管服务,关闭主从复制功能。与此同时,位于slave恢复正常后,从master上同步数据,作为备用。
5、依次循环。
注意:master和slave上均开启rdb快照持久化。
 
三、具体配置步骤
1、在master和slave上分别安装redis
# su - 
# tar zxf redis-2.8.17.tar.gz
# cd zxf redis-2.8.17
# make && make install
# cd ..
# mv redis-2.8.17 /app/tomcat/redis
# chown -R tomcat.app /app/tomcat/redis
 
2、配置redis
# su - tomcat
# cd /app/tomcat/redis
# mkdir bin rdb conf log
# mv  redis.conf  sentinel.conf ./conf/
# find /app/tomcat/redis/ -maxdepth 1 -type f -delete
# cd src
# mv mkreleasehdr.sh  redis-benchmark  redis-check-aof  redis-check-dump  redis-cli  redis-sentinel  redis-server ../bin/
 
编辑/app/tomcat/redis/conf/redis.conf文件,修改如下内容:
daemonize yes   
pidfile /app/tomcat/redis/redis.pid 
port 6376  
logfile  /app/tomcat/redis/log/redis.log 
dir /app/tomcat/redis/rdb

maxmemory 16106127360   #给redis设定最大使用内存,这里是15G,根据实际情况而定。

 
3、在master和slave上分别安装keepalived

 
# su

# tar zxf keepalived-1.1.15.tar.gz

# cd keepalived-1.1.15

# ./configure --sysconf=/etc --with-kernel-dir=/usr/src/kernels/2.6.18-164.el5-x86_64

# make

# make install

# cp /usr/local/sbin/keepalived /sbin/keepalived

 
4、master上的keepalived配置
 
# cd /etc/keepalived
# >keepalived.conf
# vi keepalived.conf  #添加如下内容
! Configuration File for keepalived
 
global_defs {
        router_id redis-master
}
 
vrrp_script Monitor_redis {
        script "/etc/keepalived/scripts/redis_monitor.sh"
        interval 2
        weight 2
}
 
vrrp_instance VI_1{
        state BACKUP
        interface bond0
        virtual_router_id 51
        mcast_src_ip 10.142.130.81
        priority  100
        nopreempt
        advert_int 1
        authentication {
        auth_type PASS
        auth_pass 1122
        }
        track_script {
                Monitor_redis
        }
        virtual_ipaddress {
                10.142.130.83
        }
        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
}
 
# mkdir scripts
# cd scripts
# vi redis_monitor.sh  #redis监控脚本
#!/bin/bash    
value=$(/app/tomcat/redis/bin/redis-cli -h 10.142.130.81 -p 6379 get name)
 
if [ "${value}" == "test" ]; then
    exit 0
    else
    /etc/init.d/keepalived stop
    exit 1
fi            
 
# vi redis_master.sh #当状态为master时执行的脚本,用来关闭主从复制
#!/bin/bash    
REDISCLI="/app/tomcat/redis/bin/redis-cli"  
LOGFILE="/app/tomcat/redis/log/keepalived-redis-state.log"  
echo "[master]" >> ${LOGFILE}
date >> ${LOGFILE}    
echo "Being master...." >> ${LOGFILE} 2>&1    
echo "Run SLAVEOF NO ONE cmd ..." >> ${LOGFILE}
${REDISCLI} SLAVEOF NO ONE >> ${LOGFILE} 2>&1
 
# vi redis_backup.sh #当状态为backup时执行的脚本,用于开启主从复制
#!/bin/bash    
REDISCLI="/app/tomcat/redis/bin/redis-cli"  
LOGFILE="/app/tomcat/redis/log/keepalived-redis-state.log"  
echo "[backup]" >> ${LOGFILE}
date >> ${LOGFILE}
echo "Being slave...." >> ${LOGFILE} 2>&1    
sleep 10    
echo "Run SLAVEOF cmd ..." >> ${LOGFILE}
${REDISCLI} SLAVEOF 10.142.130.82 6379 >> ${LOGFILE}  2>&1
 
# vi redis_fault.sh
#!/bin/bash  
LOGFILE="/app/tomcat/redis/redis/log/keepalived-redis-state.log"
echo "[fault]" >> $LOGFILE  
date >> $LOGFILE
 
# vi redis_stop.sh
#!/bin/bash  
LOGFILE="/app/tomcat/redis/log/keepalived-redis-state.log"  
echo "[stop]" >> $LOGFILE  
date >> $LOGFILE
 
给脚本加上可执行权限

chmod +x /etc/keepalived/scripts/*.sh

 
 
5、slave上的keepalived配置
# cd /etc/keepalived
# >keepalived.conf
# vi keepalived.conf  #添加如下内容

! Configuration File for keepalived

 
global_defs {  
  router_id redis_backup
}  
 
vrrp_script Monitor_redis {  
  script "/etc/keepalived/scripts/redis_monitor.sh"  
  interval 2  
  weight 2    
}  
 
vrrp_instance VI_1{  
  state BACKUP  
  interface bond0  
  virtual_router_id 51  
  mcast_src_ip 10.142.130.82
  priority  90  
  advert_int 1  
  authentication {  
  auth_type PASS  
  auth_pass 1122
 }  
  track_script {  
   Monitor_redis  
 }  
  virtual_ipaddress {  
   10.142.130.83
  }  
 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    
}
 
# mkdir scripts
# cd scripts
# vi redis_monitor.sh  #redis监控脚本

#!/bin/bash

value=$(/app/tomcat/redis/bin/redis-cli -h 10.142.130.82 -p 6379 get name)
if [ "${value}" == "test" ]; then
    exit 0
    else
    /etc/init.d/keepalived stop
    exit 1
fi
 
# vi redis_master.sh
#!/bin/bash    
REDISCLI="/app/tomcat/redis/bin/redis-cli"  
LOGFILE="/app/tomcat/redis/log/keepalived-redis-state.log"  
 
echo "[master]" >> ${LOGFILE}
date >> ${LOGFILE}    
echo "Being master...." >> ${LOGFILE} 2>&1    
echo "Run SLAVEOF NO ONE cmd ..." >> ${LOGFILE}
${REDISCLI} SLAVEOF NO ONE >> ${LOGFILE} 2>&1 
 
# vi redis_backup.sh
#!/bin/bash    
REDISCLI="/app/tomcat/redis/bin/redis-cli"  
LOGFILE="/app/tomcat/redis/log/keepalived-redis-state.log"  
echo "[backup]" >> ${LOGFILE}
date >> ${LOGFILE}
echo "Being slave...." >> ${LOGFILE} 2>&1    
sleep 10    
echo "Run SLAVEOF cmd ..." >> ${LOGFILE}
${REDISCLI} SLAVEOF 10.142.130.81 6379 >> ${LOGFILE}  2>&1
 
# vi redis_fault.sh
#!/bin/bash  
LOGFILE="/app/tomcat/redis/redis/log/keepalived-redis-state.log"
echo "[fault]" >> ${LOGFILE}
date >> ${LOGFILE}
 
# vi redis_stop.sh
#!/bin/bash  
LOGFILE="/app/tomcat/redis/log/keepalived-redis-state.log"  
echo "[stop]" >> ${LOGFILE}
date >> ${LOGFILE}
 
给脚本加上可执行权限

chmod +x /etc/keepalived/scripts/*.sh

 
四、启动
为方便后续管理,编写一个简单脚本重启redis
# vi restart-redis.sh
#!/bin/bash
ps -ef|awk '/app\/tomcat\/redis\/bin\/redis-server/{print $2}'|xargs kill -9
/app/tomcat/redis/bin/redis-server /app/tomcat/redis/conf/redis.conf
 
启动顺序
1、分别启动master和slave上redis
2、给master和slave上的redis设置检测的key和value
     # /app/tomcat/redis/bin/redis-cli set name test
3、先启动salve上的keepalived,启动方法,用root用户执行命令/etc/init.d/keepalived start
4、用同样的方法启动master上的keepalived
 
注意事项:
执行ip addr show bond0命令查看VIP绑定在哪个主机上,执行redis-cli info查看主从关系,确保VIP绑定的机器一定要是redis中的master。
如果不是,通过手动关闭keepalived进行调整,一定要保证VIP和主从复制关系正确。首次使用前调整好,后续自动切换基本不会有问题。
 
 
五、监控
master挂了,slave会接管服务,那如何去恢复master,让他恢复之后成为slave的角色呢。这就需要要每台主机上部署一个监控脚本,定时每分钟监测一次。脚本如下:
# cat monitor_redis.sh
#!/bin/bash
num_proc=$(/bin/ps -fe|grep [k]eepalived|wc -l)
active=$(/app/tomcat/redis/bin/redis-cli get name)
if [ "${num_proc}" != "3" ];then
        if [ "${active}" != "test" ];then
                /app/tomcat/bin/restart-redis.sh &>/dev/null
                if [ "$(/bin/ps -ef|grep [r]edis-server|wc -l)" == "1" -a "${active}" == "test" ];then
                        sleep 30
                        /usr/bin/sudo /etc/init.d/keepalived start &>/dev/null
                else
                        /app/tomcat/redis/bin/redis-cli set name test
                        [ "${active}" == "test" ] && /usr/bin/sudo /etc/init.d/keepalived start &>/dev/null
                fi
        else
                /usr/bin/sudo /etc/init.d/keepalived start &>/dev/null
        fi
fi
 
六、总结
1、keepalived的master与slave一般通过state和priority来指定,但是这两项设置满足不了我们上面的切换要求,这里是将state 都设置为BACKUP,然后以优先级riority的高低来决定     最初的master归属,另外优先级高的那台需要添加 nopreempt,这个参数的作用是keepalived恢复之后不主动抢占master的角色,等另一端挂掉之后会自动接管。
2、redis启动成功了之后并且能get设定的那个值后keepalived才能正常启动。
3、随着dump.rdb文件日益增大,redis重启后完全将数据加载进内存的时间会越来越长。monitor_redis.sh脚本中sleep 时间也需要作相应调整。

Keepalived实现Redis Failover的更多相关文章

  1. 通过Keepalived实现Redis Failover自动故障切换功能

    通过Keepalived实现Redis Failover自动故障切换功能[实践分享] 参考资料: http://patrick-tang.blogspot.com/2012/06/redis-keep ...

  2. 基于keepalived对redis做高可用配置---转载

    关于keepalived的详细介绍,请移步本人相关博客:http://wangfeng7399.blog.51cto.com/3518031/1405785 功能 ip地址 安装软件 主redis 1 ...

  3. redis+Keepalived实现Redis主从复制

    redis+Keepalived实现Redis主从复制: 环境:CentOs6.5Master: 10.10.10.203Slave:   10.10.10.204Virtural IP Addres ...

  4. Keepalived+nginx+redis主从+tomcat一机多实例实现会话共享

    Keepalived+nginx+redis主从+tomcat一机多实例实现会话共享 2014-09-09 14:14:25 标签:会话共享 主从 原创作品,允许转载,转载时请务必以超链接形式标明文章 ...

  5. Redis主从配置及通过Keepalived实现Redis自动切换高可用

    Redis主从配置及通过Keepalived实现Redis自动切换高可用 [日期:2014-07-23] 来源:Linux社区  作者:fuquanjun [字体:大 中 小]   一:环境介绍: M ...

  6. Redis failover过程

    在Leader触发failover之前,首先wait数秒(随即0~5),以便让其他sentinel实例准备和调整.如果一切正常,那么leader就需要开始将一个salve提升为master,此slav ...

  7. Keepalived+Redis高可用部署(第二版)

    更新 20150625 脚本由5个减少为4个,sh脚本指令做了精简. 修改了另外3个脚本,在日志里增加了日期显示. 新增redis数据类型,持久化,主从同步简介. 新增hiredis简介. 新增c语言 ...

  8. Keepalived+Redis高可用部署

    1   Redis简介及安装 Redis是一个开源,先进的key-value存储,并用于构建高性能,可扩展的Web应用程序的完美解决方案. Redis从它的许多竞争继承来的三个主要特点: Redis数 ...

  9. install keepalived on RedHat/CentOS to provide IP failover for web cluster

    Contents [hide]  1 Introduction 2 Our Sample Setup 3 Install Keepalived 4 Install Kernel Headers 5 C ...

随机推荐

  1. (转)resize扩展

    jquery 默认的resize只能监听到浏览器窗口大小的改变,但我们在实际使用过程中有可能还需要监听某个div或其它标签的大小改变来执行相应的处理,如果使用默认的resize就无能为力了.怎么办呢, ...

  2. Git CMD - pull: Fetch from and integrate with another repository or a local branch

    命令格式 git pull [options] [<repository> [<refspec>…​]] 命令参数 -q, --quiet 安静模式. -v, --verbos ...

  3. 惠普 Compaq 6520s 无线开关打不开

    问题描述:键盘上面的无线开关怎么按都打不开,始终是出于黄色的状态   解决方法:尝试恢复bios默认值测试: 开机不停点击F10进入bios,选择File选项,选择Restore Defaults-- ...

  4. java中怎么进行字符串替换?

    String str = "test.doc"; String newStr = str.replaceAll("doc","html");

  5. framework not found -fno-arc编译错误

    由于我是刚接手的代码  然后我拿来运行根本就是运行不了的  然后需要在linker 那边删除点东西就可以了. 把下边的两个删除就可以了 关于other linker flags 的介绍 请参考http ...

  6. Ubuntu14.04 Tomcat 安装过程记录

    Ubuntu14.04 Tomcat 安装过程记录 检查java的版本 zhousp@ubuntu:~$ sudo java -version [sudo] password for zhousp: ...

  7. 【POJ3461】【KMP】Oulipo

    Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, without t ...

  8. javascript--15条规则解析JavaScript对象布局(__proto__、prototype、constructor)

    大家都说JavaScript的属性多,记不过来,各种结构复杂不易了解.确实JS是一门入门快提高难的语言,但是也有其他办法可以辅助记忆.下面就来讨论一下JS的一大难点-对象布局,究竟设计JS这门语言的人 ...

  9. 使用require.js时,解决AMD封装jquery1.4.1的问题。

    require.config({ baseUrl: "js/", paths: { "jquery": "jquery-1.4.1.min" ...

  10. 浏览器兼容性判定写法格式(ie)

    条件注释判断浏览器<!--[if !IE]><!--[if IE]><!--[if lt IE 6]><!--[if gte IE 6]> <!- ...