redis+Keepalived实现Redis主从复制

redis+Keepalived实现Redis主从复制:

环境:CentOs6.5
Master: 10.10.10.203
Slave:   10.10.10.204
Virtural IP Address (VIP): 10.10.10.205

所需软件:
keepalived-1.2.12.tar.gz
redis-2.8.7.tar.gz

实现功能:
当 Master 与 Slave 均运作正常时, Master负责服务,Slave负责Standby;
当 Master 挂掉,Slave 正常时, Slave接管服务,同时关闭主从复制功能;

一、安装配置前准备工作:

yum -y install gcc gcc+ gcc-c++ openssl openssl-devel pcre pcre-devel

Redis属于内存数据库,所以调优都集中到了内存上,需要将 vm.overcommit 设置为1
echo "vm.overcommit_memory=1" >> /etc/sysctl
sysctl -p

1.在主服务器10.10.10.203上面做下面操作
echo " 10.10.10.203 gc-dabairedis1" >> /etc/hosts
echo " 10.10.10.204 gc-dabairedis2" >> /etc/hosts
2.在从服务器10.10.10.204上面做下面操作
echo " 10.10.10.203 gc-dabairedis1" >> /etc/hosts
echo " 10.10.10.204 gc-dabairedis2" >> /etc/hosts

二、在Master和Slave上安装redis:

tar -zxvf redis-2.8.7.tar.gz
cd redis-2.8.7
make && make install
cd src
mkdir -p /usr/local/redis/bin
cp redis-server redis-cli redis-benchmark redis-check-aof redis-check-dump /usr/local/redis/bin/

mkdir -p /usr/local/redis/etc

将源码中的 redis.conf 复制到 /usr/local/redis/etc/
redis-2.8.7]# cp redis.conf /usr/local/redis/etc/

修改 /usr/local/redis/etc/redis.conf里面可以把 daemonize no 修改为daemonize yes

制作一个 init.d 的启动脚本:

vim /etc/init.d/redis-server
chmod o+x /etc/init.d/redis-server
chkconfig --add redis-server
/etc/init.d/redis-server start

###########################################################################################
三、在Master和Slave上安装keepalived:

tar -zxvf keepalived-1.2.12.tar.gz
cd keepalived-1.2.12

./configure
make && make install

cp /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/
cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
chmod +x /etc/init.d/keepalived
chkconfig --add keepalived

mkdir /etc/keepalived
ln -s /usr/local/sbin/keepalived /usr/sbin

Keepalived在转换状态时会依照状态来呼叫:
当进入Master状态时会呼叫notify_master
当进入Backup状态时会呼叫notify_backup
当发现异常情况时进入Fault状态呼叫notify_fault
当Keepalived程序终止时则呼叫notify_stop

 Master上创建配置:

在Master上创建配置文件 keepalived.conf 如下
vim /etc/keepalived/keepalived.conf

 1 vrrp_script chk_redis {
 2 script "/etc/keepalived/scripts/redis_check.sh"
 3 interval 1
 4 }
 5
 6 vrrp_instance mes_Redis {
 7 state MASTER
 8 interface eth0
 9 garp_master_delay 10
10 smtp_alert
11 virtual_router_id 3
12 priority 101
13 nopreempt
14 advert_int 1
15 authentication {
16 auth_type PASS
17 auth_pass 1111
18 }
19 virtual_ipaddress {
20 10.10.10.205
21 }
22 track_script {
23 chk_redis
24 }
25 notify_master /etc/keepalived/scripts/redis_master.sh
26 notify_backup /etc/keepalived/scripts/redis_slave.sh
27 notify_fault /etc/keepalived/scripts/redis_fault.sh
28 notify_stop /etc/keepalived/scripts/redis_stop.sh
29 }

在Redis Master上创建notity_master与notify_backup脚本:

vim /etc/keepalived/scripts/redis_master.sh

#!/bin/bash
REDISCLI="/usr/local/redis/bin/redis-cli"
LOGFILE="/var/log/keepalived-redis-state.log"
echo "[master]" >> $LOGFILE
date >> $LOGFILE
echo "Being master...." >> $LOGFILE 2>&1
echo "Run SLAVEOF cmd ..." >> $LOGFILE
$REDISCLI SLAVEOF 10.10.10.204 6379 >> $LOGFILE  2>&1
sleep 5 #延迟10秒以后待数据同步完成后再取消同步状态
echo "Run SLAVEOF NO ONE cmd ..." >> $LOGFILE
$REDISCLI SLAVEOF NO ONE >> $LOGFILE 2>&1

vim /etc/keepalived/scripts/redis_backup.sh 

#!/bin/bash
REDISCLI="/usr/local/redis/bin/redis-cli"
LOGFILE="/var/log/keepalived-redis-state.log"

echo "[backup]" >> $LOGFILE
date >> $LOGFILE
echo "Being slave...." >> $LOGFILE 2>&1

sleep 15 #延迟15秒待数据被对方同步完成之后再切换主从角色
echo "Run SLAVEOF cmd ..." >> $LOGFILE
$REDISCLI SLAVEOF 10.10.10.204 6379 >> $LOGFILE  2>&1

在slave上创建配置:

keepalived.conf 如下:

 vim /etc/keepalived/keepalived.conf 1 vrrp_script chk_redis {
 2 script "/etc/keepalived/scripts/redis_check.sh"   ###监控脚本
 3 interval 1                                        ###监控时间
 4 }
 5
 6 vrrp_instance mes_Redis {
 7 state BACKUP                                      ###设置为BACKUP
 8 interface eth0                                    ###监控网卡
 9 garp_master_delay 10
10 smtp_alert
11 virtual_router_id 3
12 priority 100                                      ###比MASTRE权重值低
13 nopreempt
14 advert_int 1
15 authentication {
16 auth_type PASS
17 auth_pass 1111                                       ###密码与MASTRE相同
18 }
19 virtual_ipaddress {
20 10.10.10.205                                         ###VIP地址
21 }
22 track_script {
23 chk_redis                                            ###执行上面定义的chk_redis
24 }
25 notify_master /etc/keepalived/scripts/redis_master.sh
26 notify_backup /etc/keepalived/scripts/redis_backup.sh
27 notify_fault /etc/keepalived/scripts/redis_fault.sh
28 notify_stop /etc/keepalived/scripts/redis_stop.sh
29 }

在Redis Slave上创建notity_master与notify_backup脚本:

vim /etc/keepalived/scripts/redis_master.sh
#!/bin/bash
REDISCLI="/usr/local/redis/bin/redis-cli"
LOGFILE="/var/log/keepalived-redis-state.log"
echo "[master]" >> $LOGFILE
date >> $LOGFILE
echo "Being master...." >> $LOGFILE 2>&1
echo "Run SLAVEOF cmd ..." >> $LOGFILE
$REDISCLI SLAVEOF 10.10.10.203 6379 >> $LOGFILE  2>&1
sleep 5 #延迟10秒以后待数据同步完成后再取消同步状态
echo "Run SLAVEOF NO ONE cmd ..." >> $LOGFILE
$REDISCLI SLAVEOF NO ONE >> $LOGFILE 2>&1

vim /etc/keepalived/scripts/redis_backup.sh
#!/bin/bash
REDISCLI="/usr/local/redis/bin/redis-cli"
LOGFILE="/var/log/keepalived-redis-state.log"
echo "[backup]" >> $LOGFILE
date >> $LOGFILE
echo "Being slave...." >> $LOGFILE 2>&1
sleep 15 #延迟15秒待数据被对方同步完成之后再切换主从角色
echo "Run SLAVEOF cmd ..." >> $LOGFILE
$REDISCLI SLAVEOF 10.10.10.203 6379 >> $LOGFILE  2>&1

在Master和Slave上创建相同监控Redis的脚本:

vim /etc/keepalived/scripts/redis_check.sh
#!/bin/bash
ALIVE=`/opt/redis/bin/redis-cli PING`
if [ "$ALIVE" == "PONG" ]; then
  echo $ALIVE
  exit 0
else
  echo $ALIVE
  exit 1
fi

Master与Slave创建如下相同的脚本:

 vim /etc/keepalived/scripts/redis_fault.sh
#!/bin/bash
LOGFILE=/var/log/keepalived-redis-state.log
echo "[fault]" >> $LOGFILE
date >> $LOGFILE

vim /etc/keepalived/scripts/redis_stop.sh
#!/bin/bash
LOGFILE=/var/log/keepalived-redis-state.log
echo "[stop]" >> $LOGFILE
date >> $LOGFILE

给脚本都加上可执行权限:
chmod +x /etc/keepalived/scripts/*.sh

脚本创建完成以后,我们开始按照如下流程进行测试:

 1 启动Master上的Redis
 2 /etc/init.d/redis start
 3 启动Slave上的Redis
 4 /etc/init.d/redis start
 5 启动Master上的Keepalived
 6 /etc/init.d/keepalived start
 7 启动Slave上的Keepalived
 8 /etc/init.d/keepalived start
时间: 2024-10-28 10:40:39

redis+Keepalived实现Redis主从复制的相关文章

redis+Keepalived实现Redis高可用性

目前,Redis还没有一个类似于MySQL Proxy或Oracle RAC的官方HA方案.Redis作者有一个名为Redis Sentinel的计划(http://redis.io/topics/sentinel),据称将会有监控,报警和自动故障转移三大功能,非常不错.但可惜的是短期内恐怕还不能开发完成.官方的redis集群方案还在开发阶段,3.0.0beta版本已经支持redis cluster功能.因此,如何在出现故障时自动转移是一个需要解决的问题.通过对网上一些资料的搜索,有建议采用HA

linux 双Redis + keepalived 主从复制+宕机自主切换

主要核心思想,如果master 和 salve 全部存活的情况,VIP就漂移到 master.读写都从master操作,如果master宕机,VIP就会漂移到salve,并将之前的salve切换为master,当宕机的master可以继续服务的时候,首先会从salve同步数据,然后VIP漂移到master服务器上面,持续提供服务. 环境准备: master:ip 192.168.28.139:redis 19020:redis 19021:keepalived slave :ip 192.168

redis+keepalived 主从热备 实例

qiehuan 环境: 操作系统:centos 5.6 x64 redis版本:redis-2.8.10.tar.gz keepalived版本:keepalived-1.2.12.tar.gz redis  master : 192.168.101.50 redis     salve : 192.168.101.51 原理:如果返回时间超过keepalived vrrp_scripts的超时时间,keepalived会进入faild 状态,从而触发keepalived集群开始进行vip漂移

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

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

Redis+keepalived实现双机热备

一. 简介 安装使用centos 5.6(64) Master 192.168.2.235 Slave 192.168.2.236 Vip 192.168.2.200 编译环境 yum -y install gcc gcc+ gcc-c++ openssl openssl-devel pcre pcre-devel 当 Master 与 Slave 均运作正常时, Master负责服务,Slave负责Standby: 当 Master 挂掉,Slave 正常时, Slave接管服务,同时关闭主从

redis+keepalived实现高可用

Redis简介: Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API.从2010年3月15日起,Redis的开发工作由VMware主持. redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(sorted set –有序集合)和hash(哈希类型).这些数据类型都支持push/pop.ad

使用keepalived打造redis双机热备

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

redis+Keepalived主从热备秒级切换

一 简介 安装使用centos 5.10 Master 192.168.235.135 Slave 192.168.235.152 Vip 192.168.235.200 编译环境 yum -y install gcc gcc+ gcc-c++ openssl openssl-devel pcre pcre-devel 当 Master 与 Slave 均运作正常时, Master负责服务,Slave负责Standby: 当 Master 挂掉,Slave 正常时, Slave接管服务,同时关闭

通过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