您的位置:首页 > 数据库 > Redis

redis cluster 集群配置示例: 创建, 添加节点, 重新分片, 删除节点

2016-05-19 15:50 399 查看

1 redis集群搭建

Redis 从3.0.0正式版开始官方支持集群, 下面开始做一个集群配置的示例.

官网地址: http://redis.io/download

当前最新版下载地址: http://download.redis.io/releases/redis-3.2.0.tar.gz

[root@localhost ~]# wget
http://download.redis.io/releases/redis-3.2.0.tar.gz

[root@localhost ~]# tar xf redis-3.2.0.tar.gz

[root@localhost ~]# cd redis-3.2.0

[root@localhost redis-3.2.0]# make && make PREFIX=/usr/local/redis install

安装之后复制 src/redis-trib.rb 文件到/usr/local/redis/bin/
下,后续创建集群的时候用的着.这是官方提供的一个用ruby语言编写的用于创建redis集群的工具.

[root@localhost redis-3.2.0]# cp -a src/redis-trib.rb /usr/local/redis/bin/



接着我们创建6个目录,对应6个redis实例,在创建集群时,将分三个主实例,三个从实例.(事实上,一个实例就可以创建集群,但是redis-trib.rb工具默认至少需要三个实例方可创建集群,这也是为了集群的自动故障转移功能可用,如果某个主节点挂了,会自动选举一个它的从节点替代它作为主节点)

...# mkdir -p /data/redis-cluster/{7001,7002,7003,7004,7005,7006}

...# ls /data/redis-cluster/

7001 7002 7003 7004 7005 7006

复制redis包根目录下的redis.conf配置文件到各个节点目录下

...# for dir in /data/redis-cluster/700* ; do cp -v redis.conf $dir ; done



然后修改配置文件.因为是在一台机器模拟多个节点,所以主要是修改各个实例的端口以及确保去掉了集群的配置注释.主要是以下三个配置项:

cluster-enabled yes

cluster-config-file nodes-6379.conf //这个文件不能删除也不能去修改

cluster-node-timeout 15000

[root@localhost redis-3.2.0]# for dir in /data/redis-cluster/700*; do

sed -r -i "

s@^\s*port.*@port ${dir##*/}@ ;

s@^\s*daemonize.*@daemonize yes@ ;

s@^\s*pidfile.*@pidfile /var/run/redis_${dir##*/}.pid@ ;

s@^\s*logfile.*@logfile /var/log/redis_${dir##*/}.log@;

s@^\s*#?\s*cluster-enabled.*@cluster-enabled yes@;

s@^\s*#?\s*cluster-config-file.*@cluster-config-file $dir/nodes-${dir##*/}.conf@;

s@^\s*#?\s*cluster-node-timeout.*@cluster-node-timeout 15000@;

s@^\s*dir.*@dir $dir@;

" $dir/redis.conf

done



可以去看下配置是否修改正确, 确认修改无误后启动这6个实例.

... # for dir in /data/redis-cluster/700*; do /usr/local/redis/bin/redis-server $dir/redis.conf ; done



可以看到6个实例都以cluster的方式正常工作了,接下来就是配置集群.

这就用到了 redis-trib.rb 这个工具了, 因为这个工具是ruby写的,所以需要ruby环境的支持,我们需要安装下ruby环境,
否则无法运行此工具.

[root@localhost redis-3.2.0]# yum -y install ruby rubygems

注意:请确保
ruby版本是
1.8.7+版本, 否则,执行
redis-trib.rb命令时可能会提示无法连接redis服务.


[root@localhost redis-3.2.0]# ruby --version

ruby 1.8.7 (2013-06-27 patchlevel 374) [x86_64-linux]

[root@localhost redis-3.2.0]#

接着安装ruby环境的redis库, 注意,
因天朝特殊的网络环境的原因,我们可能需要更换一下
gem的镜像, 更换镜像可以使用淘宝的,请参考:http://ruby.taobao.org/

[root@localhost redis-3.2.0]# gem install redis



好了,开始使用redis-trib.rb
这个工具创建集群.可以先执行redis-trib.rb看看这个工具的用法.

[root@localhost redis-3.2.0]# /usr/local/redis/bin/redis-trib.rb

Usage: redis-trib <command> <options> <arguments ...>

info host:port

reshard host:port

--pipeline <arg>

--slots <arg>

--from <arg>

--yes

--timeout <arg>

--to <arg>

rebalance host:port

--pipeline <arg>

--auto-weights

--threshold <arg>

--use-empty-masters

--weight <arg>

--timeout <arg>

--simulate

check host:port

fix host:port

--timeout <arg>

set-timeout host:port milliseconds

del-node host:port node_id

add-node new_host:new_port existing_host:existing_port

--slave

--master-id <arg>

import host:port

--copy

--from <arg>

--replace

create host1:port1 ... hostN:portN

--replicas <arg>

help (show this help)

call host:port command arg arg .. arg

For check, fix, reshard, del-node, set-timeout you can specify the host and port of any working node in the cluster.

开始使用工具创建集群.

[root@localhost redis-3.2.0]# /usr/local/redis/bin/redis-trib.rb create --replicas 1 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 127.0.0.1:7006

create //表示创建集群功能

--replicas 1 //表示为每个主节点自动分配一个从节点.也就是自动分配三个主节点和三个从节点.

>>> Creating cluster

>>> Performing hash slots allocation on 6 nodes...

Using 3 masters:

127.0.0.1:7001

127.0.0.1:7002

127.0.0.1:7003

Adding replica 127.0.0.1:7004 to 127.0.0.1:7001

Adding replica 127.0.0.1:7005 to 127.0.0.1:7002

Adding replica 127.0.0.1:7006 to 127.0.0.1:7003

M: a2eee0ea546f2c3701b08981737c07938039857c 127.0.0.1:7001

slots:0-5460 (5461 slots) master

M: 8ab3d14eba181c06dc8826bea0db1becdead2533 127.0.0.1:7002

slots:5461-10922 (5462 slots) master

M: fecbca3b75adc5cf6e79175630ff59c5764962ae 127.0.0.1:7003

slots:10923-16383 (5461 slots) master

S: 2b577338d01f6be1502c493cd80af719e719ddbd 127.0.0.1:7004

replicates a2eee0ea546f2c3701b08981737c07938039857c

S: 590e206fe468991138e2fce92b6ccd10a9eddb2e 127.0.0.1:7005

replicates 8ab3d14eba181c06dc8826bea0db1becdead2533

S: 888b802702cb9304913a0b25d7ff3daf953a4507 127.0.0.1:7006

replicates fecbca3b75adc5cf6e79175630ff59c5764962ae

Can I set the above configuration? (type 'yes' to accept): yes

>>> Nodes configuration updated

>>> Assign a different config epoch to each node

>>> Sending CLUSTER MEET messages to join the cluster

Waiting for the cluster to join.....

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

M: a2eee0ea546f2c3701b08981737c07938039857c 127.0.0.1:7001

slots:0-5460 (5461 slots) master

M: 8ab3d14eba181c06dc8826bea0db1becdead2533 127.0.0.1:7002

slots:5461-10922 (5462 slots) master

M: fecbca3b75adc5cf6e79175630ff59c5764962ae 127.0.0.1:7003

slots:10923-16383 (5461 slots) master

M: 2b577338d01f6be1502c493cd80af719e719ddbd 127.0.0.1:7004

slots: (0 slots) master

replicates a2eee0ea546f2c3701b08981737c07938039857c

M: 590e206fe468991138e2fce92b6ccd10a9eddb2e 127.0.0.1:7005

slots: (0 slots) master

replicates 8ab3d14eba181c06dc8826bea0db1becdead2533

M: 888b802702cb9304913a0b25d7ff3daf953a4507 127.0.0.1:7006

slots: (0 slots) master

replicates fecbca3b75adc5cf6e79175630ff59c5764962ae

[OK] All nodes agree about slots configuration.

>>> Check for open slots...

>>> Check slots coverage...

[OK] All 16384 slots covered.

至此, redis的集群环境已经搭建完成了,我们可以看一下节点情况.

[root@localhost redis-3.2.0]# /usr/local/redis/bin/redis-cli -p 7002 cluster nodes

2b577338d01f6be1502c493cd80af719e719ddbd 127.0.0.1:7004 slave a2eee0ea546f2c3701b08981737c07938039857c 0 1463476480206 4 connected

fecbca3b75adc5cf6e79175630ff59c5764962ae 127.0.0.1:7003 master - 0 1463476479204 3 connected 10923-16383

a2eee0ea546f2c3701b08981737c07938039857c 127.0.0.1:7001 master - 0 1463476482210 1 connected 0-5460

8ab3d14eba181c06dc8826bea0db1becdead2533 127.0.0.1:7002 myself,master - 0 0 2 connected 5461-10922

590e206fe468991138e2fce92b6ccd10a9eddb2e 127.0.0.1:7005 slave 8ab3d14eba181c06dc8826bea0db1becdead2533 0 1463476481208 5 connected

888b802702cb9304913a0b25d7ff3daf953a4507 127.0.0.1:7006 slave fecbca3b75adc5cf6e79175630ff59c5764962ae 0 1463476477700 6 connected

[root@localhost redis-3.2.0]#

2 添加新节点

当集群各个节点扛不住业务的需要时,我们可能需要往集群里添加新的节点以供业务的正常使用,现在介绍如何添加新节点.

同上面一样的, 我们先创建两个实例目录,一个实例做为新节点的主实例,一个实例做为新节点的从实例.

[root@localhost redis-3.2.0]# mkdir -p /data/redis-cluster/{7007,7008}

[root@localhost redis-3.2.0]#

[root@localhost redis-3.2.0]# for dir in /data/redis-cluster/{7007,7008} ; do cp -v redis.conf $dir ; done

`redis.conf' -> `/data/redis-cluster/7007/redis.conf'

`redis.conf' -> `/data/redis-cluster/7008/redis.conf'

[root@localhost redis-3.2.0]#

[root@localhost redis-3.2.0]# for dir in /data/redis-cluster/{7007,7008}; do sed -r -i "

s@^\s*port.*@port ${dir##*/}@ ;

s@^\s*daemonize.*@daemonize yes@ ;

s@^\s*pidfile.*@pidfile /var/run/redis_${dir##*/}.pid@ ;

s@^\s*logfile.*@logfile /var/log/redis_${d##*/}.log@;

s@^\s*#?\s*cluster-enabled.*@cluster-enabled yes@;

s@^\s*#?\s*cluster-config-file.*@cluster-config-file $dir/nodes-${dir##*/}.conf@;

s@^\s*#?\s*cluster-node-timeout.*@cluster-node-timeout 15000@;

s@^\s*dir.*@dir $dir@;

" $dir/redis.conf; done

[root@localhost redis-3.2.0]#



接下来启动这两个实例.

[root@localhost redis-3.2.0]# for dir in /data/redis-cluster/{7007,7008} ; do /usr/local/redis/bin/redis-server $dir/redis.conf ; done



我们看到启动成功了, 但是这两个实例还没有加入到集群,我们现在要把它加入到集群里边儿来,这又用到了redis-trib.rb这个工具.

[root@localhost redis-3.2.0]# /usr/local/redis/bin/redis-trib.rb add-node 127.0.0.1:7007127.0.0.1:7001

PS:这个IP:PORT可以是集群里边儿任意一个主节点的IP和端口,下同

>>> Adding node 127.0.0.1:7007 to cluster 127.0.0.1:7001

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

M: a2eee0ea546f2c3701b08981737c07938039857c 127.0.0.1:7001

slots:0-5460 (5461 slots) master

1 additional replica(s)

S: 888b802702cb9304913a0b25d7ff3daf953a4507 127.0.0.1:7006

slots: (0 slots) slave

replicates fecbca3b75adc5cf6e79175630ff59c5764962ae

S: 2b577338d01f6be1502c493cd80af719e719ddbd 127.0.0.1:7004

slots: (0 slots) slave

replicates a2eee0ea546f2c3701b08981737c07938039857c

S: 590e206fe468991138e2fce92b6ccd10a9eddb2e 127.0.0.1:7005

slots: (0 slots) slave

replicates 8ab3d14eba181c06dc8826bea0db1becdead2533

M: fecbca3b75adc5cf6e79175630ff59c5764962ae 127.0.0.1:7003

slots:10923-16383 (5461 slots) master

1 additional replica(s)

M: 8ab3d14eba181c06dc8826bea0db1becdead2533 127.0.0.1:7002

slots:5461-10922 (5462 slots) master

1 additional replica(s)

[OK] All nodes agree about slots configuration.

>>> Check for open slots...

>>> Check slots coverage...

[OK] All 16384 slots covered.

>>> Send CLUSTER MEET to node 127.0.0.1:7007 to make it join the cluster.

[OK] New node added correctly.

[root@localhost redis-3.2.0]#

再看下集群节点, 发现7007这个实例已经做为主节点加到集群里边儿来了.

[root@localhost redis-3.2.0]# /usr/local/redis/bin/redis-cli -c -p 7002 cluster nodes

36d53c7f1896838249c0b4afdcf680bac2f4ec2e 127.0.0.1:7007 master - 0 1463476564369 0 connected

2b577338d01f6be1502c493cd80af719e719ddbd 127.0.0.1:7004 slave a2eee0ea546f2c3701b08981737c07938039857c 0 1463476564870 4 connected

fecbca3b75adc5cf6e79175630ff59c5764962ae 127.0.0.1:7003 master - 0 1463476567376 3 connected 10923-16383

a2eee0ea546f2c3701b08981737c07938039857c 127.0.0.1:7001 master - 0 1463476566374 1 connected 0-5460

8ab3d14eba181c06dc8826bea0db1becdead2533 127.0.0.1:7002 myself,master - 0 0 2 connected 5461-10922

590e206fe468991138e2fce92b6ccd10a9eddb2e 127.0.0.1:7005 slave 8ab3d14eba181c06dc8826bea0db1becdead2533 0 1463476565371 5 connected

888b802702cb9304913a0b25d7ff3daf953a4507 127.0.0.1:7006 slave fecbca3b75adc5cf6e79175630ff59c5764962ae 0 1463476568378 6 connected

[root@localhost redis-3.2.0]#

接下来还要把7008做为7007的从节点也加入到集群里边儿来,这里的话我们首先要记住7007这个主节点的节点id.从节点加入到集群的时候要用到.

[root@localhost redis-3.2.0]# /usr/local/redis/bin/redis-trib.rb add-node --slave --master-id 36d53c7f1896838249c0b4afdcf680bac2f4ec2e 127.0.0.1:7008 127.0.0.1:7001

>>> Adding node 127.0.0.1:7008 to cluster 127.0.0.1:7001

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

M: a2eee0ea546f2c3701b08981737c07938039857c 127.0.0.1:7001

slots:0-5460 (5461 slots) master

1 additional replica(s)

M: 36d53c7f1896838249c0b4afdcf680bac2f4ec2e 127.0.0.1:7007

slots: (0 slots) master

0 additional replica(s)

S: 888b802702cb9304913a0b25d7ff3daf953a4507 127.0.0.1:7006

slots: (0 slots) slave

replicates fecbca3b75adc5cf6e79175630ff59c5764962ae

S: 2b577338d01f6be1502c493cd80af719e719ddbd 127.0.0.1:7004

slots: (0 slots) slave

replicates a2eee0ea546f2c3701b08981737c07938039857c

S: 590e206fe468991138e2fce92b6ccd10a9eddb2e 127.0.0.1:7005

slots: (0 slots) slave

replicates 8ab3d14eba181c06dc8826bea0db1becdead2533

M: fecbca3b75adc5cf6e79175630ff59c5764962ae 127.0.0.1:7003

slots:10923-16383 (5461 slots) master

1 additional replica(s)

M: 8ab3d14eba181c06dc8826bea0db1becdead2533 127.0.0.1:7002

slots:5461-10922 (5462 slots) master

1 additional replica(s)

[OK] All nodes agree about slots configuration.

>>> Check for open slots...

>>> Check slots coverage...

[OK] All 16384 slots covered.

>>> Send CLUSTER MEET to node 127.0.0.1:7008 to make it join the cluster.

Waiting for the cluster to join.

>>> Configure node as replica of 127.0.0.1:7007.

[OK] New node added correctly.

[root@localhost redis-3.2.0]#

[root@localhost redis-3.2.0]# /usr/local/redis/bin/redis-cli -c -p 7002 cluster nodes

36d53c7f1896838249c0b4afdcf680bac2f4ec2e 127.0.0.1:7007 master - 0 1463476686650 7 connected

2b577338d01f6be1502c493cd80af719e719ddbd 127.0.0.1:7004 slave a2eee0ea546f2c3701b08981737c07938039857c 0 1463476682641 4 connected

fecbca3b75adc5cf6e79175630ff59c5764962ae 127.0.0.1:7003 master - 0 1463476685647 3 connected 10923-16383

a2eee0ea546f2c3701b08981737c07938039857c 127.0.0.1:7001 master - 0 1463476684646 1 connected 0-5460

8ab3d14eba181c06dc8826bea0db1becdead2533 127.0.0.1:7002 myself,master - 0 0 2 connected 5461-10922

590e206fe468991138e2fce92b6ccd10a9eddb2e 127.0.0.1:7005 slave 8ab3d14eba181c06dc8826bea0db1becdead2533 0 1463476684645 5 connected

888b802702cb9304913a0b25d7ff3daf953a4507 127.0.0.1:7006 slave fecbca3b75adc5cf6e79175630ff59c5764962ae 0 1463476681639 6 connected

66c0169faa7a00da01ddb681dcc56cae730e2ced 127.0.0.1:7008 slave 36d53c7f1896838249c0b4afdcf680bac2f4ec2e 0 1463476683643 7 connected

[root@localhost redis-3.2.0]#

可以看到 7008 已经做为一个从节点加入到了集群.

从图中可以看到, 7007 这个节点实例并没有分配slots.加入到集群的新节点,我们还需要给它分配slots,才能真正用于集群存取数据,否则加进来没有任何意义.

3 重新分片

所谓重新分片,就是重新分配各个节点拥有的slots,这里我们的主要目的是从老的节点中迁移一部分slots放到新节点中去,以便让新节点真正成为集群中的一员.

同样, 还是利用redis-trib.rb
工具.

[root@localhost redis-3.2.0]# /usr/local/redis/bin/redis-trib.rb reshard 127.0.0.1:7001

//PS: 这条命令是交互的,按照提示操作即可.

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

M: a2eee0ea546f2c3701b08981737c07938039857c 127.0.0.1:7001

slots:0-5460 (5461 slots) master

1 additional replica(s)

M: 36d53c7f1896838249c0b4afdcf680bac2f4ec2e 127.0.0.1:7007

slots: (0 slots) master

1 additional replica(s)

S: 888b802702cb9304913a0b25d7ff3daf953a4507 127.0.0.1:7006

slots: (0 slots) slave

replicates fecbca3b75adc5cf6e79175630ff59c5764962ae

S: 2b577338d01f6be1502c493cd80af719e719ddbd 127.0.0.1:7004

slots: (0 slots) slave

replicates a2eee0ea546f2c3701b08981737c07938039857c

S: 66c0169faa7a00da01ddb681dcc56cae730e2ced 127.0.0.1:7008

slots: (0 slots) slave

replicates 36d53c7f1896838249c0b4afdcf680bac2f4ec2e

S: 590e206fe468991138e2fce92b6ccd10a9eddb2e 127.0.0.1:7005

slots: (0 slots) slave

replicates 8ab3d14eba181c06dc8826bea0db1becdead2533

M: fecbca3b75adc5cf6e79175630ff59c5764962ae 127.0.0.1:7003

slots:10923-16383 (5461 slots) master

1 additional replica(s)

M: 8ab3d14eba181c06dc8826bea0db1becdead2533 127.0.0.1:7002

slots:5461-10922 (5462 slots) master

1 additional replica(s)

[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)?4096 //输入一个数,这个4096表示迁移多少个slots数

What is the receiving node ID?
36d53c7f1896838249c0b4afdcf680bac2f4ec2e //输入目标节点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.

Source node #1:all //输入all表示从老的所有节点进行重分配,凑够4096个slots给到新节点.

也可以输入源节点id, 可以输入多个源节点id,最后输入done.就开始从你输入的源节点id的节点进行迁移了.

Ready to move 4096 slots.

Source nodes:

M: a2eee0ea546f2c3701b08981737c07938039857c 127.0.0.1:7001

slots:0-5460 (5461 slots) master

1 additional replica(s)

M: fecbca3b75adc5cf6e79175630ff59c5764962ae 127.0.0.1:7003

slots:10923-16383 (5461 slots) master

1 additional replica(s)

M: 8ab3d14eba181c06dc8826bea0db1becdead2533 127.0.0.1:7002

slots:5461-10922 (5462 slots) master

1 additional replica(s)

Destination node:

M: 36d53c7f1896838249c0b4afdcf680bac2f4ec2e 127.0.0.1:7007

slots: (0 slots) master

1 additional replica(s)

Resharding plan:

Moving slot 5461 from 8ab3d14eba181c06dc8826bea0db1becdead2533

Moving slot 5462 from 8ab3d14eba181c06dc8826bea0db1becdead2533

Moving slot 5463 from 8ab3d14eba181c06dc8826bea0db1becdead2533

Moving slot 5464 from 8ab3d14eba181c06dc8826bea0db1becdead2533

Moving slot 5465 from 8ab3d14eba181c06dc8826bea0db1becdead2533

Moving slot 5466 from 8ab3d14eba181c06dc8826bea0db1becdead2533

......

当然也可以采用非交互自动的进行迁移, 如:

[root@localhost redis-3.2.0]# /usr/local/redis/bin/redis-trib.rb reshard --from a2eee0ea546f2c3701b08981737c07938039857c --to 36d53c7f1896838249c0b4afdcf680bac2f4ec2e --slots 45 --yes 127.0.0.1:7001

--from a2eee0ea546f2c3701b08981737c07938039857c //表示从哪个节点进行迁移,即源节点

--to 36d53c7f1896838249c0b4afdcf680bac2f4ec2e //迁移到哪个节点,即目标节点

--slots 45 //迁移多少slot

--yes //无需输入yes确认

不过非交互的迁移就不能使用多个源节点进行迁移slots了.

4 删除节点

要删除集群中的某个节点(注:这里说的是集群中的主节点),首先必须确保这个节点没有拥有任何一个slots.我们现在来删除7001
这个节点.

首先,我们要把7001这个节点上的slots全部迁移出去.

[root@localhost redis-3.2.0]# /usr/local/redis/bin/redis-cli -c -p 7002 cluster nodes

36d53c7f1896838249c0b4afdcf680bac2f4ec2e 127.0.0.1:7007 master - 0 1463477003339 7 connected 0-1364 5461-6826 10923-12287

2b577338d01f6be1502c493cd80af719e719ddbd 127.0.0.1:7004 slave a2eee0ea546f2c3701b08981737c07938039857c 0 1463477000332 4 connected

fecbca3b75adc5cf6e79175630ff59c5764962ae 127.0.0.1:7003 master - 0 1463477002336 3 connected 12288-16383

a2eee0ea546f2c3701b08981737c07938039857c 127.0.0.1:7001 master - 0 1463477001334 1 connected 1365-5460

8ab3d14eba181c06dc8826bea0db1becdead2533 127.0.0.1:7002 myself,master - 0 0 2 connected 6827-10922

590e206fe468991138e2fce92b6ccd10a9eddb2e 127.0.0.1:7005 slave 8ab3d14eba181c06dc8826bea0db1becdead2533 0 1463477004340 5 connected

888b802702cb9304913a0b25d7ff3daf953a4507 127.0.0.1:7006 slave fecbca3b75adc5cf6e79175630ff59c5764962ae 0 1463476999831 6 connected

66c0169faa7a00da01ddb681dcc56cae730e2ced 127.0.0.1:7008 slave 36d53c7f1896838249c0b4afdcf680bac2f4ec2e 0 1463477004841 7 connected

[root@localhost redis-3.2.0]#

[root@localhost redis-3.2.0]#

[root@localhost redis-3.2.0]#

我们直接执行命令删除看看.

[root@localhost redis-3.2.0]# /usr/local/redis/bin/redis-trib.rb del-node 127.0.0.1:7001 a2eee0ea546f2c3701b08981737c07938039857c

>>> Removing node a2eee0ea546f2c3701b08981737c07938039857c from cluster 127.0.0.1:7001

[ERR] Node 127.0.0.1:7001 is not empty! Reshard data away and try again.

[root@localhost redis-3.2.0]#

由此可知,直接删会提示不为空,不能删除,分片后再删吧.把这个节点拥有的slots全部迁移出去即可.

[root@localhost redis-3.2.0]# /usr/local/redis/bin/redis-trib.rb reshard 127.0.0.1:7001

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

M: a2eee0ea546f2c3701b08981737c07938039857c 127.0.0.1:7001

slots:1365-5460 (4096 slots) master

1 additional replica(s)

M: 36d53c7f1896838249c0b4afdcf680bac2f4ec2e 127.0.0.1:7007

slots:0-1364,5461-6826,10923-12287 (4096 slots) master

1 additional replica(s)

S: 888b802702cb9304913a0b25d7ff3daf953a4507 127.0.0.1:7006

slots: (0 slots) slave

replicates fecbca3b75adc5cf6e79175630ff59c5764962ae

S: 2b577338d01f6be1502c493cd80af719e719ddbd 127.0.0.1:7004

slots: (0 slots) slave

replicates a2eee0ea546f2c3701b08981737c07938039857c

S: 66c0169faa7a00da01ddb681dcc56cae730e2ced 127.0.0.1:7008

slots: (0 slots) slave

replicates 36d53c7f1896838249c0b4afdcf680bac2f4ec2e

S: 590e206fe468991138e2fce92b6ccd10a9eddb2e 127.0.0.1:7005

slots: (0 slots) slave

replicates 8ab3d14eba181c06dc8826bea0db1becdead2533

M: fecbca3b75adc5cf6e79175630ff59c5764962ae 127.0.0.1:7003

slots:12288-16383 (4096 slots) master

1 additional replica(s)

M: 8ab3d14eba181c06dc8826bea0db1becdead2533 127.0.0.1:7002

slots:6827-10922 (4096 slots) master

1 additional replica(s)

[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)?16384 //输入一个大于或等于7001节点所拥有的slots数的数即可.

What is the receiving node ID?
8ab3d14eba181c06dc8826bea0db1becdead2533 //接收这些slots的目标节点,这里是7002节点

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 #1:a2eee0ea546f2c3701b08981737c07938039857c //因为我们要删除7001这个节点,所以源节点的id就是7001的节点ID

Source node #2:done //输入done,回车,就会开始从7001
这个节点迁移16384个slot(没有这么多就迁移拥有的全部)到7002节点中去.

Ready to move 16384 slots.

Source nodes:

M: a2eee0ea546f2c3701b08981737c07938039857c 127.0.0.1:7001

slots:1365-5460 (4096 slots) master

1 additional replica(s)

Destination node:

M: 8ab3d14eba181c06dc8826bea0db1becdead2533 127.0.0.1:7002

slots:6827-10922 (4096 slots) master

1 additional replica(s)

Resharding plan:

Moving slot 1365 from a2eee0ea546f2c3701b08981737c07938039857c

Moving slot 1366 from a2eee0ea546f2c3701b08981737c07938039857c

Moving slot 1367 from a2eee0ea546f2c3701b08981737c07938039857c

Moving slot 1368 from a2eee0ea546f2c3701b08981737c07938039857c

Moving slot 1369 from a2eee0ea546f2c3701b08981737c07938039857c

Moving slot 1370 from a2eee0ea546f2c3701b08981737c07938039857c

....

.

.

Moving slot 5457 from 127.0.0.1:7001 to 127.0.0.1:7002:

Moving slot 5458 from 127.0.0.1:7001 to 127.0.0.1:7002:

Moving slot 5459 from 127.0.0.1:7001 to 127.0.0.1:7002:

Moving slot 5460 from 127.0.0.1:7001 to 127.0.0.1:7002:

再看各个节点的状态:

[root@localhost redis-3.2.0]# /usr/local/redis/bin/redis-cli -c -p 7002 cluster nodes

36d53c7f1896838249c0b4afdcf680bac2f4ec2e 127.0.0.1:7007 master - 0 1463477346180 7 connected 0-1364 5461-6826 10923-12287

2b577338d01f6be1502c493cd80af719e719ddbd 127.0.0.1:7004 slave 8ab3d14eba181c06dc8826bea0db1becdead2533 0 1463477345178 8 connected

fecbca3b75adc5cf6e79175630ff59c5764962ae 127.0.0.1:7003 master - 0 1463477350189 3 connected 12288-16383

a2eee0ea546f2c3701b08981737c07938039857c 127.0.0.1:7001 master - 0 1463477349186 1 connected

8ab3d14eba181c06dc8826bea0db1becdead2533 127.0.0.1:7002 myself,master - 0 0 8 connected 1365-5460 6827-10922

590e206fe468991138e2fce92b6ccd10a9eddb2e 127.0.0.1:7005 slave 8ab3d14eba181c06dc8826bea0db1becdead2533 0 1463477347182 8 connected

888b802702cb9304913a0b25d7ff3daf953a4507 127.0.0.1:7006 slave fecbca3b75adc5cf6e79175630ff59c5764962ae 0 1463477350189 6 connected

66c0169faa7a00da01ddb681dcc56cae730e2ced 127.0.0.1:7008 slave 36d53c7f1896838249c0b4afdcf680bac2f4ec2e 0 1463477348183 7 connected

[root@localhost redis-3.2.0]#

7001 已经没有分配slots了,可以从集群中删除了.

[root@localhost redis-3.2.0]# redis-trib.rb del-node 127.0.0.1:7002 a2eee0ea546f2c3701b08981737c07938039857c

>>> Removing node a2eee0ea546f2c3701b08981737c07938039857c from cluster 127.0.0.1:7002

>>> Sending CLUSTER FORGET messages to the cluster...

>>> SHUTDOWN the node. //同时停止了进程

[root@localhost redis-3.2.0]# ps -ef | grep redis

root 34736 43075 0 05:32 pts/1 00:00:00 grep redis

root 42204 1 0 05:12 ? 00:00:06 redis-server 127.0.0.1:7002 [cluster]

root 42206 1 0 05:12 ? 00:00:04 redis-server 127.0.0.1:7003 [cluster]

root 42208 1 0 05:12 ? 00:00:00 redis-server 127.0.0.1:7004 [cluster]

root 42210 1 0 05:12 ? 00:00:00 redis-server 127.0.0.1:7005 [cluster]

root 42212 1 0 05:12 ? 00:00:00 redis-server 127.0.0.1:7006 [cluster]

root 42214 1 0 05:12 ? 00:00:05 redis-server 127.0.0.1:7007 [cluster]

root 42216 1 0 05:12 ? 00:00:00 redis-server 127.0.0.1:7008 [cluster]

[root@localhost redis-3.2.0]#

另外, 经过观察,这个主节点被删除之后,它之前拥有的从节点会自动成为其他主节点的从节点.

以上就是利用官方提供的redis-trib.rb 工具来完成上述的各项工作,事实上这个工具也是利用cluster的内部命令进行的整合以方便我们使用和管理.如果想了解更多的细节,需要查看官方的文档,可以完全脱离redis-trib.rb的便利性纯手工使用cluster内部命令来完成集群的搭建以及分配slots,添加节点,删除节点等等.

参考文档:

http://redisdoc.com/topic/cluster-tutorial.html

http://redisdoc.com/topic/cluster-spec.html

http://redis.io/documentation

https://www.zybuluo.com/phper/note/195558

https://www.zybuluo.com/phper/note/205009

/article/1532847.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: