一、环境说明
操作系统版本: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. Android(java)学习笔记213:开源框架post和get方式提交数据(qq登录案例)

    1.前面提到Http的get/post方式  . HttpClient方式,实际工作的时候不常用到,因为这些方式编写代码是很麻烦的 2.Android应用会经常使用http协议进行传输,网上会有很完善 ...

  2. sizeToFit的用法和用途

    最近有遇到过sizeToFit的方法,比较好奇,所以查了点资料 在官方文档中 - (void)sizeToFit; // calls sizeThatFits: with current view b ...

  3. 11.1 morning

    完美的序列(sequence)Time Limit:1000ms Memory Limit:64MB题目描述LYK 认为一个完美的序列要满足这样的条件:对于任意两个位置上的数都不相同.然而并不是所有的 ...

  4. 自定义HtmlHelper方法

    原文:http://www.cnblogs.com/wenjiang/archive/2013/03/30/2990854.html HtmlHelper方法是ASP.NET MVC中非常强大的特性, ...

  5. Java-Android 之短信发送

    file:///F:/workspace3/Android_ver2.5/src/cn/szy/com/MainActivity.java package cn.szy.com; import jav ...

  6. HTML5 Canvas实现刮刮卡效果实例

    HTML: <style> #canvas { border: 1px solid blue; position: absolute; left: 10px; top: 10px; bac ...

  7. 利用反射把数据集合转换成List

    ---ResultSet数据集 public static List toList(ResultSet rs, Class cls) { List list = new ArrayList(); tr ...

  8. Kettle 实现mysql数据库不同表之间数据同步——实验过程

    下面是试验的主要步骤: 在上一篇文章中LZ已经介绍了,实验的环境和实验目的. 在本篇文章中主要介绍侧重于对Kettle ETL的相应使用方法, 在这里LZ需要说明一下,LZ成为了避免涉及索引和表连接等 ...

  9. Linux下安装Nginx1.9.3-0303(本人亲手实践)

    Linux下安装Nginx1.9.3 Linux操作系统 Oel 5.8 64bit 最新版Nginx: 1.9.3 最近同事让我帮忙搞 ngix,两天时间 安装.配置搞定了.继续 Nginx 1.9 ...

  10. hdoj 1251 字典树

    代码: #include <stdio.h>#define  MAX    26 typedef struct TrieNode{     int nCount;      struct ...