Redis版本:5.0.0
redis 5.0.0版本后可以不用安装ruby环境.
集群命令说明:

redis-cli --cluster help
Cluster Manager Commands:
  create         host1:port1 ... hostN:portN   #创建集群
                 --cluster-replicas <arg>      #从节点个数
  check          host:port                     #检查集群
                 --cluster-search-multiple-owners #检查是否有槽同时被分配给了多个节点
  info           host:port                     #查看集群状态
  fix            host:port                     #修复集群
                 --cluster-search-multiple-owners #修复槽的重复分配问题
  reshard        host:port                     #指定集群的任意一节点进行迁移slot,重新分slots
                 --cluster-from <arg>          #需要从哪些源节点上迁移slot,可从多个源节点完成迁移,以逗号隔开,传递的是节点的node id,还可以直接传递--from all,这样源节点就是集群的所有节点,不传递该参数的话,则会在迁移过程中提示用户输入
                 --cluster-to <arg>            #slot需要迁移的目的节点的node id,目的节点只能填写一个,不传递该参数的话,则会在迁移过程中提示用户输入
                 --cluster-slots <arg>         #需要迁移的slot数量,不传递该参数的话,则会在迁移过程中提示用户输入。
                 --cluster-yes                 #指定迁移时的确认输入
                 --cluster-timeout <arg>       #设置migrate命令的超时时间
                 --cluster-pipeline <arg>      #定义cluster getkeysinslot命令一次取出的key数量,不传的话使用默认值为10
                 --cluster-replace             #是否直接replace到目标节点
  rebalance      host:port                                      #指定集群的任意一节点进行平衡集群节点slot数量 
                 --cluster-weight <node1=w1...nodeN=wN>         #指定集群节点的权重
                 --cluster-use-empty-masters                    #设置可以让没有分配slot的主节点参与,默认不允许
                 --cluster-timeout <arg>                        #设置migrate命令的超时时间
                 --cluster-simulate                             #模拟rebalance操作,不会真正执行迁移操作
                 --cluster-pipeline <arg>                       #定义cluster getkeysinslot命令一次取出的key数量,默认值为10
                 --cluster-threshold <arg>                      #迁移的slot阈值超过threshold,执行rebalance操作
                 --cluster-replace                              #是否直接replace到目标节点
  add-node       new_host:new_port existing_host:existing_port  #添加节点,把新节点加入到指定的集群,默认添加主节点
                 --cluster-slave                                #新节点作为从节点,默认随机一个主节点
                 --cluster-master-id <arg>                      #给新节点指定主节点
  del-node       host:port node_id                              #删除给定的一个节点,成功后关闭该节点服务
  call           host:port command arg arg .. arg               #在集群的所有节点执行相关命令
  set-timeout    host:port milliseconds                         #设置cluster-node-timeout
  import         host:port                                      #将外部redis数据导入集群
                 --cluster-from <arg>                           #将指定实例的数据导入到集群
                 --cluster-copy                                 #migrate时指定copy
                 --cluster-replace                              #migrate时指定replace
  help           

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

Redis群集TCP端口说明:
每个Redis群集节点都需要打开两个TCP连接。用于服务客户端的常规Redis TCP端口,例如6379,再加上将数据端口加10000所获得的端口,因此在示例中为16379。

第二个高端口用于群集总线,即使用二进制协议的节点到节点通信通道。节点将群集总线用于故障检测,配置更新,故障转移授权等。客户端永远不要尝试与群集总线端口进行通信,而应始终与普通的Redis命令端口进行通信,但是请确保您在防火墙中同时打开了这两个端口,否则Redis群集节点将无法进行通信。

命令端口和集群总线端口的偏移量是固定的,并且始终为10000。

请注意,对于每个节点,要使Redis群集正常工作,您需要:

普通客户端通信端口(通常为6379)用于与客户端通信,以向需要访问群集的所有客户端以及所有其他群集节点(使用客户端端口进行密钥迁移)开放。
群集总线端口(客户端端口+ 10000)必须可以从所有其他群集节点访问。
如果您没有同时打开两个TCP端口,则群集将无法正常工作。

集群总线使用不同的二进制协议进行节点到节点的数据交换,它更适合于使用很少的带宽和处理时间在节点之间交换信息。

环境说明:
172.16.1.102:7000-7005 总共六个节点的集群,3主3从。
启动多两个多端口7006 7007,如何启动请参考搭建redis5.0.4集群

实验

###测试环境
[root@localhosts redis]# redis-cli -a 123456 --cluster check 172.16.1.102:7000
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
172.16.1.102:7000 (a968b760...) -> 33321 keys | 5461 slots | 1 slaves.
172.16.1.102:7001 (d42969de...) -> 33390 keys | 5462 slots | 1 slaves.
172.16.1.102:7002 (93faf299...) -> 33291 keys | 5461 slots | 1 slaves.
[OK] 100002 keys in 3 masters.
6.10 keys per slot on average.
>>> Performing Cluster Check (using node 172.16.1.102:7000)
M: a968b7602483bd05f0f7ea5c2cc13eeff6ee7918 172.16.1.102:7000
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 0a2ff6c720ba32c398c8fc3d5ef14bd241252904 172.16.1.102:7005
   slots: (0 slots) slave
   replicates 93faf299a645c7eaac1b898fde6da48de28c6f1c
S: 4672c47668ea7f179f484cb64bc00e1347155ff5 172.16.1.102:7004
   slots: (0 slots) slave
   replicates d42969de78528d247580a39106e594dfbf9b1005
M: d42969de78528d247580a39106e594dfbf9b1005 172.16.1.102:7001
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
M: 93faf299a645c7eaac1b898fde6da48de28c6f1c 172.16.1.102:7002
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 6d31edca810ddcef5c1191394aec1153c159a7dc 172.16.1.102:7003
   slots: (0 slots) slave
   replicates a968b7602483bd05f0f7ea5c2cc13eeff6ee7918
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered. 

#添加节点,把7006节点 添加  到 现在有的集群上
[root@localhosts redis]# redis-cli -a 123456 --cluster add-node 172.16.1.102:7006  172.16.1.102:7000
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Adding node 172.16.1.102:7006 to cluster 172.16.1.102:7000
>>> Performing Cluster Check (using node 172.16.1.102:7000)
M: a968b7602483bd05f0f7ea5c2cc13eeff6ee7918 172.16.1.102:7000
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 0a2ff6c720ba32c398c8fc3d5ef14bd241252904 172.16.1.102:7005
   slots: (0 slots) slave
   replicates 93faf299a645c7eaac1b898fde6da48de28c6f1c
S: 4672c47668ea7f179f484cb64bc00e1347155ff5 172.16.1.102:7004
   slots: (0 slots) slave
   replicates d42969de78528d247580a39106e594dfbf9b1005
M: d42969de78528d247580a39106e594dfbf9b1005 172.16.1.102:7001
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
M: 93faf299a645c7eaac1b898fde6da48de28c6f1c 172.16.1.102:7002
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 6d31edca810ddcef5c1191394aec1153c159a7dc 172.16.1.102:7003
   slots: (0 slots) slave
   replicates a968b7602483bd05f0f7ea5c2cc13eeff6ee7918
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 172.16.1.102:7006 to make it join the cluster.
[OK] New node added correctly.  

#再次check集群信息, 发现4个主3从,但是新加入的主slots 为0
[root@localhosts redis]# redis-cli -a 123456 --cluster check 172.16.1.102:7000          Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
172.16.1.102:7000 (a968b760...) -> 33321 keys | 5461 slots | 1 slaves.
172.16.1.102:7006 (3bf112e8...) -> 0 keys | 0 slots | 0 slaves.
172.16.1.102:7001 (d42969de...) -> 33390 keys | 5462 slots | 1 slaves.
172.16.1.102:7002 (93faf299...) -> 33291 keys | 5461 slots | 1 slaves.
[OK] 100002 keys in 4 masters.
6.10 keys per slot on average.
>>> Performing Cluster Check (using node 172.16.1.102:7000)
M: a968b7602483bd05f0f7ea5c2cc13eeff6ee7918 172.16.1.102:7000
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 0a2ff6c720ba32c398c8fc3d5ef14bd241252904 172.16.1.102:7005
   slots: (0 slots) slave
   replicates 93faf299a645c7eaac1b898fde6da48de28c6f1c
S: 4672c47668ea7f179f484cb64bc00e1347155ff5 172.16.1.102:7004
   slots: (0 slots) slave
   replicates d42969de78528d247580a39106e594dfbf9b1005
M: 3bf112e8fab64ddae44eaf33c7c13eff1a536112 172.16.1.102:7006
   slots: (0 slots) master
M: d42969de78528d247580a39106e594dfbf9b1005 172.16.1.102:7001
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
M: 93faf299a645c7eaac1b898fde6da48de28c6f1c 172.16.1.102:7002
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 6d31edca810ddcef5c1191394aec1153c159a7dc 172.16.1.102:7003
   slots: (0 slots) slave
   replicates a968b7602483bd05f0f7ea5c2cc13eeff6ee7918
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

[root@localhosts redis]# redis-cli -a 123456 --cluster info 172.16.1.102:7000 
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
172.16.1.102:7000 (a968b760...) -> 33321 keys | 5461 slots | 1 slaves.
172.16.1.102:7006 (3bf112e8...) -> 0 keys | 0 slots | 0 slaves.
172.16.1.102:7001 (d42969de...) -> 33390 keys | 5462 slots | 1 slaves.
172.16.1.102:7002 (93faf299...) -> 33291 keys | 5461 slots | 1 slaves.
[OK] 100002 keys in 4 masters.
6.10 keys per slot on average.


#把7007做为7006的slave,7006是master
[root@localhosts redis]# redis-cli -a 123456 --cluster add-node  --cluster-slave  --cluster-master-id 3bf112e8fab64ddae44eaf33c7c13eff1a536112 172.16.1.102:7007 172.16.1.102:7000 
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Adding node 172.16.1.102:7007 to cluster 172.16.1.102:7000
>>> Performing Cluster Check (using node 172.16.1.102:7000)
M: a968b7602483bd05f0f7ea5c2cc13eeff6ee7918 172.16.1.102:7000
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 0a2ff6c720ba32c398c8fc3d5ef14bd241252904 172.16.1.102:7005
   slots: (0 slots) slave
   replicates 93faf299a645c7eaac1b898fde6da48de28c6f1c
S: 4672c47668ea7f179f484cb64bc00e1347155ff5 172.16.1.102:7004
   slots: (0 slots) slave
   replicates d42969de78528d247580a39106e594dfbf9b1005
M: 3bf112e8fab64ddae44eaf33c7c13eff1a536112 172.16.1.102:7006
   slots: (0 slots) master
M: d42969de78528d247580a39106e594dfbf9b1005 172.16.1.102:7001
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
M: 93faf299a645c7eaac1b898fde6da48de28c6f1c 172.16.1.102:7002
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 6d31edca810ddcef5c1191394aec1153c159a7dc 172.16.1.102:7003
   slots: (0 slots) slave
   replicates a968b7602483bd05f0f7ea5c2cc13eeff6ee7918
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 172.16.1.102:7007 to make it join the cluster.
Waiting for the cluster to join

>>> Configure node as replica of 172.16.1.102:7006.
[OK] New node added correctly.

 
#再次check集群信息 ,每个主都有一个从。
[root@localhosts redis]# redis-cli -a 123456 --cluster check 172.16.1.102:7000          Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
172.16.1.102:7000 (a968b760...) -> 33321 keys | 5461 slots | 1 slaves.
172.16.1.102:7006 (3bf112e8...) -> 0 keys | 0 slots | 1 slaves.
172.16.1.102:7001 (d42969de...) -> 33390 keys | 5462 slots | 1 slaves.
172.16.1.102:7002 (93faf299...) -> 33291 keys | 5461 slots | 1 slaves.
[OK] 100002 keys in 4 masters.
6.10 keys per slot on average.
>>> Performing Cluster Check (using node 172.16.1.102:7000)
M: a968b7602483bd05f0f7ea5c2cc13eeff6ee7918 172.16.1.102:7000
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 0a2ff6c720ba32c398c8fc3d5ef14bd241252904 172.16.1.102:7005
   slots: (0 slots) slave
   replicates 93faf299a645c7eaac1b898fde6da48de28c6f1c
S: 4672c47668ea7f179f484cb64bc00e1347155ff5 172.16.1.102:7004
   slots: (0 slots) slave
   replicates d42969de78528d247580a39106e594dfbf9b1005
M: 3bf112e8fab64ddae44eaf33c7c13eff1a536112 172.16.1.102:7006
   slots: (0 slots) master
   1 additional replica(s)
M: d42969de78528d247580a39106e594dfbf9b1005 172.16.1.102:7001
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
M: 93faf299a645c7eaac1b898fde6da48de28c6f1c 172.16.1.102:7002
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 2297a3c5efd6e459539f562faf35edafd3566889 172.16.1.102:7007
   slots: (0 slots) slave
   replicates 3bf112e8fab64ddae44eaf33c7c13eff1a536112
S: 6d31edca810ddcef5c1191394aec1153c159a7dc 172.16.1.102:7003
   slots: (0 slots) slave
   replicates a968b7602483bd05f0f7ea5c2cc13eeff6ee7918
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

#把7000的master槽 全部移动到 7006 上,7006现在slot为0, 7000总共有5461个槽位,--cluster-slots 5461
redis-cli -a 123456 --cluster reshard 172.16.1.102:7001 --cluster-from a968b7602483bd05f0f7ea5c2cc13eeff6ee7918 --cluster-to 3bf112e8fab64ddae44eaf33c7c13eff1a536112 --cluster-slots 5461
yes
yes


#再次check集群信息
[root@localhosts redis]# redis-cli -a 123456 --cluster check 172.16.1.102:7000          Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
172.16.1.102:7000 (a968b760...) -> 0 keys | 0 slots | 0 slaves.
172.16.1.102:7006 (3bf112e8...) -> 33321 keys | 5461 slots | 2 slaves.
172.16.1.102:7001 (d42969de...) -> 33390 keys | 5462 slots | 1 slaves.
172.16.1.102:7002 (93faf299...) -> 33291 keys | 5461 slots | 1 slaves.
[OK] 100002 keys in 4 masters.
6.10 keys per slot on average.
>>> Performing Cluster Check (using node 172.16.1.102:7000)
M: a968b7602483bd05f0f7ea5c2cc13eeff6ee7918 172.16.1.102:7000
   slots: (0 slots) master
S: 0a2ff6c720ba32c398c8fc3d5ef14bd241252904 172.16.1.102:7005
   slots: (0 slots) slave
   replicates 93faf299a645c7eaac1b898fde6da48de28c6f1c
S: 4672c47668ea7f179f484cb64bc00e1347155ff5 172.16.1.102:7004
   slots: (0 slots) slave
   replicates d42969de78528d247580a39106e594dfbf9b1005
M: 3bf112e8fab64ddae44eaf33c7c13eff1a536112 172.16.1.102:7006
   slots:[0-5460] (5461 slots) master
   2 additional replica(s)
M: d42969de78528d247580a39106e594dfbf9b1005 172.16.1.102:7001
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
M: 93faf299a645c7eaac1b898fde6da48de28c6f1c 172.16.1.102:7002
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 2297a3c5efd6e459539f562faf35edafd3566889 172.16.1.102:7007
   slots: (0 slots) slave
   replicates 3bf112e8fab64ddae44eaf33c7c13eff1a536112
S: 6d31edca810ddcef5c1191394aec1153c159a7dc 172.16.1.102:7003
   slots: (0 slots) slave
   replicates 3bf112e8fab64ddae44eaf33c7c13eff1a536112
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

#移除节点

[root@localhosts redis]# redis-cli -a 123456 --cluster del-node 172.16.1.102:7001  a968b7602483bd05f0f7ea5c2cc13eeff6ee7918
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Removing node a968b7602483bd05f0f7ea5c2cc13eeff6ee7918 from cluster 172.16.1.102:7001
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.

#再次check集群信息
[root@localhosts redis]# redis-cli -a 123456 --cluster check 172.16.1.102:7001
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
172.16.1.102:7001 (d42969de...) -> 33390 keys | 5462 slots | 1 slaves.
172.16.1.102:7002 (93faf299...) -> 33291 keys | 5461 slots | 1 slaves.
172.16.1.102:7006 (3bf112e8...) -> 33321 keys | 5461 slots | 2 slaves.
[OK] 100002 keys in 3 masters.
6.10 keys per slot on average.
>>> Performing Cluster Check (using node 172.16.1.102:7001)
M: d42969de78528d247580a39106e594dfbf9b1005 172.16.1.102:7001
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
M: 93faf299a645c7eaac1b898fde6da48de28c6f1c 172.16.1.102:7002
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
M: 3bf112e8fab64ddae44eaf33c7c13eff1a536112 172.16.1.102:7006
   slots:[0-5460] (5461 slots) master
   2 additional replica(s)
S: 0a2ff6c720ba32c398c8fc3d5ef14bd241252904 172.16.1.102:7005
   slots: (0 slots) slave
   replicates 93faf299a645c7eaac1b898fde6da48de28c6f1c
S: 6d31edca810ddcef5c1191394aec1153c159a7dc 172.16.1.102:7003
   slots: (0 slots) slave
   replicates 3bf112e8fab64ddae44eaf33c7c13eff1a536112
S: 2297a3c5efd6e459539f562faf35edafd3566889 172.16.1.102:7007
   slots: (0 slots) slave
   replicates 3bf112e8fab64ddae44eaf33c7c13eff1a536112
S: 4672c47668ea7f179f484cb64bc00e1347155ff5 172.16.1.102:7004
   slots: (0 slots) slave
   replicates d42969de78528d247580a39106e594dfbf9b1005
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

Redis 集群管理常见操作一览