目录
一、3主3从Redis集群配置
1、新建6个docker容器实例
2、进入容器redis-node-1并为6台机器构建集群关系
3、以6381为切入点,查看集群状态
二、主从容错切换迁移案例
1、数据读写存储
2、假如6381宕机了,他的从库6386会不会切换
三、主从扩容案例
1、新建6387、6388两个节点+新建后启动+查看是否是8个节点
2、进入6387容器实例内部
3、将新增的 6387节点(空槽号)作为master节点加入原集群
4、检查下集群信息
5、重新分配槽号
6、再次检查集群信息
7、为6287主节点分配6388从节点
8、再次检查集群信息
四、主从缩容案例
1、6387和6388下线
2、检查集群情况1 获得6388的节点ID
3、将6388删除(从集群中将4号从节点6388删除)
4、将6387的槽号清空,重新分配
5、检查集群情况2
6、将6387删除
7、检查集群情况3
Redis集群(3主3从-docker配置案例)
一、3主3从Redis集群配置
1、新建6个docker容器实例
[root@localhost usr]# docker run -d --name=redis-node-1 --net host --privileged=true -v /usr/redis/share/redis-node-1:/data redis:latest --cluster-enabled yes --appendonly yes --port 6381
[root@localhost usr]# docker run -d --name=redis-node-2 --net host --privileged=true -v /usr/redis/share/redis-node-2:/data redis:latest --cluster-enabled yes --appendonly yes --port 6382
[root@localhost usr]# docker run -d --name=redis-node-3 --net host --privileged=true -v /usr/redis/share/redis-node-3:/data redis:latest --cluster-enabled yes --appendonly yes --port 6383
[root@localhost usr]# docker run -d --name=redis-node-4 --net host --privileged=true -v /usr/redis/share/redis-node-4:/data redis:latest --cluster-enabled yes --appendonly yes --port 6384
[root@localhost usr]# docker run -d --name=redis-node-5 --net host --privileged=true -v /usr/redis/share/redis-node-5:/data redis:latest --cluster-enabled yes --appendonly yes --port 6385
[root@localhost usr]# docker run -d --name=redis-node-6 --net host --privileged=true -v /usr/redis/share/redis-node-6:/data redis:latest --cluster-enabled yes --appendonly yes --port 6386
参数详解:
--net host #使用宿主机的IP和端口,默认
--privileged=true #获取宿主机root用户权限
--cluster-enabled yes #开启Redis集群
--appendonly yes #开启数据持久化
--port 6386 #Redis端口
[root@localhost usr]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9f9f03c4e2d1 redis:latest "docker-entrypoint.s…" 6 seconds ago Up 5 seconds redis-node-6
2c79b54096cd redis:latest "docker-entrypoint.s…" 11 seconds ago Up 10 seconds redis-node-5
b1827a5d2e49 redis:latest "docker-entrypoint.s…" 17 seconds ago Up 16 seconds redis-node-4
5ac81bea561a redis:latest "docker-entrypoint.s…" 22 seconds ago Up 21 seconds redis-node-3
d4115e41eb85 redis:latest "docker-entrypoint.s…" 27 seconds ago Up 26 seconds redis-node-2
b7f06253c3ae redis:latest "docker-entrypoint.s…" 39 seconds ago Up 38 seconds redis-node-1
[root@localhost usr]#
2、进入容器redis-node-1并为6台机器构建集群关系
[root@localhost redis-node-1]# docker exec -it redis-node-1 /bin/bash
root@localhost:/data#
#构建集群
root@localhost:/data# redis-cli --cluster create 192.168.153.128:6381 192.168.153.128:6382 192.168.153.128:6383 192.168.153.128:6384 192.168.153.128:6385 192.168.153.128: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.153.128:6385 to 192.168.153.128:6381
Adding replica 192.168.153.128:6386 to 192.168.153.128:6382
Adding replica 192.168.153.128:6384 to 192.168.153.128:6383
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 343e013448c5772761c20e7118e73ee95abb6527 192.168.153.128:6381
slots:[0-5460] (5461 slots) master
M: a84d8fe5084c20bd077b45dcf89250c38dd428b7 192.168.153.128:6382
slots:[5461-10922] (5462 slots) master
M: 1f828dceacb0310fbb55384d88ef13e84a83d310 192.168.153.128:6383
slots:[10923-16383] (5461 slots) master
S: 9fe421e46bacfe26b3f787fb647bbb68b3cbbedd 192.168.153.128:6384
replicates a84d8fe5084c20bd077b45dcf89250c38dd428b7
S: 79a453b20c62c44d461433b508b8ef7de580ce72 192.168.153.128:6385
replicates 1f828dceacb0310fbb55384d88ef13e84a83d310
S: e6b026a690cae99466085985f9891cefd57fb10c 192.168.153.128:6386
replicates 343e013448c5772761c20e7118e73ee95abb6527
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.153.128:6381)
M: 343e013448c5772761c20e7118e73ee95abb6527 192.168.153.128:6381
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: a84d8fe5084c20bd077b45dcf89250c38dd428b7 192.168.153.128:6382
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
M: 1f828dceacb0310fbb55384d88ef13e84a83d310 192.168.153.128:6383
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 79a453b20c62c44d461433b508b8ef7de580ce72 192.168.153.128:6385
slots: (0 slots) slave
replicates 1f828dceacb0310fbb55384d88ef13e84a83d310
S: e6b026a690cae99466085985f9891cefd57fb10c 192.168.153.128:6386
slots: (0 slots) slave
replicates 343e013448c5772761c20e7118e73ee95abb6527
S: 9fe421e46bacfe26b3f787fb647bbb68b3cbbedd 192.168.153.128:6384
slots: (0 slots) slave
replicates a84d8fe5084c20bd077b45dcf89250c38dd428b7
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
root@localhost:/data#
3、以6381为切入点,查看集群状态
root@localhost:/data# redis-cli -p 6381 -h 192.168.153.128
192.168.153.128:6381>
192.168.153.128:6381>
192.168.153.128: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:368
cluster_stats_messages_pong_sent:406
cluster_stats_messages_sent:774
cluster_stats_messages_ping_received:401
cluster_stats_messages_pong_received:368
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:774
192.168.153.128:6381>
192.168.153.128:6381> cluster nodes
343e013448c5772761c20e7118e73ee95abb6527 192.168.153.128:6381@16381 myself,master - 0 1719382344000 1 connected 0-5460
a84d8fe5084c20bd077b45dcf89250c38dd428b7 192.168.153.128:6382@16382 master - 0 1719382345000 2 connected 5461-10922
1f828dceacb0310fbb55384d88ef13e84a83d310 192.168.153.128:6383@16383 master - 0 1719382344000 3 connected 10923-16383
79a453b20c62c44d461433b508b8ef7de580ce72 192.168.153.128:6385@16385 slave 1f828dceacb0310fbb55384d88ef13e84a83d310 0 1719382347187 3 connected
e6b026a690cae99466085985f9891cefd57fb10c 192.168.153.128:6386@16386 slave 343e013448c5772761c20e7118e73ee95abb6527 0 1719382345000 1 connected
9fe421e46bacfe26b3f787fb647bbb68b3cbbedd 192.168.153.128:6384@16384 slave a84d8fe5084c20bd077b45dcf89250c38dd428b7 0 1719382348306 2 connected
192.168.153.128:6381>
完成!
二、主从容错切换迁移案例
1、数据读写存储
1、redis 读写error 说明
现在我们在6381里边
我们set一个数据
192.168.153.128:6381> set k1 v1
(error) MOVED 12706 192.168.153.128:6383
192.168.153.128:6381>
报错了,为啥,因为他计算出k1的值应该存储在6383上,所以会报错,这不是搞笑吗,我存个数据,还要这么麻烦
需要加个参数 -c 防止路由失效 并新增两个key
root@localhost:/data# redis-cli -p 6381 -h 192.168.153.128 -c
192.168.153.128:6381>
192.168.153.128:6381> set k1 v1
-> Redirected to slot [12706] located at 192.168.153.128:6383
OK
192.168.153.128:6383>
192.168.153.128:6383> get k1
"v1"
192.168.153.128:6383>
因为是集群环境,他会随意切换。完美
在查看下集群信息
root@localhost:/data# redis-cli --cluster check 192.168.153.128:6381
192.168.153.128:6381 (343e0134...) -> 2 keys | 5461 slots | 1 slaves. #6381 有2个key
192.168.153.128:6382 (a84d8fe5...) -> 1 keys | 5462 slots | 1 slaves. #6382 有1个key
192.168.153.128:6383 (1f828dce...) -> 1 keys | 5461 slots | 1 slaves. #6383 有1个key
[OK] 4 keys in 3 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.153.128:6381)
M: 343e013448c5772761c20e7118e73ee95abb6527 192.168.153.128:6381
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: a84d8fe5084c20bd077b45dcf89250c38dd428b7 192.168.153.128:6382
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
M: 1f828dceacb0310fbb55384d88ef13e84a83d310 192.168.153.128:6383
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 79a453b20c62c44d461433b508b8ef7de580ce72 192.168.153.128:6385
slots: (0 slots) slave
replicates 1f828dceacb0310fbb55384d88ef13e84a83d310
S: e6b026a690cae99466085985f9891cefd57fb10c 192.168.153.128:6386
slots: (0 slots) slave
replicates 343e013448c5772761c20e7118e73ee95abb6527
S: 9fe421e46bacfe26b3f787fb647bbb68b3cbbedd 192.168.153.128:6384
slots: (0 slots) slave
replicates a84d8fe5084c20bd077b45dcf89250c38dd428b7
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
root@localhost:/data#
2、假如6381宕机了,他的从库6386会不会切换
1、先停止6381
[root@localhost redis-node-1]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9f9f03c4e2d1 redis:latest "docker-entrypoint.s…" 4 hours ago Up 4 hours redis-node-6
2c79b54096cd redis:latest "docker-entrypoint.s…" 4 hours ago Up 4 hours redis-node-5
b1827a5d2e49 redis:latest "docker-entrypoint.s…" 4 hours ago Up 4 hours redis-node-4
5ac81bea561a redis:latest "docker-entrypoint.s…" 4 hours ago Up 4 hours redis-node-3
d4115e41eb85 redis:latest "docker-entrypoint.s…" 4 hours ago Up 4 hours redis-node-2
b7f06253c3ae redis:latest "docker-entrypoint.s…" 4 hours ago Up 4 hours redis-node-1
[root@localhost redis-node-1]#
[root@localhost redis-node-1]# docker stop redis-node-1
redis-node-1
[root@localhost redis-node-1]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9f9f03c4e2d1 redis:latest "docker-entrypoint.s…" 4 hours ago Up 4 hours redis-node-6
2c79b54096cd redis:latest "docker-entrypoint.s…" 4 hours ago Up 4 hours redis-node-5
b1827a5d2e49 redis:latest "docker-entrypoint.s…" 4 hours ago Up 4 hours redis-node-4
5ac81bea561a redis:latest "docker-entrypoint.s…" 4 hours ago Up 4 hours redis-node-3
d4115e41eb85 redis:latest "docker-entrypoint.s…" 4 hours ago Up 4 hours redis-node-2
[root@localhost redis-node-1]#
6381已经停机了,我们进入6382,查看下信息
[root@localhost ~]# docker exec -it redis-node-2 bash
root@localhost:/data#
root@localhost:/data#
root@localhost:/data# redis-cli -h 192.168.153.128 -p 6382 -c
192.168.153.128:6382>
192.168.153.128:6382> cluster nodes
9fe421e46bacfe26b3f787fb647bbb68b3cbbedd 192.168.153.128:6384@16384 slave a84d8fe5084c20bd077b45dcf89250c38dd428b7 0 1719385316975 2 connected
a84d8fe5084c20bd077b45dcf89250c38dd428b7 192.168.153.128:6382@16382 myself,master - 0 1719385315000 2 connected 5461-10922
1f828dceacb0310fbb55384d88ef13e84a83d310 192.168.153.128:6383@16383 master - 0 1719385316000 3 connected 10923-16383
79a453b20c62c44d461433b508b8ef7de580ce72 192.168.153.128:6385@16385 slave 1f828dceacb0310fbb55384d88ef13e84a83d310 0 1719385316000 3 connected
e6b026a690cae99466085985f9891cefd57fb10c 192.168.153.128:6386@16386 master - 0 1719385318096 7 connected 0-5460
343e013448c5772761c20e7118e73ee95abb6527 192.168.153.128:6381@16381 master,fail - 1719385170602 1719385163000 1 disconnected
192.168.153.128:6382>
以前我们6381 是master 现在6381挂了,他的从节点是6386,现在看 6386成了master
刚才k3存在了6381,我们在查下,6381的从节点6386也有,说明数据同步也没问题。
192.168.153.128:6382> get k3
-> Redirected to slot [4576] located at 192.168.153.128:6386
"v3"
192.168.153.128:6386>
假如现在6381恢复了,那么他是master 还是slave?
[root@localhost redis-node-1]# docker start redis-node-1
redis-node-1
[root@localhost redis-node-1]#
192.168.153.128:6386> cluster nodes
9fe421e46bacfe26b3f787fb647bbb68b3cbbedd 192.168.153.128:6384@16384 slave a84d8fe5084c20bd077b45dcf89250c38dd428b7 0 1719385738626 2 connected
343e013448c5772761c20e7118e73ee95abb6527 192.168.153.128:6381@16381 slave e6b026a690cae99466085985f9891cefd57fb10c 0 1719385738000 7 connected
a84d8fe5084c20bd077b45dcf89250c38dd428b7 192.168.153.128:6382@16382 master - 0 1719385738000 2 connected 5461-10922
79a453b20c62c44d461433b508b8ef7de580ce72 192.168.153.128:6385@16385 slave 1f828dceacb0310fbb55384d88ef13e84a83d310 0 1719385739725 3 connected
e6b026a690cae99466085985f9891cefd57fb10c 192.168.153.128:6386@16386 myself,master - 0 1719385739000 7 connected 0-5460
1f828dceacb0310fbb55384d88ef13e84a83d310 192.168.153.128:6383@16383 master - 0 1719385736398 3 connected 10923-16383
192.168.153.128:6386>
可以看到6381 是从节点了。你走了,再来,只能是从节点了。如果还想让6381为主节点,只能先把6386停掉,再起来。然后6386就成了6381的从节点了
[root@localhost redis-node-1]# docker stop redis-node-6
redis-node-6
[root@localhost redis-node-1]#
[root@localhost redis-node-1]# docker start redis-node-6
redis-node-6
[root@localhost redis-node-1]#
[root@localhost redis-node-1]#
192.168.153.128:6382> cluster nodes
9fe421e46bacfe26b3f787fb647bbb68b3cbbedd 192.168.153.128:6384@16384 slave a84d8fe5084c20bd077b45dcf89250c38dd428b7 0 1719385960341 2 connected
a84d8fe5084c20bd077b45dcf89250c38dd428b7 192.168.153.128:6382@16382 myself,master - 0 1719385956000 2 connected 5461-10922
1f828dceacb0310fbb55384d88ef13e84a83d310 192.168.153.128:6383@16383 master - 0 1719385957000 3 connected 10923-16383
79a453b20c62c44d461433b508b8ef7de580ce72 192.168.153.128:6385@16385 slave 1f828dceacb0310fbb55384d88ef13e84a83d310 0 1719385959222 3 connected
e6b026a690cae99466085985f9891cefd57fb10c 192.168.153.128:6386@16386 slave 343e013448c5772761c20e7118e73ee95abb6527 0 1719385957000 8 connected
343e013448c5772761c20e7118e73ee95abb6527 192.168.153.128:6381@16381 master - 0 1719385959000 8 connected 0-5460
192.168.153.128:6382>
192.168.153.128:6382>
三、主从扩容案例
假如现在流量上来了,3台机器已经满足不了需求了,需要扩容,在增加两台机器,6387(主)和6388(从),该如何实现呢
如果在家一个6387的话,Hash槽该怎么分配呢?分多少呢?
1、新建6387、6388两个节点+新建后启动+查看是否是8个节点
[root@localhost redis-node-1]# docker run -d --name=redis-node-7 --net host --privileged=true -v /usr/redis/share/redis-node-7:/data redis:latest --cluster-enabled yes --appendonly yes --port 6387
ba3fd656217e38fc7743529846a1356cd58d7c021edf8482b8a0302d963d1402
[root@localhost redis-node-1]#
[root@localhost redis-node-1]# docker run -d --name=redis-node-8 --net host --privileged=true -v /usr/redis/share/redis-node-8:/data redis:latest --cluster-enabled yes --appendonly yes --port 6388
f5589a1830a14e944d84954226a811231472bc5b55e0aa1e0d5d2c6806bcd5bd
[root@localhost redis-node-1]#
[root@localhost redis-node-1]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f5589a1830a1 redis:latest "docker-entrypoint.s…" 4 seconds ago Up 3 seconds redis-node-8
ba3fd656217e redis:latest "docker-entrypoint.s…" 23 seconds ago Up 22 seconds redis-node-7
9f9f03c4e2d1 redis:latest "docker-entrypoint.s…" 4 hours ago Up 19 minutes redis-node-6
2c79b54096cd redis:latest "docker-entrypoint.s…" 4 hours ago Up 4 hours redis-node-5
b1827a5d2e49 redis:latest "docker-entrypoint.s…" 4 hours ago Up 4 hours redis-node-4
5ac81bea561a redis:latest "docker-entrypoint.s…" 4 hours ago Up 4 hours redis-node-3
d4115e41eb85 redis:latest "docker-entrypoint.s…" 4 hours ago Up 4 hours redis-node-2
b7f06253c3ae redis:latest "docker-entrypoint.s…" 4 hours ago Up 22 minutes redis-node-1
[root@localhost redis-node-1]#
2、进入6387容器实例内部
[root@localhost redis-node-1]# docker exec -it redis-node-7 bash
root@localhost:/data#
3、将新增的 6387节点(空槽号)作为master节点加入原集群
添加一个参数 add-node
命令:redis-cli --cluster add-node 实际IP:6387 实际IP:6381
就是说 6387将作为master节点新增,6381作为原来节点的领路人,相当于6387拜拜6381的码头,从而加入集群
root@localhost:/data# redis-cli --cluster add-node 192.168.153.128:6387 192.168.153.128:6381
>>> Adding node 192.168.153.128:6387 to cluster 192.168.153.128:6381
>>> Performing Cluster Check (using node 192.168.153.128:6381)
M: 343e013448c5772761c20e7118e73ee95abb6527 192.168.153.128:6381
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: 1f828dceacb0310fbb55384d88ef13e84a83d310 192.168.153.128:6383
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 9fe421e46bacfe26b3f787fb647bbb68b3cbbedd 192.168.153.128:6384
slots: (0 slots) slave
replicates a84d8fe5084c20bd077b45dcf89250c38dd428b7
S: 79a453b20c62c44d461433b508b8ef7de580ce72 192.168.153.128:6385
slots: (0 slots) slave
replicates 1f828dceacb0310fbb55384d88ef13e84a83d310
M: a84d8fe5084c20bd077b45dcf89250c38dd428b7 192.168.153.128:6382
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: e6b026a690cae99466085985f9891cefd57fb10c 192.168.153.128:6386
slots: (0 slots) slave
replicates 343e013448c5772761c20e7118e73ee95abb6527
[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.153.128:6387 to make it join the cluster.
[OK] New node added correctly.
root@localhost:/data#
4、检查下集群信息
root@localhost:/data# redis-cli --cluster check 192.168.153.128:6381
192.168.153.128:6381 (343e0134...) -> 2 keys | 5461 slots | 1 slaves.
192.168.153.128:6383 (1f828dce...) -> 1 keys | 5461 slots | 1 slaves.
192.168.153.128:6387 (a6f8bf49...) -> 0 keys | 0 slots | 0 slaves.
#从这条就能看出来 已经成功加入了集群,但是没有分配槽位,没有从节点
192.168.153.128:6382 (a84d8fe5...) -> 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.153.128:6381)
M: 343e013448c5772761c20e7118e73ee95abb6527 192.168.153.128:6381
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: 1f828dceacb0310fbb55384d88ef13e84a83d310 192.168.153.128:6383
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 9fe421e46bacfe26b3f787fb647bbb68b3cbbedd 192.168.153.128:6384
slots: (0 slots) slave
replicates a84d8fe5084c20bd077b45dcf89250c38dd428b7
M: a6f8bf499c0ca429260af33c46ba58d13a4da3f4 192.168.153.128:6387
slots: (0 slots) master
S: 79a453b20c62c44d461433b508b8ef7de580ce72 192.168.153.128:6385
slots: (0 slots) slave
replicates 1f828dceacb0310fbb55384d88ef13e84a83d310
M: a84d8fe5084c20bd077b45dcf89250c38dd428b7 192.168.153.128:6382
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: e6b026a690cae99466085985f9891cefd57fb10c 192.168.153.128:6386
slots: (0 slots) slave
replicates 343e013448c5772761c20e7118e73ee95abb6527
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
root@localhost:/data#
root@localhost:/data#
5、重新分配槽号
参数 reshard
命令:redis-cli --cluster reshard 192.168.153.128:6381
root@localhost:/data# redis-cli --cluster reshard 192.168.153.128:6381
M: a6f8bf499c0ca429260af33c46ba58d13a4da3f4 192.168.153.128:6387
slots: (0 slots) master
现在6387还是0槽位
How many slots do you want to move (from 1 to 16384)? 4096
What is the receiving node ID? a6f8bf499c0ca429260af33c46ba58d13a4da3f4
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
6、再次检查集群信息
root@localhost:/data# redis-cli --cluster check 192.168.153.128:6381
192.168.153.128:6381 (343e0134...) -> 1 keys | 4096 slots | 1 slaves.
192.168.153.128:6383 (1f828dce...) -> 1 keys | 4096 slots | 1 slaves.
192.168.153.128:6387 (a6f8bf49...) -> 1 keys | 4096 slots | 0 slaves.
192.168.153.128:6382 (a84d8fe5...) -> 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.153.128:6381)
M: 343e013448c5772761c20e7118e73ee95abb6527 192.168.153.128:6381
slots:[1365-5460] (4096 slots) master
1 additional replica(s)
M: 1f828dceacb0310fbb55384d88ef13e84a83d310 192.168.153.128:6383
slots:[12288-16383] (4096 slots) master
1 additional replica(s)
S: 9fe421e46bacfe26b3f787fb647bbb68b3cbbedd 192.168.153.128:6384
slots: (0 slots) slave
replicates a84d8fe5084c20bd077b45dcf89250c38dd428b7
M: a6f8bf499c0ca429260af33c46ba58d13a4da3f4 192.168.153.128:6387
slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master
S: 79a453b20c62c44d461433b508b8ef7de580ce72 192.168.153.128:6385
slots: (0 slots) slave
replicates 1f828dceacb0310fbb55384d88ef13e84a83d310
M: a84d8fe5084c20bd077b45dcf89250c38dd428b7 192.168.153.128:6382
slots:[6827-10922] (4096 slots) master
1 additional replica(s)
S: e6b026a690cae99466085985f9891cefd57fb10c 192.168.153.128:6386
slots: (0 slots) slave
replicates 343e013448c5772761c20e7118e73ee95abb6527
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
root@localhost:/data#
可以看到6387的槽位是slots:[0-1364],[5461-6826],[10923-12287]
因为重新分配的成本太高,所以前3家各自匀出来一部分给6387
7、为6287主节点分配6388从节点
命令:
redis-cli --cluster add-node IP:新slave端口 IP:新master端口 --cluster-slave --cluster-master-id 新主节点ID
root@localhost:/data# redis-cli --cluster add-node 192.168.153.128:6388 192.168.153.128:6387 --cluster-slave --cluster-master-id a6f8bf499c0ca429260af33c46ba58d13a4da3f4
>>> Adding node 192.168.153.128:6388 to cluster 192.168.153.128:6387
>>> Performing Cluster Check (using node 192.168.153.128:6387)
M: a6f8bf499c0ca429260af33c46ba58d13a4da3f4 192.168.153.128:6387
slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master
S: e6b026a690cae99466085985f9891cefd57fb10c 192.168.153.128:6386
slots: (0 slots) slave
replicates 343e013448c5772761c20e7118e73ee95abb6527
M: 343e013448c5772761c20e7118e73ee95abb6527 192.168.153.128:6381
slots:[1365-5460] (4096 slots) master
1 additional replica(s)
M: a84d8fe5084c20bd077b45dcf89250c38dd428b7 192.168.153.128:6382
slots:[6827-10922] (4096 slots) master
1 additional replica(s)
S: 9fe421e46bacfe26b3f787fb647bbb68b3cbbedd 192.168.153.128:6384
slots: (0 slots) slave
replicates a84d8fe5084c20bd077b45dcf89250c38dd428b7
S: 79a453b20c62c44d461433b508b8ef7de580ce72 192.168.153.128:6385
slots: (0 slots) slave
replicates 1f828dceacb0310fbb55384d88ef13e84a83d310
M: 1f828dceacb0310fbb55384d88ef13e84a83d310 192.168.153.128: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.
>>> Send CLUSTER MEET to node 192.168.153.128:6388 to make it join the cluster.
Waiting for the cluster to join
>>> Configure node as replica of 192.168.153.128:6387.
[OK] New node added correctly.
root@localhost:/data#
8、再次检查集群信息
root@localhost:/data# redis-cli --cluster check 192.168.153.128:6381
192.168.153.128:6381 (343e0134...) -> 1 keys | 4096 slots | 1 slaves.
192.168.153.128:6383 (1f828dce...) -> 1 keys | 4096 slots | 1 slaves.
192.168.153.128:6387 (a6f8bf49...) -> 1 keys | 4096 slots | 1 slaves.
192.168.153.128:6382 (a84d8fe5...) -> 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.153.128:6381)
M: 343e013448c5772761c20e7118e73ee95abb6527 192.168.153.128:6381
slots:[1365-5460] (4096 slots) master
1 additional replica(s)
M: 1f828dceacb0310fbb55384d88ef13e84a83d310 192.168.153.128:6383
slots:[12288-16383] (4096 slots) master
1 additional replica(s)
S: 9fe421e46bacfe26b3f787fb647bbb68b3cbbedd 192.168.153.128:6384
slots: (0 slots) slave
replicates a84d8fe5084c20bd077b45dcf89250c38dd428b7
S: ff95cd3de6ede6eb1b5b073248f718e24b4fae6e 192.168.153.128:6388
slots: (0 slots) slave
replicates a6f8bf499c0ca429260af33c46ba58d13a4da3f4
M: a6f8bf499c0ca429260af33c46ba58d13a4da3f4 192.168.153.128:6387
slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master
1 additional replica(s)
S: 79a453b20c62c44d461433b508b8ef7de580ce72 192.168.153.128:6385
slots: (0 slots) slave
replicates 1f828dceacb0310fbb55384d88ef13e84a83d310
M: a84d8fe5084c20bd077b45dcf89250c38dd428b7 192.168.153.128:6382
slots:[6827-10922] (4096 slots) master
1 additional replica(s)
S: e6b026a690cae99466085985f9891cefd57fb10c 192.168.153.128:6386
slots: (0 slots) slave
replicates 343e013448c5772761c20e7118e73ee95abb6527
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
root@localhost:/data#
四、主从缩容案例
刚才是流量高峰,3主3从,满足不了业务需求,我们扩容至4主4从,现在高峰已经过去了,用不了4主4从了,要进行缩容
该怎么操作?删除6387和6388,恢复3主3从
1、6387和6388下线
2、检查集群情况1 获得6388的节点ID
root@localhost:/data# redis-cli --cluster check 192.168.153.128:6381
S: ff95cd3de6ede6eb1b5b073248f718e24b4fae6e 192.168.153.128:6388
slots: (0 slots) slave
replicates a6f8bf499c0ca429260af33c46ba58d13a4da3f4
3、将6388删除(从集群中将4号从节点6388删除)
命令:del-node
redis-cli --cluster del-node ip:从机端口 从机6388 ID
root@localhost:/data# redis-cli --cluster del-node 192.168.153.128:6388 ff95cd3de6ede6eb1b5b073248f718e24b4fae6e
>>> Removing node ff95cd3de6ede6eb1b5b073248f718e24b4fae6e from cluster 192.168.153.128:6388
>>> Sending CLUSTER FORGET messages to the cluster...
>>> Sending CLUSTER RESET SOFT to the deleted node.
root@localhost:/data#
root@localhost:/data#
root@localhost:/data# redis-cli --cluster check 192.168.153.128:6381
192.168.153.128:6381 (343e0134...) -> 1 keys | 4096 slots | 1 slaves.
192.168.153.128:6383 (1f828dce...) -> 1 keys | 4096 slots | 1 slaves.
192.168.153.128:6387 (a6f8bf49...) -> 1 keys | 4096 slots | 0 slaves.
192.168.153.128:6382 (a84d8fe5...) -> 1 keys | 4096 slots | 1 slaves.
4、将6387的槽号清空,重新分配
本例将清出来的 槽号给6381,也可以每个节点都给一点,
重新洗牌
root@localhost:/data# redis-cli --cluster reshard 192.168.153.128:6381
How many slots do you want to move (from 1 to 16384)? 4096
What is the receiving node ID?343e013448c5772761c20e7118e73ee95abb6527 #谁来接手 6381的节点ID
Source node #1: a6f8bf499c0ca429260af33c46ba58d13a4da3f4 #6387的节点ID,告知删除哪个
Source node #2: done
Do you want to proceed with the proposed reshard plan (yes/no)? yes
5、检查集群情况2
root@localhost:/data# redis-cli --cluster check 192.168.153.128:6381
192.168.153.128:6381 (343e0134...) -> 2 keys | 8192 slots | 1 slaves.
192.168.153.128:6383 (1f828dce...) -> 1 keys | 4096 slots | 1 slaves.
192.168.153.128:6387 (a6f8bf49...) -> 0 keys | 0 slots | 0 slaves.
192.168.153.128:6382 (a84d8fe5...) -> 1 keys | 4096 slots | 1 slaves.
#可以看到 6387的槽位已经为0了,6381的槽位是2个4096
[OK] 4 keys in 4 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.153.128:6381)
M: 343e013448c5772761c20e7118e73ee95abb6527 192.168.153.128:6381
slots:[0-6826],[10923-12287] (8192 slots) master
1 additional replica(s)
M: 1f828dceacb0310fbb55384d88ef13e84a83d310 192.168.153.128:6383
slots:[12288-16383] (4096 slots) master
1 additional replica(s)
S: 9fe421e46bacfe26b3f787fb647bbb68b3cbbedd 192.168.153.128:6384
slots: (0 slots) slave
replicates a84d8fe5084c20bd077b45dcf89250c38dd428b7
M: a6f8bf499c0ca429260af33c46ba58d13a4da3f4 192.168.153.128:6387
slots: (0 slots) master
S: 79a453b20c62c44d461433b508b8ef7de580ce72 192.168.153.128:6385
slots: (0 slots) slave
replicates 1f828dceacb0310fbb55384d88ef13e84a83d310
M: a84d8fe5084c20bd077b45dcf89250c38dd428b7 192.168.153.128:6382
slots:[6827-10922] (4096 slots) master
1 additional replica(s)
S: e6b026a690cae99466085985f9891cefd57fb10c 192.168.153.128:6386
slots: (0 slots) slave
replicates 343e013448c5772761c20e7118e73ee95abb6527
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
root@localhost:/data#
root@localhost:/data#
6、将6387删除
root@localhost:/data# redis-cli --cluster del-node 192.168.153.128:6387 a6f8bf499c0ca429260af33c46ba58d13a4da3f4
>>> Removing node a6f8bf499c0ca429260af33c46ba58d13a4da3f4 from cluster 192.168.153.128:6387
>>> Sending CLUSTER FORGET messages to the cluster...
>>> Sending CLUSTER RESET SOFT to the deleted node.
root@localhost:/data#
7、检查集群情况3
root@localhost:/data# redis-cli --cluster check 192.168.153.128:6381
192.168.153.128:6381 (343e0134...) -> 2 keys | 8192 slots | 1 slaves.
192.168.153.128:6383 (1f828dce...) -> 1 keys | 4096 slots | 1 slaves.
192.168.153.128:6382 (a84d8fe5...) -> 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.153.128:6381)
M: 343e013448c5772761c20e7118e73ee95abb6527 192.168.153.128:6381
slots:[0-6826],[10923-12287] (8192 slots) master
1 additional replica(s)
M: 1f828dceacb0310fbb55384d88ef13e84a83d310 192.168.153.128:6383
slots:[12288-16383] (4096 slots) master
1 additional replica(s)
S: 9fe421e46bacfe26b3f787fb647bbb68b3cbbedd 192.168.153.128:6384
slots: (0 slots) slave
replicates a84d8fe5084c20bd077b45dcf89250c38dd428b7
S: 79a453b20c62c44d461433b508b8ef7de580ce72 192.168.153.128:6385
slots: (0 slots) slave
replicates 1f828dceacb0310fbb55384d88ef13e84a83d310
M: a84d8fe5084c20bd077b45dcf89250c38dd428b7 192.168.153.128:6382
slots:[6827-10922] (4096 slots) master
1 additional replica(s)
S: e6b026a690cae99466085985f9891cefd57fb10c 192.168.153.128:6386
slots: (0 slots) slave
replicates 343e013448c5772761c20e7118e73ee95abb6527
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
root@localhost:/data#