1、模拟环境准备
Cluster节点6个,3主三从
192.168.0.143:7379
192.168.0.143:7380
192.168.0.143:7381
192.168.0.143:7382
192.168.0.143:7383
192.168.0.143:7384
新增两个节点
192.168.0.143:7385
192.168.0.143:7386
模拟环境总共8接redis进程,6个节点组成一个cluster
2、把节点加入到集群
redis-cli --cluster add-node 192.168.0.143:7385 192.168.0.143:7379
备注:192.168.0.143:7379是一个slave节点
As you can see I used the add-node command specifying the address of the new node as first argument, and the address of a random existing node in the cluster as second argument.
第一个参数是新增的节点,第二个参数原集群里面的任意节点
节点是新增了,但是新增的节点并没有被分配hash槽,因此需要重新分配hash槽
3、重新切分集群(Resharding the cluster)
redis-cli --cluster reshard 192.168.0.143:7379
如图所示,从192.168.0.143:7379节点重新分配多少个槽
输入:1500,回车
输入:28426726990ccb3b3e532ea6666ba9721fd3c903
节点192.168.0.143:7385的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.
Resharding后,执行cluster nodes
4、增加从节点
redis-cli --cluster add-node 192.168.0.143:7386 192.168.0.143:7385 --cluster-slave
Note that the command line here is exactly like the one we used to add a new master, so we are not specifying to which master we want to add the replica. In this case what happens is that redis-cli will add the new node as replica of a random master among the masters with less replicas.
说明:192.168.0.143:7386节点为需要新增的节点,192.168.0.143:7385为集群中任意节点,默认新增从节点会分配到原先集群中没有从节点的主节点
192.168.0.143:7386作为从节点已经加入到集群中
如果,集群中的节点已经是偶数个,并且是一主一从的配置,那么新增一个从节点
redis-cli --cluster add-node 192.168.0.143:7387 192.168.0.143:7383 --cluster-slave
实际运行结果新增slave节点被分配给192.168.0.143:7385作为从节点,实际运行结果符合官方文档:第二个参数是192.168.0.143:7383集群中的任意节点。
5、增加slave到指定master
redis-cli --cluster add-node 192.168.0.143:7388 192.168.0.143:7379 --cluster-slave --cluster-master-id afb53591fb0d10071e6ecd6de5153e6ddc18b6e4
说明:
1.192.168.0.143:7379是一个master节点
2.afb53591fb0d10071e6ecd6de5153e6ddc18b6e4 是192.168.0.143:7384的ID;
6、删除一个从节点
redis-cli --cluster del-node 192.168.0.143:7384 47271c934fd92fa20fb2cbbc35b850ed8007e141
The first argument is just a random node in the cluster, the second argument is the ID of the node you want to remove.
第一个参数是集群中任意节点,第二个参数要删除的节点
如图,从节点删除后,节点从集群中移除,并且进程也被关闭