Keepalived实现Redis Failover

一、环境说明

操作系统版本: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-serve ../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 时间也需要作相应调整。

时间: 2024-11-05 02:28:33

Keepalived实现Redis Failover的相关文章

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

通过Keepalived实现Redis Failover自动故障切换功能[实践分享] 参考资料: http://patrick-tang.blogspot.com/2012/06/redis-keepalived-failover-system.html http://deidara.blog.51cto.com/400447/302402 目前,Redis还没有一个类似于MySQL Proxy或Oracle RAC的官方HA方案.Redis作者有一个名为Redis Sentinel的计划(ht

利用redis-sentinel+keepalived实现redis高可用

目标.需求: 为上层应用提供高可靠.低延迟.低(无限接近0)数据损失的Redis缓存服务 方案概述: 采用同一网络内的三台主机(可以是物理主机.虚拟机或docker容器),要求三台主机之间都能相互访问,每一台主机上都安装redis-server.redis-sentinel和keepalived. redis-server负责提供Redis缓存服务,三台主机间的关系是master-slave-slave redis-sentinel负责提供Redis高可用,三台主机间的关系与redis-serv

使用keepalived打造redis双机热备

redis是非常实用的优秀nosql开源产品,在高并发的服务器环境中,可以作为缓存和队列使用.美中不足的是,到现在为止,redis并没有推出自己的集群方案,对一个完善的生产环境,负载均衡,高可用,主从备份都是必不可少的选项,redis提供了稳定·的主从备份功能,提供了数种持久化策略,在主从备份上也提供了增量备份的功能,现在通过sentinel提供主从热备的功能,可惜功能上并不完善,达不到生产环境的要求. 现在我们选定keepalived作为redis的高可用支持,keepalived不仅可以比较

keepalived实现redis服务器虚拟ip漂移

两个都设置成 BACKUP  并且指定 nopreempt 这样 后启动的 keepalived就不会抢占虚拟ip 所以这样存在一个缺点: 如果两个redis服务器都正常,那么必须先启动 主redis 的 keepalived 服务 redis服务要在keepalived服务之前启动 主redis配置: 从redis配置: 监控脚本:

redis主从配置及通过keepalived实现redis自动切换高可用

一:环境介绍: Master: 192.168.1.4 Slave: 192.168.1.5 Virtural IP Address (VIP): 192.168.1.253 二:设计思路: 当 Master 与 Slave 均运作正常时, Master负责服务,Slave负责Standby: 当 Master 挂掉,Slave 正时, Slave接管服务,同时关闭主从复制功能: 当 Master 恢复正常,则从Slave同步数据,同步数据之后关闭主从复制功能,恢复Master身份,于此同时Sl

redis+Keepalived实现Redis主从复制

redis+Keepalived实现Redis主从复制: 环境:CentOs6.5Master: 10.10.10.203Slave:   10.10.10.204Virtural IP Address (VIP): 10.10.10.205 所需软件:keepalived-1.2.12.tar.gzredis-2.8.7.tar.gz 实现功能:当 Master 与 Slave 均运作正常时, Master负责服务,Slave负责Standby:当 Master 挂掉,Slave 正常时, S

keepalived实现redis主备切换

试验目标: 1:主redis发生故障时,从redis自动切换为主redis. 2:主redis恢复正常后,自动降级为从redis. 试验环境: 主redis:192.168.189.138 从redis:192.168.189.131 VIP:192.168.189.123 主从redis均安装了keepalived与redis,redis已配置好主从.Redis端口44444. 1:制作redis监控与主备切换脚本: Redis监控脚本:(监控redis是否正常) # cat /usr/loc

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

Keepalived+nginx+redis主从+tomcat一机多实例实现会话共享 2014-09-09 14:14:25 标签:会话共享 主从 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://lovelace.blog.51cto.com/1028430/1550198 ### keepalived配置 ### nginx安装培训 - 安装nginx 1 2 3 ``` cpp yum install nginx -y `

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

关于keepalived的详细介绍,请移步本人相关博客:http://wangfeng7399.blog.51cto.com/3518031/1405785 功能 ip地址 安装软件 主redis 10.13.6.13 redis,keepalived 从redis 10.13.6.16 redis,keepalived VIP 10.13.6.17   一.redis主从搭建 1.redis安装 本文通过yum源的安装方式来安装(需要配置epel源),也可以通过源码编译的方式来安装 1 2 [