redis-3.2 集群

简介

集群简介

Redis 集群是一个提供在多个Redis间节点间共享数据的程序集。

Redis集群并不支持处理多个keys的命令,因为这需要在不同的节点间移动数据,从而达不到像Redis那样的性能,在高负载的情况下可能会导致不可预料的错误。

Redis 集群通过分区来提供一定程度的可用性,在实际环境中当某个节点宕机或者不可达的情况下继续处理命令。

Redis 集群是一个分布式(distributed)、容错(fault-tolerant)的 Redis 实现, 集群可以使用的功能是普通单机 Redis 所能使用的功能的一个子集(subset)。

Redis 集群中不存在中心(central)节点或者代理(proxy)节点,集群的其中一个主要设计目标是达到线性可扩展性(linear scalability)。

Redis 集群为了保证一致性(consistency)而牺牲了一部分容错性: 系统会在保证对网络断线(net split)和节点失效(node failure)具有有限(limited)抵抗力的前提下,尽可能地保持数据的一致性。

Redis 集群的数据分片

Redis 集群没有使用一致性hash,而是引入了 哈希槽的概念。Redis 集群有16384个哈希槽,每个key通过CRC16校验后对16384取模来决定放置哪个槽。集群的每个节点负责一部分hash槽。举个例子,比如当前集群有3个节点,那么:

  • 节点 A 包含 0 到 5500号哈希槽.

  • 节点 B 包含5501 到 11000 号哈希槽.

  • 节点 C 包含11001 到 16384号哈希槽.

    这种结构很容易添加或者删除节点。比如如果我想新添加个节点D, 我需要从节点 A, B, C中得部分槽到D上。如果我想移除节点A,需要将A中得槽移到B和C节点上,然后将没有任何槽的A节点从集群中移除即可。由于从一个节点将哈希槽移动到另一个节点并不会停止服务,所以无论添加删除或者改变某个节点的哈希槽的数量都不会造成集群不可用的状态。

Redis 集群的主从复制模型

为了使在部分节点失败或者大部分节点无法通信的情况下集群仍然可用,所以集群使用了主从复制模型,每个节点都会有N-1个复制品。在我们例子中具有A,B,C三个节点的集群,在没有复制模型的情况下,如果节点B失败了,那么整个集群就会以为缺少5501-11000这个范围的槽而不可用。然而如果在集群创建的时候(或者过一段时间)我们为每个节点添加一个从节点A1,B1,C1,那么整个集群便有三个master节点和三个slave节点组成,这样在节点B失败后,集群便会选举B1为新的主节点继续服务,整个集群便不会因为槽找不到而不可用了。

不过当B和B1 都失败后,集群是不可用的。

ps:集群有一个参数cluster-require-full-coverage,默认值是yes,表示只要有master节点宕机且没有slave导致找不到solt,整个集群就全部停止服务;no,允许其中一个master节点失效时集群仍然提供服务,不过程序连接该节点时会发生Connection refused的连接错误。

Redis 一致性保证

Redis 并不能保证数据的强一致性. 这意味这在实际中集群在特定的条件下可能会丢失写操作。

第一个原因是因为集群是用了异步复制。写操作过程:

  • 客户端向主节点B写入一条命令.
  • 主节点B向客户端回复命令状态.
  • 主节点将写操作复制给他得从节点 B1, B2 和 B3.
    主节点对命令的复制工作发生在返回命令回复之后,因为如果每次处理命令请求都需要等待复制操作完成的话,那么主节点处理命令请求的速度将极大地降低 —— 我们必须在性能和一致性之间做出权衡。 注意:Redis 集群可能会在将来提供同步写的方法。

Redis 集群另外一种可能会丢失命令的情况是集群出现了网络分区, 并且一个客户端与至少包括一个主节点在内的少数实例被孤立。举个例子 假设集群包含 A 、 B 、 C 、 A1 、 B1 、 C1 六个节点, 其中 A 、B 、C 为主节点, A1 、B1 、C1 为A,B,C的从节点, 还有一个客户端 Z1 假设集群中发生网络分区,那么集群可能会分为两方,大部分的一方包含节点 A 、C 、A1 、B1 和 C1 ,小部分的一方则包含节点 B 和客户端 Z1 。Z1仍然能够向主节点B中写入, 如果网络分区发生时间较短,那么集群将会继续正常运作,如果分区的时间足够让大部分的一方将B1选举为新的master,那么Z1写入B中得数据便丢失了.

注意,在网络分裂出现期间,客户端 Z1 可以向主节点 B 发送写命令的最大时间是有限制的, 这一时间限制称为节点超时时间(node timeout), 是 Redis 集群的一个重要的配置选项。

redis 集群间的通信

Redis Cluster的新节点识别能力、故障判断及故障转移能力是通过集群中的每个node都在和其它nodes进行通信,这被称为集群总线(cluster bus)。它们使用特殊的端口号,即对外服务端口号加10000。例如如果某个node的端口号是6001,那么它与其它nodes通信的端口号是16001。nodes之间的通信采用特殊的二进制协议。


环境

安装Ruby

一个用ruby写的管理工具

[root@ ~]# yum install ruby  
[root@ ~]# yum install rubygems  
[root@ ~]# gem install redis --version 3.2.2

部署

安装Redis略

增加六个实例,增加以下配置,cluster-config-file集群信息文件名,由redis自己维护

# cluster
cluster-enabled yes
cluster-config-file nodes${port}.conf
cluster-node-timeout 15000

如果有认证:ps:有密码的情况 redis-trib.rb脚本对迁移老是失败,这里盖个章!

requirepass ff752d
masterauth ff752d

启用六个实例

[root@ ~]# for port in 6001 6001 6002 6003 6004 6005; 
> do 
> su - redis -s /sbin/nologin -c "redis-server /data/conf/redis/redis$port.conf"    
> done
[root@ ~]# netstat -lntp | grep redis 
tcp        0      0 127.0.0.1:6001              0.0.0.0:*                   LISTEN      3257/redis-server 1 
tcp        0      0 127.0.0.1:6002              0.0.0.0:*                   LISTEN      3272/redis-server 1 
tcp        0      0 127.0.0.1:6003              0.0.0.0:*                   LISTEN      3291/redis-server 1 
tcp        0      0 127.0.0.1:6004              0.0.0.0:*                   LISTEN      3308/redis-server 1 
tcp        0      0 127.0.0.1:6005              0.0.0.0:*                   LISTEN      3325/redis-server 1         
tcp        0      0 127.0.0.1:16001             0.0.0.0:*                   LISTEN      3257/redis-server 1 
tcp        0      0 127.0.0.1:16002             0.0.0.0:*                   LISTEN      3272/redis-server 1 
tcp        0      0 127.0.0.1:16003             0.0.0.0:*                   LISTEN      3291/redis-server 1 
tcp        0      0 127.0.0.1:16004             0.0.0.0:*                   LISTEN      3308/redis-server 1 
tcp        0      0 127.0.0.1:16005             0.0.0.0:*                   LISTEN      3325/redis-server 1 

通过使用 Redis 集群命令行工具 redis-trib.rb 进行集群管理。redis-trib.rb 位于 Redis 源码的 src 文件夹中,它是基于redis提供的集群命令封装成简单、便捷、实用的一个 Ruby 程序,这个程序通过向实例发送特殊命令来完成创建新集群,检查集群,或者对集群进行重新分片(reshared)等工作。

[root@ src]# cp redis-trib.rb /usr/local/redis/bin/
[root@ src]# ln -s /usr/local/redis/bin/redis-trib.rb  /usr/local/bin/

创建集群

通过使用 redis-trib.rb create --replicas 1 ip:port 的方式创建集群。

可以使用选项 –replicas 1 表示我们希望为集群中的每个主节点创建一个从节点。

这里,我们只创建master节点:

[root@ ~]# redis-trib.rb create  127.0.0.1:6001  127.0.0.1:6002 127.0.0.1:6003 127.0.0.1:6004 127.0.0.1:6005                     
>>> Creating cluster
>>> Performing hash slots allocation on 5 nodes...
Using 5 masters:
127.0.0.1:6001
127.0.0.1:6002
127.0.0.1:6003
127.0.0.1:6004
127.0.0.1:6005
M: 7d1c7dcc0f76cb3811154d79021f0f298bf96617 127.0.0.1:6001
   slots:0-3276 (3277 slots) master
M: 673a07342c1a34e809c29a15827940999d878785 127.0.0.1:6002
   slots:3277-6553 (3277 slots) master
M: 8c69b93b929458bff813ae15d1a7cf4f45535355 127.0.0.1:6003
   slots:6554-9829 (3276 slots) master
M: 7337ac96a432f2db6ea4c92e4da8a5805d21b2fc 127.0.0.1:6004
   slots:9830-13106 (3277 slots) master
M: d8325f7d69908cdf252727b54271aae2dc13cf14 127.0.0.1:6005
   slots:13107-16383 (3277 slots) master
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:6001)
M: 7d1c7dcc0f76cb3811154d79021f0f298bf96617 127.0.0.1:6001
   slots:0-3276 (3277 slots) master
   0 additional replica(s)
M: d8325f7d69908cdf252727b54271aae2dc13cf14 127.0.0.1:6005
   slots:13107-16383 (3277 slots) master
   0 additional replica(s)
M: 673a07342c1a34e809c29a15827940999d878785 127.0.0.1:6002
   slots:3277-6553 (3277 slots) master
   0 additional replica(s)
M: 7337ac96a432f2db6ea4c92e4da8a5805d21b2fc 127.0.0.1:6004
   slots:9830-13106 (3277 slots) master
   0 additional replica(s)
M: 8c69b93b929458bff813ae15d1a7cf4f45535355 127.0.0.1:6003
   slots:6554-9829 (3276 slots) master
   0 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

如果redis实例配置了密码,需要在 /usr/lib/ruby/gems/1.8/gems/redis-3.2.2/lib/redis/client.rb 中指定密码:

DEFAULTS = {
  ***略***
  :password => 'ff752d',
  ***略***
  }

集群节点信息

[root@ ~]# /usr/local/redis/bin/redis-cli -c  -h 127.0.0.1 -p 6001 -a ff752d
127.0.0.1:6001> cluster nodes
d8325f7d69908cdf252727b54271aae2dc13cf14 127.0.0.1:6005 master - 0 1516620961339 5 connected 13107-16383
7d1c7dcc0f76cb3811154d79021f0f298bf96617 127.0.0.1:6001 myself,master - 0 0 1 connected 0-3276
673a07342c1a34e809c29a15827940999d878785 127.0.0.1:6002 master - 0 1516620959322 2 connected 3277-6553
7337ac96a432f2db6ea4c92e4da8a5805d21b2fc 127.0.0.1:6004 master - 0 1516620958313 4 connected 9830-13106
8c69b93b929458bff813ae15d1a7cf4f45535355 127.0.0.1:6003 master - 0 1516620960332 3 connected 6554-9829

上面的节点信息和创建是的输出信息是一致的:每个节点的ID、 ip:port 、solt的范围等。

或者使用redis-trib.rb check 命令:

[root@ ~]# redis-trib.rb  check 127.0.0.1:6001
>>> Performing Cluster Check (using node 127.0.0.1:6001)
M: 7d1c7dcc0f76cb3811154d79021f0f298bf96617 127.0.0.1:6001
   slots:0-3276 (3277 slots) master
   0 additional replica(s)
M: d8325f7d69908cdf252727b54271aae2dc13cf14 127.0.0.1:6005
   slots:13107-16383 (3277 slots) master
   0 additional replica(s)
M: 673a07342c1a34e809c29a15827940999d878785 127.0.0.1:6002
   slots:3277-6553 (3277 slots) master
   0 additional replica(s)
M: 7337ac96a432f2db6ea4c92e4da8a5805d21b2fc 127.0.0.1:6004
   slots:9830-13106 (3277 slots) master
   0 additional replica(s)
M: 8c69b93b929458bff813ae15d1a7cf4f45535355 127.0.0.1:6003
   slots:6554-9829 (3276 slots) master
   0 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

查看集群状态信息:

[root@ ~]# redis-cli -c -h 127.0.0.1 -p 6001 -a vff752d
127.0.0.1:6001>cluster info
cluster infocluster_state:ok   所有slots正常则显示为OK,否则为error
cluster_slots_assigned:16384   多少slots被分配了,即多少被master管理了,16384为全部slots
cluster_slots_ok:16384    有多少slots是正常的
cluster_slots_pfail:0      有多少slots可能处于异常状态,处于这个状态并不表示有问题,仍能继续提供服务
cluster_slots_fail:0    有多少slots处于异常状态,需要修复才能服务
cluster_known_nodes:5    集群中的节点数
cluster_size:5        集群中master个数
cluster_current_epoch:11     本地的当前时间变量,用于故障切换时生成独一无二的增量版本号
cluster_my_epoch:0
cluster_stats_messages_sent:4049 通过集群消息总线发送的消息总数
cluster_stats_messages_received:4051 通过过集通过群消息总线收到的消息总数

测试

登录集群节点需要跟上 -c 参数

[root@ ~]# redis-cli  -c -h 127.0.0.1 -p 6001  -a ff752d
127.0.0.1:6001> set key_1 value_1
-> Redirected to slot [11998] located at 127.0.0.1:6004
OK
127.0.0.1:6004> get key_1
"value_1"
127.0.0.1:6004> quit
[root@ ~]# redis-cli  -c -h 127.0.0.1 -p 6002  -a ff752d
127.0.0.1:6002> get key_1
-> Redirected to slot [11998] located at 127.0.0.1:6004
"value_1"
127.0.0.1:6004> quit
[root@ ~]# redis-cli  -c -h 127.0.0.1 -p 6004  -a ff752d 
127.0.0.1:6004> get key_1
"value_1"
127.0.0.1:6004> quit

从以上可以看出:在任何节点上创建的key都会被Redirected 到特定的slot中,也就是存在指定的master节点上,并复制到对应的slave节点。客户端登录任何节点,并get该key时都会被Redirected到这个key所在的节点上,然后获取该key对应的value。

如果登录非数据实际写入节点,不使用-c参数,会发生如下错误:

[root@ ~]# /usr/local/redis/bin/redis-cli -h 127.0.0.1 -p 6001 -a ff752d   
127.0.0.1:6001> get key_1
(error) MOVED 11998 127.0.0.1:6004

节点增减

再进行节点增删的时候,可以试着写个脚本不断对集群进行操作,会发现,这个过程进行都可以提供不间断的服务

[root@ ~]# cat redis_test.sh 
for i in `seq 3000` 
do 
redis-cli  -c  -h 127.0.0.1 -p 6001  set ckey"$i" vvvv"$i" 
sleep 1s 
done

新增一个master节点

我们这里新增一个master节点,新增redis实例略,6006端口

加入空节点到集群。 使用redis-trib.rb add-node 将一个节点添加到集群里面, 其中参数,第一个是新节点ip:port, 第二个是任意一个已存在节点ip:port:

[root@ ~]# redis-trib.rb add-node 127.0.0.1:6006  127.0.0.1:6001
>>> Adding node 127.0.0.1:6006 to cluster 127.0.0.1:6001
>>> Performing Cluster Check (using node 127.0.0.1:6001)
M: 7d1c7dcc0f76cb3811154d79021f0f298bf96617 127.0.0.1:6001
   slots:0-3276 (3277 slots) master
   0 additional replica(s)
M: d8325f7d69908cdf252727b54271aae2dc13cf14 127.0.0.1:6005
   slots:13107-16383 (3277 slots) master
   0 additional replica(s)
M: 673a07342c1a34e809c29a15827940999d878785 127.0.0.1:6002
   slots:3277-6553 (3277 slots) master
   0 additional replica(s)
M: 7337ac96a432f2db6ea4c92e4da8a5805d21b2fc 127.0.0.1:6004
   slots:9830-13106 (3277 slots) master
   0 additional replica(s)
M: 8c69b93b929458bff813ae15d1a7cf4f45535355 127.0.0.1:6003
   slots:6554-9829 (3276 slots) master
   0 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:6006 to make it join the cluster.
[OK] New node added correctly.

注意,这里需要为新master节点分配slot。新master节点没有包含任何数据也不能写入数据, 因为它没有包含任何slot。ps:由于新加入的加点是一个主节点, 当集群需要将某个从节点升级为新的主节点时, 这个新节点不会被选中。

使用redis-trib.rb reshard 命令进行slot分配。下面的命令中,需要根据提示写入:新节点slots的数量、新节点ID、slots的来源,我们对新加入的master节点只分配500个slot。

[root@ ~]# redis-trib.rb reshard  127.0.0.1:6006 
>>> Performing Cluster Check (using node 127.0.0.1:6006)
**略**
[OK] All 16384 slots covered.
How many slots do you want to move (from 1 to 16384)? 500  #分配给新节点多少个slot
What is the receiving node ID? f18d51e29c644f4305797ef20d7c9eff0dd5c6b4 #新节点的IP
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  # slot来源节点的ID,all表示已有的所有节点

一个集群总共有16384个slots,新加入的节点只给了500个,会不会太抠门了?所有节点都均衡一下怎样?

使用 redis-trib.rb rebalance 命令可以做到,

[root@ ~]# redis-trib.rb rebalance 127.0.0.1:6001
Performing Cluster Check (using node 127.0.0.1:6001)
[OK] All nodes agree about slots configuration.
Check for open slots...
Check slots coverage...
[OK] All 16384 slots covered.
Rebalancing across 6 nodes. Total weight = 6
Moving 292 slots from 127.0.0.1:6002 to 127.0.0.1:6006
Moving 292 slots from 127.0.0.1:6004 to 127.0.0.1:6006
Moving 292 slots from 127.0.0.1:6005 to 127.0.0.1:6006
Moving 291 slots from 127.0.0.1:6003 to 127.0.0.1:6006
Moving 291 slots from 127.0.0.1:6001 to 127.0.0.1:6006

均衡之后:

[root@~]# redis-trib.rb check  127.0.0.1:6001          
Performing Cluster Check (using node 127.0.0.1:6001)
M: d0505bdad489f23927939988fd5582eecb0835dc 127.0.0.1:6001
   slots:547-3276 (2730 slots) master
   0 additional replica(s)
M: 3e4c4957f78443f2faa15b0995d77b5c7992ab4d 127.0.0.1:6004
   slots:10377-13106 (2730 slots) master
   0 additional replica(s)
M: 067cbe24cb1bc8b18d3f6e8b6a80049f11d6ef7d 127.0.0.1:6003
   slots:7100-9829 (2730 slots) master
   0 additional replica(s)
M: 7e466391fe181888bd4696dcc7aeed4cf784fbf3 127.0.0.1:6005
   slots:13654-16383 (2730 slots) master
   0 additional replica(s)
M: 73f789a25ad8c736621b19a472f6684903a4b5b4 127.0.0.1:6006
   slots:0-546,3277-3823,6554-7099,9830-10376,13107-13653 (2734 slots) master
   0 additional replica(s)
M: cba8c47b5862184250b4faf93e4d96749c169889 127.0.0.1:6002
   slots:3824-6553 (2730 slots) master
   0 additional replica(s)
[OK] All nodes agree about slots configuration.
Check for open slots...
Check slots coverage...
[OK] All 16384 slots covered.

新增一个slave节点

我们来为一个master6001节点增加一个slave节点,新增redis实例略,6007端口,不要忘记了配置:

# cluster
cluster-enabled yes
cluster-config-file nodes6007.conf
cluster-node-timeout 15000

使用redis-trib.rb add-node --slave命令来添加slave节点,其中参数, 第一个是slave节点ip:port, 第二个是任意一个master节点ip:port:

[root@ ~]# redis-trib.rb  add-node --slave --master-id 7d1c7dcc0f76cb3811154d79021f0f298bf96617 127.0.0.1:6007 127.0.0.1:6001
>>> Adding node 127.0.0.1:6007 to cluster 127.0.0.1:6001
>>> Performing Cluster Check (using node 127.0.0.1:6001)
M: 7d1c7dcc0f76cb3811154d79021f0f298bf96617 127.0.0.1:6001
   slots:20-3276 (3257 slots) master
   0 additional replica(s)
M: d8325f7d69908cdf252727b54271aae2dc13cf14 127.0.0.1:6005
   slots:13127-16383 (3257 slots) master
   0 additional replica(s)
M: 55989d213b38ed7e5d8b5a7e54c0a6ba4e01234b 127.0.0.1:6006
   slots:0-19,3277-3296,6554-6572,9830-9850,13107-13126 (100 slots) master
   0 additional replica(s)
M: 673a07342c1a34e809c29a15827940999d878785 127.0.0.1:6002
   slots:3297-6553 (3257 slots) master
   0 additional replica(s)
M: 7337ac96a432f2db6ea4c92e4da8a5805d21b2fc 127.0.0.1:6004
   slots:9851-13106 (3256 slots) master
   0 additional replica(s)
M: 8c69b93b929458bff813ae15d1a7cf4f45535355 127.0.0.1:6003
   slots:6573-9829 (3257 slots) master
   0 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:6007 to make it join the cluster.
Waiting for the cluster to join.
>>> Configure node as replica of 127.0.0.1:6001.
[OK] New node added correctly.

现在我们来看看集群的状态:

[root@BaseOS-2_192.168.31.128 ~]# redis-trib.rb  check 127.0.0.1:6001
Performing Cluster Check (using node 127.0.0.1:6001)
M: 7d1c7dcc0f76cb3811154d79021f0f298bf96617 127.0.0.1:6001
   slots:20-3276 (3257 slots) master
   1 additional replica(s)
M: d8325f7d69908cdf252727b54271aae2dc13cf14 127.0.0.1:6005
   slots:13127-16383 (3257 slots) master
   0 additional replica(s)
M: 55989d213b38ed7e5d8b5a7e54c0a6ba4e01234b 127.0.0.1:6006
   slots:0-19,3277-3296,6554-6572,9830-9850,13107-13126 (100 slots) master
   0 additional replica(s)
S: 1989d6617e226645f4f3d775be9fbb9c60d2b2ea 127.0.0.1:6007
   slots: (0 slots) slave
   replicates 7d1c7dcc0f76cb3811154d79021f0f298bf96617
M: 673a07342c1a34e809c29a15827940999d878785 127.0.0.1:6002
   slots:3297-6553 (3257 slots) master
   0 additional replica(s)
M: 7337ac96a432f2db6ea4c92e4da8a5805d21b2fc 127.0.0.1:6004
   slots:9851-13106 (3256 slots) master
   0 additional replica(s)
M: 8c69b93b929458bff813ae15d1a7cf4f45535355 127.0.0.1:6003
   slots:6573-9829 (3257 slots) master
   0 additional replica(s)
[OK] All nodes agree about slots configuration.
Check for open slots...
Check slots coverage...
[OK] All 16384 slots covered.

嗯,上面的信息很清楚很明白,就不写了。

删除一个master节点

我们把6002节点从集群中删除看看。

现在该节点中写入一点数据吧:

[root@ ~]# redis-cli -c -h 127.0.0.1 -p 6002 -a ff752d
127.0.0.1:6002> set key2 vvvv 
OK
127.0.0.1:6002> get key2 
"vvvv"

删除master节点之前首先要使用reshard将master上全部slot转移到其他的master节点,然后再删除当前节点(目前只能把被删除master的slot迁移到一个节点上)

我们把6002的数据迁移到6003吧,使用命令:redis-trib.rb reshard --from ${src_id} --to ${dist_id} --slots ${count} --yes ,或者如下:

[root@ ~]# redis-trib.rb reshard 127.0.0.1:6006 
How many slots do you want to move (from 1 to 16384)? 500 #被删除master的所有slot数量 
What is the receiving node ID? c4a31c852f81686f6ed8bcd6d1b13accdc947fd2 (ps:127.0.0.1:6000的node-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:34cd05497a68c98c1e23c89b7c8c6db02de81d8b #被删除master的node-id)  
Source node #2:done   
#Do you want to proceed with the proposed reshard plan (yes/no)? yes 

删除master节点:

#redis-trib del-node ip:port '<node-id>'  
[root@ ~]# redis-trib.rb del-node 127.0.0.1:6006 55989d213b38ed7e5d8b5a7e54c0a6ba4e01234b
>>> Removing node 55989d213b38ed7e5d8b5a7e54c0a6ba4e01234b from cluster 127.0.0.1:6006
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node

删除一个slave节点

可以直接使用redis-trib del-node ip:port ${node-id} 命令,这里就不多说明了。

一些问题

在集群使用过程中遇到的一些问题,不定期补上

slots迁移失败:

Check for open slots...
[WARNING] Node 127.0.0.1:6006 has slots in importing state (3300).
[WARNING] Node 127.0.0.1:6002 has slots in migrating state (3300).
[WARNING] The following slots are open: 3300

我们先来看看有没有脏数据吧:

[root@BaseOS-2_192.168.31.128 ~]# redis-cli -c  -h 127.0.0.1 -p 6006 -a ff752d
127.0.0.1:6002>  CLUSTER GETKEYSINSLOT 3300 100
(empty list or set)
127.0.0.1:6002> quit
[root@BaseOS-2_192.168.31.128 ~]# redis-cli -c  -h 127.0.0.1 -p 6002 -a ff752d
127.0.0.1:6002>  CLUSTER GETKEYSINSLOT 3300 100
1) "key1007"
127.0.0.1:6002> quit

没有,那就取消迁移:

[root@BaseOS-2_192.168.31.128 ~]# redis-cli -c  -h 127.0.0.1 -p 6006 -a ff752d
127.0.0.1:6002> cluster setslot 3300 stable
OK
127.0.0.1:6002> quit
[root@BaseOS-2_192.168.31.128 ~]# redis-cli -c  -h 127.0.0.1 -p 6002 -a ff752d
127.0.0.1:6002> cluster setslot 3300 stable
OK
127.0.0.1:6002> quit

rebalnce失败:

[ERR] Calling MIGRATE: ERR Target instance replied with error: NOAUTH Authentication required.

有密码的情况下,不知道怎么搞密码认证一直不对,check一下是哪个节点的哪个slot,取消该slot的迁移,或者删除里面的key,重新rebalanc。


Redis Cluster 在5.0之后取消了ruby脚本 redis-trib.rb的支持(手动命令行添加集群的方式不变),集合到redis-cli里,避免了再安装ruby的相关环境。直接使用redis-clit的参数--cluster 来取代。

posted @ 2017-06-02 15:21  wshenJin  阅读(1036)  评论(0编辑  收藏  举报