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,别的都一样.

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

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

    主要核心思想,如果master 和 salve 全部存活的情况,VIP就漂移到 master.读写都从master操作,如果master宕机,VIP就会漂移到salve,并将之前的salve切换为ma ...

  2. Redis+Keepalived高可用环境部署记录

    Keepalived 实现VRRP(虚拟路由冗余)协议,从路由级别实现VIP切换,可以完全避免类似heartbeat脑裂问题,可以很好的实现主从.主备.互备方案,尤其是无状态业务,有状态业务就需要额外 ...

  3. 4、解析配置文件 redis.conf、Redis持久化RDB、Redis的主从复制

    1.Units单位 配置大小单位,开头定义了一些基本的度量单位,只支持bytes,不支持bit 对大小写不敏感 2.INCLUDES包含 和我们的Struts2配置文件类似,可以通过includes包 ...

  4. redis+Keepalived主从热备秒级切换

    一 简介 安装使用centos 5.10 Master 192.168.235.135 Slave 192.168.235.152 Vip 192.168.235.200 编译环境 yum -y in ...

  5. 通过Keepalived实现Redis Failover自动故障切换功能

    通过Keepalived实现Redis Failover自动故障切换功能[实践分享] 参考资料: http://patrick-tang.blogspot.com/2012/06/redis-keep ...

  6. Redis - Keepalived + redis 主备热备切换

    1. 热备方案 硬件:server两台,分别用于master-redis及slave-redis 软件:redis.keepalived 实现目标: 由keepalived对外提供虚拟IP(VIP)进 ...

  7. 基于keepalived对redis做高可用配置---转载

    关于keepalived的详细介绍,请移步本人相关博客:http://wangfeng7399.blog.51cto.com/3518031/1405785 功能 ip地址 安装软件 主redis 1 ...

  8. redis(三)redis+Keepalived主从热备秒级切换

    一 简介 安装使用centos 5.10 Master 192.168.235.135 Slave 192.168.235.152 Vip 192.168.235.200 编译环境 yum -y in ...

  9. 测试redis+keepalived实现简单的主备切换【转载】

    转自: 测试redis+keepalived实现简单的主备切换 - Try My Best 尽力而为 - ITeye技术网站http://raising.iteye.com/blog/2311757 ...

随机推荐

  1. Node.js模块

    每一个Node.js都是一个Node.js模块,包括JavaScript文件(.js).JSON文本文件(.json)和二进制模块文件(.node). mymodul.js function Hell ...

  2. VS调试网页的localhost

    那个像是iis的服务器叫: 只能访问自己的localhost,也就是127.0.0.1是回环地址(LoopBack),就是表示访问本机,是不用经过网卡的. 别名就是localhost.这种地址无法再外 ...

  3. viewport ---移动端详解

    转自---http://www.cnblogs.com/2050/p/3877280.html 移动前端开发之viewport的深入理解 在移动设备上进行网页的重构或开发,首先得搞明白的就是移动设备上 ...

  4. js-FCC算法-Pairwise

    找到你的另一半 都说优秀的程序员擅长面向对象编程,但却经常找不到另一半,这是为什么呢?因为你总是把自己局限成为一个程序员,没有打开自己的思维. 这是一个社群的时代啊,在这里你应该找到与你有相同价值观但 ...

  5. 【BZOJ-2177】曼哈顿最小生成树 Kruskal + 树状数组

    2177: 曼哈顿最小生成树 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 190  Solved: 77[Submit][Status][Discu ...

  6. MapReduce实现WordCount

    package algorithm; import java.io.IOException; import java.util.StringTokenizer; import org.apache.h ...

  7. SSH无密码登陆Agent admitted failure to sign using the key

    A :CentOS_Master B:Slave_1 C:Slave_2 普通用户hxsyl 1.现在A 上 ssh-keygen -t rsa 一路回车,不需要输入密码 执行该操作将在/home/h ...

  8. C语言之流的重定向

    写c的小程序断不了需要输入输出,手动输入可太麻烦了.下面介绍IO的重定向方式: .重定向标准输入输出和错误,直接在命令行使用符号< > > >> >>等,还可 ...

  9. bzoj2765[JLOI2010]铁人双项比赛

    题意:铁人双项比赛由长跑和骑自行车组成,参赛选手必须先完成k公里的长跑,然后完成r公里的骑车,才能到达终点.参赛选手有的擅长长跑,有的擅长骑车. 如果总赛程s=k+r一定,那么K越大,对擅长长跑的选手 ...

  10. css中 Span 元素的 width 属性无效果原因及多种解决方案

    先运行下程序看下: <span style='width:300px;'>123</span> 输出:123 可以看到 span会自动根据包含的内容来变化宽度 这是因为:对于内 ...