Redis主从自动failover

Redis主从架构持久化存在一个问题,即前次测试的结论,持久化需要配置在主实例上才能跨越实例保证数据不丢失,这样以来主实例在持久化数据到硬盘的过程中,势必会造成磁盘的I/O等待,经过实际测试,这个持久化写硬盘的过程给应用程序带来的影响无法忍受;因而在大多数场景下,会考虑把持久化配置在从实例上,当主实例宕机后,通过手动或者自动的方式将从实例提升为主实例,继续提供服务!当主实例恢复后,先从原从实例上同步数据,同步完成后再恢复到原始的主从状态!要实现这种的要求,需要有keepalive的配合,一方面keepalive提供了VIP,可以避免修改应用程序连接,同时redis实例的配置文件监听部分也需要修改为全网监听;另一方面keepalive定时调度脚本来监控主从实例的状态,根据具体情况进行切换!本文将重点介绍下使用keepalive实现redis主从自动failover!

环境介绍
操作系统版本均为:rhel5.4 64bit
redis版本:2.6.4
redis实例端口均为:6379
redis实例密码均为:123
VIP:192.168.1.120
主实例为server11(192.168.1.112)
从实例为server12(192.168.1.113,开启快照持久化)

一:安装keepalive软件,server11安装完成后直接scp至server12上即可
01.[root@server11 ~]# wget http://keepalived.org/software/keepalived-1.1.19.tar.gz  02.[root@server11 ~]# tar -zxvf ../tarbag/keepalived-1.1.19.tar.gz  03.[root@server11 ~]# cd keepalived-1.1.19/  04.[root@server11 ~]# ./configure --prefix=/usr/local/keepalived && make && make install
二:配置主节点server11配置文件
01.[root@server11 ~]# cat /usr/local/keepalived/etc/keepalived/keepalived.conf  02.! Configuration File for keepalived  03. 04.global_defs {  05. router_id LVS_DEVEL  06.}  07. 08.vrrp_script Monitor_redis {  09. script "/usr/local/scripts/redis_monitor.sh"  10. interval 2   11. weight 2    12.}  13. 14.vrrp_instance VI_1{   15. state MASTER  16. interface eth0  17. virtual_router_id 51  18. mcast_src_ip 192.168.1.112  19. priority  100  20. advert_int 1  21. authentication {  22. auth_type PASS  23. auth_pass password_123  24.}  25. track_script {  26. Monitor_redis  27.}  28. virtual_ipaddress {  29. 192.168.1.120  30. }  31. notify_fault  /usr/local/scripts/redis_fault.sh    32. notify_stop   /usr/local/scripts/redis_stop.sh    33. 34.} 
三:配置从节点server12配置文件
01.[root@server12 ~]# cat /usr/local/keepalived/etc/keepalived/keepalived.conf  02.! Configuration File for keepalived  03. 04.global_defs {  05. router_id LVS_DEVEL  06.}  07. 08.vrrp_script Monitor_redis {  09. script "/usr/local/scripts/redis_monitor.sh"  10. interval 2   11. weight 2    12.}  13. 14.vrrp_instance VI_1{   15. state BACKUP   16. interface eth0  17. virtual_router_id 51  18. mcast_src_ip 192.168.1.113  19. priority  99  20. advert_int 1  21. authentication {  22. auth_type PASS  23. auth_pass password_123  24.}  25. track_script {  26. Monitor_redis  27.}  28. virtual_ipaddress {  29. 192.168.1.120  30. }  31. notify_master /usr/local/scripts/redis_master.sh    32. notify_backup /usr/local/scripts/redis_backup.sh    33. notify_fault  /usr/local/scripts/redis_fault.sh    34. notify_stop   /usr/local/scripts/redis_stop.sh    35. 36.} 
四:准备相关的脚本,主从实例上都需要存在这些脚本,同时注意脚本需要由可执行权限
01.[root@server11 ~]# cat /usr/local/scripts/redis_monitor.sh   02.#!/bin/bash    03.ALIVE=$(/usr/local/redis2/bin/redis-cli -h 192.168.1.112 -p 6379 -a 123 PING)  04. 05.if [ "$ALIVE" == "PONG" ]; then   06.    echo $ALIVE    07.    exit 0    08.    else   09.    echo $ALIVE    10.    killall -9 keepalived  11.    service network restart  12.    exit 1    13.fi   14. 15.[root@server11 ~]# sh /usr/local/scripts/redis_monitor.sh   16.PONG  17. 18.[root@server11 ~]# cat /usr/local/scripts/redis_master.sh    19.#!/bin/bash    20.REDISCLI="/usr/local/redis2/bin/redis-cli -h 192.168.1.112 -p 6379 -a 123"   21.LOGFILE="/usr/local/redis2/var/keepalived-redis-state.log"   22. 23.echo "[master]" >> $LOGFILE    24.date >> $LOGFILE    25.echo "Being master...." >> $LOGFILE 2>&1    26.echo "Run SLAVEOF cmd ..." >> $LOGFILE    27.$REDISCLI SLAVEOF 192.168.1.113 6379 >> $LOGFILE  2>&1    28.sleep 10   29.echo "Run SLAVEOF NO ONE cmd ..." >> $LOGFILE    30.$REDISCLI SLAVEOF NO ONE >> $LOGFILE 2>&1   31. 32. 33.[root@server11 ~]# cat /usr/local/scripts/redis_backup.sh   34.#!/bin/bash    35.REDISCLI="/usr/local/redis2/bin/redis-cli -h 192.168.1.112 -p 6379 -a 123"   36.LOGFILE="/usr/local/redis2/var/keepalived-redis-state.log"   37. 38.echo "[backup]" >> $LOGFILE    39.date >> $LOGFILE    40.echo "Being slave...." >> $LOGFILE 2>&1    41.sleep 15    42.echo "Run SLAVEOF cmd ..." >> $LOGFILE    43.$REDISCLI SLAVEOF 192.168.1.113 6379 >> $LOGFILE  2>&1   44. 45.[root@server11 ~]# cat /usr/local/scripts/redis_stop.sh   46.#!/bin/bash  47.LOGFILE="/usr/local/redis2/var/keepalived-redis-state.log"   48.echo "[stop]" >> $LOGFILE  49.date >> $LOGFILE   50.[root@server11 ~]# cat /usr/local/scripts/redis_fault.sh #!/bin/bash  LOGFILE="/usr/local/redis2/var/keepalived-redis-state.log" echo "[fault]" >> $LOGFILE  date >> $LOGFILE 
五:主从实例分别启动keepalive进程,测试VIP是否正常


java版本测试代码

package com.yanek.redis;
import redis.clients.jedis.Jedis;
public class Test {
/**
   * @param args
   */
public static void main(String[] args) {
  
     Jedis jedis = new Jedis("192.168.132.100");
     jedis.set("id", "10000");
     String id = jedis.get("id");
     jedis.set("name", "javaboy2012");
     String name = jedis.get("name");
     System.out.println("id="+id);
     System.out.println("name="+name);
     System.out.println("name="+jedis.get("ray"));
}
}

你可能感兴趣的:(redis)