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