[docker]redis-cluster的使用

Posted 蟒城贝勒爷

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[docker]redis-cluster的使用相关的知识,希望对你有一定的参考价值。

1、为集群添加一个主节点

首先准备一个全新的redis文件夹,这里我们叫做为7007

[[email protected] redis-cluster]# ls
7001  7002  7003  7004  7005  7006  7007
[[email protected] redis-cluster]# tree 7007
7007
├── redis-benchmark
├── redis-check-aof
├── redis-check-dump
├── redis-cli
├── redis.conf
├── redis-sentinel
└── redis-server

启动7007的redis,然后添加7007的节点

[[email protected] ~]# cd /usr/local/redis-cluster/7007
[[email protected] 7007]# ./redis-server redis.conf

[[email protected] redis-cluster]# ./redis-trib.rb add-node 192.168.133.159:7007 192.168.133.159:7001

这个时候在集群里其实已经可以但看7007的节点了

[[email protected] 7001]# ./redis-cli -h 192.168.133.159 -p 7001
192.168.133.159:7001> CLUSTER nodes
50e36c10b731d536fcf35d0478b20c6671ed3116 192.168.133.159:7004 slave ee1d1cfbd7385893ababb0342329a55644734f4c 0 1535362869157 4 connected
b40a94dee3200f716e8f628060348296b37952d1 192.168.133.159:7003 master - 0 1535362870164 3 connected 10923-16383
93d1ef27a47b5e830e2f1fdce9bdaeac6ce7689d 192.168.133.159:7006 slave b40a94dee3200f716e8f628060348296b37952d1 0 1535362865134 6 connected
ee1d1cfbd7385893ababb0342329a55644734f4c 192.168.133.159:7001 myself,master - 0 0 1 connected 0-5460
8ca1250d01509febbedc14748069ea130fdf2b76 192.168.133.159:7007 master - 0 1535362868150 0 connected
4e1ca9e4d966776c0ec1c37ab12e90315baef857 192.168.133.159:7005 slave dd05edc6f7544f502c666d9b416a4063987bdbe9 0 1535362871170 5 connected
dd05edc6f7544f502c666d9b416a4063987bdbe9 192.168.133.159:7002 master - 0 1535362870667 2 connected 5461-10922

但它还没有生效,因为redis的集群是对“槽“的操作,所以对于新加的节点,需要进行”槽“的再分配

[[email protected] redis-cluster]# ./redis-trib.rb reshard 192.168.133.159:7001

比如我们这里先分配1000个槽

How many slots do you want to move (from 1 to 16384)? 1000

然后输入接收点的ID

What is the receiving node ID? 8ca1250d01509febbedc14748069ea130fdf2b76

这个ID其实是通过cluster nodes的redis命令进行但看到的

最后输入源结点id 
输入源结点id,槽将从源结点中拿,分配后的槽在源结点中就不存在了。 
输入all表示从所有源结点中获取槽。 
输入done取消分配。

Source node #1:all

以下就日志信息

Ready to move 1000 slots.
  Source nodes:
    M: ee1d1cfbd7385893ababb0342329a55644734f4c 192.168.133.159:7001
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
    M: b40a94dee3200f716e8f628060348296b37952d1 192.168.133.159:7003
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
    M: dd05edc6f7544f502c666d9b416a4063987bdbe9 192.168.133.159:7002
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
  Destination node:
    M: 8ca1250d01509febbedc14748069ea130fdf2b76 192.168.133.159:7007
   slots: (0 slots) master
   0 additional replica(s)
  Resharding plan:
    Moving slot 5461 from dd05edc6f7544f502c666d9b416a4063987bdbe9
。。。

2、为集群添加一个从节点,用它来做为7007的从节点

创建一个7008的目录,然后启动7008的进程,然后添加从节点

[[email protected] 7008]# cd ../redis-trib.rb add-node --slave --master-id 8ca1250d01509febbedc14748069ea130fdf2b76  192.168.133.159:7008  192.168.133.159:7001

这样就完成了从节点的添加

192.168.133.159:7001> CLUSTER nodes
912b8dd1c7f35866ac15b263248ee729af7171e3 192.168.133.159:7008 slave 8ca1250d01509febbedc14748069ea130fdf2b76 0 1535368329746 7 connected
50e36c10b731d536fcf35d0478b20c6671ed3116 192.168.133.159:7004 slave ee1d1cfbd7385893ababb0342329a55644734f4c 0 1535368331758 4 connected
b40a94dee3200f716e8f628060348296b37952d1 192.168.133.159:7003 master - 0 1535368328237 3 connected 11256-16383
93d1ef27a47b5e830e2f1fdce9bdaeac6ce7689d 192.168.133.159:7006 slave b40a94dee3200f716e8f628060348296b37952d1 0 1535368327735 6 connected
ee1d1cfbd7385893ababb0342329a55644734f4c 192.168.133.159:7001 myself,master - 0 0 1 connected 333-5460
8ca1250d01509febbedc14748069ea130fdf2b76 192.168.133.159:7007 master - 0 1535368330752 7 connected 0-332 5461-5794 10923-11255
4e1ca9e4d966776c0ec1c37ab12e90315baef857 192.168.133.159:7005 slave dd05edc6f7544f502c666d9b416a4063987bdbe9 0 1535368326728 5 connected
dd05edc6f7544f502c666d9b416a4063987bdbe9 192.168.133.159:7002 master - 0 1535368328742 2 connected 5795-10922

如果需要删除一个主节点

[[email protected] redis-cluster]# ./redis-trib.rb del-node 192.168.133.159:7007 8ca1250d01509febbedc14748069ea130fdf2b76
>>> Removing node 8ca1250d01509febbedc14748069ea130fdf2b76 from cluster 192.168.133.159:7007
[ERR] Node 192.168.133.159:7007 is not empty! Reshard data away and try again.

 





以上是关于[docker]redis-cluster的使用的主要内容,如果未能解决你的问题,请参考以下文章

docker搭建redis-cluster

docker搭建redis-cluster

Docker实战之Redis-Cluster集群

docker部署redis-cluster集群

docker-compose搭建redis-cluster集群

Docker实战之Redis-Cluster集群