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 http://keepalived.org/software/keepalived-1.1.19.tar.gz
- [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
-
- }
四:准备相关的脚本,主从实例上都需要存在这些脚本,同时注意脚本需要由可执行权限
- [root@server11 ~]# cat /usr/local/scripts/redis_monitor.sh
- #!/bin/bash
-
ALIVE=$(/usr/local/redis2/bin/redis-cli -h 192.168.1.112 -p 6379 -a 123 PING)
-
- if [ "$ALIVE" == "PONG" ]; then
- echo $ALIVE
- exit 0
- else
- echo $ALIVE
- killall -9 keepalived
- service network restart
- exit 1
- fi
-
- [root@server11 ~]# sh /usr/local/scripts/redis_monitor.sh
- PONG
-
- [root@server11 ~]# cat /usr/local/scripts/redis_master.sh
- #!/bin/bash
-
REDISCLI="/usr/local/redis2/bin/redis-cli -h 192.168.1.112 -p 6379 -a 123"
-
LOGFILE="/usr/local/redis2/var/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.113 6379 >> $LOGFILE 2>&1
- sleep 10
-
echo "Run SLAVEOF NO ONE cmd ..." >> $LOGFILE
-
$REDISCLI SLAVEOF NO ONE >> $LOGFILE 2>&1
-
-
- [root@server11 ~]# cat /usr/local/scripts/redis_backup.sh
- #!/bin/bash
-
REDISCLI="/usr/local/redis2/bin/redis-cli -h 192.168.1.112 -p 6379 -a 123"
-
LOGFILE="/usr/local/redis2/var/keepalived-redis-state.log"
-
-
echo "[backup]" >> $LOGFILE
-
date >> $LOGFILE
-
echo "Being slave...." >> $LOGFILE 2>&1
- sleep 15
-
echo "Run SLAVEOF cmd ..." >> $LOGFILE
-
$REDISCLI SLAVEOF 192.168.1.113 6379 >> $LOGFILE 2>&1
-
- [root@server11 ~]# cat /usr/local/scripts/redis_stop.sh
- #!/bin/bash
-
LOGFILE="/usr/local/redis2/var/keepalived-redis-state.log"
-
echo "[stop]" >> $LOGFILE
-
date >> $LOGFILE
- [root@server11 ~]# cat /usr/local/scripts/redis_fault.sh
#!/bin/bash
LOGFILE="/usr/local/redis2/var/keepalived-redis-state.log"
echo "[fault]" >> $LOGFILE
date >> $LOGFILE
五:主从实例分别启动keepalive进程,测试VIP是否正常(这里就要修改redis配置文件的监听地址为0.0.0.0)
- [root@server11 ~]# /usr/local/keepalived/sbin/keepalived -D -f /usr/local/keepalived/etc/keepalived/keepalived.conf
- [root@server11 ~]# tail -f /var/log/messages
- Dec 12 09:25:49 server11 Keepalived_healthcheckers[7710]: Configuration is using : 5499 Bytes
- Dec 12 09:25:49 server11 Keepalived_healthcheckers[7710]: Using LinkWatch kernel netlink reflector...
- Dec 12 09:25:49 server11 Keepalived_vrrp[7712]: VRRP sockpool: [ifindex(2), proto(112), fd(12,13)]
- Dec 12 09:25:49 server11 Keepalived_vrrp[7712]: VRRP_Script(Monitor_redis) succeeded
- Dec 12 09:25:50 server11 Keepalived_vrrp[7712]: VRRP_Instance(VI_1{) Transition to MASTER STATE
- Dec 12 09:25:51 server11 Keepalived_vrrp[7712]: VRRP_Instance(VI_1{) Entering MASTER STATE
- Dec 12 09:25:51 server11 Keepalived_vrrp[7712]: VRRP_Instance(VI_1{) setting protocol VIPs.
- Dec 12 09:25:51 server11 Keepalived_vrrp[7712]: VRRP_Instance(VI_1{) Sending gratuitous ARPs on eth0 for 192.168.1.120
- Dec 12 09:25:51 server11 avahi-daemon[4519]: Registering new address record for 192.168.1.120 on eth0.
- Dec 12 09:25:51 server11 Keepalived_healthcheckers[7710]: Netlink reflector reports IP 192.168.1.120 added
- Dec 12 09:25:51 server11 Keepalived_vrrp[7712]: Netlink reflector reports IP 192.168.1.120 added
- Dec 12 09:25:56 server11 Keepalived_vrrp[7712]: VRRP_Instance(VI_1{) Sending gratuitous ARPs on eth0 for 192.168.1.120
-
- [root@server11 ~]# ip a |grep 192
- inet 192.168.1.112/24 brd 192.168.1.255 scope global eth0
- inet 192.168.1.120/32 scope global eth0
-
- [root@server12 ~]# /usr/local/keepalived/sbin/keepalived -D -f /usr/local/keepalived/etc/keepalived/keepalived.conf
- [root@server12 ~]# tail -f /var/log/messages
- Dec 12 09:26:55 server12 Keepalived_healthcheckers[3106]: Configuration is using : 5595 Bytes
- Dec 12 09:26:55 server12 Keepalived_vrrp[3108]: VRRP_Instance(VI_1{) Entering BACKUP STATE
- Dec 12 09:26:55 server12 Keepalived_healthcheckers[3106]: Using LinkWatch kernel netlink reflector...
- Dec 12 09:26:55 server12 Keepalived_vrrp[3108]: VRRP sockpool: [ifindex(2), proto(112), fd(12,13)]
- Dec 12 09:26:55 server12 Keepalived_vrrp[3108]: VRRP_Script(Monitor_redis) succeeded
-
- [root@server11 ~]# /usr/local/redis2/bin/redis-cli -h 192.168.1.120 -a 123 info |grep -A 3 'Replication'
- # Replication
- role:master
- connected_slaves:1
- slave0:192.168.1.113,6379,online
六:主实例写入测试数据,该脚本原则上会写入25条测试数据,不过由于未优化redis默认并发数,会导致一些写入请求失败,最终功写入231839条测试数据,占内存总大小为25M左右,写入过程中可以观察主从实例的持久化文件变化情况,主实例的持久化文件维持在30k,从实例的则不断的扩展!
- [root@server11 ~]# cat test.sh
- #!/bin/bash
-
REDISCLI="/usr/local/redis2/bin/redis-cli -h 192.168.1.120 -a 123 -n 1 SET"
-
ID=1
-
while(($ID<50001))
- do
-
INSTANCE_NAME="i-2-$ID-VM"
-
UUID=`cat /proc/sys/kernel/random/uuid`
-
PRIVATE_IP_ADDRESS=10.`echo "$RANDOM % 255 + 1" | bc`.`echo "$RANDOM % 255 + 1" | bc`.`echo
-
- "$RANDOM % 255 + 1" | bc`\
-
CREATED=`date "+%Y-%m-%d %H:%M:%S"`
- $REDISCLI vm_instance:$ID:instance_name "$INSTANCE_NAME"
- $REDISCLI vm_instance:$ID:uuid "$UUID"
- $REDISCLI vm_instance:$ID:private_ip_address "$PRIVATE_IP_ADDRESS"
- $REDISCLI vm_instance:$ID:created "$CREATED"
- $REDISCLI vm_instance:$INSTANCE_NAME:id "$ID"
-
ID=$(($ID+1))
- done
-
- [root@server11 ~]# sh test.sh
- [root@server11 redis2]# /usr/local/redis2/bin/redis-cli -h 192.168.1.120 -a 123 info |egrep
-
- 'used_memory_peak_human|db1:keys'
- used_memory_peak_human:24.98M
-
db1:keys=231839,expires=0
七:模拟主实例故障,观察日志输出,验证从实例是否能成功接管VIP,同时将实例变成读写模式
- [root@server11 ~]# killall -9 redis-server
- [root@server11 ~]# ip a |grep 192
- inet 192.168.1.112/24 brd 192.168.1.255 scope global eth0
-
- [root@server11 ~]# ps -ef |grep redis
- root 15886 6458 0 09:49 pts/0 00:00:00 grep redis
- [root@server11 ~]# ps -ef |grep keep
- root 16029 6458 0 09:49 pts/0 00:00:00 grep keep
-
-
- [root@server12 ~]# tail -f /usr/local/redis2/var/keepalived-redis-state.log
- [master]
- Wed Dec 12 09:48:52 CST 2012
- Being master....
- Run SLAVEOF cmd ...
- OK Already connected to specified master
- Run SLAVEOF NO ONE cmd ...
- OK
-
- [root@server12 ~]# tail -f /var/log/messages
- Dec 12 09:48:51 server12 Keepalived_vrrp[3108]: VRRP_Instance(VI_1{) Transition to MASTER STATE
- Dec 12 09:48:52 server12 Keepalived_vrrp[3108]: VRRP_Instance(VI_1{) Entering MASTER STATE
- Dec 12 09:48:52 server12 Keepalived_vrrp[3108]: VRRP_Instance(VI_1{) setting protocol VIPs.
- Dec 12 09:48:52 server12 Keepalived_vrrp[3108]: VRRP_Instance(VI_1{) Sending gratuitous ARPs on eth0 for 192.168.1.120
- Dec 12 09:48:52 server12 Keepalived_vrrp[3108]: Netlink reflector reports IP 192.168.1.120 added
- Dec 12 09:48:52 server12 avahi-daemon[2921]: Registering new address record for 192.168.1.120 on eth0.
- Dec 12 09:48:52 server12 Keepalived_healthcheckers[3106]: Netlink reflector reports IP 192.168.1.120 added
- Dec 12 09:48:57 server12 Keepalived_vrrp[3108]: VRRP_Instance(VI_1{) Sending gratuitous ARPs on eth0 for 192.168.1.120
-
- [root@server12 ~]# ip a |grep 192
- inet 192.168.1.113/24 brd 192.168.1.255 scope global eth0
- inet 192.168.1.120/32 scope global eth0
-
- [root@server12 ~]# /usr/local/redis2/bin/redis-cli -h 192.168.1.120 -a 123 info |grep -A 3 'Replication'
- # Replication
- role:master
- connected_slaves:0
- [root@server12 ~]# sh test.sh
- [root@server12 ~]# /usr/local/redis2/bin/redis-cli -h 192.168.1.120 -a 123 info |egrep
-
- 'used_memory_peak_human|db1:keys'
- used_memory_peak_human:26.78M
-
db1:keys=249925,expires=0
九:主实例角色的恢复过程,使用shell脚本自动恢复
- [root@server11 ~]# ssh-keygen
- [root@server11 ~]# cd .ssh/
- [root@server11 .ssh]# ssh-copy-id -i id_rsa.pub root@192.168.1.113
- [root@server11 ~]# cat /usr/local/scripts/recover_mastart.sh
- #!/bin/sh
-
ALIVE=$(/usr/local/redis2/bin/redis-cli -h 192.168.1.113 -p 6379 -a 123 PING)
-
MDB=/usr/local/redis2/master_dump.rdb
-
SDB=/usr/local/redis2/slave_dump.rdb
-
- if [ "$ALIVE" == "PONG" ]; then
- echo $ALIVE
- scp root@192.168.1.113:$SDB $MDB
- else
- echo $ALIVE
- exit 1
- fi
-
- /usr/local/redis2/bin/redis-server /usr/local/redis2/etc/redis.conf
- /usr/local/keepalived/sbin/keepalived -D -f
-
- /usr/local/keepalived/etc/keepalived/keepalived.conf
-
- [root@server11 ~]# chmod +x /usr/local/scripts/recover_mastart.sh
- [root@server11 ~]# sh /usr/local/scripts/recover_mastart.sh
十:验证数据完整性和主从角色恢复情况
- [root@server11 ~]# /usr/local/redis2/bin/redis-cli -h 192.168.1.120 -a 123 info |egrep 'used_memory_peak_human|db1:keys'
- used_memory_peak_human:26.78M
-
db1:keys=249925,expires=0
-
- [root@server11 ~]# /usr/local/redis2/bin/redis-cli -h 192.168.1.120 -a 123 info |grep -A 3 'Replication'
- # Replication
- role:master
- connected_slaves:1
- slave0:192.168.1.113,6379,online
-
- [root@server12 ~]# /usr/local/redis2/bin/redis-cli -h 192.168.1.113 -a 123 info |grep -A 3 'Replication'
- # Replication
- role:slave
- master_host:192.168.1.112
- master_port:6379
-
- [root@server12 ~]# /usr/local/redis2/bin/redis-cli -h 192.168.1.120 -a 123 info |egrep 'used_memory_peak_human|db1:keys'
- used_memory_peak_human:26.78M
-
db1:keys=249925,expires=0
-
- 主实例keepalive日志:
- [root@server11 ~]# tail -f /var/log/messages
- Dec 12 10:08:13 server11 Keepalived_vrrp[20231]: VRRP sockpool: [ifindex(2), proto(112), fd(11,12)]
- Dec 12 10:08:13 server11 Keepalived_vrrp[20231]: VRRP_Script(Monitor_redis) succeeded
- Dec 12 10:08:13 server11 Keepalived_vrrp[20231]: VRRP_Instance(VI_1{) Transition to MASTER STATE
- Dec 12 10:08:13 server11 Keepalived_vrrp[20231]: VRRP_Instance(VI_1{) Received higher prio advert
- Dec 12 10:08:13 server11 Keepalived_vrrp[20231]: VRRP_Instance(VI_1{) Entering BACKUP STATE
- Dec 12 10:08:15 server11 Keepalived_vrrp[20231]: VRRP_Instance(VI_1{) forcing a new MASTER election
- Dec 12 10:08:16 server11 Keepalived_vrrp[20231]: VRRP_Instance(VI_1{) Transition to MASTER STATE
- Dec 12 10:08:17 server11 Keepalived_vrrp[20231]: VRRP_Instance(VI_1{) Entering MASTER STATE
- Dec 12 10:08:17 server11 Keepalived_vrrp[20231]: VRRP_Instance(VI_1{) setting protocol VIPs.
- Dec 12 10:08:17 server11 Keepalived_healthcheckers[20230]: Netlink reflector reports IP 192.168.1.120 added
- Dec 12 10:08:17 server11 Keepalived_vrrp[20231]: VRRP_Instance(VI_1{) Sending gratuitous ARPs on eth0 for 192.168.1.120
- Dec 12 10:08:17 server11 Keepalived_vrrp[20231]: Netlink reflector reports IP 192.168.1.120 added
- Dec 12 10:08:17 server11 avahi-daemon[4519]: Registering new address record for 192.168.1.120 on eth0.
-
- [root@server11 ~]# ip a |grep 192
- inet 192.168.1.112/24 brd 192.168.1.255 scope global eth0
- inet 192.168.1.120/32 scope global eth0
-
- 从实例keepalive日志:
- [root@server12 ~]# tail -f /var/log/messages
- Dec 12 09:56:01 server12 last message repeated 4 times
- Dec 12 10:08:13 server12 Keepalived_vrrp[3108]: VRRP_Instance(VI_1{) Received lower prio advert, forcing new election
- Dec 12 10:08:13 server12 Keepalived_vrrp[3108]: VRRP_Instance(VI_1{) Sending gratuitous ARPs on eth0 for 192.168.1.120
- Dec 12 10:08:15 server12 Keepalived_vrrp[3108]: VRRP_Instance(VI_1{) Received higher prio advert
- Dec 12 10:08:15 server12 Keepalived_vrrp[3108]: VRRP_Instance(VI_1{) Entering BACKUP STATE
- Dec 12 10:08:15 server12 Keepalived_vrrp[3108]: VRRP_Instance(VI_1{) removing protocol VIPs.
- Dec 12 10:08:15 server12 Keepalived_healthcheckers[3106]: Netlink reflector reports IP 192.168.1.120 removed
- Dec 12 10:08:15 server12 Keepalived_vrrp[3108]: Netlink reflector reports IP 192.168.1.120 removed
- Dec 12 10:08:15 server12 avahi-daemon[2921]: Withdrawing address record for 192.168.1.120 on eth0.
-
- 从实例角色转换日志:
- [root@server12 ~]# tail -f /usr/local/redis2/var/keepalived-redis-state.log
- [backup]
- Wed Dec 12 10:08:15 CST 2012
- Being slave....
- Run SLAVEOF cmd ...
- OK
分享到:
相关推荐
总结以上步骤,完成Redis主从配置与高可用哨兵模式的搭建,再整合Spring Cache,可以使应用程序在保证性能的同时具备高可用性。需要注意的是,所有的配置操作都需要在保证安全的前提下进行,避免生产环境下的操作...
redis主从模式与哨兵模式例子 # 当前Sentinel服务运行的端口 port 26378 # 哨兵监听的主服务器 sentinel myid 09a71e001825114de399e0b6214c4b6f5449ab3a # 3秒内mymaster没响应,则认为mymaster宕机 sentinel deny...
在Redis主从结构中,有一个主节点(Master)负责接收写操作,多个从节点(Slave)通过复制主节点的数据保持同步,确保数据的安全性。当主节点出现故障时,从节点可以接管服务,实现故障切换。 1. **安装Redis** 在...
Redis主从同步是Redis高可用性的一个重要特性,它允许数据在多个实例间进行复制,以实现数据备份、负载均衡和故障恢复。主从结构中,一个Redis实例作为主节点负责处理写请求,而其他实例作为从节点,接收并同步主...
### Redis 主从配置及哨兵模式详解 #### 一、实验环境 - **Redis 版本**: Redis 4.0.6 - **操作系统**: Linux (安装于虚拟机中) - **宿主主机**: Windows 10 x64 - **远程终端工具**: Xshell 5 #### 二、主从...
Redis Sentinel是一个用于实现高可用性的组件,它可以监控主从节点的状态,并在主节点发生故障时自动进行故障转移。 1. **创建配置文件**:创建一个名为`sentinel.conf`的文件,通常位于与`6379.conf`相同的目录中...
目录Redis故障转移测试这是用于测试主/从Redis配置之间的故障转移方案的测试平台。启动Redis实例要使用和3个哨兵监视主服务器来启动主/从Redis设置,请执行以下操作: docker-compose up 在不带-d标志的情况下运行...
它能够监控主从结构的 Redis 集群,并在主节点出现故障时,自动将一个从节点提升为主节点,从而确保服务的连续性。 **Sentinel 系统的主要功能包括:** 1. **监控(Monitoring)**:Sentinel 实例会持续检查主...
- **主从复制(Replication)**:Redis 集群中的每个节点都可以是主节点或从节点,主节点负责处理写操作,从节点则复制主节点的数据并处理读操作,提高读性能。 - **槽分配(Slots)**:集群将键空间划分为16384个...
哨兵系统是Redis的高可用性解决方案,它负责监控主从集群的状态,自动故障转移,并且提供了一种安全的客户端发现机制。 1. **安装Sentinel**: 首先,确保已经安装了Redis。然后,创建一个新的配置文件`sentinel....
一、Redis主从复制 主从复制是哨兵模式的基础,它允许数据在主节点和一个或多个从节点之间同步。当主节点写入新数据时,这些数据会被复制到从节点,实现数据备份和负载均衡。这样,即使主节点出现问题,从节点也能...
通过自动故障检测和恢复,可以提高 Redis 主从集群的稳定性。哨兵集群通常由多个哨兵实例组成,它们之间通过互相通信实现故障检测与转移。 **步骤一:创建哨兵实例** - 重复上述主从集群中创建实例的过程,为哨兵...
3. 配置Redis主从复制: 在`/etc/redis/redis.conf`配置文件中,为主服务器设置`slaveof`选项,指定主服务器的IP和端口,然后重启Redis服务。 4. 安装Sentinel: ```bash sudo apt-get install redis-sentinel ...
Redis哨兵集群是一种高可用性解决方案,用于监控和管理Redis主从复制结构。哨兵系统通过监控、故障检测和自动故障转移确保了集群的稳定运行。以下是对哨兵集群配置的详细步骤和相关知识点的解释: 1. **哨兵安装与...
为了保证数据库的高可用性和故障转移,Redis提供了哨兵模式(Sentinel mode)来监控Redis主从服务器,实现自动故障恢复。本文将详细介绍在Windows环境下搭建Redis哨兵模式的步骤,并解释相关配置参数。 首先,要...
- 哨兵是一个独立的监控进程,用于监视 Redis 集群中的主从节点状态,当检测到故障时,能够自动进行故障转移,确保高可用性。 - 单个哨兵可以监控主从数据库,而多个哨兵则能实现互相监控,提高系统稳定性。 - ...
哨兵模式是Redis的一种高可用解决方案,它能够监控Redis主从节点的状态,自动进行故障转移,并在故障恢复后自动恢复主从关系。 **1. Redis哨兵模式介绍** Redis哨兵系统由多个哨兵节点组成,它们之间通过Gossip...
2. **复制与故障转移**:Redis 集群支持主从复制,每个节点可以有多个从节点。如果主节点失败,其从节点将自动晋升为主节点,确保服务不间断。这个过程称为故障转移(Failover)。 3. **槽迁移**:在节点添加或移除...
### Redis 操作文档知识点 ...以上是关于Redis从安装到使用的详细介绍,包括安装、配置、主从配置、Sentinel配置、Java操作Redis以及实施注意事项等方面的知识点。这些内容对于初学者来说是非常实用的指导资料。