Redis主从架构持久化存在一个问题,即前次测试的结论,持久化需要配置在主实例上才能跨越实例保证数据不丢失,这样以来主实例在持久化数据到硬 盘的过程中,势必会造成磁盘的I/O等待,经过实际测试,这个持久化写硬盘的过程给应用程序带来的影响无法忍受;因而在大多数场景下,会考虑把持久化配置 在从实例上,当主实例宕机后,通过手动或者自动的方式将从实例提升为主实例,继续提供服务!当主实例恢复后,先从原从实例上同步数据,同步完成后再恢复到 原始的主从状态!要实现这种的要求,需要有keepalive的配合,一方面keepalive提供了VIP,可以避免修改应用程序连接,同时redis 实例的配置文件监听部分也需要修改为全网监听;另一方面keepalive定时调度脚本来监控主从实例的状态,根据具体情况进行切换!本文将重点介绍下使 用keepalive实现redis主从自动failover!
环境介绍
操作系统版本均为:rhel5.4 64bit
redis版本:2.6.4
redis实例端口均为:6379
redis实例密码均为:123
VIP:192.168.1.120
主实例为server11(192.168.1.112)
从实例为server12(192.168.1.113,开启快照持久化)
一:安装keepalive软件,server11安装完成后直接scp至server12上即可
[root@server11 ~]# wget
[root@server11 ~]# tar -zxvf ../tarbag/keepalived-1.1.19.tar.gz
[root@server11 ~]# cd keepalived-1.1.19/
[root@server11 ~]# ./configure --prefix=/usr/local/keepalived && make && make install
二:配置主节点server11配置文件
[root@server11 ~]# cat /usr/local/keepalived/etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id LVS_DEVEL
}
vrrp_script Monitor_redis {
script "/usr/local/scripts/redis_monitor.sh"
interval 2
weight 2
}
vrrp_instance VI_1{
state MASTER
interface eth0
virtual_router_id 51
mcast_src_ip 192.168.1.112
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass password_123
}
track_script {
Monitor_redis
}
virtual_ipaddress {
192.168.1.120
}
notify_fault /usr/local/scripts/redis_fault.sh
notify_stop /usr/local/scripts/redis_stop.sh
}
三:配置从节点server12配置文件
[root@server12 ~]# cat /usr/local/keepalived/etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id LVS_DEVEL
}
vrrp_script Monitor_redis {
script "/usr/local/scripts/redis_monitor.sh"
interval 2
weight 2
}
vrrp_instance VI_1{
state BACKUP
interface eth0
virtual_router_id 51
mcast_src_ip 192.168.1.113
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass password_123
}
track_script {
Monitor_redis
}
virtual_ipaddress {
192.168.1.120
}
notify_master /usr/local/scripts/redis_master.sh
notify_backup /usr/local/scripts/redis_backup.sh
notify_fault /usr/local/scripts/redis_fault.sh
notify_stop /usr/local/scripts/redis_stop.sh
}
四:准备相关的脚本,主从实例上都需要存在这些脚本,同时注意脚本需要由可执行权限