redis+Keepalived实现Redis主从复制
redis+Keepalived实现Redis主从复制:
环境:CentOs6.5
Master: 192.168.1.203
Slave: 192.168.1.204
Virtural IP Address (VIP): 192.168.1.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.在主服务器192.168.1.203上面做下面操作
echo " 192.168.1.203 gc-xxooredis1" >> /etc/hosts
echo " 192.168.1.204 gc-xxooredis2" >> /etc/hosts
2.在从服务器192.168.1.204上面做下面操作
echo " 192.168.1.203 gc-xxooredis1" >> /etc/hosts
echo " 192.168.1.204 gc-xxooredis2" >> /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
制作一个redis init.d 的启动脚本:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #!/bin/bash 2 # 3 # redis - this script starts and stops the redis-server daemon 4 # 5 # chkconfig: - 80 12 6 # description: Redis is a persistent key-value database 7 # processname: redis-server 8 # config: /usr/local/redis/etc/redis.conf 9 # pidfile: /usr/local/redis/var/redis.pid 10 11 source /etc/init.d/functions 12 13 BIN="/usr/local/redis/bin" 14 CONFIG="/usr/local/redis/etc/redis.conf" 15 PIDFILE="/usr/local/redis/var/redis.pid" 16 17 18 ### Read configuration 19 [ -r "$SYSCONFIG" ] && source "$SYSCONFIG" 20 21 RETVAL=0 22 prog="redis-server" 23 desc="Redis Server" 24 25 start() { 26 27 if [ -e $PIDFILE ];then 28 echo "$desc already running...." 29 exit 1 30 fi 31 32 echo -n $"Starting $desc: " 33 daemon $BIN/$prog $CONFIG 34 35 RETVAL=$? 36 echo 37 [ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog 38 return $RETVAL 39 } 40 41 stop() { 42 echo -n $"Stop $desc: " 43 killproc $prog 44 RETVAL=$? 45 echo 46 [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog $PIDFILE 47 return $RETVAL 48 } 49 50 restart() { 51 stop 52 start 53 } 54 55 case "$1" in 56 start) 57 start 58 ;; 59 stop) 60 stop 61 ;; 62 restart) 63 restart 64 ;; 65 condrestart) 66 [ -e /var/lock/subsys/$prog ] && restart 67 RETVAL=$? 68 ;; 69 status) 70 status $prog 71 RETVAL=$? 72 ;; 73 *) 74 echo $"Usage: $0 {start|stop|restart|condrestart|status}" 75 RETVAL=1 76 esac 77 78 exit $RETVAL
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 192.168.1.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 192.168.1.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 192.168.1.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 192.168.1.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 192.168.1.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 192.168.1.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
脚本创建完成以后,我们开始按照如下流程进行测试:
启动Master上的Redis
/etc/init.d/redis start
启动Slave上的Redis
/etc/init.d/redis start
启动Master上的Keepalived
/etc/init.d/keepalived start
启动Slave上的Keepalived
/etc/init.d/keepalived start
尝试通过VIP 192.168.1.205 连接Redis:
redis-cli -h 192.168.1.205 INFO
尝试插入一些数据:
redis-cli -h 192.168.1.205 SET Hello Redis
OK
从VIP读取数据
redis-cli -h 192.168.1.205 GET Hello
"Redis"
从Master读取数据
redis-cli -h 192.168.1.205 GET Hello
"Redis"
从Slave读取数据
redis-cli -h 192.168.1.205 GET Hello
"Redis"
下面,模拟故障产生:
将Master上的Redis进程杀死:
killall -9 redis-server
查看Master上的Keepalived日志
tailf /var/log/keepalived-redis-state.log
[fault]
2015年 11月 09日 星期一 20:49:17 CST
同时Slave上的日志显示:
$ tailf /var/log/keepalived-redis-state.log
[master]
2015年 11月 09日 星期一 20:49:19 CST
Being master....
Run SLAVEOF cmd ...
OK Already connected to specified master
Run SLAVEOF NO ONE cmd ...
OK
然后我们可以发现,Slave已经接管服务,并且担任Master的角色了。
daemonize yes #启用守护进程 pidfile /var/run/redis.pid port 6379 #监听的端口号 tcp-backlog 511 timeout 0 #当客户端闲置多长时间后关闭连接,如果指定为0,表示关闭该功能 tcp-keepalive 0 #表示将周期性使用SO_KEEPALIVE检测客户端是否还处于健康状态,单位秒 loglevel notice #指定日志记录级别,Redis总共支持四个级别:debug、verbose、notice、warning logfile "/var/log/redis.log" #指定日志路径 syslog-enabled no syslog-ident redis syslog-facility local0 databases 16 #设置数据库的数量 save 900 1 #表示900秒(15分钟)内有1个更改,保存数据到磁盘 save 300 10 #300秒(5分钟)内有10个更改 save 60 10000 #60秒内有10000个更改 stop-writes-on-bgsave-error yes #当持久化出现错误之后,是否继续提供写服务 rdbcompression yes rdbchecksum yes dbfilename dump.rdb dir /usr/local/redis/var/ slaveof 192.168.1.203 6379 #Master的ip地址,这个是master和slave配置文件不一样的地方 slave-serve-stale-data yes slave-read-only yes repl-timeout 60 repl-disable-tcp-nodelay no slave-priority 100 maxmemory 4g appendonly no appendfilename "appendonly.aof" appendfsync everysec no-appendfsync-on-rewrite no auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb lua-time-limit 5000 slowlog-log-slower-than 10000 slowlog-max-len 128 notify-keyspace-events "" hash-max-ziplist-entries 512 hash-max-ziplist-value 64 list-max-ziplist-entries 512 list-max-ziplist-value 64 set-max-intset-entries 512 zset-max-ziplist-entries 128 zset-max-ziplist-value 64 activerehashing yes client-output-buffer-limit normal 0 0 0 client-output-buffer-limit slave 256mb 64mb 60 client-output-buffer-limit pubsub 32mb 8mb 60 hz 10
redis Slave配置文件如下:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 daemonize yes 2 pidfile /var/run/redis.pid 3 port 6379 4 tcp-backlog 511 5 timeout 0 6 tcp-keepalive 0 7 loglevel notice 8 logfile "/var/log/redis.log" 9 syslog-enabled no 10 syslog-ident redis 11 syslog-facility local0 12 databases 16 13 save 900 1 14 save 300 10 15 save 60 10000 16 stop-writes-on-bgsave-error yes 17 rdbcompression yes 18 rdbchecksum yes 19 dbfilename dump.rdb 20 dir /usr/local/redis/var/ 21 slaveof 10.10.10.203 6379 22 slave-serve-stale-data yes 23 slave-read-only yes 24 repl-timeout 60 25 repl-disable-tcp-nodelay no 26 slave-priority 100 27 maxmemory 4g 28 appendonly no 29 appendfilename "appendonly.aof" 30 appendfsync everysec 31 no-appendfsync-on-rewrite no 32 auto-aof-rewrite-percentage 100 33 auto-aof-rewrite-min-size 64mb 34 lua-time-limit 5000 35 slowlog-log-slower-than 10000 36 slowlog-max-len 128 37 notify-keyspace-events "" 38 hash-max-ziplist-entries 512 39 hash-max-ziplist-value 64 40 list-max-ziplist-entries 512 41 list-max-ziplist-value 64 42 set-max-intset-entries 512 43 zset-max-ziplist-entries 128 44 zset-max-ziplist-value 64 45 activerehashing yes 46 client-output-buffer-limit normal 0 0 0 47 client-output-buffer-limit slave 256mb 64mb 60 48 client-output-buffer-limit pubsub 32mb 8mb 60 49 hz 10 50 aof-rewrite-incremental-fsync yes
只比redis_slave.con 的配置多slaveof 192.168.1.203 6379,别的都一样.