一、环境说明
操作系统版本: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. xslt语法之---基础语法

    1. XSLT常用元素: 1.1 <xsl:template>:创建模板     Match属性的作用是使模板和XML元素相关联 <xsl:template match=" ...

  2. js中offsetHeight、clientHeight、scrollHeight等相关属性区分总结

    今天再次遇到了offset***.client***.scroll***的这三类属性的问题,总是混淆,现归纳总结如下: 大体上来说可以这样理解: client***属性(clientWidth.cli ...

  3. warning:This application is modifying the autolayout engine from a background thread

    警告提示:This application is modifying the autolayout engine from a background thread, which can lead to ...

  4. CentOS 6.7编译安装PHP 5.6

    1.准备编译环境 yum install gcc gcc-c++ pcre* openssl* gd-devel* libxml2-devel bzip2-devel libcurl-devel 2. ...

  5. ASP.NET html转图片

    using System.IO; using System.Drawing; using System.Threading; using System.Windows.Forms; public cl ...

  6. div宽度设置无效问题解决

    问题描述: 要设置两个div在同一行显示,都加入了display:inline样式,但是其中一个div的宽度设置无效,在浏览器显示它的宽度始终是1003px. 解决办法: 方法1/给div加入样式:f ...

  7. EntityFramework在root目录web.config中的配置设置

    未找到具有固定名称“System.Data.SqlClient”的 ADO.NET 提供程序的实体框架提供程序.请确保在应用程序配置文件的“entityFramework”节中注册了该提供程序.有关详 ...

  8. session marked for kill处理oracle中杀不掉的锁

    ora-00031:session marked for kill处理oracle中杀不掉的锁   一些ORACLE中的进程被杀掉后,状态被置为"killed",但是锁定的资源很长 ...

  9. Axis,axis2,Xfire以及cxf对比 (转)

    Axis,axis2,Xfire以及cxf对比   http://ws.apache.org/axis/ http://axis.apache.org/axis2/java/core/ http:// ...

  10. IOS-UI-UILable

    //用于文本展示 UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(10, 30, 200, 300)]; //使用测色器自选颜色 ...