docker Redis集群

分布式存储算法

  • 哈希取余算法分区(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.

收工

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,324评论 6 498
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,356评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,328评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,147评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,160评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,115评论 1 296
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,025评论 3 417
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,867评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,307评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,528评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,688评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,409评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,001评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,657评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,811评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,685评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,573评论 2 353

推荐阅读更多精彩内容