redis-sentinel(哨兵) && redis-cluster(集群)部署

本文详细介绍了Redis的持久化机制RDB和AOF的优缺点,以及主从复制的全量和增量同步过程。接着,重点讲解了Redis Sentinel哨兵系统的原理与配置,包括哨兵如何监控主从节点状态以及故障转移机制。最后,阐述了Redis Cluster集群的创建与使用,包括节点的环境准备、集群配置、故障自动恢复等。

1、RDB和AOF的优缺点

(1)RDB 模式优缺点

  • RDB快照保存了某个时间点的数据,可以通过脚本执行redis指令bgsave(非阻塞,后台执行)或者save(会阻塞写操作,不推荐)命令自定义时间点备份,可以保留多个备份,当出现问题可以恢复到不同时间点的版本,很适合备份,并且此文件格式也支持有不少第三方工具可以进行后续的数据分析

    比如: 可以在最近的24小时内,每小时备份一次RDB文件,并且在每个月的每一天,也备份一个ROB文件。这样的话,即使遇上问题,也可以随时将数据集还原到不同的版本。

  • RDB可以最大化Redis的性能,父进程在保存 RDB文件时唯一要做的就是fork出一个子进程,然后这个子进程就会处理接下来的所有保存工作,父进程无须执行任何磁盘 I/O 操作。

  • RDB在大量数据,比如几个G的数据,恢复的速度比AOF的快

(2)AOF 模式优缺点

  • 不能实时保存数据,可能会丢失自上一次执行RDB备份到当前的内存数据

    如果你需要尽量避免在服务器故障时丢失数据,那么RDB不适合你。虽然Redis允许你设置不同的保存点(save point)来控制保存RDB文件的频率,但是,因为ROB文件需要保存整个数据集的状态,所以它并不是一个轻松的操作。因此你可能会至少5分钟才保存一次RDB文件。在这种情况下,一旦发生故障停机,你就可能会丢失好几分钟的数据。

  • 当数据量非常大的时候,从父进程fork子进程进行保存至RDB文件时需要一点时间,可能是毫秒或者秒,取决于磁盘IO性能在数据集比较庞大时,fork()可能会非常耗时,造成服务器在一定时间内停止处理客户端﹔

    如果数据集非常巨大,并且CPU时间非常紧张的话,那么这种停止时间甚至可能会长达整整一秒或更久。虽然 AOF重写也需要进行fork(),但无论AOF重写的执行间隔有多长,数据的持久性都不会有任何损失。

2、master和slave同步过程

Redis主从复制分为全量同步和增量同步

2.1 全量复制过程

在这里插入图片描述

首次同步是全量同步,主从同步可以让从服务器从主服务器同步数据,而且从服务器还可再有其它的从服务器,即另外一台redis服务器可以从一台从服务器进行数据同步,redis 的主从同步是非阻塞的,master收到从服务器的psync(2.8版本之前是SYNC)命令,会fork一个子进程在后台执行bgsave命令,并将新写入的数据写入到一个缓冲区中,bgsave执行完成之后,将生成的RDB文件发送给slave,然后master再将缓冲区的内容以redis协议格式再全部发送给slave,slave 先删除旧数据,slave将收到后的RDB文件载入自己的内存,再加载所有收到缓冲区的内容 从而这样一次完整的数据同步Redis全量复制一般发生在Slave首次初始化阶段,这时Slave需要将Master上的所有数据都复制一份。

2.2 增量复制过程

在这里插入图片描述

全量同步之后再次需要同步时,从服务器只要发送当前的offset位置(等同于MySQL的binlog的位置)给主服务器,然后主服务器根据相应的位置将之后的数据(包括写在缓冲区的积压数据)发送给从服务器,再次其保存到其内存即可。

2.3 主从同步完整过程

  1. 从服务器连接主服务器,发送PSYNC命令
  2. 主服务器接收到PSYNC命令后,开始执行BGSAVE命令生成RDB快照文件并使用缓冲区记录此后执行的所有写命令
  3. 主服务器BGSAVE执行完后,向所有从服务器发送RDB快照文件,并在发送期间继续记录被执行的写命令
  4. 从服务器收到快照文件后丢弃所有旧数据,载入收到的快照至内存
  5. 主服务器快照发送完毕后,开始向从服务器发送缓冲区中的写命令
  6. 从服务器完成对快照的载入,开始接收命令请求,并执行来自主服务器缓冲区的写命令
  7. 后期同步会先发送自己slave_repl_offset位置,只同步新增加的数据,不再全量同步

3、哨兵的使用和实现机制

Sentinel(哨兵)进程是用于监控redis集群中Master主服务器工作的状态,在Master主服务器发生故障的时候,可以实现Master和Slave服务器的切换,保证系统的高可用

哨兵(Sentinel) 是一个分布式系统,可以在一个架构中运行多个哨兵(sentinel) 进程,这些进程使用流言协议(gossip protocols)来接收关于Master主服务器是否下线的信息,并使用投票协议(Agreement Protocols)来决定是否执行自动故障迁移,以及选择哪个Slave作为新的Master。每个哨兵(Sentinel)进程会向其它哨兵(Sentinel)、Master、Slave定时发送消息,以确认对方是否”活”着,如果发现对方在指定配置时间(此项可配置)内未得到回应,则暂时认为对方已离线,也就是所谓的”主观认为宕机” (主观:是每个成员都具有的独自的而且可能相同也可能不同的意识),英文名称:Subjective Down,简称SDOWN

主观宕机,对应的有客观宕机。当“哨兵群”中的多数Sentinel进程在对Master主服务器做出SDOWN 的判断,并且通过 SENTINEL is-master-down-by-addr 命令互相交流之后,得出的Master Server下线判断,这种方式就是“客观宕机”(客观:是不依赖于某种意识而已经实际存在的一切事物),英文名称是:Objectively Down, 简称 ODOWN

通过一定的vote算法,从剩下的slave从服务器节点中,选一台提升为Master服务器节点,然后自动修改相关配置,并开启故障转移(failover)

3.1 实现哨兵

在这里插入图片描述

3.1.1 哨兵的准备–实现主从复制架构

哨兵的前提是已经实现了一个redis的主从复制的运行环境,从而实现一个一主两从基于哨兵的高可用redis架构

注意: master 的配置文件中masterauth 和slave 都必须相同

— 所有主从节点的redis.conf中关健配置

master:

[root@master ~]#dnf -y install redis
[root@master ~]#vim /etc/redis.conf
bind 0.0.0.0
masterauth "123456"
requirepass "123456"
#或者非交互执行
[root@master ~]#sed -i -e 's/bind 127.0.0.1/bind 0.0.0.0/' -e 's/^# masterauth .*/masterauth 123456/' -e 's/^# requirepass .*/requirepass 123456/' /etc/redis.conf

[root@master ~]#systemctl enable --now redis

salve 两个slave相同配置

[root@slave ~]#dnf -y install redis
[root@slave ~]#vim /etc/redis.conf
bind 0.0.0.0
masterauth "123456"
requirepass "123456"
replicaof 10.0.0.8 6379
#或者非交互执行
[root@slave ~]#sed -i -e 's/bind 127.0.0.1/bind 0.0.0.0/' -e 's/^# masterauth .*/masterauth 123456/' -e 's/^# requirepass .*/requirepass 123456/' -e '/^# replicaof/a replicaof 10.0.0.8 6379' /etc/redis.conf

[root@slave ~]#systemctl enable --now redis

3.1.2 编辑哨兵的配置文件

sentinel配置

Sentinel实际上是一个特殊的redis服务器,有些redis指令支持,但很多指令并不支持.默认监听在26379/tcp端口.

哨兵可以不和Redis服务器部署在一起,但一般部署在一起,所有redis节点使用相同的以下示例的配置文件

三个哨兵服务器的配置都如下:

[root@redis-master ~]#grep -vE "^#|^$" /etc/redis-sentinel.conf 
port 26379
daemonize no
pidfile "/var/run/redis-sentinel.pid"
logfile "/var/log/redis/sentinel.log"
dir "/tmp"
sentinel monitor mymaster 10.0.0.8 6379 2      #修改此行
sentinel auth-pass mymaster 123456             #增加此行
sentinel down-after-milliseconds mymaster 3000 #修改此行
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes
#以下内容自动生成,不需要修改
sentinel myid 50547f34ed71fd48c197924969937e738a39975b  #此行自动生成必须唯一,修改此值需重启redis和sentinel服务
.....
# Generated by CONFIG REWRITE
protected-mode no
supervised systemd
sentinel leader-epoch mymaster 0
sentinel known-replica mymaster 10.0.0.28 6379
sentinel known-replica mymaster 10.0.0.18 6379
sentinel current-epoch 0

[root@redis-master ~]#scp /etc/redis-sentinel.conf redis-slave1:/etc/
[root@redis-master ~]#scp /etc/redis-sentinel.conf redis-slave2:/etc/

3.1.3 启动哨兵

三台哨兵服务器都要启动

#确保每个哨兵主机myid不同
[root@redis-master ~]#vim /etc/redis-sentinel.conf
sentinel myid 309830f0ec8a20bafe9edc80c409d37e7e3fd731

[root@redis-slave1 ~]#vim /etc/redis-sentinel.conf
sentinel myid 2a6b1659eb936299f8899d936bc53a8deb07f635

[root@redis-slave2 ~]#vim /etc/redis-sentinel.conf
sentinel myid f3732cf910a9c1e7ca5c171102dc1468bbf46f3

[root@redis-master ~]#systemctl enable --now redis-sentinel.service
[root@redis-slave1 ~]#systemctl enable --now redis-sentinel.service
[root@redis-slave2 ~]#systemctl enable --now redis-sentinel.service

3.1.4 验证哨兵端口

[root@redis-master ~]#ss -ntl
State       Recv-Q       Send-Q     Local Address:Port     Peer Address:Port   
LISTEN       0             128               0.0.0.0:22            0.0.0.0:* 
LISTEN       0             128               0.0.0.0:26379         0.0.0.0:*   
LISTEN       0             128               0.0.0.0:6379          0.0.0.0:*   
LISTEN       0             128                  [::]:22               [::]:*   
LISTEN       0             128                  [::]:26379            [::]:*   
LISTEN       0             128                  [::]:6379             [::]:*

3.1.5 查看各节点上哨兵信息:

[root@redis-master ~]#redis-cli -a 123456 -p 26379
Warning: Using a password with '-a' or '-u' option on the command line interface 
may not be safe.
127.0.0.1:26379> INFO sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=10.0.0.18:6379,slaves=2,sentinels=3#两个slave,三个sentinel服务器,如果sentinels值不符合,检查myid可能冲突

当master宕机之后,将会发生故障转移,sentinel 会选择出一个节点提升为新master,故障转移之后, 原来的master重新启动,会自动加入集群成为新master的从节点,而且配置文件也会自动修改,无需人工手动修改

4、redis cluster集群创建和使用(基于Redis 5 的 redis cluster 部署)

4.1 创建 redis cluster集群的环境准备

  1. 每个redis 节点采用相同的硬件配置、相同的密码、相同的redis版本

  2. 所有redis服务器必须没有任何数据

  3. 准备六台主机,地址如下:

    redis-node1 : 10.0.0.8
    redis-node2 : 10.0.0.18
    redis-node3 : 10.0.0.28
    redis-node4 : 10.0.0.38
    redis-node5 : 10.0.0.48
    redis-node6 : 10.0.0.58
    

4.2 启用 redis cluster 配置

所有6台主机都执行以下配置:

yum -y install redis

每个节点修改redis配置,必须开启cluster功能的参数 :

#手动修改配置文件
[root@redis-node1 ~]vim /etc/redis.conf
bind 0.0.0.0
masterauth 123456   #建议配置,否则后期的master和slave主从复制无法成功,还需再配置
requirepass 123456
cluster-enabled yes #取消此行注释,必须开启集群,开启后redis 进程会有cluster显示
cluster-config-file nodes-6379.conf #取消此行注释,此为集群状态文件,记录主从关系及slot范围信息,由redis cluster 集群自动创建和维护
cluster-require-full-coverage no   #默认值为yes,设为no可以防止一个节点不可用导致整个cluster不可能


#或者执行下面命令,批量修改
[root@redis-node1 ~]#sed -i.bak -e 's/bind 127.0.0.1/bind 0.0.0.0/' -e '/masterauth/a masterauth 123456' -e '/# requirepass/a requirepass 123456' -e '/# cluster-enabled yes/a cluster-enabled yes' -e '/# cluster-config-file nodes-6379.conf/a cluster-config-file nodes-6379.conf' -e '/cluster-require-full-coverage yes/c cluster-require-full-coverage no' /etc/redis.conf


[root@redis-node1 ~]#systemctl enable --now redis

验证当前Redis服务状态:

#开启了16379的cluster的端口,实际的端口=redis port + 10000
[root@centos8 ~]#ss -ntl
State       Recv-Q       Send-Q Local Address:Port     Peer Address:Port  
LISTEN       0             128           0.0.0.0:22            0.0.0.0:* 
LISTEN       0             100         127.0.0.1:25            0.0.0.0:*     
LISTEN       0             128           0.0.0.0:16379         0.0.0.0:*
LISTEN       0             128           0.0.0.0:6379          0.0.0.0:*
LISTEN       0             128              [::]:22               [::]:* 
LISTEN       0             100             [::1]:25               [::]:*     
 
 
#注意进程有[cluster]状态
[root@centos8 ~]#ps -ef|grep redis
redis   1939    1  0 10:54 ?    00:00:00 /usr/bin/redis-server 0.0.0.0:6379 [cluster]
root    1955   1335  0 10:57 pts/0    00:00:00 grep --color=auto redis

4.3 创建集群

# redis-cli --cluster-replicas 1 表示每个master对应一个slave节点
#自动的将前三个主机作为master,后三个依次对应成为前三个master的slave
[root@redis-node1 ~]#redis-cli -a 123456 --cluster create 10.0.0.8:6379 10.0.0.18:6379 10.0.0.28:6379 10.0.0.38:6379 10.0.0.48:6379 10.0.0.58:6379 --cluster-replicas 1

Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 10.0.0.38:6379 to 10.0.0.8:6379
Adding replica 10.0.0.48:6379 to 10.0.0.18:6379
Adding replica 10.0.0.58:6379 to 10.0.0.28:6379
M: 7b920bd4dc8defb2527ea19301ef124478cb1905 10.0.0.8:6379   #带M的为master 
   slots:[0-5460] (5461 slots) master   #当前master的槽位起始和结束位
M: 9630712594995a6285e30d215fafdba3d7c992ab 10.0.0.18:6379
   slots:[5461-10922] (5462 slots) master
M: b73d40c3448b846a83e254fb6f7da44caf973a63 10.0.0.28:6379
   slots:[10923-16383] (5461 slots) master
S: e69a5ec778801215d5f6737c8aa2ad51958ccf78 10.0.0.38:6379   #带S的为slave
   replicates 7b920bd4dc8defb2527ea19301ef124478cb1905
S: 21dfbd69383e450b437d642c3092481ce9cd04c2 10.0.0.48:6379
   replicates 9630712594995a6285e30d215fafdba3d7c992ab
S: c5663645f159d0b01bbbe5531441be20cc55e75b 10.0.0.58:6379
   replicates b73d40c3448b846a83e254fb6f7da44caf973a63
Can I set the above configuration? (type 'yes' to accept): yes#输入yes自动创建集群
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
....
>>> Performing Cluster Check (using node 10.0.0.8:6379)
M: 7b920bd4dc8defb2527ea19301ef124478cb1905 10.0.0.8:6379
   slots:[0-5460] (5461 slots) master   #已经分配的槽位
   1 additional replica(s)				#分配了一个slave
S: 21dfbd69383e450b437d642c3092481ce9cd04c2 10.0.0.48:6379
   slots: (0 slots) slave				#slave没有分配槽位
   replicates 9630712594995a6285e30d215fafdba3d7c992ab  #对应的master的10.0.0.28的ID(redis-cli -a 123456 cluster nodes可查看每个节点ID)
M: b73d40c3448b846a83e254fb6f7da44caf973a63 10.0.0.28:6379
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
M: 9630712594995a6285e30d215fafdba3d7c992ab 10.0.0.18:6379
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: c5663645f159d0b01bbbe5531441be20cc55e75b 10.0.0.58:6379
   slots: (0 slots) slave
   replicates b73d40c3448b846a83e254fb6f7da44caf973a63  #对应的master的10.0.0.18的ID
S: e69a5ec778801215d5f6737c8aa2ad51958ccf78 10.0.0.38:6379  
   slots: (0 slots) slave
   replicates 7b920bd4dc8defb2527ea19301ef124478cb1905  #对应的master的10.0.0.8的ID
[OK] All nodes agree about slots configuration.  #所有节点槽位分配完成
>>> Check for open slots...     #检查打开的槽位
>>> Check slots coverage...     #检查插槽覆盖范围
[OK] All 16384 slots covered.   #所有槽位(16384个)分配完成



#观察以上结果,可以看到3组master/slave
master:10.0.0.8  --- slave:10.0.0.38
master:10.0.0.18 --- slave:10.0.0.48
master:10.0.0.28 --- slave:10.0.0.58

4.4 查看主从状态

[root@redis-node1 ~]#redis-cli -a 123456 -c INFO replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:master
connected_slaves:1
slave0:ip=10.0.0.38,port=6379,state=online,offset=1512,lag=0
master_replid:9494342864b0f715b417d3967403b46675c51b8f
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1512
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:1512


[root@redis-node2 ~]#redis-cli -a 123456 -c INFO replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:master
connected_slaves:1
slave0:ip=10.0.0.48,port=6379,state=online,offset=1582,lag=1
master_replid:8eac8f1eb94a80931a9228dd58007c454bc6919a
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1582
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:1582


[root@redis-node3 ~]#redis-cli -a 123456 -c INFO replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:master
connected_slaves:1
slave0:ip=10.0.0.58,port=6379,state=online,offset=1582,lag=1
master_replid:572ab8caad8f1e5b3f5860558489fcfc3bdb66f6
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1582
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:1582


[root@redis-node4 ~]#redis-cli -a 123456 -c INFO replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:slave
master_host:10.0.0.8
master_port:6379
master_link_status:up
master_last_io_seconds_ago:2
master_sync_in_progress:0
slave_repl_offset:1582
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:9494342864b0f715b417d3967403b46675c51b8f
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1582
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:1582


[root@redis-node5 ~]#redis-cli -a 123456 -c INFO replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:slave
master_host:10.0.0.18
master_port:6379
master_link_status:up
master_last_io_seconds_ago:2
master_sync_in_progress:0
slave_repl_offset:1582
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:8eac8f1eb94a80931a9228dd58007c454bc6919a
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1582
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:1582


[root@redis-node6 ~]#redis-cli -a 123456 -c INFO replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:slave
master_host:10.0.0.28
master_port:6379
master_link_status:up
master_last_io_seconds_ago:3
master_sync_in_progress:0
slave_repl_offset:1582
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:572ab8caad8f1e5b3f5860558489fcfc3bdb66f6
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1582
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:1582

4.5 查看集群node对应关系和master对应的slave信息

[root@redis-node1 ~]#redis-cli  -a 123456 cluster nodes
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
9630712594995a6285e30d215fafdba3d7c992ab 10.0.0.18:6379@16379 master - 0 1603614844000 2 connected 5461-10922
e69a5ec778801215d5f6737c8aa2ad51958ccf78 10.0.0.38:6379@16379 slave 7b920bd4dc8defb2527ea19301ef124478cb1905 0 1603614845080 4 connected
7b920bd4dc8defb2527ea19301ef124478cb1905 10.0.0.8:6379@16379 master - 0 1603614846098 1 connected 0-5460
21dfbd69383e450b437d642c3092481ce9cd04c2 10.0.0.48:6379@16379 slave 9630712594995a6285e30d215fafdba3d7c992ab 0 1603614845000 5 connected
b73d40c3448b846a83e254fb6f7da44caf973a63 10.0.0.28:6379@16379 master - 0 1603614844062 3 connected 10923-16383
c5663645f159d0b01bbbe5531441be20cc55e75b 10.0.0.58:6379@16379 myself,slave b73d40c3448b846a83e254fb6f7da44caf973a63 0 1603614843000 6 connected



#查看指定master节点的slave节点信息,其中7b920bd4dc8defb2527ea19301ef124478cb1905 为master节点的ID
[root@redis-node1 ~]#redis-cli -a 123456 cluster slaves 7b920bd4dc8defb2527ea19301ef124478cb1905
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
1) "e69a5ec778801215d5f6737c8aa2ad51958ccf78 10.0.0.38:6379@16379 slave 7b920bd4dc8defb2527ea19301ef124478cb1905 0 1603615471078 4 connected"

4.6 验证集群状态

[root@redis-node1 ~]#redis-cli -a 123456 cluster  info
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6 	#节点数
cluster_size:3			#三个集群
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:2033
cluster_stats_messages_pong_sent:2049
cluster_stats_messages_sent:4082
cluster_stats_messages_ping_received:2044
cluster_stats_messages_pong_received:2033
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:4082


#查看任意节点的集群状态
[root@redis-node1 ~]#redis-cli -a 123456 --cluster info 10.0.0.18:6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
10.0.0.18:6379 (96307125...) -> 0 keys | 5462 slots | 1 slaves.
10.0.0.8:6379 (7b920bd4...) -> 0 keys | 5461 slots | 1 slaves.
10.0.0.28:6379 (b73d40c3...) -> 0 keys | 5461 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.

[root@redis-node1 ~]#redis-cli -a 123456 --cluster info 10.0.0.58:6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
10.0.0.18:6379 (96307125...) -> 0 keys | 5462 slots | 1 slaves.
10.0.0.8:6379 (7b920bd4...) -> 0 keys | 5461 slots | 1 slaves.
10.0.0.28:6379 (b73d40c3...) -> 0 keys | 5461 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.

4.7 验证集群写入key

经过算法计算,得到当前key的槽位,需要写入指定的node

— redis cluster 计算key所属的槽位(slot)

#计算的到hello对应槽位为886
[root@redis-node1 ~]#redis-cli -h 10.0.0.8 -a 123456 --no-auth-warning cluster keyslot hello
(integer) 866   


#886槽位位于node1(10.0.0.8),只能在node1写入,其他节点无法写入
[root@redis-node1 ~]#redis-cli -h 10.0.0.8 -a 123456 --no-auth-warning set hello magedu
OK
[root@redis-node1 ~]#redis-cli -h 10.0.0.18 -a 123456 --no-auth-warning set hello magedu
(error) MOVED 866 10.0.0.8:6379

当集群中任意master节点故障宕机,对应的slave节点会自动提升为新的master

经过算法计算得到当前key的槽位,需要写入指定的node

— redis cluster 计算key所属的槽位(slot)

#计算的到hello对应槽位为886
[root@redis-node1 ~]#redis-cli -h 10.0.0.8 -a 123456 --no-auth-warning cluster keyslot hello
(integer) 866   


#886槽位位于node1(10.0.0.8),只能在node1写入,其他节点无法写入
[root@redis-node1 ~]#redis-cli -h 10.0.0.8 -a 123456 --no-auth-warning set hello magedu
OK
[root@redis-node1 ~]#redis-cli -h 10.0.0.18 -a 123456 --no-auth-warning set hello magedu
(error) MOVED 866 10.0.0.8:6379

当集群中任意master节点故障宕机,对应的slave节点会自动提升为新的master

经过修复,恢复之前故障的master,其会自动成为slave节点

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值