1、为集群添加一个主节点
首先准备一个全新的redis文件夹,这里我们叫做为7007
[root@CentOS7 redis-cluster]# ls
[root@CentOS7 redis-cluster]# tree ├── redis-benchmark
├── redis-check-aof
├── redis-check-dump
├── redis-cli
├── redis.conf
├── redis-sentinel
└── redis-server
启动7007的redis,然后添加7007的节点
[root@CentOS7 ~]# cd /usr/local/redis-cluster/7007
[root@CentOS7 7007]# ./redis-server redis.conf
[root@CentOS7 redis-cluster]# ./redis-trib.rb add-node 192.168.133.159: 192.168.133.159:
这个时候在集群里其实已经可以但看7007的节点了
[root@CentOS7 ]# ./redis-cli -h 192.168.133.159 -p
192.168.133.159:> CLUSTER nodes
50e36c10b731d536fcf35d0478b20c6671ed3116 192.168.133.159: slave ee1d1cfbd7385893ababb0342329a55644734f4c connected
b40a94dee3200f716e8f628060348296b37952d1 192.168.133.159: master - connected -
93d1ef27a47b5e830e2f1fdce9bdaeac6ce7689d 192.168.133.159: slave b40a94dee3200f716e8f628060348296b37952d1 connected
ee1d1cfbd7385893ababb0342329a55644734f4c 192.168.133.159: myself,master - connected -
8ca1250d01509febbedc14748069ea130fdf2b76 192.168.133.159: master - connected
4e1ca9e4d966776c0ec1c37ab12e90315baef857 192.168.133.159: slave dd05edc6f7544f502c666d9b416a4063987bdbe9 connected
dd05edc6f7544f502c666d9b416a4063987bdbe9 192.168.133.159: master - connected -
但它还没有生效,因为redis的集群是对“槽“的操作,所以对于新加的节点,需要进行”槽“的再分配
[root@CentOS7 redis-cluster]# ./redis-trib.rb reshard 192.168.133.159:
比如我们这里先分配1000个槽
How many slots do you want to move (from to )?
然后输入接收点的ID
What is the receiving node ID? 8ca1250d01509febbedc14748069ea130fdf2b76
这个ID其实是通过cluster nodes的redis命令进行但看到的
最后输入源结点id
输入源结点id
,槽将从源结点中拿,分配后的槽在源结点中就不存在了。
输入all
表示从所有源结点中获取槽。
输入done
取消分配。
Source node #:all
以下就日志信息
Ready to move slots.
Source nodes:
M: ee1d1cfbd7385893ababb0342329a55644734f4c 192.168.133.159:
slots:- ( slots) master
additional replica(s)
M: b40a94dee3200f716e8f628060348296b37952d1 192.168.133.159:
slots:- ( slots) master
additional replica(s)
M: dd05edc6f7544f502c666d9b416a4063987bdbe9 192.168.133.159:
slots:- ( slots) master
additional replica(s)
Destination node:
M: 8ca1250d01509febbedc14748069ea130fdf2b76 192.168.133.159:
slots: ( slots) master
additional replica(s)
Resharding plan:
Moving slot from dd05edc6f7544f502c666d9b416a4063987bdbe9
。。。
2、为集群添加一个从节点,用它来做为7007的从节点
创建一个7008的目录,然后启动7008的进程,然后添加从节点
[root@CentOS7 ]# cd ../redis-trib.rb add-node --slave --master-id 8ca1250d01509febbedc14748069ea130fdf2b76 192.168.133.159:7008 192.168.133.159:7001
这样就完成了从节点的添加
192.168.133.159:> CLUSTER nodes
912b8dd1c7f35866ac15b263248ee729af7171e3 192.168.133.159: slave 8ca1250d01509febbedc14748069ea130fdf2b76 connected
50e36c10b731d536fcf35d0478b20c6671ed3116 192.168.133.159: slave ee1d1cfbd7385893ababb0342329a55644734f4c connected
b40a94dee3200f716e8f628060348296b37952d1 192.168.133.159: master - connected -
93d1ef27a47b5e830e2f1fdce9bdaeac6ce7689d 192.168.133.159: slave b40a94dee3200f716e8f628060348296b37952d1 connected
ee1d1cfbd7385893ababb0342329a55644734f4c 192.168.133.159: myself,master - connected -
8ca1250d01509febbedc14748069ea130fdf2b76 192.168.133.159: master - connected - - -
4e1ca9e4d966776c0ec1c37ab12e90315baef857 192.168.133.159: slave dd05edc6f7544f502c666d9b416a4063987bdbe9 connected
dd05edc6f7544f502c666d9b416a4063987bdbe9 192.168.133.159: master - connected -
3、删除一个主节点
[root@CentOS7 redis-cluster]# ./redis-trib.rb del-node 192.168.133.159: 8ca1250d01509febbedc14748069ea130fdf2b76
>>> Removing node 8ca1250d01509febbedc14748069ea130fdf2b76 from cluster 192.168.133.159:
[ERR] Node 192.168.133.159: is not empty! Reshard data away and try again.
已经占有hash槽的节点会删除失败,需要先将hash槽分配出去
首先、使用客户端连接集群后移动哈希槽,执行命令:
[root@CentOS7 redis-cluster]# ./redis-trib.rb reshard 192.168.133.159:
系统会提示我们移动多少个哈希槽,这里输入1000,因为当时我们就给7007设置了1000个哈希槽
How many slots do you want to move (from to )?
系统会提示我们接收者的ID,我们这里将这些哈希槽还给7001
What is the receiving node ID? ee1d1cfbd7385893ababb0342329a55644734f4c
系统会提示我们输入转出者的ID,最后输入done即可
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 #:8ca1250d01509febbedc14748069ea130fdf2b76
Source node #:done
系统日志:
Ready to move slots.
Source nodes:
M: 8ca1250d01509febbedc14748069ea130fdf2b76 192.168.133.159:
slots:-,-,- ( slots) master
additional replica(s)
Destination node:
M: ee1d1cfbd7385893ababb0342329a55644734f4c 192.168.133.159:
slots:- ( slots) master
additional replica(s)
Resharding plan:
Moving slot from 8ca1250d01509febbedc14748069ea130fdf2b76
日志省略了一大片,最后执行删除的步骤,删除7007的ID
[root@CentOS7 redis-cluster]# ./redis-trib.rb del-node 192.168.133.159: 8ca1250d01509febbedc14748069ea130fdf2b76
看到日志:
>>> Removing node 8ca1250d01509febbedc14748069ea130fdf2b76 from cluster 192.168.133.159:
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.
4、删除一个从节点
[root@CentOS7 redis-cluster]# ./redis-trib.rb del-node 192.168.133.159: 912b8dd1c7f35866ac15b263248ee729af7171e3
>>> Removing node 912b8dd1c7f35866ac15b263248ee729af7171e3 from cluster 192.168.133.159:
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.
5、使用客户端连接redis,一定要加-c参数,这样节点之前就可以互相跳转
[root@CentOS7 bin]# ./redis-cli -p -c
127.0.0.1:> KEYS *
(empty list or set)
127.0.0.1:> set a
-> Redirected to slot [] located at 192.168.133.159:
OK
192.168.133.159:> set b
-> Redirected to slot [] located at 192.168.133.159:
OK
192.168.133.159:> seb c
(error) ERR unknown command 'seb'
192.168.133.159:> set c
-> Redirected to slot [] located at 192.168.133.159:
OK
192.168.133.159:> get a
-> Redirected to slot [] located at 192.168.133.159:
""
192.168.133.159:> get b
-> Redirected to slot [] located at 192.168.133.159:
""
192.168.133.159:> get c
-> Redirected to slot [] located at 192.168.133.159:
""