Redis集群与高可用-主从复制

Redis集群与高可用的三种模式:

  • Redis主从复制
  • Redis哨兵(Sentinel)
  • Redis Cluster

Redis 主从复制

和MySQL主从模式类似,可以实现跨主机的远程备份。APP连接到LB集群虚拟IP,由LB调度用户请求到后端Redis服务器来提供服务。

当master出现故障后,可以提升一个slave变成新的master.

slave需要设置和master相同的连接密码,master需要开启数据持久化防止数据丢失。

创建主节点:10.0.0.7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# master:10.0.0.7
# redis默认为master节点,可以通过<INFO REPLICATION> 查看

10.0.0.7:6379> INFO REPLICATION
# Replication
role:master
connected_slaves:0
master_failover_state:no-failover
master_replid:66f0a0d55d211ac362470f1a95d3f8a8d49d16a1
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
使用安装脚本编译安装redis服务器,做为slave端:10.0.0.11 10.0.0.9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# slave:10.0.0.11
# 设置为10.0.0.7的slave
10.0.0.11:6379> REPLICAOF 10.0.0.7 6379
OK
# 输入master端的访问密码
10.0.0.11:6379> CONFIG SET masterauth 12345678
OK
# 查看状态,role变为slave
10.0.0.11:6379> INFO REPLICATION
# Replication
role:slave
master_host:10.0.0.7
master_port:6379
master_link_status:up
master_last_io_seconds_ago:3
master_sync_in_progress:0
slave_read_repl_offset:196
slave_repl_offset:196
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:f86f616f8fd0c06157d2bf4b67e3c6cbb6b9ca74
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:196
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:196
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# slave:10.0.0.9
10.0.0.9:6379> REPLICAOF 10.0.0.7 6379
OK
10.0.0.9:6379> CONFIG SET masterauth 12345678
OK
10.0.0.9:6379> INFO REPLICATION
# Replication
role:slave
master_host:10.0.0.7
master_port:6379
master_link_status:up
master_last_io_seconds_ago:10
master_sync_in_progress:0
slave_read_repl_offset:532
slave_repl_offset:532
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:f86f616f8fd0c06157d2bf4b67e3c6cbb6b9ca74
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:532
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:519
repl_backlog_histlen:14

查看master端状态

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
10.0.0.7:6379> INFO REPLICATION
# Replication
role:master
connected_slaves:2
slave0:ip=10.0.0.11,port=6379,state=online,offset=574,lag=1
slave1:ip=10.0.0.9,port=6379,state=online,offset=588,lag=0
master_failover_state:no-failover
master_replid:f86f616f8fd0c06157d2bf4b67e3c6cbb6b9ca74
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:588
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:588
10.0.0.7:6379> INFO REPLICATION
# Replication
role:master
connected_slaves:2
slave0:ip=10.0.0.11,port=6379,state=online,offset=588,lag=1
slave1:ip=10.0.0.9,port=6379,state=online,offset=588,lag=0
master_failover_state:no-failover
master_replid:f86f616f8fd0c06157d2bf4b67e3c6cbb6b9ca74
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:588
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:588

修改slave配置文件

1
2
3
[root@rocky9 ~]# vim /apps/redis/etc/redis.conf 
replicaof 10.0.0.7 6379
masterauth 12345678
删除主从同步
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
10.0.0.9:6379> REPLICAOF NO ONE
OK
10.0.0.9:6379> INFO REPLICATION
# Replication
role:master
connected_slaves:0
master_failover_state:no-failover
master_replid:f842a5a9130f9156260f5ceee8be657d06024e8c
master_replid2:f86f616f8fd0c06157d2bf4b67e3c6cbb6b9ca74
master_repl_offset:782
second_repl_offset:783
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:519
repl_backlog_histlen:264
故障恢复

1、slave节点故障时,将client指向其它slave节点即可,然后修复故障slave节点

2、master节点故障时,需将其中一个slave提升为master,并将其它slave指向新的master

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 提升为master,执行<REPLICAOF NO ONE>即可

10.0.0.9:6379> REPLICAOF NO ONE
OK

10.0.0.9:6379> INFO REPLICATION
# Replication
role:master
connected_slaves:0
master_failover_state:no-failover
master_replid:f842a5a9130f9156260f5ceee8be657d06024e8c
master_replid2:f86f616f8fd0c06157d2bf4b67e3c6cbb6b9ca74
master_repl_offset:782
second_repl_offset:783
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:519
repl_backlog_histlen:264
1
2
3
4
5
6
# 其它slave指向新的master 10.0.0.9
10.0.0.11:6379> REPLICAOF 10.0.0.9 6379
OK
# 若连接密码不一致,重新指定连接密码
10.0.0.11:6379> CONFIG SET masterauth 12345678
OK
主从复制的缺陷
  • master 和 slave 无法自动切换,无法实现故障转移
  • 只有一个master主节点支持写入,业务量大时容易达到性能瓶颈

Redis集群与高可用-主从复制
https://www.xcjyc.top/2024/05/20/Redis集群与高可用-主从复制/
作者
XCJYC
发布于
2024年5月20日
许可协议