添加1主1从到集群
添加7007到集群
[root@kafka-1 7002]# redis-cli -a 'Mwbktest821' --cluster add-node 172.16.3.76:7007 172.16.3.76:7001
[root@kafka-1 ~]# redis-cli -c -a Mwbktest821 -h 172.16.3.76 -p 7001
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
172.16.3.76:7001> CLUSTER NODES
4a74f58ece0bf8ff7316a6c7a2b5d98115f9ae52 172.16.3.76:7003@17003 slave 766a2d0d41c5f4eabd6210044077918593e80faf 0 1566442218689 7 connected
22ba8dc349761beffeb626ac060ae101f8546555 172.16.3.76:7002@17002 master - 0 1566442216000 2 connected 5461-10922
629d74fecb658c62a4a849736d7aeb69af0ee549 172.16.3.76:7007@17007 master - 0 1566442217000 0 connected
766a2d0d41c5f4eabd6210044077918593e80faf 172.16.3.76:7006@17006 master - 0 1566442218000 7 connected 10923-16383
98fe0ab2bc8c7080c90063f2966fe68759043ba7 172.16.3.76:7001@17001 myself,master - 0 1566442216000 1 connected 0-5460
69084a7e7ef332e140cd779e92b30afee69f9ba2 172.16.3.76:7004@17004 slave 98fe0ab2bc8c7080c90063f2966fe68759043ba7 0 1566442218000 4 connected
4d19efc4dca7cd3b2d42caeacbb45474c2bd2eca 172.16.3.76:7005@17005 slave 22ba8dc349761beffeb626ac060ae101f8546555 0 1566442219690 5 connected
7007加入集群后,是没有分配hash槽
手工给7007分配hash槽,连接集群任意一个节点,给集群重新分片
[root@kafka-1 ~]# redis-cli -a Mwbktest821 --cluster reshard 172.16.3.76:7001
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing Cluster Check (using node 172.16.3.76:7001)
M: 98fe0ab2bc8c7080c90063f2966fe68759043ba7 172.16.3.76:7001
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: 4a74f58ece0bf8ff7316a6c7a2b5d98115f9ae52 172.16.3.76:7003
slots: (0 slots) slave
replicates 766a2d0d41c5f4eabd6210044077918593e80faf
M: 22ba8dc349761beffeb626ac060ae101f8546555 172.16.3.76:7002
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
M: 629d74fecb658c62a4a849736d7aeb69af0ee549 172.16.3.76:7007
slots: (0 slots) master
M: 766a2d0d41c5f4eabd6210044077918593e80faf 172.16.3.76:7006
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 69084a7e7ef332e140cd779e92b30afee69f9ba2 172.16.3.76:7004
slots: (0 slots) slave
replicates 98fe0ab2bc8c7080c90063f2966fe68759043ba7
S: 4d19efc4dca7cd3b2d42caeacbb45474c2bd2eca 172.16.3.76:7005
slots: (0 slots) slave
replicates 22ba8dc349761beffeb626ac060ae101f8546555
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
询问分出多少个槽出来? 1500
分给哪个节点? 7007
How many slots do you want to move (from 1 to 16384)? 1500
What is the receiving node ID? 629d74fecb658c62a4a849736d7aeb69af0ee549
现有集群节点分出hash槽数量,有两种方式:
一种是all,所以节点分出出接近平均值的hash槽数
一种是指定节点,可指定多个节点共同出所有的hash槽数
Please enter all the source node IDs.
Type 'all' to use all the nodes as source nodes for the hash slots.
Type 'done' once you entered all the source nodes IDs.
Source node #1: all
all: 7001分配499,7002分配500,7006分配501
如指定7001节点,7001分配1500
Ready to move 1500 slots.
Source nodes:
M: 98fe0ab2bc8c7080c90063f2966fe68759043ba7 172.16.3.76:7001
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: 22ba8dc349761beffeb626ac060ae101f8546555 172.16.3.76:7002
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
M: 766a2d0d41c5f4eabd6210044077918593e80faf 172.16.3.76:7006
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
Destination node:
M: 629d74fecb658c62a4a849736d7aeb69af0ee549 172.16.3.76:7007
slots: (0 slots) master
....
查看节点信息
[root@kafka-1 ~]# redis-cli -a Mwbktest821 -c -h 172.16.3.76 -p 7001
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
172.16.3.76:7001> CLUSTER NODES
4a74f58ece0bf8ff7316a6c7a2b5d98115f9ae52 172.16.3.76:7003@17003 slave 766a2d0d41c5f4eabd6210044077918593e80faf 0 1566442748000 7 connected
22ba8dc349761beffeb626ac060ae101f8546555 172.16.3.76:7002@17002 master - 0 1566442746000 2 connected 5962-10922
629d74fecb658c62a4a849736d7aeb69af0ee549 172.16.3.76:7007@17007 master - 0 1566442745788 8 connected 0-498 5461-5961 10923-11421
766a2d0d41c5f4eabd6210044077918593e80faf 172.16.3.76:7006@17006 master - 0 1566442745000 7 connected 11422-16383
98fe0ab2bc8c7080c90063f2966fe68759043ba7 172.16.3.76:7001@17001 myself,master - 0 1566442747000 1 connected 499-5460
69084a7e7ef332e140cd779e92b30afee69f9ba2 172.16.3.76:7004@17004 slave 98fe0ab2bc8c7080c90063f2966fe68759043ba7 0 1566442747793 4 connected
4d19efc4dca7cd3b2d42caeacbb45474c2bd2eca 172.16.3.76:7005@17005 slave 22ba8dc349761beffeb626ac060ae101f8546555 0 1566442748793 5 connected
添加7008到集群,做7007的从节点
[root@kafka-1 ~]# redis-cli -a Mwbktest821 --cluster add-node 172.16.3.76:7008 172.16.3.76:7001
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Adding node 172.16.3.76:7008 to cluster 172.16.3.76:7001
>>> Performing Cluster Check (using node 172.16.3.76:7001)
M: 98fe0ab2bc8c7080c90063f2966fe68759043ba7 172.16.3.76:7001
slots:[499-5460] (4962 slots) master
1 additional replica(s)
S: 4a74f58ece0bf8ff7316a6c7a2b5d98115f9ae52 172.16.3.76:7003
slots: (0 slots) slave
replicates 766a2d0d41c5f4eabd6210044077918593e80faf
M: 22ba8dc349761beffeb626ac060ae101f8546555 172.16.3.76:7002
slots:[5962-10922] (4961 slots) master
1 additional replica(s)
M: 629d74fecb658c62a4a849736d7aeb69af0ee549 172.16.3.76:7007
slots:[0-498],[5461-5961],[10923-11421] (1499 slots) master
M: 766a2d0d41c5f4eabd6210044077918593e80faf 172.16.3.76:7006
slots:[11422-16383] (4962 slots) master
1 additional replica(s)
S: 69084a7e7ef332e140cd779e92b30afee69f9ba2 172.16.3.76:7004
slots: (0 slots) slave
replicates 98fe0ab2bc8c7080c90063f2966fe68759043ba7
S: 4d19efc4dca7cd3b2d42caeacbb45474c2bd2eca 172.16.3.76:7005
slots: (0 slots) slave
replicates 22ba8dc349761beffeb626ac060ae101f8546555
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 172.16.3.76:7008 to make it join the cluster.
[OK] New node added correctly.
查看集群状态
[root@kafka-1 ~]# redis-cli -a Mwbktest821 -c -h 172.16.3.76 -p 7008
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
172.16.3.76:7008> cluster nodes
e300ca72ddccdf940a12175d1c064e1f632769d1 172.16.3.76:7008@17008 myself,master - 0 1566443603000 0 connected
98fe0ab2bc8c7080c90063f2966fe68759043ba7 172.16.3.76:7001@17001 master - 0 1566443605322 1 connected 499-5460
4d19efc4dca7cd3b2d42caeacbb45474c2bd2eca 172.16.3.76:7005@17005 slave 22ba8dc349761beffeb626ac060ae101f8546555 0 1566443604000 2 connected
69084a7e7ef332e140cd779e92b30afee69f9ba2 172.16.3.76:7004@17004 slave 98fe0ab2bc8c7080c90063f2966fe68759043ba7 0 1566443601000 1 connected
4a74f58ece0bf8ff7316a6c7a2b5d98115f9ae52 172.16.3.76:7003@17003 slave 766a2d0d41c5f4eabd6210044077918593e80faf 0 1566443604319 7 connected
22ba8dc349761beffeb626ac060ae101f8546555 172.16.3.76:7002@17002 master - 0 1566443603315 2 connected 5962-10922
629d74fecb658c62a4a849736d7aeb69af0ee549 172.16.3.76:7007@17007 master - 0 1566443606325 8 connected 0-498 5461-5961 10923-11421
766a2d0d41c5f4eabd6210044077918593e80faf 172.16.3.76:7006@17006 master - 0 1566443604000 7 connected 11422-16383
7008还是主节点,没有分配hash槽
需要手动连接7008节点,执行replicate命令来指定当前节点的主节点ID为哪个
[root@kafka-1 ~]# redis-cli -a Mwbktest821 -c -h 172.16.3.76 -p 7008
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
172.16.3.76:7008> cluster nodes
e300ca72ddccdf940a12175d1c064e1f632769d1 172.16.3.76:7008@17008 myself,master - 0 1566443893000 0 connected
98fe0ab2bc8c7080c90063f2966fe68759043ba7 172.16.3.76:7001@17001 master - 0 1566443892000 1 connected 499-5460
4d19efc4dca7cd3b2d42caeacbb45474c2bd2eca 172.16.3.76:7005@17005 slave 22ba8dc349761beffeb626ac060ae101f8546555 0 1566443892000 2 connected
69084a7e7ef332e140cd779e92b30afee69f9ba2 172.16.3.76:7004@17004 slave 98fe0ab2bc8c7080c90063f2966fe68759043ba7 0 1566443894912 1 connected
4a74f58ece0bf8ff7316a6c7a2b5d98115f9ae52 172.16.3.76:7003@17003 slave 766a2d0d41c5f4eabd6210044077918593e80faf 0 1566443892906 7 connected
22ba8dc349761beffeb626ac060ae101f8546555 172.16.3.76:7002@17002 master - 0 1566443893000 2 connected 5962-10922
629d74fecb658c62a4a849736d7aeb69af0ee549 172.16.3.76:7007@17007 master - 0 1566443891905 8 connected 0-498 5461-5961 10923-11421
766a2d0d41c5f4eabd6210044077918593e80faf 172.16.3.76:7006@17006 master - 0 1566443893909 7 connected 11422-16383
172.16.3.76:7008> cluster replicate 629d74fecb658c62a4a849736d7aeb69af0ee549
OK
172.16.3.76:7008> cluster nodes
e300ca72ddccdf940a12175d1c064e1f632769d1 172.16.3.76:7008@17008 myself,slave 629d74fecb658c62a4a849736d7aeb69af0ee549 0 1566443925000 0 connected
98fe0ab2bc8c7080c90063f2966fe68759043ba7 172.16.3.76:7001@17001 master - 0 1566443924970 1 connected 499-5460
4d19efc4dca7cd3b2d42caeacbb45474c2bd2eca 172.16.3.76:7005@17005 slave 22ba8dc349761beffeb626ac060ae101f8546555 0 1566443927973 2 connected
69084a7e7ef332e140cd779e92b30afee69f9ba2 172.16.3.76:7004@17004 slave 98fe0ab2bc8c7080c90063f2966fe68759043ba7 0 1566443925000 1 connected
4a74f58ece0bf8ff7316a6c7a2b5d98115f9ae52 172.16.3.76:7003@17003 slave 766a2d0d41c5f4eabd6210044077918593e80faf 0 1566443927000 7 connected
22ba8dc349761beffeb626ac060ae101f8546555 172.16.3.76:7002@17002 master - 0 1566443926000 2 connected 5962-10922
629d74fecb658c62a4a849736d7aeb69af0ee549 172.16.3.76:7007@17007 master - 0 1566443926970 8 connected 0-498 5461-5961 10923-11421
766a2d0d41c5f4eabd6210044077918593e80faf 172.16.3.76:7006@17006 master - 0 1566443928975 7 connected 11422-16383
扩展1主一从到集群已经完成。
删除节点
删除7008从节点
使用del-node参数
[root@kafka-1 ~]# redis-cli -a Mwbktest821 --cluster del-node 172.16.3.76:7008 e300ca72ddccdf940a12175d1c064e1f632769d1
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Removing node e300ca72ddccdf940a12175d1c064e1f632769d1 from cluster 172.16.3.76:7008
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.
[root@kafka-1 ~]# ps -ef |grep redis
root 5048 1 0 Aug21 ? 00:01:09 redis-server 172.16.3.76:7001 [cluster]
root 5049 1 0 Aug21 ? 00:01:09 redis-server 172.16.3.76:7002 [cluster]
root 5051 1 0 Aug21 ? 00:01:08 redis-server 172.16.3.76:7006 [cluster]
root 5053 1 0 Aug21 ? 00:01:08 redis-server 172.16.3.76:7005 [cluster]
root 5054 1 0 Aug21 ? 00:01:03 redis-server 172.16.3.76:7007 [cluster]
root 5055 1 0 Aug21 ? 00:01:08 redis-server 172.16.3.76:7004 [cluster]
root 5695 1 0 Aug21 ? 00:01:04 redis-server 172.16.3.76:7003 [cluster]
root 12262 11839 0 11:23 pts/0 00:00:00 grep --color=auto redis
[root@kafka-1 ~]# redis-cli -a Mwbktest821 -c -h 172.16.3.76 -p 7001
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
172.16.3.76:7001> cluster nodes
4a74f58ece0bf8ff7316a6c7a2b5d98115f9ae52 172.16.3.76:7003@17003 slave 766a2d0d41c5f4eabd6210044077918593e80faf 0 1566444488000 7 connected
22ba8dc349761beffeb626ac060ae101f8546555 172.16.3.76:7002@17002 master - 0 1566444490000 2 connected 5962-10922
629d74fecb658c62a4a849736d7aeb69af0ee549 172.16.3.76:7007@17007 master - 0 1566444492262 8 connected 0-498 5461-5961 10923-11421
766a2d0d41c5f4eabd6210044077918593e80faf 172.16.3.76:7006@17006 master - 0 1566444491259 7 connected 11422-16383
98fe0ab2bc8c7080c90063f2966fe68759043ba7 172.16.3.76:7001@17001 myself,master - 0 1566444489000 1 connected 499-5460
69084a7e7ef332e140cd779e92b30afee69f9ba2 172.16.3.76:7004@17004 slave 98fe0ab2bc8c7080c90063f2966fe68759043ba7 0 1566444491000 4 connected
4d19efc4dca7cd3b2d42caeacbb45474c2bd2eca 172.16.3.76:7005@17005 slave 22ba8dc349761beffeb626ac060ae101f8546555 0 1566444489258 5 connected
先7008已经从集群移除,7008的服务也已经停掉
删除7007主节点
因为主节点上已分配hash槽,如果直接删除,会造成数据丢失。
需要先把7007的hash槽移走,再删除节点。
节点hash槽可以迁移多次,比如迁移499个到7001,迁移500个到7002,迁移501个到7006
[root@kafka-1 ~]# redis-cli -a Mwbktest821 --cluster reshard 172.16.3.76:7007
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing Cluster Check (using node 172.16.3.76:7007)
M: 629d74fecb658c62a4a849736d7aeb69af0ee549 172.16.3.76:7007
slots:[5961],[10923-12921] (2000 slots) master
M: 766a2d0d41c5f4eabd6210044077918593e80faf 172.16.3.76:7006
slots:[12922-16383] (3462 slots) master
1 additional replica(s)
M: 22ba8dc349761beffeb626ac060ae101f8546555 172.16.3.76:7002
slots:[5461-5960],[5962-10922] (5461 slots) master
1 additional replica(s)
S: 4d19efc4dca7cd3b2d42caeacbb45474c2bd2eca 172.16.3.76:7005
slots: (0 slots) slave
replicates 22ba8dc349761beffeb626ac060ae101f8546555
M: 98fe0ab2bc8c7080c90063f2966fe68759043ba7 172.16.3.76:7001
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: 4a74f58ece0bf8ff7316a6c7a2b5d98115f9ae52 172.16.3.76:7003
slots: (0 slots) slave
replicates 766a2d0d41c5f4eabd6210044077918593e80faf
S: 69084a7e7ef332e140cd779e92b30afee69f9ba2 172.16.3.76:7004
slots: (0 slots) slave
replicates 98fe0ab2bc8c7080c90063f2966fe68759043ba7
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
7007迁移500个hash槽到7002,迁移时槽位编号从小到大迁移。
How many slots do you want to move (from 1 to 16384)? 500
What is the receiving node ID? 22ba8dc349761beffeb626ac060ae101f8546555
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: 629d74fecb658c62a4a849736d7aeb69af0ee549
Source node #2: done
其他迁移类似,不展示了
删除7007节点
[root@kafka-1 ~]# redis-cli -a Mwbktest821 --cluster del-node 172.16.3.76:7007 629d74fecb658c62a4a849736d7aeb69af0ee549
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Removing node 629d74fecb658c62a4a849736d7aeb69af0ee549 from cluster 172.16.3.76:7007
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.
查看集群状态,已经成功删除7007
[root@kafka-1 ~]# redis-cli -a Mwbktest821 -c -h 172.16.3.76 -p 7001
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
172.16.3.76:7001> cluster nodes
4a74f58ece0bf8ff7316a6c7a2b5d98115f9ae52 172.16.3.76:7003@17003 slave 766a2d0d41c5f4eabd6210044077918593e80faf 0 1566446200470 13 connected
22ba8dc349761beffeb626ac060ae101f8546555 172.16.3.76:7002@17002 master - 0 1566446202476 12 connected 5461-10922
766a2d0d41c5f4eabd6210044077918593e80faf 172.16.3.76:7006@17006 master - 0 1566446199468 13 connected 10923-16383
98fe0ab2bc8c7080c90063f2966fe68759043ba7 172.16.3.76:7001@17001 myself,master - 0 1566446198000 11 connected 0-5460
69084a7e7ef332e140cd779e92b30afee69f9ba2 172.16.3.76:7004@17004 slave 98fe0ab2bc8c7080c90063f2966fe68759043ba7 0 1566446200000 11 connected
4d19efc4dca7cd3b2d42caeacbb45474c2bd2eca 172.16.3.76:7005@17005 slave 22ba8dc349761beffeb626ac060ae101f8546555 0 1566446201473 12 connected
主节点删除完毕。