redis主从同步

一、redis主从复制

1.redis主从同步

  • 在redis集群中的主从复制是由redis主从同步来实现的
  • 主节点(master)把数据分发给从节点(slave)
  • 主从同步的设计的好处在于高可用,redis有冗余设计

原理:

  1. 从服务器向主服务器发送 SYNC 命令。
  2. 接到 SYNC 命令的主服务器会调用BGSAVE 命令,创建一个 RDB 文件,并使用缓冲区记录接下来执行的所有写命令。
  3. 当主服务器执行完 BGSAVE 命令时,它会向从服务器发送 RDB 文件,而从服务器则会接收并载入这个文件。
  4. 主服务器将缓冲区储存的所有写命令发送给从服务器执行。

=====================
1、在开启主从复制的时候,使用的是RDB方式的,同步主从数据的
2、同步开始之后,通过主库命令传播的方式,主动的复制方式实现
3、2.8以后实现PSYNC的机制,实现断线重连

2.如何实现redis主从同步

环境准备

  1. 准备两个或者两个以上的redis实例

6380.conf

mkdir /data/638{0..2}  #创建6380 6381 6382文件夹

配置文件示例:
vim   /data/6380/redis.conf
port 6380
daemonize yes
pidfile /data/6380/redis.pid
loglevel notice
logfile "/data/6380/redis.log"
dbfilename dump.rdb
dir /data/6380
protected-mode no

6381.conf

vim   /data/6381/redis.conf
port 6381
daemonize yes
pidfile /data/6381/redis.pid
loglevel notice
logfile "/data/6381/redis.log"
dbfilename dump.rdb
dir /data/6381
protected-mode no

6382.conf

port 6382
daemonize yes
pidfile /data/6382/redis.pid
loglevel notice
logfile "/data/6382/redis.log"
dbfilename dump.rdb
dir /data/6382
protected-mode no
  1. 启动这三个实例
redis-server /data/6380/redis.conf
redis-server /data/6381/redis.conf
redis-server /data/6382/redis.conf
  1. 主从规划
6380 主
6381 6382 从
  1. 配置主从同步
6381/6382命令行
redis-cli -p 6381
SLAVEOF 127.0.0.1 6380  #指明主的地址
redis-cli -p 6382
SLAVEOF 127.0.0.1 6380  #指明主的地址

检查主从状态
从库:
127.0.0.1:6382> info replication
127.0.0.1:6381> info replication
主库:
127.0.0.1:6380> info replication
  1. 测试
主
127.0.0.1:6380> set name chaoge
从
127.0.0.1:6381> get name 
  1. 手动进行主从同步发生故障,进行主从切换
    关闭主库
redis-cli -p 6380
shutdown

查看从库里面master_link_status项,此时会发现为down

redis-cli -p 6381
info replication
redis-cli -p 6382
info replication

重新设置主从关系

# 1.先关闭6381的从库身份
redis-cli -p 6381
info replication
slaveof no one
# 2.将6382的主库身份设置成6381
[root@db03 ~]# redis-cli -p 6382
127.0.0.1:6382> SLAVEOF no one
127.0.0.1:6382> SLAVEOF 127.0.0.1 6381

这样我们就实现了手动进行切换主从

二、redis主从复制的高可用(Redis-Sentinel)

Redis-Sentinel是redis官方推荐的高可用性解决方案,
当用redis作master-slave的高可用时,如果master本身宕机,redis本身或者客户端都没有实现主从切换的功能。
而redis-sentinel就是一个独立运行的进程,用于监控多个master-slave集群,
自动发现master宕机,进行自动切换slave > master。

sentinel主要功能如下:
不时的监控redis是否良好运行,如果节点不可达就会对节点进行下线标识
如果被标识的是主节点,sentinel就会和其他的sentinel节点“协商”,如果其他节点也人为主节点不可达,就会选举一个sentinel节点来完成自动故障转义
在master-slave进行切换后,master_redis.conf、slave_redis.conf和sentinel.conf的内容都会发生改变,即master_redis.conf中会多一行slaveof的配置,sentinel.conf的监控目标会随之调换

1.安装与配置

1.redis集群相关命令整理

官网地址:http://redisdoc.com/
redis-cli info #查看redis数据库信息
redis-cli info replication #查看redis的复制授权信息
redis-cli info sentinel   #查看redis的哨兵信息

2.准备三个redis实例,一个作为主节点,另外两个作为从节点

主节点配置

port 6379
daemonize yes
logfile "6379.log"
dbfilename "dump-6379.rdb"
dir "/opt/data/6379"

第一个从节点

port 6380
daemonize yes
logfile "6380.log"
dbfilename "dump-6380.rdb"
dir "/opt/data/6380"
slaveof 127.0.0.1 6379

第二个从节点

port 6381
daemonize yes
logfile "6381.log"
dbfilename "dump-6381.rdb"
dir "/opt/data/6381"
slaveof 127.0.0.1 6379

我们不难发现,三个配置文件除了端口号的不同,从节点多了一项配置,这也是其作为从节点设置的关键slaveof 127.0.0.1 6379

按照以上配置好之后我们只需要一次执行对应的配置文件就可以了

$ redis-server /opt/data/6379/redis-6379.conf
$ redis-server /opt/data/6380/redis-6380.conf
$ redis-server /opt/data/6381/redis-6381.conf

启动之后可以用ps -aux|grep redis查看是否启动,也可以使用redis-cli -p 6380 ping,如果输出pong,就说明启动成功

查看主节点的主从关系

[root@localhost data]# redis-cli -p 6379 info replication
# Replication
role:master
connected_slaves:2
slave0:ip=127.0.0.1,port=6380,state=online,offset=140,lag=0
slave1:ip=127.0.0.1,port=6381,state=online,offset=140,lag=0
master_replid:f4f353ea84c55f87595487891738b0158ec8363a
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:140
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:140

查看从节点的主从关系

[root@localhost data]# redis-cli -p 6380 info replication
# Replication
role:slave
master_host:127.0.0.1
master_port:6379
master_link_status:up
master_last_io_seconds_ago:9
master_sync_in_progress:0
slave_repl_offset:140
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:f4f353ea84c55f87595487891738b0158ec8363a
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:140
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:140

此时可以在master上写入数据,在slave上查看数据,此时主从复制配置完成

3.配置Redis Sentinel(哨兵)

哨兵配置文件三台均一样

port 26379

daemonize yes

protected-mode no

pidfile /opt/data/redis_26379.pid

logfile "26379.log"

dir /opt/data/

sentinel monitor mymaster 127.0.0.1 6379 2 #监控地址,哨兵名称

# sentinel auth-pass mymaster 123456

sentinel down-after-milliseconds mymaster 30000

sentinel parallel-syncs mymaster 1

sentinel failover-timeout mymaster 180000

哨兵之间的配置只是端口号的不同

3.检测哨兵机制

方法:杀掉master进程,然后等一会查看是否将从节点切换为master

# 主节点
[root@localhost data]# redis-cli -p 6379 info replication
# Replication
role:master
connected_slaves:2
slave0:ip=127.0.0.1,port=6380,state=online,offset=280814,lag=0
slave1:ip=127.0.0.1,port=6381,state=online,offset=280814,lag=0
master_replid:699f910ab151d2c201fc1cd50e20dd5a14a63bb7
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:280814
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:280814

# 从节点
[root@localhost data]# redis-cli -p 6380 info replication
# Replication
role:slave
master_host:127.0.0.1
master_port:6379
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_repl_offset:359564
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:699f910ab151d2c201fc1cd50e20dd5a14a63bb7
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:359564
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:359564

[root@localhost data]# ps -aux|grep redis
root       7253  0.1  0.3 147460  3016 ?        Ssl  15:34   0:04 redis-server *:6379
root       7258  0.1  0.2 147460  2976 ?        Ssl  15:34   0:04 redis-server *:6380
root       7264  0.1  0.2 147460  2980 ?        Ssl  15:35   0:04 redis-server *:6381
root       7290  0.1  0.2 145412  2372 ?        Ssl  15:36   0:06 redis-sentinel *:26379 [sentinel]
root       7305  0.1  0.2 145412  2388 ?        Ssl  15:37   0:06 redis-sentinel *:26380 [sentinel]
root       7310  0.1  0.2 145412  2316 ?        Ssl  15:37   0:06 redis-sentinel *:26381 [sentinel]
root       8051  0.0  0.0 112824   984 pts/4    S+   16:36   0:00 grep --color=auto redis
[root@localhost data]# kill -9 7253
[root@localhost data]# ps -aux|grep redis
root       7258  0.1  0.3 147460  3136 ?        Ssl  15:34   0:04 redis-server *:6380
root       7264  0.1  0.3 147460  3132 ?        Ssl  15:35   0:04 redis-server *:6381
root       7290  0.1  0.2 145412  2372 ?        Ssl  15:36   0:06 redis-sentinel *:26379 [sentinel]
root       7305  0.1  0.2 145412  2388 ?        Ssl  15:37   0:06 redis-sentinel *:26380 [sentinel]
root       7310  0.1  0.2 145412  2316 ?        Ssl  15:37   0:06 redis-sentinel *:26381 [sentinel]
root       8075  0.0  0.0 112824   984 pts/4    S+   16:37   0:00 grep --color=auto redis
# 从节点变成了主节点
[root@localhost data]# redis-cli -p 6380 info replication
# Replication
role:master
connected_slaves:1
slave0:ip=127.0.0.1,port=6381,state=online,offset=728844,lag=1
master_replid:631a73cb579f84a172aeb9a3a918dc445e4d577b
master_replid2:699f910ab151d2c201fc1cd50e20dd5a14a63bb7
master_repl_offset:728844
second_repl_offset:720387
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:728844
posted @ 2021-02-20 17:28  kingdoms  阅读(363)  评论(0)    收藏  举报