通过redis-trib.rb脚本构建并增删改查redis cluster集群

首先是创建redis cluster集群.   redis-trib.rb命令我们见过吧?  咱们安装搭建redis cluster集群的时候,必用的一条命令….. 

关于redis-trib操作的文章,原文链接是  http://xiaorui.cc/?p=1458    http://xiaorui.cc

redis-trib.rb 是官方出品的,是ruby开发的,其实他的原理也是调用reids的cluster指令… … 刚才在github发现湖南卫视开源了一个用python管理redis cluster的工具,很像redis-trib.rb,且包含了rb的大多数功能。 

需要注意的是replicas 是给master分配slave个数的参数,我们给的参数是1 ,那每个master就有一个从节点..

cd src
./redis-trib.rb  create –replicas 1 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005

检查集群,我们通过check cluster的一个节点,就知道整个集群的状况,可以看出来谁是主,谁是从…..

root@ubuntu:~/redis-3.0.1/src# ./redis-trib.rb check 127.0.0.1:7000
Connecting to node 127.0.0.1:7000: OK
Connecting to node 127.0.0.1:7001: OK
Connecting to node 127.0.0.1:7002: OK
Connecting to node 127.0.0.1:7003: OK
Connecting to node 127.0.0.1:7004: OK
Connecting to node 127.0.0.1:7005: OK
>>> Performing Cluster Check (using node 127.0.0.1:7000)
M: bde32811cbc7a55886413c52b8444d3353b9da86 127.0.0.1:7000
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
M: d8815ad0a69436196908ae458f94704ff7325a53 127.0.0.1:7001
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
M: ee05942ee38a56421a07eea01bc6072fe5e23bfd 127.0.0.1:7002
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: 25f13acb0707a29cd2952dd0c6bdffa13721b001 127.0.0.1:7003
   slots: (0 slots) slave
   replicates bde32811cbc7a55886413c52b8444d3353b9da86
S: d6dd1200fe377dc3aee2fd7d11a57504a6bd7529 127.0.0.1:7004
   slots: (0 slots) slave
   replicates d8815ad0a69436196908ae458f94704ff7325a53
S: 2282e7f24bacf1b6b316382eb8f6448bc41bd89d 127.0.0.1:7005
   slots: (0 slots) slave
   replicates ee05942ee38a56421a07eea01bc6072fe5e23bfd
[OK] All nodes agree about slots configuration.
>>> Check for open slots…
>>> Check slots coverage…
[OK] All 16384 slots covered.
root@ubuntu:~/redis-3.0.1/src#
root@ubuntu:~/redis-3.0.1/src#

我们再来测试下增加节点的功能,redis-trib.rb add-node 你想要的增加的ip:port  现在已经存在的IP:port 


root@ubuntu:~/redis-3.0.1/src# ./redis-trib.rb add-node 127.0.0.1:7006 127.0.0.1:7000
>>> Adding node 127.0.0.1:7006 to cluster 127.0.0.1:7000
Connecting to node 127.0.0.1:7000: OK
Connecting to node 127.0.0.1:7001: OK
Connecting to node 127.0.0.1:7002: OK
Connecting to node 127.0.0.1:7003: OK
Connecting to node 127.0.0.1:7004: OK
Connecting to node 127.0.0.1:7005: OK
>>> Performing Cluster Check (using node 127.0.0.1:7000)
M: bde32811cbc7a55886413c52b8444d3353b9da86 127.0.0.1:7000
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
M: d8815ad0a69436196908ae458f94704ff7325a53 127.0.0.1:7001
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
M: ee05942ee38a56421a07eea01bc6072fe5e23bfd 127.0.0.1:7002
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: 25f13acb0707a29cd2952dd0c6bdffa13721b001 127.0.0.1:7003
   slots: (0 slots) slave
   replicates bde32811cbc7a55886413c52b8444d3353b9da86
S: d6dd1200fe377dc3aee2fd7d11a57504a6bd7529 127.0.0.1:7004
   slots: (0 slots) slave
   replicates d8815ad0a69436196908ae458f94704ff7325a53
S: 2282e7f24bacf1b6b316382eb8f6448bc41bd89d 127.0.0.1:7005
   slots: (0 slots) slave
   replicates ee05942ee38a56421a07eea01bc6072fe5e23bfd
[OK] All nodes agree about slots configuration.
>>> Check for open slots…
>>> Check slots coverage…
[OK] All 16384 slots covered.
Connecting to node 127.0.0.1:7006: OK
[ERR] Node 127.0.0.1:7006 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.


上面的问题比较常见,尤其大家在测试的环境下,需要删除你redis.conf里面cluster-config-file 所在的文件。 如果是你线上的环境一般很难遇到这样的问题,如果是小范围测试的时候,不注意把redis.conf都复制成一样,或者端口不一样,cluster的配置文件是一样… 所以redis启动不了,但势必会造成这个问题….    如果删除文件不好用,那么就直接redis-cli -p port进去,然后删掉所有的数据……   这两件事情都做OK了后,就可以正常启动了,不会提示你 not empty了。


root@ubuntu:~/redis-3.0.1/src# ./redis-trib.rb add-node 127.0.0.1:7006 127.0.0.1:7000
>>> Adding node 127.0.0.1:7006 to cluster 127.0.0.1:7000
Connecting to node 127.0.0.1:7000: OK
Connecting to node 127.0.0.1:7001: OK
Connecting to node 127.0.0.1:7002: OK
Connecting to node 127.0.0.1:7003: OK
Connecting to node 127.0.0.1:7004: OK
Connecting to node 127.0.0.1:7005: OK
>>> Performing Cluster Check (using node 127.0.0.1:7000)
M: bde32811cbc7a55886413c52b8444d3353b9da86 127.0.0.1:7000
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
M: d8815ad0a69436196908ae458f94704ff7325a53 127.0.0.1:7001
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
M: ee05942ee38a56421a07eea01bc6072fe5e23bfd 127.0.0.1:7002
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: 25f13acb0707a29cd2952dd0c6bdffa13721b001 127.0.0.1:7003
   slots: (0 slots) slave
   replicates bde32811cbc7a55886413c52b8444d3353b9da86
S: d6dd1200fe377dc3aee2fd7d11a57504a6bd7529 127.0.0.1:7004
   slots: (0 slots) slave
   replicates d8815ad0a69436196908ae458f94704ff7325a53
S: 2282e7f24bacf1b6b316382eb8f6448bc41bd89d 127.0.0.1:7005
   slots: (0 slots) slave
   replicates ee05942ee38a56421a07eea01bc6072fe5e23bfd
[OK] All nodes agree about slots configuration.
>>> Check for open slots…
>>> Check slots coverage…
[OK] All 16384 slots covered.
Connecting to node 127.0.0.1:7006: OK
>>> Send CLUSTER MEET to node 127.0.0.1:7006 to make it join the cluster.
[OK] New node added correctly.

添加一个节点后,我们再来检查这个集群,会发现刚才我们添加的7006端口的redis已经成为了slave从。
root@ubuntu:~/redis-3.0.1/src# ./redis-trib.rb check 127.0.0.1:7000
Connecting to node 127.0.0.1:7000: OK
Connecting to node 127.0.0.1:7006: OK
Connecting to node 127.0.0.1:7001: OK
Connecting to node 127.0.0.1:7002: OK
Connecting to node 127.0.0.1:7003: OK
Connecting to node 127.0.0.1:7004: OK
Connecting to node 127.0.0.1:7005: OK
>>> Performing Cluster Check (using node 127.0.0.1:7000)
M: bde32811cbc7a55886413c52b8444d3353b9da86 127.0.0.1:7000
   slots:0-5460 (5461 slots) master
   2 additional replica(s)
S: 4258076cdfe20617272debe9fd9b1d9b9591c033 127.0.0.1:7006
   slots: (0 slots) slave
   replicates bde32811cbc7a55886413c52b8444d3353b9da86
M: d8815ad0a69436196908ae458f94704ff7325a53 127.0.0.1:7001
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
M: ee05942ee38a56421a07eea01bc6072fe5e23bfd 127.0.0.1:7002
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: 25f13acb0707a29cd2952dd0c6bdffa13721b001 127.0.0.1:7003
   slots: (0 slots) slave
   replicates bde32811cbc7a55886413c52b8444d3353b9da86
S: d6dd1200fe377dc3aee2fd7d11a57504a6bd7529 127.0.0.1:7004
   slots: (0 slots) slave
   replicates d8815ad0a69436196908ae458f94704ff7325a53
S: 2282e7f24bacf1b6b316382eb8f6448bc41bd89d 127.0.0.1:7005
   slots: (0 slots) slave
   replicates ee05942ee38a56421a07eea01bc6072fe5e23bfd
[OK] All nodes agree about slots configuration.
>>> Check for open slots…
>>> Check slots coverage…
[OK] All 16384 slots covered.

我们可以在线进行reshard,目的是让slot重新按照我们的规则分配,这样能一定程度的解决数据分配不均匀的情况。 如果你先增加了一个节点,也可以用reshard的方式让一定数目的slot迁移过去….   这样解决了新节点的slot分配….  

./redis-trib.rb reshard <host>:<port> –from <node-id> –to <node-id> –slots –yes


>>> Performing Cluster Check (using node 127.0.0.1:7007)

[OK] All nodes agree about slots configuration.
>>> Check for open slots…
>>> Check slots coverage…
[OK] All 16384 slots covered.
How many slots do you want to move (from 1 to 16384)? 600
What is the receiving node ID? ee05942ee38a56421a07eea01bc6072fe5e23bfd

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.

(这里需要注意的是,要添加master主机    ** The specified node is not known or not a master, please retry.)

从reids cluster中删除一个redis节点…. 

root@ubuntu:~/redis-3.0.1/src# ./redis-trib.rb del-node 127.0.0.1:7007 ‘a7c4da616ce671e3da6f4447af7f89e189af6ffe’
>>> Removing node a7c4da616ce671e3da6f4447af7f89e189af6ffe from cluster 127.0.0.1:7007
Connecting to node 127.0.0.1:7007: OK
Connecting to node 127.0.0.1:7000: OK
Connecting to node 127.0.0.1:7004: OK
Connecting to node 127.0.0.1:7003: OK
Connecting to node 127.0.0.1:7002: OK
Connecting to node 127.0.0.1:7001: OK
Connecting to node 127.0.0.1:7006: OK
Connecting to node 127.0.0.1:7005: OK
>>> Sending CLUSTER FORGET messages to the cluster…
>>> SHUTDOWN the node.
root@ubuntu:~/redis-3.0.1/src#

我们还可以给指定的redis master节点添加从节点…  默认add-node是添加主master节点…..

./redis-trib.rb add-node –slave –master-id  ‘ee05942ee38a56421a07eea01bc6072fe5e23bfd’ 127.0.0.1:7008  127.0.0.1:7000


M: ee05942ee38a56421a07eea01bc6072fe5e23bfd 127.0.0.1:7002
   slots:0-248,5461-5711,10923-16383 (5961 slots) master
   2 additional replica(s)
S: 5ee754517145fab6cdf92076a182a7b3bb3f306e 127.0.0.1:7008
   slots: (0 slots) slave
   replicates ee05942ee38a56421a07eea01bc6072fe5e23bfd







大家觉得文章对你有些作用! 如果想赏钱,可以用微信扫描下面的二维码,感谢!
另外再次标注博客原地址  xiaorui.cc

发表评论

邮箱地址不会被公开。 必填项已用*标注