redis+Keepalived实现Redis主从复制

时间:2023-03-08 16:00:40

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 的启动脚本:

 #!/bin/bash
#
# redis - this script starts and stops the redis-server daemon
#
# chkconfig: -
# description: Redis is a persistent key-value database
# processname: redis-server
# config: /usr/local/redis/etc/redis.conf
# pidfile: /usr/local/redis/var/redis.pid source /etc/init.d/functions BIN="/usr/local/redis/bin"
CONFIG="/usr/local/redis/etc/redis.conf"
PIDFILE="/usr/local/redis/var/redis.pid" ### Read configuration
[ -r "$SYSCONFIG" ] && source "$SYSCONFIG" RETVAL=
prog="redis-server"
desc="Redis Server" start() { if [ -e $PIDFILE ];then
echo "$desc already running...."
exit
fi echo -n $"Starting $desc: "
daemon $BIN/$prog $CONFIG RETVAL=$?
echo
[ $RETVAL -eq ] && touch /var/lock/subsys/$prog
return $RETVAL
} stop() {
echo -n $"Stop $desc: "
killproc $prog
RETVAL=$?
echo
[ $RETVAL -eq ] && rm -f /var/lock/subsys/$prog $PIDFILE
return $RETVAL
} restart() {
stop
start
} case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
condrestart)
[ -e /var/lock/subsys/$prog ] && restart
RETVAL=$?
;;
status)
status $prog
RETVAL=$?
;;
*)
echo $"Usage: $0 {start|stop|restart|condrestart|status}"
RETVAL=
esac 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

 vrrp_script chk_redis {
script "/etc/keepalived/scripts/redis_check.sh"
interval
} vrrp_instance mes_Redis {
state MASTER
interface eth0
garp_master_delay
smtp_alert
virtual_router_id
priority
nopreempt
advert_int
authentication {
auth_type PASS
auth_pass
}
virtual_ipaddress {
192.168.1.205
}
track_script {
chk_redis
}
notify_master /etc/keepalived/scripts/redis_master.sh
notify_backup /etc/keepalived/scripts/redis_slave.sh
notify_fault /etc/keepalived/scripts/redis_fault.sh
notify_stop /etc/keepalived/scripts/redis_stop.sh
}

在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 >&
echo "Run SLAVEOF cmd ..." >> $LOGFILE
$REDISCLI SLAVEOF 192.168.1.204 >> $LOGFILE >&
sleep #延迟10秒以后待数据同步完成后再取消同步状态
echo "Run SLAVEOF NO ONE cmd ..." >> $LOGFILE
$REDISCLI SLAVEOF NO ONE >> $LOGFILE >& 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 >& sleep #延迟15秒待数据被对方同步完成之后再切换主从角色
echo "Run SLAVEOF cmd ..." >> $LOGFILE
$REDISCLI SLAVEOF 192.168.1.204 >> $LOGFILE >&

在slave上创建配置:

keepalived.conf 如下:

 vim /etc/keepalived/keepalived.conf
1 vrrp_script chk_redis {
script "/etc/keepalived/scripts/redis_check.sh" ###监控脚本
interval ###监控时间
} vrrp_instance mes_Redis {
state BACKUP ###设置为BACKUP
interface eth0 ###监控网卡
garp_master_delay
smtp_alert
virtual_router_id
priority ###比MASTRE权重值低
nopreempt
advert_int
authentication {
auth_type PASS
auth_pass ###密码与MASTRE相同
}
virtual_ipaddress {
192.168.1.205 ###VIP地址
}
track_script {
chk_redis ###执行上面定义的chk_redis
}
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
}

在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 >&
echo "Run SLAVEOF cmd ..." >> $LOGFILE
$REDISCLI SLAVEOF 192.168.1.203 >> $LOGFILE >&
sleep #延迟10秒以后待数据同步完成后再取消同步状态
echo "Run SLAVEOF NO ONE cmd ..." >> $LOGFILE
$REDISCLI SLAVEOF NO ONE >> $LOGFILE >&

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 >&
sleep #延迟15秒待数据被对方同步完成之后再切换主从角色
echo "Run SLAVEOF cmd ..." >> $LOGFILE
$REDISCLI SLAVEOF 192.168.1.203 >> $LOGFILE >&

在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
else
echo $ALIVE
exit
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 #监听的端口号
tcp-backlog
timeout #当客户端闲置多长时间后关闭连接,如果指定为0,表示关闭该功能
tcp-keepalive #表示将周期性使用SO_KEEPALIVE检测客户端是否还处于健康状态,单位秒
loglevel notice #指定日志记录级别,Redis总共支持四个级别:debug、verbose、notice、warning
logfile "/var/log/redis.log" #指定日志路径
syslog-enabled no
syslog-ident redis
syslog-facility local0
databases #设置数据库的数量
save #表示900秒(15分钟)内有1个更改,保存数据到磁盘
save #300秒(5分钟)内有10个更改
save #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 #Master的ip地址,这个是master和slave配置文件不一样的地方
slave-serve-stale-data yes
slave-read-only yes
repl-timeout
repl-disable-tcp-nodelay no
slave-priority
maxmemory 4g
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage
auto-aof-rewrite-min-size 64mb
lua-time-limit
slowlog-log-slower-than
slowlog-max-len
notify-keyspace-events ""
hash-max-ziplist-entries
hash-max-ziplist-value
list-max-ziplist-entries
list-max-ziplist-value
set-max-intset-entries
zset-max-ziplist-entries
zset-max-ziplist-value
activerehashing yes
client-output-buffer-limit normal
client-output-buffer-limit slave 256mb 64mb
client-output-buffer-limit pubsub 32mb 8mb
hz

redis Slave配置文件如下:

 daemonize yes
pidfile /var/run/redis.pid
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 0
loglevel notice
logfile "/var/log/redis.log"
syslog-enabled no
syslog-ident redis
syslog-facility local0
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /usr/local/redis/var/
slaveof 10.10.10.203 6379
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
aof-rewrite-incremental-fsync yes

redis_slave.conf

只比redis_slave.con 的配置多slaveof 192.168.1.203 6379,别的都一样.