当前位置:首页 > 开发 > 互联网 > 正文

keepalived实现redis主从高可用

发表于: 2015-01-12   作者:bylijinnan   来源:转载   浏览:
摘要: 方案说明 两台机器(称为A和B),以统一的VIP对外提供服务 1.正常情况下,A和B都启动,B会把A的数据同步过来(B is slave of A) 2.当A挂了后,VIP漂移到B;B的keepalived 通知redis 执行:slaveof no one,由B提供服务 3.当A起来后,VIP不切换,仍在B上面;而A的keepalived 通知redis 执行slaveof B,开始
方案说明

两台机器(称为A和B),以统一的VIP对外提供服务
1.正常情况下,A和B都启动,B会把A的数据同步过来(B is slave of A)
2.当A挂了后,VIP漂移到B;B的keepalived 通知redis 执行:slaveof no one,由B提供服务
3.当A起来后,VIP不切换,仍在B上面;而A的keepalived 通知redis 执行slaveof B,开始把B的数据同步过来(A is slave of B)
4.依此循环

也就是说:
当两台机器都正常时,一个为master,一个为slave;
当master挂掉时,slave升级为master;
当原master再次起来时,它不会抢占,而是作为slave;依此循环


机器

10.75.201.67:/home/redis
10.75.201.66: /home/redis
初次启动时,10.75.201.67为master,10.75.201.66为slave

配置
//A机器
vrrp_script chk_nutcraker {
                script "/home/redis/redis/bin/redis_check.sh"
                interval 2
}
vrrp_instance VI_2 {
        state BACKUP        #both BACKUP
        interface eth1
        virtual_router_id 12
        priority 101    #101 on master, 100 on backup
        nopreempt       #both nopreempt
        track_script {
                chk_nutcraker
        }
        virtual_ipaddress {
             10.75.201.3
        }
        notify /home/redis/redis/bin/redis_notify.sh
}

//B机器
vrrp_script chk_nutcraker {
                script "/home/redis/redis/bin/redis_check.sh"
                interval 2
}
vrrp_instance VI_2 {
        state BACKUP        #both BACKUP
        interface eth1
        virtual_router_id 12
        priority 100    #101 on master, 100 on backup
        nopreempt       #both nopreempt
        track_script {
                chk_nutcraker
        }
        virtual_ipaddress {
             10.75.201.3
        }
        notify /home/redis/redis/bin/redis_notify.sh
}


检测redis是否正常的脚本:
[root@redhat1 ~]# cat /home/redis/redis/bin/redis_check.sh
#!/bin/bash

ALIVE=`/home/redis/redis/bin/redis-cli PING`
if [ "$ALIVE" == "PONG" ]; then
  echo $ALIVE
  exit 0
else
  echo $ALIVE
  exit 1
fi


redis根据keepalived状态转移而更新自己的状态:进入master时slaveof no one,进入backup时,slaveof另一台机器:
[root@redhat1 ~]# cat /home/redis/redis/bin/redis_notify.sh
#!/bin/bash

REMOTE=10.75.201.67  #在10.75.201.67机器上则为10.75.201.66
PORT=6379

REDIS_HOME=/home/redis/redis
REDISCLI="$REDIS_HOME/bin/redis-cli"
LOGFILE="$REDIS_HOME/logs/redis_notify.log"

DATE_TIME=`date "+%Y-%m-%d %H:%M:%S"`

TYPE=$1
NAME=$2
STATE=$3

case $STATE in
        "MASTER") 
                  echo "$DATE_TIME:became master:" >> $LOGFILE
                  $REDISCLI SLAVEOF NO ONE >> $LOGFILE 2>&1 
                  exit 0
                  ;;
        "BACKUP")  
                  echo "$DATE_TIME:became slave:" >> $LOGFILE
                  $REDISCLI SLAVEOF $REMOTE $PORT >> $LOGFILE  2>&1
                  exit 0
                  ;;
        "FAULT")  
                  echo "$DATE_TIME:[fault]" >> $LOGFILE
                  exit 0
                  ;;
        *)        echo "unknown state"
                  exit 1
                  ;;
Esac


keepalived实现redis主从高可用

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号