分布式存储算法
- 哈希取余算法分区(hash取模)
- 一致性哈希算法分区(hash闭环)
- 哈希槽算法分区(hash中间代理)Redis集群使用
Redis集群搭建(3主3从案例)
- 启动6个容器
// 节点1-6 端口6381-6386
docker run -d --name redis-node-1 --net host --privileged=true -v /data/redis/share/redis-node-1:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6381
docker run -d --name redis-node-2 --net host --privileged=true -v /data/redis/share/redis-node-2:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6382
docker run -d --name redis-node-3 --net host --privileged=true -v /data/redis/share/redis-node-3:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6383
docker run -d --name redis-node-4 --net host --privileged=true -v /data/redis/share/redis-node-4:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6384
docker run -d --name redis-node-5 --net host --privileged=true -v /data/redis/share/redis-node-5:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6385
docker run -d --name redis-node-6 --net host --privileged=true -v /data/redis/share/redis-node-6:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6386
// 查看运行的容器
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
75802596bb67 redis:6.0.8 "docker-entrypoint.s…" About a minute ago Up About a minute redis-node-6
c41b787f923c redis:6.0.8 "docker-entrypoint.s…" About a minute ago Up About a minute redis-node-5
6b67d6956c48 redis:6.0.8 "docker-entrypoint.s…" About a minute ago Up About a minute redis-node-4
8f4d3d46f060 redis:6.0.8 "docker-entrypoint.s…" About a minute ago Up About a minute redis-node-3
ec77cdc1af2a redis:6.0.8 "docker-entrypoint.s…" About a minute ago Up About a minute redis-node-2
9b3f843d9982 redis:6.0.8 "docker-entrypoint.s…" About a minute ago Up About a minute redis-node-1
- 进入容器redis-node-1
docker exec -it redis-node-1 bash
- 为6台机器构建集群关系,构建主从关系,ip为机器的地址 --cluster-replicas 1 表示为每个master创建一个slave节点
redis-cli --cluster create 192.168.223.135:6381 192.168.223.135:6382 192.168.223.135:6383 192.168.223.135:6384 192.168.223.135:6385 192.168.223.135:6386 --cluster-replicas 1
root@test:/data# redis-cli --cluster create 192.168.223.135:6381 192.168.223.135:6382 192.168.223.135:6383 192.168.223.135:6384 192.168.223.135:6385 192.168.223.135: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.223.135:6385 to 192.168.223.135:6381
Adding replica 192.168.223.135:6386 to 192.168.223.135:6382
Adding replica 192.168.223.135:6384 to 192.168.223.135:6383
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 7130be699c2d6970c4824efa69047adc85375d65 192.168.223.135:6381
slots:[0-5460] (5461 slots) master
M: 7874b26214a08a6816cd2aaada241d84d3d12880 192.168.223.135:6382
slots:[5461-10922] (5462 slots) master
M: ed4bea7210969391e54fe5334c685b76263c4e85 192.168.223.135:6383
slots:[10923-16383] (5461 slots) master
S: 9712b4047d92f217131b5ff8d3e42448be817bcb 192.168.223.135:6384
replicates ed4bea7210969391e54fe5334c685b76263c4e85
S: 2c2fdb0a6a24a7972e5e66ee5eeabefbc139f5b9 192.168.223.135:6385
replicates 7130be699c2d6970c4824efa69047adc85375d65
S: 510af78ad32df6fa5cfd51dff27d3bef53bda01b 192.168.223.135:6386
replicates 7874b26214a08a6816cd2aaada241d84d3d12880
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.223.135:6381)
M: 7130be699c2d6970c4824efa69047adc85375d65 192.168.223.135:6381
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: 7874b26214a08a6816cd2aaada241d84d3d12880 192.168.223.135:6382
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
M: ed4bea7210969391e54fe5334c685b76263c4e85 192.168.223.135:6383
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 9712b4047d92f217131b5ff8d3e42448be817bcb 192.168.223.135:6384
slots: (0 slots) slave
replicates ed4bea7210969391e54fe5334c685b76263c4e85
S: 510af78ad32df6fa5cfd51dff27d3bef53bda01b 192.168.223.135:6386
slots: (0 slots) slave
replicates 7874b26214a08a6816cd2aaada241d84d3d12880
S: 2c2fdb0a6a24a7972e5e66ee5eeabefbc139f5b9 192.168.223.135:6385
slots: (0 slots) slave
replicates 7130be699c2d6970c4824efa69047adc85375d65
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
对应关系:
1-5 2-6 3-4
- 查看集群状态cluster info,以6381作为切入点,任意都可以
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 6个节点
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:592
cluster_stats_messages_pong_sent:582
cluster_stats_messages_sent:1174
cluster_stats_messages_ping_received:577
cluster_stats_messages_pong_received:592
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:1174
- 查看集群节点信息
cluster nodes
127.0.0.1:6381> cluster nodes
7874b26214a08a6816cd2aaada241d84d3d12880 192.168.223.135:6382@16382 master - 0 1700745136235 2 connected 5461-10922
7130be699c2d6970c4824efa69047adc85375d65 192.168.223.135:6381@16381 myself,master - 0 1700745135000 1 connected 0-5460
ed4bea7210969391e54fe5334c685b76263c4e85 192.168.223.135:6383@16383 master - 0 1700745137242 3 connected 10923-16383
9712b4047d92f217131b5ff8d3e42448be817bcb 192.168.223.135:6384@16384 slave ed4bea7210969391e54fe5334c685b76263c4e85 0 1700745135000 3 connected
510af78ad32df6fa5cfd51dff27d3bef53bda01b 192.168.223.135:6386@16386 slave 7874b26214a08a6816cd2aaada241d84d3d12880 0 1700745134220 2 connected
2c2fdb0a6a24a7972e5e66ee5eeabefbc139f5b9 192.168.223.135:6385@16385 slave 7130be699c2d6970c4824efa69047adc85375d65 0 1700745135228 1 connected
主从容错切换迁移案例
- 数据读写存储,由于是集群环境,因此需要加-c参数,用于自动路由到对应节点
redis-cli -c -p 6381
root@test:/data# redis-cli -c -p 6381
127.0.0.1:6381> set k1 v1
-> Redirected to slot [12706] located at 192.168.223.135:6383
OK
192.168.223.135:6383> set k2 v2
-> Redirected to slot [449] located at 192.168.223.135:6381
OK
192.168.223.135:6381> get k1
-> Redirected to slot [12706] located at 192.168.223.135:6383
"v1"
192.168.223.135:6383> get k2
-> Redirected to slot [449] located at 192.168.223.135:6381
"v2"
192.168.223.135:6381> get k1
-> Redirected to slot [12706] located at 192.168.223.135:6383
"v1"
192.168.223.135:6383> get k1
"v1"
- 集群检查
redis-cli --cluster check 192.168.223.135:6381
root@test:/data# redis-cli --cluster check 192.168.223.135:6381
192.168.223.135:6381 (7130be69...) -> 2 keys | 5461 slots | 1 slaves.
192.168.223.135:6382 (7874b262...) -> 1 keys | 5462 slots | 1 slaves.
192.168.223.135:6383 (ed4bea72...) -> 1 keys | 5461 slots | 1 slaves.
[OK] 4 keys in 3 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.223.135:6381)
M: 7130be699c2d6970c4824efa69047adc85375d65 192.168.223.135:6381
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: 7874b26214a08a6816cd2aaada241d84d3d12880 192.168.223.135:6382
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
M: ed4bea7210969391e54fe5334c685b76263c4e85 192.168.223.135:6383
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 9712b4047d92f217131b5ff8d3e42448be817bcb 192.168.223.135:6384
slots: (0 slots) slave
replicates ed4bea7210969391e54fe5334c685b76263c4e85
S: 510af78ad32df6fa5cfd51dff27d3bef53bda01b 192.168.223.135:6386
slots: (0 slots) slave
replicates 7874b26214a08a6816cd2aaada241d84d3d12880
S: 2c2fdb0a6a24a7972e5e66ee5eeabefbc139f5b9 192.168.223.135:6385
slots: (0 slots) slave
replicates 7130be699c2d6970c4824efa69047adc85375d65
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
- 主从切换(主从容错迁移),将6381停止,检测6381的slave6385是否变成master
[root@test ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
75802596bb67 redis:6.0.8 "docker-entrypoint.s…" About an hour ago Up About an hour redis-node-6
c41b787f923c redis:6.0.8 "docker-entrypoint.s…" About an hour ago Up About an hour redis-node-5
6b67d6956c48 redis:6.0.8 "docker-entrypoint.s…" About an hour ago Up About an hour redis-node-4
8f4d3d46f060 redis:6.0.8 "docker-entrypoint.s…" About an hour ago Up About an hour redis-node-3
ec77cdc1af2a redis:6.0.8 "docker-entrypoint.s…" About an hour ago Up About an hour redis-node-2
9b3f843d9982 redis:6.0.8 "docker-entrypoint.s…" About an hour ago Up About an hour redis-node-1
停止6381
[root@test ~]# docker stop redis-node-1
redis-node-1
[root@test ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
75802596bb67 redis:6.0.8 "docker-entrypoint.s…" About an hour ago Up About an hour redis-node-6
c41b787f923c redis:6.0.8 "docker-entrypoint.s…" About an hour ago Up About an hour redis-node-5
6b67d6956c48 redis:6.0.8 "docker-entrypoint.s…" About an hour ago Up About an hour redis-node-4
8f4d3d46f060 redis:6.0.8 "docker-entrypoint.s…" About an hour ago Up About an hour redis-node-3
ec77cdc1af2a redis:6.0.8 "docker-entrypoint.s…" About an hour ago Up About an hour redis-node-2
查看集群状态
root@test:/data# redis-cli --cluster check 192.168.223.135:6383
Could not connect to Redis at 192.168.223.135:6381: Connection refused
192.168.223.135:6383 (ed4bea72...) -> 1 keys | 5461 slots | 1 slaves.
192.168.223.135:6385 (2c2fdb0a...) -> 2 keys | 5461 slots | 0 slaves.
192.168.223.135:6382 (7874b262...) -> 1 keys | 5462 slots | 1 slaves.
[OK] 4 keys in 3 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.223.135:6383)
M: ed4bea7210969391e54fe5334c685b76263c4e85 192.168.223.135:6383
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 510af78ad32df6fa5cfd51dff27d3bef53bda01b 192.168.223.135:6386
slots: (0 slots) slave
replicates 7874b26214a08a6816cd2aaada241d84d3d12880
S: 9712b4047d92f217131b5ff8d3e42448be817bcb 192.168.223.135:6384
slots: (0 slots) slave
replicates ed4bea7210969391e54fe5334c685b76263c4e85
M: 2c2fdb0a6a24a7972e5e66ee5eeabefbc139f5b9 192.168.223.135:6385
slots:[0-5460] (5461 slots) master
M: 7874b26214a08a6816cd2aaada241d84d3d12880 192.168.223.135:6382
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.
发现6385由slave变为master了
再次启动6381
[root@test ~]# docker start redis-node-1
redis-node-1
查看集群状态
root@test:/data# redis-cli --cluster check 192.168.223.135:6383
192.168.223.135:6383 (ed4bea72...) -> 1 keys | 5461 slots | 1 slaves.
192.168.223.135:6385 (2c2fdb0a...) -> 2 keys | 5461 slots | 1 slaves.
192.168.223.135:6382 (7874b262...) -> 1 keys | 5462 slots | 1 slaves.
[OK] 4 keys in 3 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.223.135:6383)
M: ed4bea7210969391e54fe5334c685b76263c4e85 192.168.223.135:6383
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 510af78ad32df6fa5cfd51dff27d3bef53bda01b 192.168.223.135:6386
slots: (0 slots) slave
replicates 7874b26214a08a6816cd2aaada241d84d3d12880
S: 9712b4047d92f217131b5ff8d3e42448be817bcb 192.168.223.135:6384
slots: (0 slots) slave
replicates ed4bea7210969391e54fe5334c685b76263c4e85
S: 7130be699c2d6970c4824efa69047adc85375d65 192.168.223.135:6381
slots: (0 slots) slave
replicates 2c2fdb0a6a24a7972e5e66ee5eeabefbc139f5b9
M: 2c2fdb0a6a24a7972e5e66ee5eeabefbc139f5b9 192.168.223.135:6385
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: 7874b26214a08a6816cd2aaada241d84d3d12880 192.168.223.135:6382
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.
发现6381变为6385的slave了,如果想让6381变为master 可以将6385停止后重新启动
扩容节点
- 新建6387、6388节点
docker run -d --name redis-node-7 --net host --privileged=true -v /data/redis/share/redis-node-7:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6387
docker run -d --name redis-node-8 --net host --privileged=true -v /data/redis/share/redis-node-8:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6388
[root@test ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
696436d25a81 redis:6.0.8 "docker-entrypoint.s…" 3 seconds ago Up 2 seconds redis-node-8
8ba5919bb9ba redis:6.0.8 "docker-entrypoint.s…" 4 seconds ago Up 4 seconds redis-node-7
75802596bb67 redis:6.0.8 "docker-entrypoint.s…" About an hour ago Up About an hour redis-node-6
6b67d6956c48 redis:6.0.8 "docker-entrypoint.s…" About an hour ago Up About an hour redis-node-4
8f4d3d46f060 redis:6.0.8 "docker-entrypoint.s…" About an hour ago Up About an hour redis-node-3
ec77cdc1af2a redis:6.0.8 "docker-entrypoint.s…" About an hour ago Up About an hour redis-node-2
9b3f843d9982 redis:6.0.8 "docker-entrypoint.s…" About an hour ago Up 8 minutes redis-node-1
- 进入节点7容器 将6387节点加入集群
docker exec -it redis-node-7 bash
将新增的6387作为master节点加入集群
redis-cli --cluster add-node 自己实际IP地址:6387 自己实际IP地址:6381
6387 就是将要作为master新增节点
6381 就是原来集群节点里面的领路人,相当于6387拜拜6381的码头从而找到组织加入集群
root@test:/data# redis-cli --cluster add-node 192.168.223.135:6387 192.168.223.135:6381
>>> Adding node 192.168.223.135:6387 to cluster 192.168.223.135:6381
Could not connect to Redis at 192.168.223.135:6385: Connection refused
>>> Performing Cluster Check (using node 192.168.223.135:6381)
M: 7130be699c2d6970c4824efa69047adc85375d65 192.168.223.135:6381
slots:[0-5460] (5461 slots) master
S: 510af78ad32df6fa5cfd51dff27d3bef53bda01b 192.168.223.135:6386
slots: (0 slots) slave
replicates 7874b26214a08a6816cd2aaada241d84d3d12880
M: 7874b26214a08a6816cd2aaada241d84d3d12880 192.168.223.135:6382
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: 9712b4047d92f217131b5ff8d3e42448be817bcb 192.168.223.135:6384
slots: (0 slots) slave
replicates ed4bea7210969391e54fe5334c685b76263c4e85
M: ed4bea7210969391e54fe5334c685b76263c4e85 192.168.223.135:6383
slots:[10923-16383] (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.
>>> Send CLUSTER MEET to node 192.168.223.135:6387 to make it join the cluster.
[OK] New node added correctly.
查看集群状态,加入成功,但是槽位为0
root@test:/data# redis-cli --cluster check 192.168.223.135:6383
192.168.223.135:6383 (ed4bea72...) -> 1 keys | 5461 slots | 1 slaves.
192.168.223.135:6381 (7130be69...) -> 2 keys | 5461 slots | 1 slaves.
192.168.223.135:6387 (ec07e09e...) -> 0 keys | 0 slots | 0 slaves.
192.168.223.135:6382 (7874b262...) -> 1 keys | 5462 slots | 1 slaves.
[OK] 4 keys in 4 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.223.135:6383)
M: ed4bea7210969391e54fe5334c685b76263c4e85 192.168.223.135:6383
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 510af78ad32df6fa5cfd51dff27d3bef53bda01b 192.168.223.135:6386
slots: (0 slots) slave
replicates 7874b26214a08a6816cd2aaada241d84d3d12880
S: 9712b4047d92f217131b5ff8d3e42448be817bcb 192.168.223.135:6384
slots: (0 slots) slave
replicates ed4bea7210969391e54fe5334c685b76263c4e85
M: 7130be699c2d6970c4824efa69047adc85375d65 192.168.223.135:6381
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: ec07e09e0c98c7fac6db05728f4a8236dfc25b65 192.168.223.135:6387
slots: (0 slots) master
S: 2c2fdb0a6a24a7972e5e66ee5eeabefbc139f5b9 192.168.223.135:6385
slots: (0 slots) slave
replicates 7130be699c2d6970c4824efa69047adc85375d65
M: 7874b26214a08a6816cd2aaada241d84d3d12880 192.168.223.135:6382
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.
- 重新分派槽号:命令:redis-cli --cluster reshard ip地址:端口号
root@test:/data# redis-cli --cluster reshard 192.168.223.135:6381
>>> Performing Cluster Check (using node 192.168.223.135:6381)
M: 7130be699c2d6970c4824efa69047adc85375d65 192.168.223.135:6381
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: 2c2fdb0a6a24a7972e5e66ee5eeabefbc139f5b9 192.168.223.135:6385
slots: (0 slots) slave
replicates 7130be699c2d6970c4824efa69047adc85375d65
S: 510af78ad32df6fa5cfd51dff27d3bef53bda01b 192.168.223.135:6386
slots: (0 slots) slave
replicates 7874b26214a08a6816cd2aaada241d84d3d12880
M: ec07e09e0c98c7fac6db05728f4a8236dfc25b65 192.168.223.135:6387
slots: (0 slots) master 该节点没有槽号,需要分配
M: 7874b26214a08a6816cd2aaada241d84d3d12880 192.168.223.135:6382
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: 9712b4047d92f217131b5ff8d3e42448be817bcb 192.168.223.135:6384
slots: (0 slots) slave
replicates ed4bea7210969391e54fe5334c685b76263c4e85
M: ed4bea7210969391e54fe5334c685b76263c4e85 192.168.223.135:6383
slots:[10923-16383] (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 AA分=16384/4(节点数)
What is the receiving node ID? ec07e09e0c98c7fac6db05728f4a8236dfc25b65 这里填需要分配的节点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
......
Do you want to proceed with the proposed reshard plan (yes/no)? yes
执行完毕后,检测集群状态
root@test:/data# redis-cli --cluster check 192.168.223.135:6383
192.168.223.135:6383 (ed4bea72...) -> 1 keys | 4096 slots | 1 slaves.
192.168.223.135:6381 (7130be69...) -> 1 keys | 4096 slots | 1 slaves.
192.168.223.135:6387 (ec07e09e...) -> 1 keys | 4096 slots | 0 slaves.
192.168.223.135:6382 (7874b262...) -> 1 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.223.135:6383)
M: ed4bea7210969391e54fe5334c685b76263c4e85 192.168.223.135:6383
slots:[12288-16383] (4096 slots) master
1 additional replica(s)
S: 510af78ad32df6fa5cfd51dff27d3bef53bda01b 192.168.223.135:6386
slots: (0 slots) slave
replicates 7874b26214a08a6816cd2aaada241d84d3d12880
S: 9712b4047d92f217131b5ff8d3e42448be817bcb 192.168.223.135:6384
slots: (0 slots) slave
replicates ed4bea7210969391e54fe5334c685b76263c4e85
M: 7130be699c2d6970c4824efa69047adc85375d65 192.168.223.135:6381
slots:[1365-5460] (4096 slots) master
1 additional replica(s)
M: ec07e09e0c98c7fac6db05728f4a8236dfc25b65 192.168.223.135:6387
slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master 该节点已经成功分配槽位
为什么6387是3个新的区间,重新分配成本太高,所以前3家各自匀出来一部分,从6381/6382/6383三个旧节点分别匀出1364个坑位给新节点6387
S: 2c2fdb0a6a24a7972e5e66ee5eeabefbc139f5b9 192.168.223.135:6385
slots: (0 slots) slave
replicates 7130be699c2d6970c4824efa69047adc85375d65
M: 7874b26214a08a6816cd2aaada241d84d3d12880 192.168.223.135:6382
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.
- 为主节点6387分配从节点6388
命令: redis-cli --cluster add-node ip:新slave端口 ip:新master端口 --cluster-slave --cluster-master-id 新主机节点ID
root@test:/data# redis-cli --cluster add-node 192.168.223.135:6388 192.168.223.135:6387 --cluster-slave --cluster-master-id ec07e09e0c98c7fac6db05728f4a8236dfc25b65
>>> Adding node 192.168.223.135:6388 to cluster 192.168.223.135:6387
>>> Performing Cluster Check (using node 192.168.223.135:6387)
M: ec07e09e0c98c7fac6db05728f4a8236dfc25b65 192.168.223.135:6387
slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master
S: 2c2fdb0a6a24a7972e5e66ee5eeabefbc139f5b9 192.168.223.135:6385
slots: (0 slots) slave
replicates 7130be699c2d6970c4824efa69047adc85375d65
M: 7874b26214a08a6816cd2aaada241d84d3d12880 192.168.223.135:6382
slots:[6827-10922] (4096 slots) master
1 additional replica(s)
S: 9712b4047d92f217131b5ff8d3e42448be817bcb 192.168.223.135:6384
slots: (0 slots) slave
replicates ed4bea7210969391e54fe5334c685b76263c4e85
M: ed4bea7210969391e54fe5334c685b76263c4e85 192.168.223.135:6383
slots:[12288-16383] (4096 slots) master
1 additional replica(s)
M: 7130be699c2d6970c4824efa69047adc85375d65 192.168.223.135:6381
slots:[1365-5460] (4096 slots) master
1 additional replica(s)
S: 510af78ad32df6fa5cfd51dff27d3bef53bda01b 192.168.223.135:6386
slots: (0 slots) slave
replicates 7874b26214a08a6816cd2aaada241d84d3d12880
[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.223.135:6388 to make it join the cluster.
Waiting for the cluster to join
>>> Configure node as replica of 192.168.223.135:6387.
[OK] New node added correctly.
检查集群状态
root@test:/data# redis-cli --cluster check 192.168.223.135:6383
192.168.223.135:6383 (ed4bea72...) -> 1 keys | 4096 slots | 1 slaves.
192.168.223.135:6381 (7130be69...) -> 1 keys | 4096 slots | 1 slaves.
192.168.223.135:6387 (ec07e09e...) -> 1 keys | 4096 slots | 1 slaves.
192.168.223.135:6382 (7874b262...) -> 1 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.223.135:6383)
M: ed4bea7210969391e54fe5334c685b76263c4e85 192.168.223.135:6383
slots:[12288-16383] (4096 slots) master
1 additional replica(s)
S: 06392a32a424b1823e818fcc4c25985ae2e9e51b 192.168.223.135:6388
slots: (0 slots) slave slave已经成功加入6387
replicates ec07e09e0c98c7fac6db05728f4a8236dfc25b65
S: 510af78ad32df6fa5cfd51dff27d3bef53bda01b 192.168.223.135:6386
slots: (0 slots) slave
replicates 7874b26214a08a6816cd2aaada241d84d3d12880
S: 9712b4047d92f217131b5ff8d3e42448be817bcb 192.168.223.135:6384
slots: (0 slots) slave
replicates ed4bea7210969391e54fe5334c685b76263c4e85
M: 7130be699c2d6970c4824efa69047adc85375d65 192.168.223.135:6381
slots:[1365-5460] (4096 slots) master
1 additional replica(s)
M: ec07e09e0c98c7fac6db05728f4a8236dfc25b65 192.168.223.135:6387
slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master
1 additional replica(s)
S: 2c2fdb0a6a24a7972e5e66ee5eeabefbc139f5b9 192.168.223.135:6385
slots: (0 slots) slave
replicates 7130be699c2d6970c4824efa69047adc85375d65
M: 7874b26214a08a6816cd2aaada241d84d3d12880 192.168.223.135:6382
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.
缩容节点
- 目的:6387和6388下线
- 逻辑:检查集群状态,先删除缩容节点对应的slave节点6388,再将缩容节点6387槽号清空,重新分配,本例子将清出来的槽号都给6381,再次检查集群,删除6387,检差集群
root@test:/data# redis-cli --cluster check 192.168.223.135:6388
192.168.223.135:6387 (ec07e09e...) -> 1 keys | 4096 slots | 1 slaves.
192.168.223.135:6381 (7130be69...) -> 1 keys | 4096 slots | 1 slaves.
192.168.223.135:6383 (ed4bea72...) -> 1 keys | 4096 slots | 1 slaves.
192.168.223.135:6382 (7874b262...) -> 1 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.223.135:6388)
S: 06392a32a424b1823e818fcc4c25985ae2e9e51b 192.168.223.135:6388
slots: (0 slots) slave
replicates ec07e09e0c98c7fac6db05728f4a8236dfc25b65
M: ec07e09e0c98c7fac6db05728f4a8236dfc25b65 192.168.223.135:6387
slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master
1 additional replica(s)
M: 7130be699c2d6970c4824efa69047adc85375d65 192.168.223.135:6381
slots:[1365-5460] (4096 slots) master
1 additional replica(s)
S: 9712b4047d92f217131b5ff8d3e42448be817bcb 192.168.223.135:6384
slots: (0 slots) slave
replicates ed4bea7210969391e54fe5334c685b76263c4e85
M: ed4bea7210969391e54fe5334c685b76263c4e85 192.168.223.135:6383
slots:[12288-16383] (4096 slots) master
1 additional replica(s)
S: 510af78ad32df6fa5cfd51dff27d3bef53bda01b 192.168.223.135:6386
slots: (0 slots) slave
replicates 7874b26214a08a6816cd2aaada241d84d3d12880
M: 7874b26214a08a6816cd2aaada241d84d3d12880 192.168.223.135:6382
slots:[6827-10922] (4096 slots) master
1 additional replica(s)
S: 2c2fdb0a6a24a7972e5e66ee5eeabefbc139f5b9 192.168.223.135:6385
slots: (0 slots) slave
replicates 7130be699c2d6970c4824efa69047adc85375d65
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
可以发现6388的id为06392a32a424b1823e818fcc4c25985ae2e9e51b
- 删除6388节点
命令: redis-cli --cluster del-node ip:从机端口 从机6388节点ID
root@test:/data# redis-cli --cluster del-node 192.168.223.135:6388 06392a32a424b1823e818fcc4c25985ae2e9e51b
>>> Removing node 06392a32a424b1823e818fcc4c25985ae2e9e51b from cluster 192.168.223.135:6388
>>> Sending CLUSTER FORGET messages to the cluster...
>>> Sending CLUSTER RESET SOFT to the deleted node.
检查集群
root@test:/data# redis-cli --cluster check 192.168.223.135:6383
192.168.223.135:6383 (ed4bea72...) -> 1 keys | 4096 slots | 1 slaves.
192.168.223.135:6381 (7130be69...) -> 1 keys | 4096 slots | 1 slaves.
192.168.223.135:6387 (ec07e09e...) -> 1 keys | 4096 slots | 0 slaves.
192.168.223.135:6382 (7874b262...) -> 1 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.223.135:6383)
M: ed4bea7210969391e54fe5334c685b76263c4e85 192.168.223.135:6383
slots:[12288-16383] (4096 slots) master
1 additional replica(s)
S: 510af78ad32df6fa5cfd51dff27d3bef53bda01b 192.168.223.135:6386
slots: (0 slots) slave
replicates 7874b26214a08a6816cd2aaada241d84d3d12880
S: 9712b4047d92f217131b5ff8d3e42448be817bcb 192.168.223.135:6384
slots: (0 slots) slave
replicates ed4bea7210969391e54fe5334c685b76263c4e85
M: 7130be699c2d6970c4824efa69047adc85375d65 192.168.223.135:6381
slots:[1365-5460] (4096 slots) master
1 additional replica(s)
M: ec07e09e0c98c7fac6db05728f4a8236dfc25b65 192.168.223.135:6387
slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master
S: 2c2fdb0a6a24a7972e5e66ee5eeabefbc139f5b9 192.168.223.135:6385
slots: (0 slots) slave
replicates 7130be699c2d6970c4824efa69047adc85375d65
M: 7874b26214a08a6816cd2aaada241d84d3d12880 192.168.223.135:6382
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.
发现已经没有6388节点了 删除成功
- 再将缩容节点6387槽号清空,重新分配,本例子将清出来的槽号都给6381
root@test:/data# redis-cli --cluster reshard 192.168.223.135:6381
>>> Performing Cluster Check (using node 192.168.223.135:6381)
M: 7130be699c2d6970c4824efa69047adc85375d65 192.168.223.135:6381
slots:[1365-5460] (4096 slots) master
1 additional replica(s)
S: 2c2fdb0a6a24a7972e5e66ee5eeabefbc139f5b9 192.168.223.135:6385
slots: (0 slots) slave
replicates 7130be699c2d6970c4824efa69047adc85375d65
S: 510af78ad32df6fa5cfd51dff27d3bef53bda01b 192.168.223.135:6386
slots: (0 slots) slave
replicates 7874b26214a08a6816cd2aaada241d84d3d12880
M: ec07e09e0c98c7fac6db05728f4a8236dfc25b65 192.168.223.135:6387
slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master
M: 7874b26214a08a6816cd2aaada241d84d3d12880 192.168.223.135:6382
slots:[6827-10922] (4096 slots) master
1 additional replica(s)
S: 9712b4047d92f217131b5ff8d3e42448be817bcb 192.168.223.135:6384
slots: (0 slots) slave
replicates ed4bea7210969391e54fe5334c685b76263c4e85
M: ed4bea7210969391e54fe5334c685b76263c4e85 192.168.223.135:6383
slots:[12288-16383] (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.
How many slots do you want to move (from 1 to 16384)? 4096 将要移动的槽位 6387的槽位为4096,
What is the receiving node ID? 7130be699c2d6970c4824efa69047adc85375d65 将槽位移动给谁,我们移动到6381中,指定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: ec07e09e0c98c7fac6db05728f4a8236dfc25b65 槽位来源,6387的id
Source node #2: done
......
Do you want to proceed with the proposed reshard plan (yes/no)? yes
- 检查集群
root@test:/data# redis-cli --cluster check 192.168.223.135:6383
192.168.223.135:6383 (ed4bea72...) -> 1 keys | 4096 slots | 1 slaves.
192.168.223.135:6381 (7130be69...) -> 2 keys | 8192 slots | 1 slaves.
192.168.223.135:6387 (ec07e09e...) -> 0 keys | 0 slots | 0 slaves. 空槽位
192.168.223.135:6382 (7874b262...) -> 1 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.223.135:6383)
M: ed4bea7210969391e54fe5334c685b76263c4e85 192.168.223.135:6383
slots:[12288-16383] (4096 slots) master
1 additional replica(s)
S: 510af78ad32df6fa5cfd51dff27d3bef53bda01b 192.168.223.135:6386
slots: (0 slots) slave
replicates 7874b26214a08a6816cd2aaada241d84d3d12880
S: 9712b4047d92f217131b5ff8d3e42448be817bcb 192.168.223.135:6384
slots: (0 slots) slave
replicates ed4bea7210969391e54fe5334c685b76263c4e85
M: 7130be699c2d6970c4824efa69047adc85375d65 192.168.223.135:6381
slots:[0-6826],[10923-12287] (8192 slots) master 槽位增加了
1 additional replica(s)
M: ec07e09e0c98c7fac6db05728f4a8236dfc25b65 192.168.223.135:6387
slots: (0 slots) master 空槽位
S: 2c2fdb0a6a24a7972e5e66ee5eeabefbc139f5b9 192.168.223.135:6385
slots: (0 slots) slave
replicates 7130be699c2d6970c4824efa69047adc85375d65
M: 7874b26214a08a6816cd2aaada241d84d3d12880 192.168.223.135:6382
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.
- 移除6387节点
命令: redis-cli --cluster del-node ip:端口 节点ID
root@test:/data# redis-cli --cluster del-node 192.168.223.135:6387 ec07e09e0c98c7fac6db05728f4a8236dfc25b65
>>> Removing node ec07e09e0c98c7fac6db05728f4a8236dfc25b65 from cluster 192.168.223.135:6387
>>> Sending CLUSTER FORGET messages to the cluster...
>>> Sending CLUSTER RESET SOFT to the deleted node.
- 检查集群
root@test:/data# redis-cli --cluster check 192.168.223.135:6383
192.168.223.135:6383 (ed4bea72...) -> 1 keys | 4096 slots | 1 slaves.
192.168.223.135:6381 (7130be69...) -> 2 keys | 8192 slots | 1 slaves.
192.168.223.135:6382 (7874b262...) -> 1 keys | 4096 slots | 1 slaves.
[OK] 4 keys in 3 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.223.135:6383)
M: ed4bea7210969391e54fe5334c685b76263c4e85 192.168.223.135:6383
slots:[12288-16383] (4096 slots) master
1 additional replica(s)
S: 510af78ad32df6fa5cfd51dff27d3bef53bda01b 192.168.223.135:6386
slots: (0 slots) slave
replicates 7874b26214a08a6816cd2aaada241d84d3d12880
S: 9712b4047d92f217131b5ff8d3e42448be817bcb 192.168.223.135:6384
slots: (0 slots) slave
replicates ed4bea7210969391e54fe5334c685b76263c4e85
M: 7130be699c2d6970c4824efa69047adc85375d65 192.168.223.135:6381
slots:[0-6826],[10923-12287] (8192 slots) master
1 additional replica(s)
S: 2c2fdb0a6a24a7972e5e66ee5eeabefbc139f5b9 192.168.223.135:6385
slots: (0 slots) slave
replicates 7130be699c2d6970c4824efa69047adc85375d65
M: 7874b26214a08a6816cd2aaada241d84d3d12880 192.168.223.135:6382
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.