Redis-集群(cluster)

Redis-集群(cluster)

介绍

什么是redis集群

由于数据量过大单个Master复制集难以承担,因此需要对多个复制集进行集群,形成水平扩展每个复制集只负责存储整个数据集

的一部分,这就是Redis的集群,其作用是提供在多个Redis节点间共享数据的程序集。

有哪些功能?

redis集群支持多个master,每个master又可以挂在多个slave。具有读写分离、数据高可用的效果。

由于集群自带了哨兵的故障转移机制,内置了高可用的支持,所以使用集群无需再去使用哨兵功能。

客户端与redis的节点连接,不再需要连接集群中所有的节点,只需要任意连接集群中的一个可用节点即可。

槽位slot负责分配到各个物理服务节点,由对应的集群来负责维护节点,插槽和数据之间的关系。

集群算法-分片-分片与槽位slot

 

分片与槽位slot的优势

为什redis集群的最大槽数时16384个

正常的心跳数据包带有节点的完整配置,可以用幂等方式用旧的节点替换旧节点,以便更新旧的配置。

这意味着它们包含原始节点的插槽配置,该节点使用2k的空间和16k的插槽,但是会使用8k的空间(使用65k的插槽)。
同时,由于其他设计折衷,Redis集群不太可能扩展到1000个以上的主节点。
因此16k处于正确的范围内,以确保每个主机具有足够的插槽,最多可容纳1000个矩阵,但数量足够少,可以轻松地将插槽配置作为原始位图传播。请注意,在小型群集中,位图将难以压缩,因为当N较小时,位图将设置的slot / N位占设置位的很大百分比。
 

(1)如果槽位为65536,发送心跳信息的消息头达8k,发送的心跳包过于庞大。

在消息头中最占空间的是myslots[CLUSTER_SLOTS/8]。 当槽位为65536时,这块的大小是: 65536÷8÷1024=8kb 

在消息头中最占空间的是myslots[CLUSTER_SLOTS/8]。 当槽位为16384时,这块的大小是: 16384÷8÷1024=2kb 

因为每秒钟,redis节点需要发送一定数量的ping消息作为心跳包,如果槽位为65536,这个ping消息的消息头太大了,浪费带宽。

 

(2)redis的集群主节点数量基本不可能超过1000个。

集群节点越多,心跳包的消息体内携带的数据越多。如果节点过1000个,也会导致网络拥堵。因此redis作者不建议redis cluster节点数量超过1000个。 那么,对于节点数在1000以内的redis cluster集群,16384个槽位够用了。没有必要拓展到65536个。

 

(3)槽位越小,节点少的情况下,压缩比高,容易传输

Redis主节点的配置信息中它所负责的哈希槽是通过一张bitmap的形式来保存的,在传输过程中会对bitmap进行压缩,但是如果bitmap的填充率slots / N很高的话(N表示节点数),bitmap的压缩率就很低。 如果节点数很少,而哈希槽数量很多的话,bitmap的压缩率就很低。

槽位映射的发展

第一阶段:哈希取余分区

第二阶段:一致性哈希算法分区

 

第三阶段:哈希槽分区

配置集群

前置条件:

三台虚拟机,各自新建:mkdir-p /myredis/cluster

配置6个独立redis服务,配置文件展示:

bind 0.0.0.0
daemonize yes
protected-mode no
port 6381
logfile "/myredis/cluster/cluster6381.log"
pidfile /myredis/cluster6381.pid
dir /myredis/cluster
dbfilename dump6381.rdb
appendonly yes
appendfilename "appendonly6381.aof"
requirepass 111111
masterauth 111111
 
cluster-enabled yes
cluster-config-file nodes-6381.conf
cluster-node-timeout 5000

配置如上,记得修改端口。

遇到的报错:

[root@192 cluster]# redis-server /myredis/cluster/redis-6381.conf 

*** FATAL CONFIG FILE ERROR (Redis 7.0.0) ***
replicaof directive not allowed in cluster mode

哨兵在配置文件中追加了replicaof相关的内容,将哨兵追加的内容删除后重新启动。

构建集群

[root@192 cluster]# redis-cli -a 111111 --cluster create --cluster-replicas 1 192.168.1.11:6381 192.168.1.11:6382 192.168.1.12:6383 192.168.1.12:6384 192.168.1.13:6385 192.168.1.13:6386 # 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 192.168.1.12:6384 to 192.168.1.11:6381
Adding replica 192.168.1.13:6386 to 192.168.1.12:6383
Adding replica 192.168.1.11:6382 to 192.168.1.13:6385
M: 491bb2df7fa39bafd917f7aaa0a409e19e41b07f 192.168.1.11:6381
   slots:[0-5460] (5461 slots) master
S: cc9a9f6bad23314263e2b89e4beede4fc59c3ef3 192.168.1.11:6382
   replicates 95d8800931e38739a85d6face5296d719a8d5bfa
M: 3075d51754554aa421ee23ccbd8e263c0d5c4f0e 192.168.1.12:6383
   slots:[5461-10922] (5462 slots) master
S: 4a60743d7b067dfd03c6a0d4403c88ce173abb1c 192.168.1.12:6384
   replicates 491bb2df7fa39bafd917f7aaa0a409e19e41b07f
M: 95d8800931e38739a85d6face5296d719a8d5bfa 192.168.1.13:6385
   slots:[10923-16383] (5461 slots) master
S: 458cf2edf6cc3005cee4fc3aa93278efa86637ec 192.168.1.13:6386
   replicates 3075d51754554aa421ee23ccbd8e263c0d5c4f0e
Can I set the above configuration? (type 'yes' to accept): yes #2输入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 192.168.1.11:6381)
M: 491bb2df7fa39bafd917f7aaa0a409e19e41b07f 192.168.1.11:6381
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 458cf2edf6cc3005cee4fc3aa93278efa86637ec 192.168.1.13:6386
   slots: (0 slots) slave
   replicates 3075d51754554aa421ee23ccbd8e263c0d5c4f0e
S: cc9a9f6bad23314263e2b89e4beede4fc59c3ef3 192.168.1.11:6382
   slots: (0 slots) slave
   replicates 95d8800931e38739a85d6face5296d719a8d5bfa
M: 95d8800931e38739a85d6face5296d719a8d5bfa 192.168.1.13:6385
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 4a60743d7b067dfd03c6a0d4403c88ce173abb1c 192.168.1.12:6384
   slots: (0 slots) slave
   replicates 491bb2df7fa39bafd917f7aaa0a409e19e41b07f
M: 3075d51754554aa421ee23ccbd8e263c0d5c4f0e 192.168.1.12:6383
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered. ##3到这里构建成功。三主三从
[root@192 cluster]# 

 我这里的关系图是这样的

1638个槽位分别对应哪几个主机

场景演示

set一个值试试

127.0.0.1:6381> cluster keyslot k1 # 查看“k1”这个key对应的槽位
(integer) 12706 # 12706对应的是6385这台主机

 

我们在6381上set k1 v1

127.0.0.1:6381> set k1 v1 
(error) MOVED 12706 192.168.1.13:6385 ###报错
127.0.0.1:6381> 

 

报错原因:需要路由到位

如何解决:连接redis时增加-c参数,优化路由

[root@192 cluster]# redis-cli -a 111111 -p 6381 -c ###连接时添加-c优化路由
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6381> set k1 v1 # set成功
-> Redirected to slot [12706] located at 192.168.1.13:6385
OK

 

 

一、容错测试:6381这台master挂了,对应的slave是否会变成master

1.查看关系

127.0.0.1:6381> info replication # 查看关系
# Replication 
role:master
connected_slaves:1
slave0:ip=192.168.1.12,port=6384,state=online,offset=17286,lag=0 #6384为6381的slave
master_failover_state:no-failover
master_replid:4d3c3673de89c3c84a199d59c70ae6cf245742fc
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:17286
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:17286

 

2.shutdown 6381

127.0.0.1:6381> shutdown
not connected> quit

 

3.登录集群内任意主机,执行cluster nodes命令查看集群关系:6381已经fail,6384成为新的master

 二、6381重新登录之后,是成为master还是slave?--------slave身份

[root@192 cluster]# redis-server redis-6381.conf  # 启动6381
[root@192 cluster]# redis-cli -a 111111 -p 6381 -c @登录
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6381> cluster nodes # 查看集群关系
3075d51754554aa421ee23ccbd8e263c0d5c4f0e 192.168.1.12:6383@16383 master - 0 1706383916749 3 connected 5461-10922
4a60743d7b067dfd03c6a0d4403c88ce173abb1c 192.168.1.12:6384@16384 master - 0 1706383917558 7 connected 0-5460
95d8800931e38739a85d6face5296d719a8d5bfa 192.168.1.13:6385@16385 master - 0 1706383917558 5 connected 10923-16383
491bb2df7fa39bafd917f7aaa0a409e19e41b07f 192.168.1.11:6381@16381 myself,slave 4a60743d7b067dfd03c6a0d4403c88ce173abb1c 0 1706383917000 7 connected # 6381还是slave
458cf2edf6cc3005cee4fc3aa93278efa86637ec 192.168.1.13:6386@16386 slave 3075d51754554aa421ee23ccbd8e263c0d5c4f0e 0 1706383916000 3 connected
cc9a9f6bad23314263e2b89e4beede4fc59c3ef3 192.168.1.11:6382@16382 slave 95d8800931e38739a85d6face5296d719a8d5bfa 0 1706383917759 5 connected

 

 

三、有主机宕机后,集群的关系与当初设计时不一致了。关系被打乱。如何恢复?

以当前为例。6381的master被6384顶替了。需要登录6381之后命令:cluster failover

127.0.0.1:6381> cluster failover # 将当前主机置为master
OK
127.0.0.1:6381> cluster nodes
3075d51754554aa421ee23ccbd8e263c0d5c4f0e 192.168.1.12:6383@16383 master - 0 1706384388767 3 connected 5461-10922
4a60743d7b067dfd03c6a0d4403c88ce173abb1c 192.168.1.12:6384@16384 slave 491bb2df7fa39bafd917f7aaa0a409e19e41b07f 0 1706384389000 8 connected
95d8800931e38739a85d6face5296d719a8d5bfa 192.168.1.13:6385@16385 master - 0 1706384389573 5 connected 10923-16383
491bb2df7fa39bafd917f7aaa0a409e19e41b07f 192.168.1.11:6381@16381 myself,master - 0 1706384389000 8 connected 0-5460
458cf2edf6cc3005cee4fc3aa93278efa86637ec 192.168.1.13:6386@16386 slave 3075d51754554aa421ee23ccbd8e263c0d5c4f0e 0 1706384388057 3 connected
cc9a9f6bad23314263e2b89e4beede4fc59c3ef3 192.168.1.11:6382@16382 slave 95d8800931e38739a85d6face5296d719a8d5bfa 0 1706384389775 5 connected

 

四、扩容与缩容测试---扩容

1.复制两份配置文件,分别将端口号替换为6387和6388

2.启动两个新节点实例,此时他们都是master

[root@192 cluster]# redis-server redis-6387.conf  # 启动两个实例
[root@192 cluster]# redis-server redis-6388.conf 
[root@192 cluster]# redis-cli -a 111111 -p 6387 # 登录一个做演示
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6387> info replication # 查看关系
# Replication
role:master ###master
connected_slaves:0
master_failover_state:no-failover
master_replid:ccf0e3ef0a7ea4c965d4f1298518686b5e748a7c
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0repl_backlog_histlen:0

 

3.将新增的6387(空槽号)作为master节点加入原来的集群:命令:redis-cli -a 密码 --cluster add-node 要加入的节点ip:端口 已经在集群中的节点ip:端口(相当于介绍人)

[root@192 cluster]# redis-cli -a 111111 --cluster add-node 192.168.1.13:6387 192.168.1.11:6381
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Adding node 192.168.1.13:6387 to cluster 192.168.1.11:6381
>>> Performing Cluster Check (using node 192.168.1.11:6381)
M: 491bb2df7fa39bafd917f7aaa0a409e19e41b07f 192.168.1.11:6381
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: 3075d51754554aa421ee23ccbd8e263c0d5c4f0e 192.168.1.12:6383
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 4a60743d7b067dfd03c6a0d4403c88ce173abb1c 192.168.1.12:6384
   slots: (0 slots) slave
   replicates 491bb2df7fa39bafd917f7aaa0a409e19e41b07f
M: 95d8800931e38739a85d6face5296d719a8d5bfa 192.168.1.13:6385
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 458cf2edf6cc3005cee4fc3aa93278efa86637ec 192.168.1.13:6386
   slots: (0 slots) slave
   replicates 3075d51754554aa421ee23ccbd8e263c0d5c4f0e
S: cc9a9f6bad23314263e2b89e4beede4fc59c3ef3 192.168.1.11:6382
   slots: (0 slots) slave
   replicates 95d8800931e38739a85d6face5296d719a8d5bfa
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Getting functions from cluster
>>> Send FUNCTION LIST to 192.168.1.13:6387 to verify there is no functions in it
>>> Send FUNCTION RESTORE to 192.168.1.13:6387
>>> Send CLUSTER MEET to node 192.168.1.13:6387 to make it join the cluster.
[OK] New node added correctly.
[root@192 cluster]# 

 

4.检查集群的情况

 

5.给新的节点匀一些槽号。命令:redis-cli -a 密码 --cluster reshard IP地址:端口号

[root@192 cluster]# redis-cli -a 111111 --cluster reshard 192.168.1.11:6381 # ip端口为集群内的任意实例
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing Cluster Check (using node 192.168.1.11:6381)
M: 491bb2df7fa39bafd917f7aaa0a409e19e41b07f 192.168.1.11:6381
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: 3075d51754554aa421ee23ccbd8e263c0d5c4f0e 192.168.1.12:6383
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
M: 2f1ed0f391fa1e0a5b3fac8e5d3c669c811ac283 192.168.1.13:6387
   slots: (0 slots) master
S: 4a60743d7b067dfd03c6a0d4403c88ce173abb1c 192.168.1.12:6384
   slots: (0 slots) slave
   replicates 491bb2df7fa39bafd917f7aaa0a409e19e41b07f
M: 95d8800931e38739a85d6face5296d719a8d5bfa 192.168.1.13:6385
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 458cf2edf6cc3005cee4fc3aa93278efa86637ec 192.168.1.13:6386
   slots: (0 slots) slave
   replicates 3075d51754554aa421ee23ccbd8e263c0d5c4f0e
S: cc9a9f6bad23314263e2b89e4beede4fc59c3ef3 192.168.1.11:6382
   slots: (0 slots) slave
   replicates 95d8800931e38739a85d6face5296d719a8d5bfa
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
How many slots do you want to move (from 1 to 16384)? 4096 ###匀多少槽号,案例匀了4096个
What is the receiving node ID? 2f1ed0f391fa1e0a5b3fac8e5d3c669c811ac283 ###匀给谁,这里需要填目标节点ID(6387的ID)
Please enter all the source node IDs.
  Type 'all' to use all the nodes as source nodes for the hash slots.
  Type 'done' once you entered all the source nodes IDs.
Source node #1: all ###这里填all

Ready to move 4096 slots.
  Source nodes:
    M: 491bb2df7fa39bafd917f7aaa0a409e19e41b07f 192.168.1.11:6381
       slots:[0-5460] (5461 slots) master
       1 additional replica(s)
    M: 3075d51754554aa421ee23ccbd8e263c0d5c4f0e 192.168.1.12:6383
       slots:[5461-10922] (5462 slots) master
       1 additional replica(s)
    M: 95d8800931e38739a85d6face5296d719a8d5bfa 192.168.1.13:6385
       slots:[10923-16383] (5461 slots) master
       1 additional replica(s)
  Destination node:
    M: 2f1ed0f391fa1e0a5b3fac8e5d3c669c811ac283 192.168.1.13:6387
       slots: (0 slots) master
  Resharding plan:
    Moving slot 5461 from 3075d51754554aa421ee23ccbd8e263c0d5c4f0e
    Moving slot 5462 from 3075d51754554aa421ee23ccbd8e263c0d5c4f0e
    Moving slot 5463 from 3075d51754554aa421ee23ccbd8e263c0d5c4f0e
    Moving slot 5464 from 3075d51754554aa421ee23ccbd8e263c0d5c4f0e
.......等

 

6.再次查看集群情况

 问题:为什么6387是3个新的区间,以前的还是连续?

重新分配成本太高,所以前3家各自匀出来一部分,从6381/6383/6385三个旧节点分别匀出1364个坑位给新节点6387

7.为6387分配从节点6388。命令:redis-cli -a 密码 --cluster add-node ip:新slave端口 ip:新master端口 --cluster-slave --cluster-master-id 新master主机节点ID

 

[root@192 cluster]# redis-cli -a 111111 --cluster add-node 192.168.1.13:6388 192.168.1.13:6387 --cluster-slave --cluster-master-id 2f1ed0f391fa1e0a5b3fac8e5d3c669c811ac283
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Adding node 192.168.1.13:6388 to cluster 192.168.1.13:6387
>>> Performing Cluster Check (using node 192.168.1.13:6387)
M: 2f1ed0f391fa1e0a5b3fac8e5d3c669c811ac283 192.168.1.13:6387
   slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master
M: 491bb2df7fa39bafd917f7aaa0a409e19e41b07f 192.168.1.11:6381
   slots:[1365-5460] (4096 slots) master
   1 additional replica(s)
M: 95d8800931e38739a85d6face5296d719a8d5bfa 192.168.1.13:6385
   slots:[12288-16383] (4096 slots) master
   1 additional replica(s)
S: cc9a9f6bad23314263e2b89e4beede4fc59c3ef3 192.168.1.11:6382
   slots: (0 slots) slave
   replicates 95d8800931e38739a85d6face5296d719a8d5bfa
M: 3075d51754554aa421ee23ccbd8e263c0d5c4f0e 192.168.1.12:6383
   slots:[6827-10922] (4096 slots) master
   1 additional replica(s)
S: 4a60743d7b067dfd03c6a0d4403c88ce173abb1c 192.168.1.12:6384
   slots: (0 slots) slave
   replicates 491bb2df7fa39bafd917f7aaa0a409e19e41b07f
S: 458cf2edf6cc3005cee4fc3aa93278efa86637ec 192.168.1.13:6386
   slots: (0 slots) slave
   replicates 3075d51754554aa421ee23ccbd8e263c0d5c4f0e
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 192.168.1.13:6388 to make it join the cluster.
Waiting for the cluster to join

>>> Configure node as replica of 192.168.1.13:6387.
[OK] New node added correctly.
[root@192 cluster]# 

 

8.第三次检查集群情况

 结束

五、扩容与缩容测试---缩容

1.先查看集群的情况,获得6388的ID

[root@192 cluster]# redis-cli -a 111111 --cluster check 192.168.1.13:6387
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.1.13:6387 (2f1ed0f3...) -> 1 keys | 4096 slots | 1 slaves.
192.168.1.11:6381 (491bb2df...) -> 0 keys | 4096 slots | 1 slaves.
192.168.1.13:6385 (95d88009...) -> 1 keys | 4096 slots | 1 slaves.
192.168.1.12:6383 (3075d517...) -> 0 keys | 4096 slots | 1 slaves.
[OK] 2 keys in 4 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.1.13:6387)
M: 2f1ed0f391fa1e0a5b3fac8e5d3c669c811ac283 192.168.1.13:6387
   slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master
   1 additional replica(s)
S: cc9a9f6bad23314263e2b89e4beede4fc59c3ef3 192.168.1.11:6382
   slots: (0 slots) slave
   replicates 95d8800931e38739a85d6face5296d719a8d5bfa
M: 491bb2df7fa39bafd917f7aaa0a409e19e41b07f 192.168.1.11:6381
   slots:[1365-5460] (4096 slots) master
   1 additional replica(s)
M: 95d8800931e38739a85d6face5296d719a8d5bfa 192.168.1.13:6385
   slots:[12288-16383] (4096 slots) master
   1 additional replica(s)
S: 458cf2edf6cc3005cee4fc3aa93278efa86637ec 192.168.1.13:6386
   slots: (0 slots) slave
   replicates 3075d51754554aa421ee23ccbd8e263c0d5c4f0e
S: b01ff575c1e793617332d8786c61bb2a3d1bd290 192.168.1.13:6388
   slots: (0 slots) slave
   replicates 2f1ed0f391fa1e0a5b3fac8e5d3c669c811ac283
S: 4a60743d7b067dfd03c6a0d4403c88ce173abb1c 192.168.1.12:6384
   slots: (0 slots) slave
   replicates 491bb2df7fa39bafd917f7aaa0a409e19e41b07f
M: 3075d51754554aa421ee23ccbd8e263c0d5c4f0e 192.168.1.12:6383
   slots:[6827-10922] (4096 slots) master
   1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@192 cluster]# 

 

2.删除6388节点。命令:redis-cli -a 密码 --cluster del-node 被删ip:端口 被删节点ID

[root@192 cluster]# redis-cli -a 111111 --cluster del-node 192.168.1.13:6388 b01ff575c1e793617332d8786c61bb2a3d1bd290
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Removing node b01ff575c1e793617332d8786c61bb2a3d1bd290 from cluster 192.168.1.13:6388
>>> Sending CLUSTER FORGET messages to the cluster...
>>> Sending CLUSTER RESET SOFT to the deleted node.
[root@192 cluster]# 

 

3.清空6387的槽号,重新分配。当前案例将槽号都分配给6381

[root@192 cluster]# redis-cli -a 111111 --cluster reshard 192.168.1.11:6381 ###
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing Cluster Check (using node 192.168.1.11:6381)
M: 491bb2df7fa39bafd917f7aaa0a409e19e41b07f 192.168.1.11:6381
   slots:[1365-5460] (4096 slots) master
   1 additional replica(s)
M: 3075d51754554aa421ee23ccbd8e263c0d5c4f0e 192.168.1.12:6383
   slots:[6827-10922] (4096 slots) master
   1 additional replica(s)
M: 2f1ed0f391fa1e0a5b3fac8e5d3c669c811ac283 192.168.1.13:6387
   slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master
S: 4a60743d7b067dfd03c6a0d4403c88ce173abb1c 192.168.1.12:6384
   slots: (0 slots) slave
   replicates 491bb2df7fa39bafd917f7aaa0a409e19e41b07f
M: 95d8800931e38739a85d6face5296d719a8d5bfa 192.168.1.13:6385
   slots:[12288-16383] (4096 slots) master
   1 additional replica(s)
S: 458cf2edf6cc3005cee4fc3aa93278efa86637ec 192.168.1.13:6386
   slots: (0 slots) slave
   replicates 3075d51754554aa421ee23ccbd8e263c0d5c4f0e
S: cc9a9f6bad23314263e2b89e4beede4fc59c3ef3 192.168.1.11:6382
   slots: (0 slots) slave
   replicates 95d8800931e38739a85d6face5296d719a8d5bfa
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
How many slots do you want to move (from 1 to 16384)? 4096###
What is the receiving node ID? 491bb2df7fa39bafd917f7aaa0a409e19e41b07f ###6381的ID
Please enter all the source node IDs.
  Type 'all' to use all the nodes as source nodes for the hash slots.
  Type 'done' once you entered all the source nodes IDs.
Source node #1: 2f1ed0f391fa1e0a5b3fac8e5d3c669c811ac283 # 6387的ID
Source node #2: done ###这里填done不是all

Ready to move 4096 slots.
  Source nodes:
    M: 3075d51754554aa421ee23ccbd8e263c0d5c4f0e 192.168.1.12:6383
       slots:[6827-10922] (4096 slots) master
       1 additional replica(s)
    M: 2f1ed0f391fa1e0a5b3fac8e5d3c669c811ac283 192.168.1.13:6387
       slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master
    M: 95d8800931e38739a85d6face5296d719a8d5bfa 192.168.1.13:6385
       slots:[12288-16383] (4096 slots) master
       1 additional replica(s)
  Destination node:
    M: 491bb2df7fa39bafd917f7aaa0a409e19e41b07f 192.168.1.11:6381
       slots:[1365-5460] (4096 slots) master
       1 additional replica(s)
  Resharding plan:
    Moving slot 6827 from 3075d51754554aa421ee23ccbd8e263c0d5c4f0e
    Moving slot 6828 from 3075d51754554aa421ee23ccbd8e263c0d5c4f0e
    ........

 

 

4.删除6387master.命令:redis-cli -a 密码 --cluster del-node ip:端口 6387节点ID

[root@192 cluster]# redis-cli -a 111111 --cluster del-node 192.168.1.13:6387 2f1ed0f391fa1e0a5b3fac8e5d3c669c811ac283
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Removing node 2f1ed0f391fa1e0a5b3fac8e5d3c669c811ac283 from cluster 192.168.1.13:6387
>>> Sending CLUSTER FORGET messages to the cluster...
>>> Sending CLUSTER RESET SOFT to the deleted node.
[root@192 cluster]# 

 缩容搞定

注意点

集群也不能保证数据一致性,一定会存在数据丢失的情况。

相关命令

info replication:返回关于 Redis 主从复制的详细信息

cluster info:获取 Redis 集群的状态和统计信息

cluster nodes:获取关于集群中所有节点的详细信息。

cluster countkeysinslot <槽位数字编号>:该槽位是否被占用。1占用2未占用

cluster keyslot <keyname> 该key应该存放在哪个槽位上

cluster failover : 将当前redis的身份从slave变成master,原来的master变成slave

不在同一个slot槽位下的多键操作支持不好,通识占位符登场

 

 设置集群是否完整才能提供服务

 

 

 

 

 

 
 
posted @ 2024-01-28 05:24  邵杠杠  阅读(589)  评论(0编辑  收藏  举报