Redis主从配置及使用KeepAlived实现Redis高可用

一:环境介绍
Master: 172.16.206.29
Slave: 172.16.206.28
Virtural IP Address (VIP): 172.16.206.250
二:设计思路:
当 Master 与 Slave 均运作正常时, Master负责服务,Slave负责Standby;
当 Master 挂掉,Slave 正常时, Slave接管服务,有写权限,同时关闭主从复制功能;
当 Master 恢复正常,则从Slave同步数据,同步数据之后关闭主从复制功能,恢复Master身份,同时Slave等待Master同步数据完成之后,恢复Slave身份。
三:安装配置前准备工作
在主服务器 172.16.206.29 上面做下面操作
echo “172.16.206.29 osb29” >> /etc/hosts
echo “172.16.206.28 osb28” >> /etc/hosts
在从服务器 172.16.206.28 上面做下面操作
echo “172.16.206.29 osb29” >> /etc/hosts
echo “172.16.206.28 osb28” >> /etc/hosts
四:主服务器配置redis
1.下载redis 版本2.8.19
2.解压 tar -zxvf redis-2.8.19.tar.gz
3.cd redis-2.8.19
4.make && make install
5.cp redis.conf /etc/redis.conf
cd src/
cp redis-server redis-cli redis-benchmark redis-check-aofredis-check-dump /usr/local/bin
6.修改 /etc/redis.conf里面可以把daemonize no 修改为daemonize yes
目的是可以在后台执行redis-server。
7.init.d 启动脚本
    vi /etc/init.d/redis-server
#!/usr/bin/env bash
#
# redis start up the redis server daemon
#
# chkconfig: 345 99 99
# description: redis service in /etc/init.d/redis
# chkconfig --add redis or chkconfig --list redis
# service redis start or service redis stop
# processname: redis-server
# config: /etc/redis.conf
PATH=/usr/local/bin:/sbin:/usr/bin:/bin
REDISPORT=6379
EXEC=/usr/local/bin/redis-server
REDIS_CLI=/usr/local/bin/redis-cli
PIDFILE=/var/run/redis.pid
CONF="/etc/redis.conf"
#make sure some dir exist
if [ ! -d /var/lib/redis ] then
mkdir -p /var/lib/redis
mkdir -p /var/log/redis
fi
case "$1" in
status)
ps -A|grep redis
;;
start)
if [ -f $PIDFILE ]
then
echo "$PIDFILE exists, process is already running or crashed"
else
echo "Starting Redis server..."
$EXEC $CONF
fi
if [ "$?"="0" ]
then
echo "Redis is running..."
fi
;;
stop)
if [ ! -f $PIDFILE ]
then
echo "$PIDFILE does not exist, process is not running"
else
PID=$(cat $PIDFILE)
echo "Stopping ..."
$REDIS_CLI -p $REDISPORT SHUTDOWN
while [ -x ${PIDFILE} ]
do
echo "Waiting for Redis to shutdown ..."
sleep 1
done
echo "Redis stopped"
fi
;;
restart|force-reload)
${0} stop
${0} start
;;
*)
echo "Usage: /etc/init.d/redis {start|stop|restart|force-reload}" >&2
exit 1
esac
执行:
$ chmod o+x /etc/init.d/redis-server
$ chkconfig –add redis-server
$ service redis-server start
五:从服务器配置redis
从服务器,配置一样,只不过 修改/etc/redis.conf 中
slaveof  修改为
slaveof 172.16.206.28 6379
然后开启从服务器的redis服务。
start redis-server start
六:进行redis主从测试
#主服务器
redis-cli -p 6379 set hello world
#从服务器
redis-cli -p 6379 get hello
“world”
#主服务器
redis-cli -p 6379 set hello2 world2
#从服务器
redis-cli -p 6379 get hello2
“world2”
redis-cli -p 6379 set hello world
(error) READONLY You can’t write against a read only slave.
成功配置主从redis服务器,由于配置中有一条从服务器是只读的,所以从服务器没法设置数据,只可以读取数据。
七:安装和配置keepalived
tar -zxvf keepalived-1.2.15.tar.gz
cd keepalived-1.2.15
2.  安装openssl-devel和kernel-devel
yum install openssl-devel
yum install kernel-devel
ln -s /usr/src/kernels/2.6.32-431.el6.x86_64 /usr/src/linux
3. 配置编译安装
./configure–prefix=/usr/local/keepalived  –with-kernel-dir=/usr/src/linux   –enable-sha1
make && make install
4. 复制keepalived相关文件
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/keepalived
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/keepalived
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/keepalived
mkdir /etc/keepalived
cp /usr/local/keepalived/etc/keepalived/samples/keepalived.conf.virtualhost
/etc/keepalived/keepalived.conf
八:修改配置文件和相关脚本
1. 在Master上创建配置文件keepalived.conf
vi /etc/keepalived/keepalived.conf
//////////////////////////////////////////////////
! Configuration File for keepalived
vrrp_script chk_redis {
  script "/etc/keepalived/scripts/redis_check.sh" ###监控脚本
  interval 2 ###监控时间
}
#网卡需要注意,使用ifconfig查看一下当前活动网卡
vrrp_instance VI_1 {
  state MASTER ###设置为MASTER
  interface eth0 ###监控网卡
  virtual_router_id 51
  priority 100 ###权重值
  authentication {
    auth_type PASS ###加密
    auth_pass 1111 ###密码
  }
  track_script {
    chk_redis ###执行上面定义的chk_redis
  }
  virtual_ipaddress {
    172.16.206.250 ######VIP
  }
  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
}
2.在Slave上创建配置文件keepalived.conf
vim /etc/keepalived/keepalived.conf
//////////////////////////////////////////////////
! Configuration File for keepalived
vrrp_script chk_redis {
  script "/etc/keepalived/scripts/redis_check.sh" ###监控脚本
  interval 2 ###监控时间
}
#同样要注意网卡
vrrp_instance VI_1 {
  state BACKUP ###设置为BACKUP
  interface eth0 ###监控网卡
  virtual_router_id 51
  priority 10 ###比MASTRE权重值低
  authentication {
    auth_type PASS
    auth_pass 1111 ###密码与MASTRE相同
  }
  track_script {
    chk_redis ###执行上面定义的chk_redis
  }
  virtual_ipaddress {
    172.16.206.250 ####vip
  }
  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
}
3.在Master和Slave上创建监控Redis的脚本
$  mkdir /etc/keepalived/scripts
$  vim /etc/keepalived/scripts/redis_check.sh
#!/bin/bash
ALIVE=`/usr/local/bin/redis-cli -p 6379 -a password_of_redis PING`
if [ "$ALIVE" == "PONG" ]; then
  echo $ALIVE
  exit 0
else
  echo $ALIVE
  exit 1
fi
4.编写关键脚本
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
Keepalived在转换状态时会依照状态来执行脚本:
  • 当进入Master状态时会呼叫 notify_master
  • 当进入Backup状态时会呼叫 notify_backup
  • 当发现异常情况时进入Fault状态呼叫 notify_fault
  • 当程序终止时则呼叫 notify_stop
4.1 在Redis Master上创建notity_master与notify_backup脚本:
vi /etc/keepalived/scripts/redis_master.sh
#!/bin/bash
REDISCLI="/usr/local/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 172.16.206.sl 6379 >> $LOGFILE 2>&1
sleep 10 #延迟10秒以后待数据同步完成后再取消同步状态
echo "Run SLAVEOF NO ONE cmd ..." >> $LOGFILE
$REDISCLI SLAVEOF NO ONE >> $LOGFILE 2>&1
vi /etc/keepalived/scripts/redis_backup.sh
#!/bin/bash
REDISCLI="/usr/local/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.sl 6379 >> $LOGFILE 2>&1
4.2 在Redis Slave上创建notity_master与notify_backup脚本:
vi /etc/keepalived/scripts/redis_master.sh
#!/bin/bash
REDISCLI="/usr/local/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.ma 6379 >> $LOGFILE 2>&1
sleep 10 #延迟10秒以后待数据同步完成后再取消同步状态
echo "Run SLAVEOF NO ONE cmd ..." >> $LOGFILE
$REDISCLI SLAVEOF NO ONE >> $LOGFILE 2>&1
vi /etc/keepalived/scripts/redis_backup.sh
#!/bin/bash
REDISCLI="/usr/local/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.4 6379 >> $LOGFILE 2>&1
4.3 然后在Master与Slave创建如下相同的脚本:
vi /etc/keepalived/scripts/redis_fault.sh
#!/bin/bash
LOGFILE=/var/log/keepalived-redis-state.log
echo "[fault]" >> $LOGFILE
date >> $LOGFILE
vi /etc/keepalived/scripts/redis_stop.sh
#!/bin/bash
LOGFILE=/var/log/keepalived-redis-state.log
echo "[stop]" >> $LOGFILE
date >> $LOGFILE
4.4 在主从服务器上面给脚本都加上可执行权限:
chmod +x /etc/keepalived/scripts/*.sh
九:相关功能测试
1. 启动Master和slave上的Redis
service redis-server start
2.启动Master和slave上的Keepalived
/etc/init.d/keepalived start
3.尝试通过VIP连接Redis:
$ redis-cli -h 172.16.206.250 -p 6379 -a password_of_redis INFO
连接成功,可以看到主从机的信息,例如:
role:master
slave0:172.16.206.28,6379,online
4.尝试插入一些数据:
$ redis-cli -h 172.16.206.250 SET hello3 world3
OK
从VIP读取数据
$ redis-cli -h 172.16.206.250 GET hello3
“world3”
从Master读取数据
$ redis-cli -h 172.16.206.29 GET hello3
“world3”
从Slave读取数据
$ redis-cli -h 172.16.206.28 GET hello3
“world3”
5.将Master上的Redis进程杀死:
$ service redis-server stop
查看Master上的Keepalived日志
$ tail -f /var/log/keepalived-redis-state.log
[fault]
Mon Jan  5 14:06:22 CST 2015
同时Slave上的日志显示:
$ tail -f /var/log/keepalived-redis-state.log
[master]
Mon Jan  5 14:13:52 CST 2015
Being master….
Run SLAVEOF cmd …
OK Already connected to specified master
Run SLAVEOF NO ONE cmd …
OK
现在,Slave已经接管服务,并且拥有Master的角色
$ redis-cli -h 172.16.206.250 INFO
$ redis-cli -h 172.16.206.28 INFO
role:master
6.然后恢复Master的Redis进程
$  service redis-server start
查看Master上的Keepalived日志
$ tail -f /var/log/keepalived-redis-state.log
[master]
2015年 01月 05日 星期一 15:48:08 CST
Being master….
Run SLAVEOF cmd …
OK
Run SLAVEOF NO ONE cmd …
OK
同时Slave上的日志显示:
$ tail -f /var/log/keepalived-redis-state.log
[backup]
Mon Jan  5 14:53:16 CST 2015
Being slave….
Run SLAVEOF cmd …
OK
Master已经再次恢复了Master角色
摘自「http://abcve.com/redis-keepalived/」