码迷,mamicode.com
首页 > 其他好文 > 详细

[docker]redis-cluster的使用

时间:2018-08-27 23:19:33      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:一个   输入   cli   data   nbsp   ati   usr   nod   receiving   

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

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

[root@CentOS7 redis-cluster]# ls
7001  7002  7003  7004  7005  7006  7007
[root@CentOS7 redis-cluster]# tree 7007
7007
├── 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:7007 192.168.133.159:7001

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

[root@CentOS7 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的集群是对“槽“的操作,所以对于新加的节点,需要进行”槽“的再分配

[root@CentOS7 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的进程,然后添加从节点

[root@CentOS7 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

如果需要删除一个主节点

[root@CentOS7 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的使用

标签:一个   输入   cli   data   nbsp   ati   usr   nod   receiving   

原文地址:https://www.cnblogs.com/baylorqu/p/9545200.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!