Docker 学习笔记-07:Redis缓存集群

Redis缓存集群

缓存方案设计

哈希取余分区

数据读写通过哈希取余(hash(key)/nodeCount)进行计算,计算值用来决定数据存储在那个节点上

  • 优点:预估好节点数量且节点稳定的场景下,可以保证负载均衡、分而治之
  • 缺点:节点扩容或缩容,会直接影响哈希取余计算结果,导致计算值出现偏差,数据落点发生变动

一致性哈希分区

目的是解决分布式缓存数据映射关系变动问题,对哈希取余进行了优化改进,尽量减少数据落点波动,通过算法构建一致性哈希环(一致性哈希算法必然有个hash函数并按照算法产生hash值,这个算法的所有可能哈希值会构成一个全量集,这个集合可以成为一个hash空间[0,2^32-1],这个是一个线性空间,但是在算法中,我们通过适当的逻辑控制将它首尾相连(0 = 2^32),这样让它逻辑上形成了一个环形空间)、节点IP映射(通过hash算法得到节点在hash环上的位置)、数据落键规则(数据读写时,根据key值计算hash值,确定在hash环上的位置,从当前位置按照顺时针方向移动,第一个遇到的节点即为数据落点)

在这里插入图片描述

  • 优点:容错性较高(节点B宕机后,影响的数据范围仅在A-B区间内)、拓展性增强(增加或删除节点时,由于影响的数据范围是局部,因此可以局部数据重新进行落点计算,当节点数量较多时,局部影响的数据范围也将逐步变小)
  • 缺点:当节点较少时,容易因为节点分布不均匀而造成 数据倾斜(被缓存的数据大部分集中落在某个节点上)
    在这里插入图片描述

哈希槽分区

目的是为了解决数据倾斜问题,实现数据落点的均匀分配,在数据和节点之间加入了一层:槽(slot),用于管理数据和节点之间的关系

在这里插入图片描述

redis集群中内置了16384个哈希槽(slot),根据不同的节点数量(不超过1000)大致均等的讲哈希槽映射到不同主节点,数据方面只需要关心落点在那个哈希槽即可,
数据的移动变成了可控的哈希槽的移动,因为哈希槽数目固定,因此处理起来比较容易。数据落点计算通过slot = CRC16(key) % 16384计算得出。

Redis集群搭建

启动三主三从redis容器

docker run -d --restart=always --name redis-node-1 --net host --privileged=true -v /data/redis-cluster/redis-node-1:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6381
docker run -d --restart=always --name redis-node-2 --net host --privileged=true -v /data/redis-cluster/redis-node-2:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6382
docker run -d --restart=always --name redis-node-3 --net host --privileged=true -v /data/redis-cluster/redis-node-3:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6383
docker run -d --restart=always --name redis-node-4 --net host --privileged=true -v /data/redis-cluster/redis-node-4:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6384
docker run -d --restart=always --name redis-node-5 --net host --privileged=true -v /data/redis-cluster/redis-node-5:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6385
docker run -d --restart=always --name redis-node-6 --net host --privileged=true -v /data/redis-cluster/redis-node-6:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6386

集群创建及分配

进入容器实例,通过redis-cli创建集群(--cluster-replicas 1代表为每个master节点创建一个slave节点)

[root@localhost redis-cluster]# docker exec -it redis-node-1 /bin/bash
root@localhost:/data# redis-cli --cluster create 192.168.2.99:6381 192.168.2.99:6382 192.168.2.99:6383 192.168.2.99:6384 192.168.2.99:6385 192.168.2.99:6386 --cluster-replicas 1
>>> 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.2.99:6385 to 192.168.2.99:6381
Adding replica 192.168.2.99:6386 to 192.168.2.99:6382
Adding replica 192.168.2.99:6384 to 192.168.2.99:6383
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 47ab5f2fc3ab88df79877188473c4eb7294a682d 192.168.2.99:6381
   slots:[0-5460] (5461 slots) master
M: 6a724b62f33ab3da6634912aa79b9f6851e7470f 192.168.2.99:6382
   slots:[5461-10922] (5462 slots) master
M: 76e8c0c0951bcb68f50b1fbbad2e371ee76e53b8 192.168.2.99:6383
   slots:[10923-16383] (5461 slots) master
S: 016bcd34ea4d39b003565f57f6a689f8085cfe67 192.168.2.99:6384
   replicates 76e8c0c0951bcb68f50b1fbbad2e371ee76e53b8
S: 975743695d29d4c9d9ab32d7b7f530ee6c6a5a66 192.168.2.99:6385
   replicates 47ab5f2fc3ab88df79877188473c4eb7294a682d
S: 2dccfc883cf57b5283fb786e5ca500eac4977d89 192.168.2.99:6386
   replicates 6a724b62f33ab3da6634912aa79b9f6851e7470f
Can I set the above configuration? (type 'yes' to accept): 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.2.99:6381)
M: 47ab5f2fc3ab88df79877188473c4eb7294a682d 192.168.2.99:6381
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 016bcd34ea4d39b003565f57f6a689f8085cfe67 192.168.2.99:6384
   slots: (0 slots) slave
   replicates 76e8c0c0951bcb68f50b1fbbad2e371ee76e53b8
M: 6a724b62f33ab3da6634912aa79b9f6851e7470f 192.168.2.99:6382
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 975743695d29d4c9d9ab32d7b7f530ee6c6a5a66 192.168.2.99:6385
   slots: (0 slots) slave
   replicates 47ab5f2fc3ab88df79877188473c4eb7294a682d
M: 76e8c0c0951bcb68f50b1fbbad2e371ee76e53b8 192.168.2.99:6383
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 2dccfc883cf57b5283fb786e5ca500eac4977d89 192.168.2.99:6386
   slots: (0 slots) slave
   replicates 6a724b62f33ab3da6634912aa79b9f6851e7470f
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

进入主节点后,可以通过cluster info /nodes查看集群状态及节点映射关系

root@localhost:/data# redis-cli -p 6381
127.0.0.1:6381> cluster info
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:200
cluster_stats_messages_pong_sent:202
cluster_stats_messages_sent:402
cluster_stats_messages_ping_received:197
cluster_stats_messages_pong_received:200
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:402
127.0.0.1:6381> cluster nodes
016bcd34ea4d39b003565f57f6a689f8085cfe67 192.168.2.99:6384@16384 slave 76e8c0c0951bcb68f50b1fbbad2e371ee76e53b8 0 1651835864076 3 connected
47ab5f2fc3ab88df79877188473c4eb7294a682d 192.168.2.99:6381@16381 myself,master - 0 1651835863000 1 connected 0-5460
6a724b62f33ab3da6634912aa79b9f6851e7470f 192.168.2.99:6382@16382 master - 0 1651835861000 2 connected 5461-10922
975743695d29d4c9d9ab32d7b7f530ee6c6a5a66 192.168.2.99:6385@16385 slave 47ab5f2fc3ab88df79877188473c4eb7294a682d 0 1651835863069 1 connected
76e8c0c0951bcb68f50b1fbbad2e371ee76e53b8 192.168.2.99:6383@16383 master - 0 1651835861057 3 connected 10923-16383
2dccfc883cf57b5283fb786e5ca500eac4977d89 192.168.2.99:6386@16386 slave 6a724b62f33ab3da6634912aa79b9f6851e7470f 0 1651835863000 2 connected

分配情况如下(随机):

MasterSlave
192.168.2.99:6381(redis-node-1,slots:[0-5460])192.168.2.99:6385(redis-node-5)
192.168.2.99:6382(redis-node-2,slots:[5461-10922])192.168.2.99:6386(redis-node-6)
192.168.2.99:6383(redis-node-3,slots:[10923-16383])192.168.2.99:6384(redis-node-4)

主从容错切换

数据读写存储

进入redis集群node-1节点,进行数据的插入操作,可以看到,出现move error错误,由于采用了哈希槽分区,存储的数据需要先进行分槽运算,计算出的槽坐标存在偏移量,则无法在当前节点进行插入

[root@localhost data]# docker exec -it redis-node-1 /bin/bash
root@localhost:/data# redis-cli -p 6381
127.0.0.1:6381> set name zhangsan
(error) MOVED 5798 192.168.2.99:6382
127.0.0.1:6381> set school tju
(error) MOVED 8455 192.168.2.99:6382
127.0.0.1:6381> set key value
(error) MOVED 12539 192.168.2.99:6383
127.0.0.1:6381> set first hello
(error) MOVED 11149 192.168.2.99:6383
127.0.0.1:6381> set first world
(error) MOVED 11149 192.168.2.99:6383
127.0.0.1:6381> set first ddd
(error) MOVED 11149 192.168.2.99:6383
127.0.0.1:6381> set k2 v2
OK

选择集群模式连接node-1节点:redis-cli -p 6381 -c,加入-c参数,优化路由后即可成功插入

root@localhost:/data# redis-cli -p 6381 -c
127.0.0.1:6381> set name zhangsan
-> Redirected to slot [5798] located at 192.168.2.99:6382
OK
192.168.2.99:6382> set school tju
OK
192.168.2.99:6382> set key value
-> Redirected to slot [12539] located at 192.168.2.99:6383
OK

查看集群状态:redis-cli --cluster check ip:port

root@localhost:/data# redis-cli --cluster check 192.168.2.99:6381
192.168.2.99:6381 (47ab5f2f...) -> 1 keys | 5461 slots | 1 slaves.
192.168.2.99:6383 (76e8c0c0...) -> 1 keys | 5461 slots | 1 slaves.
192.168.2.99:6382 (6a724b62...) -> 2 keys | 5462 slots | 1 slaves.

容错切换迁移

当redis集群中主从对出现主节点宕机时,会自动将剩余从节点升级为主节点,待主节点恢复后,会被分配为从节点(按此顺序进行分配),假设集群中redis-node-3此主节点宕机,其配对从节点redis-node-4会被分配为主节点

127.0.0.1:6381> cluster nodes
016bcd34ea4d39b003565f57f6a689f8085cfe67 192.168.2.99:6384@16384 master - 0 1653918221836 7 connected 10923-16383
76e8c0c0951bcb68f50b1fbbad2e371ee76e53b8 192.168.2.99:6383@16383 master,fail - 1653918197628 1653918194000 3 disconnected

集群中redis-node-3重新启动后,则会被分配为从节点

127.0.0.1:6381> cluster nodes
016bcd34ea4d39b003565f57f6a689f8085cfe67 192.168.2.99:6384@16384 master - 0 1653918274000 7 connected 10923-16383
76e8c0c0951bcb68f50b1fbbad2e371ee76e53b8 192.168.2.99:6383@16383 slave 016bcd34ea4d39b003565f57f6a689f8085cfe67 0 1653918275289 7 connected

主从扩缩容

扩容

新增6387(master)、6388(slave)两个节点,首先启动两个docker容器

[root@localhost ~]# docker run -d --restart=always --name redis-node-7 --net host --privileged=true -v /data/redis-cluster/redis-node-7:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6387
f7e887423622ac7501832d212dc4c39a4e13953ec24255bfe2859b943006c949
[root@localhost ~]# docker run -d --restart=always --name redis-node-8 --net host --privileged=true -v /data/redis-cluster/redis-node-8:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6388
b25d3bfd414cb9f459af28ba7f0d3a8f4d7fee8afb89352a95efd749d78456d6

进入6387(master)容器内部,将其作为master节点加入集群,redis-cli --cluster add-node targetIP:targetPort existedIP:existedIP,此处已存在的节点采用6381(master)节点

[root@localhost ~]# docker exec -it redis-node-7 /bin/bash
root@localhost:/data# redis-cli --cluster add-node 192.168.2.99:6387 192.168.2.99:6381
>>> Adding node 192.168.2.99:6387 to cluster 192.168.2.99:6381
>>> Performing Cluster Check (using node 192.168.2.99:6381)
M: 47ab5f2fc3ab88df79877188473c4eb7294a682d 192.168.2.99:6381
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 2dccfc883cf57b5283fb786e5ca500eac4977d89 192.168.2.99:6386
   slots: (0 slots) slave
   replicates 6a724b62f33ab3da6634912aa79b9f6851e7470f
S: 975743695d29d4c9d9ab32d7b7f530ee6c6a5a66 192.168.2.99:6385
   slots: (0 slots) slave
   replicates 47ab5f2fc3ab88df79877188473c4eb7294a682d
M: 76e8c0c0951bcb68f50b1fbbad2e371ee76e53b8 192.168.2.99:6383
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
M: 6a724b62f33ab3da6634912aa79b9f6851e7470f 192.168.2.99:6382
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 016bcd34ea4d39b003565f57f6a689f8085cfe67 192.168.2.99:6384
   slots: (0 slots) slave
   replicates 76e8c0c0951bcb68f50b1fbbad2e371ee76e53b8
[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.2.99:6387 to make it join the cluster.
[OK] New node added correctly.

检查集群状态,可以看到6387(master)未被分配槽位,也没有从节点

root@localhost:/data# redis-cli --cluster check 192.198.2.99:6381
Could not connect to Redis at 192.198.2.99:6381: Connection refused
root@localhost:/data# redis-cli --cluster check 192.168.2.99:6381
192.168.2.99:6381 (47ab5f2f...) -> 1 keys | 5461 slots | 1 slaves.
192.168.2.99:6387 (2d2b6f6c...) -> 0 keys | 0 slots | 0 slaves.
192.168.2.99:6383 (76e8c0c0...) -> 1 keys | 5461 slots | 1 slaves.
192.168.2.99:6382 (6a724b62...) -> 2 keys | 5462 slots | 1 slaves.

为6387(master)重新分配槽位,redis-cli --cluster reshard targetIP:targetPort,此处需要注意:

  • How many slots do you want to move (from 1 to 16384)? 4096(均分槽位,16384/4=4096)
  • What is the receiving node ID? 2d2b6f6c95fbad9a3f9b460eb192bec5a3e3d740(接受分配的节点ID,即6387此节点)
  • Please enter all the source node IDs. Source node #1: all(选择为所有的节点重新分配槽位)
root@localhost:/data# redis-cli --cluster reshard 192.168.2.99:6387
>>> Performing Cluster Check (using node 192.168.2.99:6387)
M: 2d2b6f6c95fbad9a3f9b460eb192bec5a3e3d740 192.168.2.99:6387
   slots: (0 slots) master
S: 975743695d29d4c9d9ab32d7b7f530ee6c6a5a66 192.168.2.99:6385
   slots: (0 slots) slave
   replicates 47ab5f2fc3ab88df79877188473c4eb7294a682d
M: 6a724b62f33ab3da6634912aa79b9f6851e7470f 192.168.2.99:6382
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 2dccfc883cf57b5283fb786e5ca500eac4977d89 192.168.2.99:6386
   slots: (0 slots) slave
   replicates 6a724b62f33ab3da6634912aa79b9f6851e7470f
S: 016bcd34ea4d39b003565f57f6a689f8085cfe67 192.168.2.99:6384
   slots: (0 slots) slave
   replicates 76e8c0c0951bcb68f50b1fbbad2e371ee76e53b8
M: 76e8c0c0951bcb68f50b1fbbad2e371ee76e53b8 192.168.2.99:6383
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
M: 47ab5f2fc3ab88df79877188473c4eb7294a682d 192.168.2.99:6381
   slots:[0-5460] (5461 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.
How many slots do you want to move (from 1 to 16384)? 4096
What is the receiving node ID? 2d2b6f6c95fbad9a3f9b460eb192bec5a3e3d740
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

Ready to move 4096 slots.
  Source nodes:
    M: 6a724b62f33ab3da6634912aa79b9f6851e7470f 192.168.2.99:6382
       slots:[5461-10922] (5462 slots) master
       1 additional replica(s)
    M: 76e8c0c0951bcb68f50b1fbbad2e371ee76e53b8 192.168.2.99:6383
       slots:[10923-16383] (5461 slots) master
       1 additional replica(s)
    M: 47ab5f2fc3ab88df79877188473c4eb7294a682d 192.168.2.99:6381
       slots:[0-5460] (5461 slots) master
       1 additional replica(s)
  Destination node:
    M: 2d2b6f6c95fbad9a3f9b460eb192bec5a3e3d740 192.168.2.99:6387
       slots: (0 slots) master
  Resharding plan:
    Moving slot 5461 from 6a724b62f33ab3da6634912aa79b9f6851e7470f

重新查看集群节点状态,可以看到6387(master)节点分配的槽位是不连续的([0-1364],[5461-6826],[10923-12287]),也就是说在重新reshard后,新加入的节点槽位是通过其他节点分配而来,此时4个master节点的槽位大小被均分后重新分配

root@localhost:/data# redis-cli --cluster check 192.168.2.99:6382
192.168.2.99:6382 (6a724b62...) -> 1 keys | 4096 slots | 1 slaves.
192.168.2.99:6387 (2d2b6f6c...) -> 2 keys | 4096 slots | 0 slaves.
192.168.2.99:6383 (76e8c0c0...) -> 1 keys | 4096 slots | 1 slaves.
192.168.2.99:6381 (47ab5f2f...) -> 0 keys | 4096 slots | 1 slaves.
[OK] 4 keys in 4 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.2.99:6382)
M: 6a724b62f33ab3da6634912aa79b9f6851e7470f 192.168.2.99:6382
   slots:[6827-10922] (4096 slots) master
   1 additional replica(s)
S: 975743695d29d4c9d9ab32d7b7f530ee6c6a5a66 192.168.2.99:6385
   slots: (0 slots) slave
   replicates 47ab5f2fc3ab88df79877188473c4eb7294a682d
M: 2d2b6f6c95fbad9a3f9b460eb192bec5a3e3d740 192.168.2.99:6387
   slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master
S: 016bcd34ea4d39b003565f57f6a689f8085cfe67 192.168.2.99:6384
   slots: (0 slots) slave
   replicates 76e8c0c0951bcb68f50b1fbbad2e371ee76e53b8
S: 2dccfc883cf57b5283fb786e5ca500eac4977d89 192.168.2.99:6386
   slots: (0 slots) slave
   replicates 6a724b62f33ab3da6634912aa79b9f6851e7470f
M: 76e8c0c0951bcb68f50b1fbbad2e371ee76e53b8 192.168.2.99:6383
   slots:[12288-16383] (4096 slots) master
   1 additional replica(s)
M: 47ab5f2fc3ab88df79877188473c4eb7294a682d 192.168.2.99:6381
   slots:[1365-5460] (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.

为6387(master)分配6388(slave)节点,redis-cli --cluster add-node slaveNodeIP:Port masterNodeIP:Port --cluster-slave --cluster-master-id masterNodeID,可以看到6388(slave)已经被加入集群

root@localhost:/data# redis-cli --cluster add-node 192.168.2.99:6388 192.168.2.99:6387 --cluster-slave --cluster-master-id 2d2b6f6c95fbad9a3f9b460eb192bec5a3e3d740
>>> Adding node 192.168.2.99:6388 to cluster 192.168.2.99:6387
>>> Performing Cluster Check (using node 192.168.2.99:6387)
M: 2d2b6f6c95fbad9a3f9b460eb192bec5a3e3d740 192.168.2.99:6387
   slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master
S: 975743695d29d4c9d9ab32d7b7f530ee6c6a5a66 192.168.2.99:6385
   slots: (0 slots) slave
   replicates 47ab5f2fc3ab88df79877188473c4eb7294a682d
M: 6a724b62f33ab3da6634912aa79b9f6851e7470f 192.168.2.99:6382
   slots:[6827-10922] (4096 slots) master
   1 additional replica(s)
S: 2dccfc883cf57b5283fb786e5ca500eac4977d89 192.168.2.99:6386
   slots: (0 slots) slave
   replicates 6a724b62f33ab3da6634912aa79b9f6851e7470f
S: 016bcd34ea4d39b003565f57f6a689f8085cfe67 192.168.2.99:6384
   slots: (0 slots) slave
   replicates 76e8c0c0951bcb68f50b1fbbad2e371ee76e53b8
M: 76e8c0c0951bcb68f50b1fbbad2e371ee76e53b8 192.168.2.99:6383
   slots:[12288-16383] (4096 slots) master
   1 additional replica(s)
M: 47ab5f2fc3ab88df79877188473c4eb7294a682d 192.168.2.99:6381
   slots:[1365-5460] (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.
>>> Send CLUSTER MEET to node 192.168.2.99:6388 to make it join the cluster.
Waiting for the cluster to join

>>> Configure node as replica of 192.168.2.99:6387.
[OK] New node added correctly.

再次检查集群状态,可以看到6387(master)已经存在1个从节点,通过nodeID可以看出,其从节点为6388(slave)

root@localhost:/data# redis-cli --cluster check 192.168.2.99:6383
192.168.2.99:6383 (76e8c0c0...) -> 1 keys | 4096 slots | 1 slaves.
192.168.2.99:6387 (2d2b6f6c...) -> 2 keys | 4096 slots | 1 slaves.
192.168.2.99:6382 (6a724b62...) -> 1 keys | 4096 slots | 1 slaves.
192.168.2.99:6381 (47ab5f2f...) -> 0 keys | 4096 slots | 1 slaves.
[OK] 4 keys in 4 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.2.99:6383)
M: 76e8c0c0951bcb68f50b1fbbad2e371ee76e53b8 192.168.2.99:6383
   slots:[12288-16383] (4096 slots) master
   1 additional replica(s)
M: 2d2b6f6c95fbad9a3f9b460eb192bec5a3e3d740 192.168.2.99:6387
   slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master
   1 additional replica(s)
M: 6a724b62f33ab3da6634912aa79b9f6851e7470f 192.168.2.99:6382
   slots:[6827-10922] (4096 slots) master
   1 additional replica(s)
S: 016bcd34ea4d39b003565f57f6a689f8085cfe67 192.168.2.99:6384
   slots: (0 slots) slave
   replicates 76e8c0c0951bcb68f50b1fbbad2e371ee76e53b8
S: 975743695d29d4c9d9ab32d7b7f530ee6c6a5a66 192.168.2.99:6385
   slots: (0 slots) slave
   replicates 47ab5f2fc3ab88df79877188473c4eb7294a682d
S: 2dccfc883cf57b5283fb786e5ca500eac4977d89 192.168.2.99:6386
   slots: (0 slots) slave
   replicates 6a724b62f33ab3da6634912aa79b9f6851e7470f
S: 6e323db89b775b93a16fc205f0bd59d8257d1bfe 192.168.2.99:6388
   slots: (0 slots) slave
   replicates 2d2b6f6c95fbad9a3f9b460eb192bec5a3e3d740
M: 47ab5f2fc3ab88df79877188473c4eb7294a682d 192.168.2.99:6381
   slots:[1365-5460] (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.

缩容

将新加入的6387(master)、6388(slave)节点,移除集群,在移除集群操作中,需要先对从节点进行操作,先删除6388(slave)节点,redis-cli --cluster del-node nodeIP:Port nodeID,移除后可以看到6387(master)节点无slave节点

root@localhost:/data# redis-cli --cluster del-node 192.168.2.99:6388 6e323db89b775b93a16fc205f0bd59d8257d1bfe
>>> Removing node 6e323db89b775b93a16fc205f0bd59d8257d1bfe from cluster 192.168.2.99:6388
>>> Sending CLUSTER FORGET messages to the cluster...
>>> Sending CLUSTER RESET SOFT to the deleted node.
root@localhost:/data# redis-cli --cluster check 192.168.2.99:6383
192.168.2.99:6383 (76e8c0c0...) -> 0 keys | 2731 slots | 1 slaves.
192.168.2.99:6387 (2d2b6f6c...) -> 3 keys | 8192 slots | 0 slaves.
192.168.2.99:6382 (6a724b62...) -> 1 keys | 2730 slots | 1 slaves.
192.168.2.99:6381 (47ab5f2f...) -> 0 keys | 2731 slots | 1 slaves.
[OK] 4 keys in 4 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.2.99:6383)
M: 76e8c0c0951bcb68f50b1fbbad2e371ee76e53b8 192.168.2.99:6383
   slots:[13653-16383] (2731 slots) master
   1 additional replica(s)
M: 2d2b6f6c95fbad9a3f9b460eb192bec5a3e3d740 192.168.2.99:6387
   slots:[0-2729],[5461-8192],[10923-13652] (8192 slots) master
M: 6a724b62f33ab3da6634912aa79b9f6851e7470f 192.168.2.99:6382
   slots:[8193-10922] (2730 slots) master
   1 additional replica(s)
S: 016bcd34ea4d39b003565f57f6a689f8085cfe67 192.168.2.99:6384
   slots: (0 slots) slave
   replicates 76e8c0c0951bcb68f50b1fbbad2e371ee76e53b8
S: 975743695d29d4c9d9ab32d7b7f530ee6c6a5a66 192.168.2.99:6385
   slots: (0 slots) slave
   replicates 47ab5f2fc3ab88df79877188473c4eb7294a682d
S: 2dccfc883cf57b5283fb786e5ca500eac4977d89 192.168.2.99:6386
   slots: (0 slots) slave
   replicates 6a724b62f33ab3da6634912aa79b9f6851e7470f
M: 47ab5f2fc3ab88df79877188473c4eb7294a682d 192.168.2.99:6381
   slots:[2730-5460] (2731 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.

将6387(master)槽位清空,共计8192个,此处将清理出的槽位均分配给6381(master)

  • How many slots do you want to move (from 1 to 16384)? 8192(需要清空的槽位数量)
  • What is the receiving node ID? 47ab5f2fc3ab88df79877188473c4eb7294a682d(需要接受槽位的节点ID,即6381)
  • Please enter all the source node IDs.
    • Source node #1: 2d2b6f6c95fbad9a3f9b460eb192bec5a3e3d740(需要清空槽位的节点ID,即6387)
    • Source node #2: done(输入原节点 -> 目标节点,6387 -> 6381)
root@localhost:/data# redis-cli --cluster reshard 192.168.2.99:6381
>>> Performing Cluster Check (using node 192.168.2.99:6381)
M: 47ab5f2fc3ab88df79877188473c4eb7294a682d 192.168.2.99:6381
   slots:[2730-5460] (2731 slots) master
   1 additional replica(s)
M: 2d2b6f6c95fbad9a3f9b460eb192bec5a3e3d740 192.168.2.99:6387
   slots:[0-2729],[5461-8192],[10923-13652] (8192 slots) master
S: 2dccfc883cf57b5283fb786e5ca500eac4977d89 192.168.2.99:6386
   slots: (0 slots) slave
   replicates 6a724b62f33ab3da6634912aa79b9f6851e7470f
S: 975743695d29d4c9d9ab32d7b7f530ee6c6a5a66 192.168.2.99:6385
   slots: (0 slots) slave
   replicates 47ab5f2fc3ab88df79877188473c4eb7294a682d
M: 76e8c0c0951bcb68f50b1fbbad2e371ee76e53b8 192.168.2.99:6383
   slots:[13653-16383] (2731 slots) master
   1 additional replica(s)
M: 6a724b62f33ab3da6634912aa79b9f6851e7470f 192.168.2.99:6382
   slots:[8193-10922] (2730 slots) master
   1 additional replica(s)
S: 016bcd34ea4d39b003565f57f6a689f8085cfe67 192.168.2.99:6384
   slots: (0 slots) slave
   replicates 76e8c0c0951bcb68f50b1fbbad2e371ee76e53b8
[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)? 8192
What is the receiving node ID? 47ab5f2fc3ab88df79877188473c4eb7294a682d
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: 2d2b6f6c95fbad9a3f9b460eb192bec5a3e3d740
Source node #2: done

Ready to move 8192 slots.

此时检查集群状态,可以看到6387(master)槽位清空,slave节点也清空,接下来就可以将6387(master)移除集群

root@localhost:/data# redis-cli --cluster check 192.168.2.99:6383
192.168.2.99:6383 (76e8c0c0...) -> 0 keys | 2731 slots | 1 slaves.
192.168.2.99:6387 (2d2b6f6c...) -> 0 keys | 0 slots | 0 slaves.
192.168.2.99:6382 (6a724b62...) -> 1 keys | 2730 slots | 1 slaves.
192.168.2.99:6381 (47ab5f2f...) -> 3 keys | 10923 slots | 1 slaves.
######################## 将6387(master)移除集群 #####################################
root@localhost:/data# redis-cli --cluster del-node 192.168.2.99:6387 2d2b6f6c95fbad9a3f9b460eb192bec5a3e3d740
>>> Removing node 2d2b6f6c95fbad9a3f9b460eb192bec5a3e3d740 from cluster 192.168.2.99:6387
>>> Sending CLUSTER FORGET messages to the cluster...
>>> Sending CLUSTER RESET SOFT to the deleted node.
######################## 再次查看集群状态 #####################################
root@localhost:/data# redis-cli --cluster check 192.168.2.99:6383
192.168.2.99:6383 (76e8c0c0...) -> 0 keys | 2731 slots | 1 slaves.
192.168.2.99:6382 (6a724b62...) -> 1 keys | 2730 slots | 1 slaves.
192.168.2.99:6381 (47ab5f2f...) -> 3 keys | 10923 slots | 1 slaves.
[OK] 4 keys in 3 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.2.99:6383)
M: 76e8c0c0951bcb68f50b1fbbad2e371ee76e53b8 192.168.2.99:6383
   slots:[13653-16383] (2731 slots) master
   1 additional replica(s)
M: 6a724b62f33ab3da6634912aa79b9f6851e7470f 192.168.2.99:6382
   slots:[8193-10922] (2730 slots) master
   1 additional replica(s)
S: 016bcd34ea4d39b003565f57f6a689f8085cfe67 192.168.2.99:6384
   slots: (0 slots) slave
   replicates 76e8c0c0951bcb68f50b1fbbad2e371ee76e53b8
S: 975743695d29d4c9d9ab32d7b7f530ee6c6a5a66 192.168.2.99:6385
   slots: (0 slots) slave
   replicates 47ab5f2fc3ab88df79877188473c4eb7294a682d
S: 2dccfc883cf57b5283fb786e5ca500eac4977d89 192.168.2.99:6386
   slots: (0 slots) slave
   replicates 6a724b62f33ab3da6634912aa79b9f6851e7470f
M: 47ab5f2fc3ab88df79877188473c4eb7294a682d 192.168.2.99:6381
   slots:[0-8192],[10923-13652] (10923 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.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

编程小透明

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值