Redis-Cluster 搭建

环境

一共 6 个实例,分别放在 3 个机器上

192.168.65.2 7000,7001

192.168.65.3 7002,7003

192.168.65.4 7004,7005

配置文件

Redis 的安装过程这里就介绍,可以看我这篇文章:https://blog.csdn.net/qq_31144297/article/details/106068216 

vi /data/redis/redis.conf
添加或修改如下内容:
bind 192.168.65.2
logfile "/data/redis/log/redis.log"
dir "/data/redis/data"
daemonize yes
pidfile "/var/run/redis_7000.pid"
port 7000
cluster-enabled  yes
cluster-config-file  /data/redis/nodes_7000.conf
cluster-node-timeout  5000
appendonly yes

其他 5 个节点相同,修改里面的端口和路径相关的内容即可。

然后启动 Redis 服务,查看状态

[redis@test2 ~]$ redis-server /data/redis/redis.conf
[redis@test2 ~]$ redis-server /data/redis01/redis.conf
[redis@test2 ~]$ ps -ef|grep redis
redis      3004      1  0 04:18 ?        00:00:30 redis-server 192.168.65.2:7000 [cluster]
redis      3009      1  0 04:18 ?        00:00:33 redis-server 192.168.65.2:7003 [cluster]

[redis@test3 data]$ redis-server /data/redis/redis.conf
[redis@test3 data]$ redis-server /data/redis01/redis.conf
[redis@test3 data]$ ps -ef|grep redis
redis     44188      1  0 18:56 ?        00:00:32 redis-server 192.168.65.3:7001 [cluster]
redis     44193      1  0 18:56 ?        00:00:35 redis-server 192.168.65.3:7004 [cluster]

[redis@test4 ~]$ redis-server /data/redis/redis.conf
[redis@test4 ~]$ redis-server /data/redis01/redis.conf
[redis@test4 data]$ ps -ef|grep redis
redis     12631      1  0 19:57 ?        00:00:33 redis-server 192.168.65.4:7002 [cluster]
redis     12636      1  0 19:57 ?        00:00:34 redis-server 192.168.65.4:7005 [cluster]

创建集群

redis-cli --cluster create 192.168.65.2:7000 192.168.65.2:7003 192.168.65.3:7001 192.168.65.3:7004 192.168.65.4:7002 192.168.65.4:7005 --cluster-replicas 1

如果出现
[ERR] Node 192.168.65.2:7000 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.
这个错误,表示之前创建过,可能资料没有清理干净导致。
解决方法:
1)、将需要新增的节点下aof、rdb等本地备份文件删除;
2)、同时将新Node的集群配置文件删除,即:删除你redis.conf里面cluster-config-file所在的文件;
3)、再次添加新节点如果还是报错,则登录新Node,./redis-cli–h x –p对数据库进行清除:

如果正确,则会提示如下信息:
[redis@test2 data]$ redis-cli --cluster create 192.168.65.2:7000 192.168.65.2:7003 192.168.65.3:7001 192.168.65.3:7004 192.168.65.4:7002 192.168.65.4:7005 --cluster-replicas 1
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 192.168.65.3:7004 to 192.168.65.2:7000
Adding replica 192.168.65.4:7005 to 192.168.65.3:7001
Adding replica 192.168.65.2:7003 to 192.168.65.4:7002
M: 7c32d265ffb53922ef7a0a795d61845c8bd20594 192.168.65.2:7000
   slots:[0-5460] (5461 slots) master
S: e0425d6872fc7891e409f4cd70203bb01aef122a 192.168.65.2:7003
   replicates c666b7a75543419960f50fefaeab9f28796520cd
M: dd7d7a572c6faccc72b87e81e0bd73b08573f50b 192.168.65.3:7001
   slots:[5461-10922] (5462 slots) master
S: 2b70eaead74013f9b1277064e9f4131ca8a68899 192.168.65.3:7004
   replicates 7c32d265ffb53922ef7a0a795d61845c8bd20594
M: c666b7a75543419960f50fefaeab9f28796520cd 192.168.65.4:7002
   slots:[10923-16383] (5461 slots) master
S: eed011474b29cefcaf753418eb4efadd844339b7 192.168.65.4:7005
   replicates dd7d7a572c6faccc72b87e81e0bd73b08573f50b
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 192.168.65.2:7000)
M: 7c32d265ffb53922ef7a0a795d61845c8bd20594 192.168.65.2:7000
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 2b70eaead74013f9b1277064e9f4131ca8a68899 192.168.65.3:7004
   slots: (0 slots) slave
   replicates 7c32d265ffb53922ef7a0a795d61845c8bd20594
M: c666b7a75543419960f50fefaeab9f28796520cd 192.168.65.4:7002
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
M: dd7d7a572c6faccc72b87e81e0bd73b08573f50b 192.168.65.3:7001
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: eed011474b29cefcaf753418eb4efadd844339b7 192.168.65.4:7005
   slots: (0 slots) slave
   replicates dd7d7a572c6faccc72b87e81e0bd73b08573f50b
S: e0425d6872fc7891e409f4cd70203bb01aef122a 192.168.65.2:7003
   slots: (0 slots) slave
   replicates c666b7a75543419960f50fefaeab9f28796520cd
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

可以看到 6 个节点 自动被分成了 3 主 3 从,及槽位分配信息。

如果这 6 台机器你全部都是规划的为主库,则在创建集群的命令中不要 --cluster-replicas 1 这个参数,这个参数的意思是需要为每一台主库都配置一个从库。

 

检查集群状态

[redis@test2 data]$ redis-cli --cluster check 192.168.65.2:7000
192.168.65.2:7000 (7c32d265...) -> 0 keys | 5461 slots | 1 slaves.
192.168.65.4:7002 (c666b7a7...) -> 0 keys | 5461 slots | 1 slaves.
192.168.65.3:7001 (dd7d7a57...) -> 0 keys | 5462 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.65.2:7000)
M: 7c32d265ffb53922ef7a0a795d61845c8bd20594 192.168.65.2:7000
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 2b70eaead74013f9b1277064e9f4131ca8a68899 192.168.65.3:7004
   slots: (0 slots) slave
   replicates 7c32d265ffb53922ef7a0a795d61845c8bd20594
M: c666b7a75543419960f50fefaeab9f28796520cd 192.168.65.4:7002
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
M: dd7d7a572c6faccc72b87e81e0bd73b08573f50b 192.168.65.3:7001
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: eed011474b29cefcaf753418eb4efadd844339b7 192.168.65.4:7005
   slots: (0 slots) slave
   replicates dd7d7a572c6faccc72b87e81e0bd73b08573f50b
S: e0425d6872fc7891e409f4cd70203bb01aef122a 192.168.65.2:7003
   slots: (0 slots) slave
   replicates c666b7a75543419960f50fefaeab9f28796520cd
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

数据验证

[redis@test2 data]$ redis-cli -c -h 192.168.65.2 -p 7000
192.168.65.2:7000> set t y
-> Redirected to slot [15891] located at 192.168.65.4:7002
OK
192.168.65.4:7002> set a a
OK
192.168.65.4:7002> set b b
-> Redirected to slot [3300] located at 192.168.65.2:7000
OK
192.168.65.2:7000> set c c
-> Redirected to slot [7365] located at 192.168.65.3:7001
OK
192.168.65.3:7001> set d d
-> Redirected to slot [11298] located at 192.168.65.4:7002
OK

可以看到写数据分别分配到了3个主节点

去其他节点验证数据是否同步
[redis@test3 data]$ redis-cli -c -h 192.168.65.3 -p 7001
192.168.65.3:7001> get t
-> Redirected to slot [15891] located at 192.168.65.4:7002
"y"
192.168.65.4:7002> get a
"a"
192.168.65.4:7002> get b
-> Redirected to slot [3300] located at 192.168.65.2:7000
"b"
192.168.65.2:7000> get c
-> Redirected to slot [7365] located at 192.168.65.3:7001
"c"
192.168.65.3:7001> exit
[redis@test3 data]$ redis-cli -c -h 192.168.65.3 -p 7004
192.168.65.3:7004> get t
-> Redirected to slot [15891] located at 192.168.65.4:7002
"y"
192.168.65.4:7002> get a
"a"
192.168.65.4:7002> get b
-> Redirected to slot [3300] located at 192.168.65.2:7000
"b"
192.168.65.2:7000> get c
-> Redirected to slot [7365] located at 192.168.65.3:7001
"c"

到此,Redis Cluster 搭建完成,非常容易。之前的版本,还需要 ruby 的环境,现在不需要,更简单了。

 

集群常用操作

基本都在 redis-cli --cluster 中,先看看帮助
redis-cli --cluster help
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 Cluster最低要求是3个主节点,如果需要集群需要认证,则在最后加入 -a xx 即可。

创建集群主节点

redis-cli --cluster create 192.168.65.2:6379 192.168.65.2:6380 192.168.65.2:6381
>>> Performing hash slots allocation on 3 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
M: 117457eab5071954faab5e81c3170600d5192270 192.168.65.2:6379
   slots:[0-5460] (5461 slots) master
M: 815da8448f5d5a304df0353ca10d8f9b77016b28 192.168.65.2:6380
   slots:[5461-10922] (5462 slots) master
M: 56005b9413cbf225783906307a2631109e753f8f 192.168.65.2:6381
   slots:[10923-16383] (5461 slots) master
Can I set the above configuration? (type 'yes' to accept): yes #slot分配
>>> 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 192.168.65.2:6379)
M: 117457eab5071954faab5e81c3170600d5192270 192.168.65.2:6379
   slots:[0-5460] (5461 slots) master
M: 56005b9413cbf225783906307a2631109e753f8f 192.168.65.2:6381
   slots:[10923-16383] (5461 slots) master
M: 815da8448f5d5a304df0353ca10d8f9b77016b28 192.168.65.2:6380
   slots:[5461-10922] (5462 slots) master
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

创建从节点

/redis-cli --cluster create 192.168.65.2:6379 192.168.65.2:6380 192.168.65.2:6381 192.168.65.2:6382 192.168.65.2:6383 192.168.65.2:6384 --cluster-replicas 1
说明:--cluster-replicas 参数为数字,1表示每个主节点需要1个从节点。
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 192.168.65.2:6383 to 192.168.65.2:6379
Adding replica 192.168.65.2:6384 to 192.168.65.2:6380
Adding replica 192.168.65.2:6382 to 192.168.65.2:6381
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 117457eab5071954faab5e81c3170600d5192270 192.168.65.2:6379
   slots:[0-5460] (5461 slots) master
M: 815da8448f5d5a304df0353ca10d8f9b77016b28 192.168.65.2:6380
   slots:[5461-10922] (5462 slots) master
M: 56005b9413cbf225783906307a2631109e753f8f 192.168.65.2:6381
   slots:[10923-16383] (5461 slots) master
S: 3a1d04983ab6c4ae853f9602dd922d4ebadc4dbf 192.168.65.2:6382
   replicates 56005b9413cbf225783906307a2631109e753f8f
S: 0c21b6cee354594a23f4d5abf0d01b48bdc96d55 192.168.65.2:6383
   replicates 117457eab5071954faab5e81c3170600d5192270
S: f6a6957421b80409106cb36be3c7ba41f3b603ff 192.168.65.2:6384
   replicates 815da8448f5d5a304df0353ca10d8f9b77016b28
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 192.168.65.2:6379)
M: 117457eab5071954faab5e81c3170600d5192270 192.168.65.2:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: 815da8448f5d5a304df0353ca10d8f9b77016b28 192.168.65.2:6380
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 3a1d04983ab6c4ae853f9602dd922d4ebadc4dbf 192.168.65.2:6382
   slots: (0 slots) slave
   replicates 56005b9413cbf225783906307a2631109e753f8f
S: 0c21b6cee354594a23f4d5abf0d01b48bdc96d55 192.168.65.2:6383
   slots: (0 slots) slave
   replicates 117457eab5071954faab5e81c3170600d5192270
M: 56005b9413cbf225783906307a2631109e753f8f 192.168.65.2:6381
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: f6a6957421b80409106cb36be3c7ba41f3b603ff 192.168.65.2:6384
   slots: (0 slots) slave
   replicates 815da8448f5d5a304df0353ca10d8f9b77016b28
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

通过该方式创建的带有从节点的机器不能够自己手动指定主节点,所以如果需要指定的话,需要自己手动指定,先使用①或③创建好主节点后,再通过④来处理。

添加集群主节点

添加集群主节点
redis-cli --cluster add-node 192.168.163.132:6382 192.168.163.132:6379 
说明:为一个指定集群添加节点,需要先连到该集群的任意一个节点IP(192.168.163.132:6379),再把新节点加入。该2个参数的顺序有要求:新加入的节点放前

[redis@test2 ~]$ redis-cli --cluster add-node 192.168.65.2:7006 192.168.65.2:7000
>>> Adding node 192.168.65.2:7006 to cluster 192.168.65.2:7000
>>> Performing Cluster Check (using node 192.168.65.2:7000)
M: 7c32d265ffb53922ef7a0a795d61845c8bd20594 192.168.65.2:7000
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 2b70eaead74013f9b1277064e9f4131ca8a68899 192.168.65.3:7004
   slots: (0 slots) slave
   replicates 7c32d265ffb53922ef7a0a795d61845c8bd20594
M: c666b7a75543419960f50fefaeab9f28796520cd 192.168.65.4:7002
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
M: dd7d7a572c6faccc72b87e81e0bd73b08573f50b 192.168.65.3:7001
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: eed011474b29cefcaf753418eb4efadd844339b7 192.168.65.4:7005
   slots: (0 slots) slave
   replicates dd7d7a572c6faccc72b87e81e0bd73b08573f50b
S: e0425d6872fc7891e409f4cd70203bb01aef122a 192.168.65.2:7003
   slots: (0 slots) slave
   replicates c666b7a75543419960f50fefaeab9f28796520cd
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 192.168.65.2:7006 to make it join the cluster.
[OK] New node added correctly.

check 集群,可以发现信息中多了一个主节点,但是没有分配槽位
[redis@test2 ~]$ redis-cli --cluster check 192.168.65.2:7000
192.168.65.2:7000 (7c32d265...) -> 1 keys | 5461 slots | 1 slaves.
192.168.65.4:7002 (c666b7a7...) -> 3 keys | 5461 slots | 1 slaves.
192.168.65.3:7001 (dd7d7a57...) -> 1 keys | 5462 slots | 1 slaves.
192.168.65.2:7006 (4fbe96b7...) -> 0 keys | 0 slots | 0 slaves.
[OK] 5 keys in 4 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.65.2:7000)
M: 7c32d265ffb53922ef7a0a795d61845c8bd20594 192.168.65.2:7000
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 2b70eaead74013f9b1277064e9f4131ca8a68899 192.168.65.3:7004
   slots: (0 slots) slave
   replicates 7c32d265ffb53922ef7a0a795d61845c8bd20594
M: c666b7a75543419960f50fefaeab9f28796520cd 192.168.65.4:7002
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
M: dd7d7a572c6faccc72b87e81e0bd73b08573f50b 192.168.65.3:7001
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: eed011474b29cefcaf753418eb4efadd844339b7 192.168.65.4:7005
   slots: (0 slots) slave
   replicates dd7d7a572c6faccc72b87e81e0bd73b08573f50b
S: e0425d6872fc7891e409f4cd70203bb01aef122a 192.168.65.2:7003
   slots: (0 slots) slave
   replicates c666b7a75543419960f50fefaeab9f28796520cd
M: 4fbe96b7a785b5b6377631a980cb420ffba9ed23 192.168.65.2:7006
   slots: (0 slots) master
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

添加集群从节点

redis-cli --cluster add-node 192.168.65.2:7007 192.168.65.2:7000 --cluster-slave --cluster-master-id 4fbe96b7a785b5b6377631a980cb420ffba9ed23
说明:把6382节点加入到6379节点的集群中,并且当做node_id为 117457eab5071954faab5e81c3170600d5192270 的从节点。如果不指定 --cluster-master-id 会随机分配到任意一个主节点。

[redis@test2 ~]$ redis-cli --cluster add-node 192.168.65.2:7007 192.168.65.2:7000 --cluster-slave --cluster-master-id 4fbe96b7a785b5b6377631a980cb420ffba9ed23
>>> Adding node 192.168.65.2:7007 to cluster 192.168.65.2:7000
>>> Performing Cluster Check (using node 192.168.65.2:7000)
M: 7c32d265ffb53922ef7a0a795d61845c8bd20594 192.168.65.2:7000
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 2b70eaead74013f9b1277064e9f4131ca8a68899 192.168.65.3:7004
   slots: (0 slots) slave
   replicates 7c32d265ffb53922ef7a0a795d61845c8bd20594
M: c666b7a75543419960f50fefaeab9f28796520cd 192.168.65.4:7002
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
M: dd7d7a572c6faccc72b87e81e0bd73b08573f50b 192.168.65.3:7001
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: eed011474b29cefcaf753418eb4efadd844339b7 192.168.65.4:7005
   slots: (0 slots) slave
   replicates dd7d7a572c6faccc72b87e81e0bd73b08573f50b
S: e0425d6872fc7891e409f4cd70203bb01aef122a 192.168.65.2:7003
   slots: (0 slots) slave
   replicates c666b7a75543419960f50fefaeab9f28796520cd
M: 4fbe96b7a785b5b6377631a980cb420ffba9ed23 192.168.65.2:7006
   slots: (0 slots) master
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 192.168.65.2:7007 to make it join the cluster.
Waiting for the cluster to join

>>> Configure node as replica of 192.168.65.2:7006.
[OK] New node added correctly.
[redis@test2 ~]$ redis-cli --cluster check 192.168.65.2:7000
192.168.65.2:7000 (7c32d265...) -> 1 keys | 5461 slots | 1 slaves.
192.168.65.4:7002 (c666b7a7...) -> 3 keys | 5461 slots | 1 slaves.
192.168.65.3:7001 (dd7d7a57...) -> 1 keys | 5462 slots | 1 slaves.
192.168.65.2:7006 (4fbe96b7...) -> 0 keys | 0 slots | 1 slaves.
[OK] 5 keys in 4 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.65.2:7000)
M: 7c32d265ffb53922ef7a0a795d61845c8bd20594 192.168.65.2:7000
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 2b70eaead74013f9b1277064e9f4131ca8a68899 192.168.65.3:7004
   slots: (0 slots) slave
   replicates 7c32d265ffb53922ef7a0a795d61845c8bd20594
M: c666b7a75543419960f50fefaeab9f28796520cd 192.168.65.4:7002
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
M: dd7d7a572c6faccc72b87e81e0bd73b08573f50b 192.168.65.3:7001
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: eed011474b29cefcaf753418eb4efadd844339b7 192.168.65.4:7005
   slots: (0 slots) slave
   replicates dd7d7a572c6faccc72b87e81e0bd73b08573f50b
S: e0425d6872fc7891e409f4cd70203bb01aef122a 192.168.65.2:7003
   slots: (0 slots) slave
   replicates c666b7a75543419960f50fefaeab9f28796520cd
S: febe0c9390b357917850cb82e2ca35b95fd51fac 192.168.65.2:7007
   slots: (0 slots) slave
   replicates 4fbe96b7a785b5b6377631a980cb420ffba9ed23
M: 4fbe96b7a785b5b6377631a980cb420ffba9ed23 192.168.65.2:7006
   slots: (0 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.

然后查看指定的主是否有增加从
[redis@test2 ~]$ redis-cli -c -h 192.168.65.2 -p 7006 info replication
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.65.2,port=7007,state=online,offset=196,lag=0
master_replid:713a88eb7d1b01ce67056ce6320b0e850a19d963
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:196
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:196

删除节点

redis-cli --cluster del-node 192.168.65.2:7007 febe0c9390b357917850cb82e2ca35b95fd51fac
说明:指定IP、端口和node_id 来删除一个节点,从节点可以直接删除,主节点不能直接删除,删除之后,该节点会被shutdown。
[redis@test2 ~]$ redis-cli --cluster del-node 192.168.65.2:7007 febe0c9390b357917850cb82e2ca35b95fd51fac
>>> Removing node febe0c9390b357917850cb82e2ca35b95fd51fac from cluster 192.168.65.2:7007
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.

[redis@test2 ~]$ redis-cli --cluster check   192.168.65.2:7000
192.168.65.2:7000 (7c32d265...) -> 1 keys | 5461 slots | 1 slaves.
192.168.65.4:7002 (c666b7a7...) -> 3 keys | 5461 slots | 1 slaves.
192.168.65.3:7001 (dd7d7a57...) -> 1 keys | 5462 slots | 1 slaves.
192.168.65.2:7006 (4fbe96b7...) -> 0 keys | 0 slots | 0 slaves.
[OK] 5 keys in 4 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.65.2:7000)
M: 7c32d265ffb53922ef7a0a795d61845c8bd20594 192.168.65.2:7000
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 2b70eaead74013f9b1277064e9f4131ca8a68899 192.168.65.3:7004
   slots: (0 slots) slave
   replicates 7c32d265ffb53922ef7a0a795d61845c8bd20594
M: c666b7a75543419960f50fefaeab9f28796520cd 192.168.65.4:7002
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
M: dd7d7a572c6faccc72b87e81e0bd73b08573f50b 192.168.65.3:7001
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: eed011474b29cefcaf753418eb4efadd844339b7 192.168.65.4:7005
   slots: (0 slots) slave
   replicates dd7d7a572c6faccc72b87e81e0bd73b08573f50b
S: e0425d6872fc7891e409f4cd70203bb01aef122a 192.168.65.2:7003
   slots: (0 slots) slave
   replicates c666b7a75543419960f50fefaeab9f28796520cd
M: 4fbe96b7a785b5b6377631a980cb420ffba9ed23 192.168.65.2:7006
   slots: (0 slots) master
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

注意:当被删除掉的节点重新起来之后不能自动加入集群,但其和主的复制还是正常的,也可以通过该节点看到集群信息(通过其他正常节点已经看不到该被del-node节点的信息)。

如果想要再次加入集群,则需要先连接该节点,然后执行cluster reset,再用add-node进行添加,进行增量同步复制。

如果删的是主节点,且不为空
[redis@test2 ~]$ redis-cli --cluster del-node 192.168.65.4:7002 c666b7a75543419960f50fefaeab9f28796520cd
>>> Removing node c666b7a75543419960f50fefaeab9f28796520cd from cluster 192.168.65.4:7002
[ERR] Node 192.168.65.4:7002 is not empty! Reshard data away and try again.
这种情况的处理步骤:
    1.应该先将该节点中的slot迁移到其他机器(用 reshard 参数)。
    2.然后再用 del-node 来删除要下线的节点。
    3.重新平衡剩下节点中slot的分配(rebalance)

检查集群

[redis@test2 ~]$ redis-cli --cluster check   192.168.65.2:7000
192.168.65.2:7000 (7c32d265...) -> 1 keys | 5461 slots | 1 slaves.
192.168.65.4:7002 (c666b7a7...) -> 3 keys | 5461 slots | 1 slaves.
192.168.65.3:7001 (dd7d7a57...) -> 1 keys | 5462 slots | 1 slaves.
192.168.65.2:7006 (4fbe96b7...) -> 0 keys | 0 slots | 0 slaves.
[OK] 5 keys in 4 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.65.2:7000)
M: 7c32d265ffb53922ef7a0a795d61845c8bd20594 192.168.65.2:7000
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 2b70eaead74013f9b1277064e9f4131ca8a68899 192.168.65.3:7004
   slots: (0 slots) slave
   replicates 7c32d265ffb53922ef7a0a795d61845c8bd20594
M: c666b7a75543419960f50fefaeab9f28796520cd 192.168.65.4:7002
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
M: dd7d7a572c6faccc72b87e81e0bd73b08573f50b 192.168.65.3:7001
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: eed011474b29cefcaf753418eb4efadd844339b7 192.168.65.4:7005
   slots: (0 slots) slave
   replicates dd7d7a572c6faccc72b87e81e0bd73b08573f50b
S: e0425d6872fc7891e409f4cd70203bb01aef122a 192.168.65.2:7003
   slots: (0 slots) slave
   replicates c666b7a75543419960f50fefaeab9f28796520cd
M: 4fbe96b7a785b5b6377631a980cb420ffba9ed23 192.168.65.2:7006
   slots: (0 slots) master
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

集群信息查看

[redis@test2 ~]$ redis-cli --cluster info 192.168.65.4:7002
192.168.65.4:7002 (c666b7a7...) -> 3 keys | 5461 slots | 1 slaves.
192.168.65.3:7001 (dd7d7a57...) -> 1 keys | 5462 slots | 1 slaves.
192.168.65.2:7000 (7c32d265...) -> 1 keys | 5461 slots | 1 slaves.
[OK] 5 keys in 3 masters.
0.00 keys per slot on average.

 修复集群

redis-cli --cluster fix 192.168.163.132:6384 --cluster-search-multiple-owners
说明:修复集群和槽的重复分配问题

设置集群的超时时间 

redis-cli --cluster set-timeout 192.168.163.132:6382 10000
说明:连接到集群的任意一节点来设置集群的超时时间参数cluster-node-timeout

[redis@test2 redis02]$ redis-cli --cluster set-timeout 192.168.65.2:7000 10000
>>> Reconfiguring node timeout in every cluster node...
*** New timeout set for 192.168.65.2:7000
*** New timeout set for 192.168.65.3:7004
*** New timeout set for 192.168.65.4:7002
*** New timeout set for 192.168.65.3:7001
*** New timeout set for 192.168.65.4:7005
*** New timeout set for 192.168.65.2:7003
*** New timeout set for 192.168.65.2:7006
>>> New node timeout set. 7 OK, 0 ERR.

集群中执行相关命令

redis-cli --cluster call 192.168.65.2:7000 config set requirepass cc
redis-cli -a cc --cluster call 192.168.65.2:7006 config set masterauth cc
redis-cli -a cc --cluster call 192.168.65.2:7006 config rewrite
说明:连接到集群的任意一节点来对整个集群的所有节点进行设置。

redis-cli --cluster call 192.168.65.2:7000 config set cluster-node-timeout 12000
>>> Calling config set cluster-node-timeout 12000
192.168.65.2:7000: OK
192.168.65.2:7006: OK
192.168.65.3:7001: OK
192.168.65.3:7004: OK
192.168.65.4:7002:OK
192.168.65.4:7005: OK
...
...

到此,相关集群的基本操作已经介绍完,现在说明集群迁移的相关操作。

 

迁移相关

在线迁移slot

在线迁移slot :
在线把集群的一些slot从集群原来slot节点迁移到新的节点,即可以完成集群的在线横向扩容和缩容。有2种方式进行迁移

一是根据提示来进行操作:

直接连接到集群的任意一节点
redis-cli -a cc --cluster reshard 192.168.65.2:7000
[redis@test2 redis02]$ redis-cli --cluster reshard 192.168.65.2:7000
>>> Performing Cluster Check (using node 192.168.65.2:7000)
M: 7c32d265ffb53922ef7a0a795d61845c8bd20594 192.168.65.2:7000
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 2b70eaead74013f9b1277064e9f4131ca8a68899 192.168.65.3:7004
   slots: (0 slots) slave
   replicates 7c32d265ffb53922ef7a0a795d61845c8bd20594
M: c666b7a75543419960f50fefaeab9f28796520cd 192.168.65.4:7002
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
M: dd7d7a572c6faccc72b87e81e0bd73b08573f50b 192.168.65.3:7001
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: eed011474b29cefcaf753418eb4efadd844339b7 192.168.65.4:7005
   slots: (0 slots) slave
   replicates dd7d7a572c6faccc72b87e81e0bd73b08573f50b
S: e0425d6872fc7891e409f4cd70203bb01aef122a 192.168.65.2:7003
   slots: (0 slots) slave
   replicates c666b7a75543419960f50fefaeab9f28796520cd
M: 4fbe96b7a785b5b6377631a980cb420ffba9ed23 192.168.65.2:7006
   slots: (0 slots) master
[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)? 1   #这里输入要移动几个slot
What is the receiving node ID? 4fbe96b7a785b5b6377631a980cb420ffba9ed23
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: 7c32d265ffb53922ef7a0a795d61845c8bd20594   #从哪个节点移动,节点id
Source node #2: done   #有几个源节点就写几个,没有了就写done

Ready to move 1 slots.
  Source nodes:
    M: 7c32d265ffb53922ef7a0a795d61845c8bd20594 192.168.65.2:7000
       slots:[0-5460] (5461 slots) master
       1 additional replica(s)
  Destination node:
    M: 4fbe96b7a785b5b6377631a980cb420ffba9ed23 192.168.65.2:7006
       slots: (0 slots) master
  Resharding plan:
    Moving slot 0 from 7c32d265ffb53922ef7a0a795d61845c8bd20594
Do you want to proceed with the proposed reshard plan (yes/no)? yes    #确定
Moving slot 0 from 192.168.65.2:7000 to 192.168.65.2:7006: 

完成后再check
[redis@test2 redis02]$ redis-cli --cluster check 192.168.65.4:7002
192.168.65.4:7002 (c666b7a7...) -> 3 keys | 5461 slots | 1 slaves.
192.168.65.2:7006 (4fbe96b7...) -> 0 keys | 1 slots | 0 slaves.
192.168.65.3:7001 (dd7d7a57...) -> 1 keys | 5462 slots | 1 slaves.
192.168.65.2:7000 (7c32d265...) -> 1 keys | 5460 slots | 1 slaves.
[OK] 5 keys in 4 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.65.4:7002)
M: c666b7a75543419960f50fefaeab9f28796520cd 192.168.65.4:7002
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
M: 4fbe96b7a785b5b6377631a980cb420ffba9ed23 192.168.65.2:7006
   slots:[0] (1 slots) master
M: dd7d7a572c6faccc72b87e81e0bd73b08573f50b 192.168.65.3:7001
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: e0425d6872fc7891e409f4cd70203bb01aef122a 192.168.65.2:7003
   slots: (0 slots) slave
   replicates c666b7a75543419960f50fefaeab9f28796520cd
S: 2b70eaead74013f9b1277064e9f4131ca8a68899 192.168.65.3:7004
   slots: (0 slots) slave
   replicates 7c32d265ffb53922ef7a0a795d61845c8bd20594
S: eed011474b29cefcaf753418eb4efadd844339b7 192.168.65.4:7005
   slots: (0 slots) slave
   replicates dd7d7a572c6faccc72b87e81e0bd73b08573f50b
M: 7c32d265ffb53922ef7a0a795d61845c8bd20594 192.168.65.2:7000
   slots:[1-5460] (5460 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.





二是根据参数进行操作:
redis-cli -a cc --cluster reshard 192.168.163.132:6379 --cluster-from 117457eab5071954faab5e81c3170600d5192270 --cluster-to 815da8448f5d5a304df0353ca10d8f9b77016b28 --cluster-slots 10 --cluster-yes --cluster-timeout 5000 --cluster-pipeline 10 --cluster-replace
说明:连接到集群的任意一节点来对指定节点指定数量的slot进行迁移到指定的节点。

[redis@test2 redis02]$ redis-cli --cluster reshard 192.168.65.2:7000 --cluster-from 7c32d265ffb53922ef7a0a795d61845c8bd20594 --cluster-to 4fbe96b7a785b5b6377631a980cb420ffba9ed23 --cluster-slots 10 --cluster-yes --cluster-timeout 5000 --cluster-pipeline 10 --cluster-replace
>>> Performing Cluster Check (using node 192.168.65.2:7000)
M: 7c32d265ffb53922ef7a0a795d61845c8bd20594 192.168.65.2:7000
   slots:[1-5460] (5460 slots) master
   1 additional replica(s)
S: 2b70eaead74013f9b1277064e9f4131ca8a68899 192.168.65.3:7004
   slots: (0 slots) slave
   replicates 7c32d265ffb53922ef7a0a795d61845c8bd20594
M: c666b7a75543419960f50fefaeab9f28796520cd 192.168.65.4:7002
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
M: dd7d7a572c6faccc72b87e81e0bd73b08573f50b 192.168.65.3:7001
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: eed011474b29cefcaf753418eb4efadd844339b7 192.168.65.4:7005
   slots: (0 slots) slave
   replicates dd7d7a572c6faccc72b87e81e0bd73b08573f50b
S: e0425d6872fc7891e409f4cd70203bb01aef122a 192.168.65.2:7003
   slots: (0 slots) slave
   replicates c666b7a75543419960f50fefaeab9f28796520cd
M: 4fbe96b7a785b5b6377631a980cb420ffba9ed23 192.168.65.2:7006
   slots:[0] (1 slots) master
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

Ready to move 10 slots.
  Source nodes:
    M: 7c32d265ffb53922ef7a0a795d61845c8bd20594 192.168.65.2:7000
       slots:[1-5460] (5460 slots) master
       1 additional replica(s)
  Destination node:
    M: 4fbe96b7a785b5b6377631a980cb420ffba9ed23 192.168.65.2:7006
       slots:[0] (1 slots) master
  Resharding plan:
    Moving slot 1 from 7c32d265ffb53922ef7a0a795d61845c8bd20594
    Moving slot 2 from 7c32d265ffb53922ef7a0a795d61845c8bd20594
    Moving slot 3 from 7c32d265ffb53922ef7a0a795d61845c8bd20594
    Moving slot 4 from 7c32d265ffb53922ef7a0a795d61845c8bd20594
    Moving slot 5 from 7c32d265ffb53922ef7a0a795d61845c8bd20594
    Moving slot 6 from 7c32d265ffb53922ef7a0a795d61845c8bd20594
    Moving slot 7 from 7c32d265ffb53922ef7a0a795d61845c8bd20594
    Moving slot 8 from 7c32d265ffb53922ef7a0a795d61845c8bd20594
    Moving slot 9 from 7c32d265ffb53922ef7a0a795d61845c8bd20594
    Moving slot 10 from 7c32d265ffb53922ef7a0a795d61845c8bd20594
Moving slot 1 from 192.168.65.2:7000 to 192.168.65.2:7006: 
Moving slot 2 from 192.168.65.2:7000 to 192.168.65.2:7006: 
Moving slot 3 from 192.168.65.2:7000 to 192.168.65.2:7006: 
Moving slot 4 from 192.168.65.2:7000 to 192.168.65.2:7006: 
Moving slot 5 from 192.168.65.2:7000 to 192.168.65.2:7006: 
Moving slot 6 from 192.168.65.2:7000 to 192.168.65.2:7006: 
Moving slot 7 from 192.168.65.2:7000 to 192.168.65.2:7006: 
Moving slot 8 from 192.168.65.2:7000 to 192.168.65.2:7006: 
Moving slot 9 from 192.168.65.2:7000 to 192.168.65.2:7006: 
Moving slot 10 from 192.168.65.2:7000 to 192.168.65.2:7006: 

检查状态
[redis@test2 redis02]$ redis-cli --cluster check 192.168.65.4:7002
192.168.65.4:7002 (c666b7a7...) -> 3 keys | 5461 slots | 1 slaves.
192.168.65.2:7006 (4fbe96b7...) -> 0 keys | 11 slots | 0 slaves.
192.168.65.3:7001 (dd7d7a57...) -> 1 keys | 5462 slots | 1 slaves.
192.168.65.2:7000 (7c32d265...) -> 1 keys | 5450 slots | 1 slaves.
[OK] 5 keys in 4 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.65.4:7002)
M: c666b7a75543419960f50fefaeab9f28796520cd 192.168.65.4:7002
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
M: 4fbe96b7a785b5b6377631a980cb420ffba9ed23 192.168.65.2:7006
   slots:[0-10] (11 slots) master
M: dd7d7a572c6faccc72b87e81e0bd73b08573f50b 192.168.65.3:7001
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: e0425d6872fc7891e409f4cd70203bb01aef122a 192.168.65.2:7003
   slots: (0 slots) slave
   replicates c666b7a75543419960f50fefaeab9f28796520cd
S: 2b70eaead74013f9b1277064e9f4131ca8a68899 192.168.65.3:7004
   slots: (0 slots) slave
   replicates 7c32d265ffb53922ef7a0a795d61845c8bd20594
S: eed011474b29cefcaf753418eb4efadd844339b7 192.168.65.4:7005
   slots: (0 slots) slave
   replicates dd7d7a572c6faccc72b87e81e0bd73b08573f50b
M: 7c32d265ffb53922ef7a0a795d61845c8bd20594 192.168.65.2:7000
   slots:[11-5460] (5450 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.

平衡(rebalance)slot 

1)平衡集群中各个节点的slot数量
[redis@test2 redis02]$ redis-cli --cluster rebalance 192.168.65.2:7000
>>> Performing Cluster Check (using node 192.168.65.2:7000)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Rebalancing across 4 nodes. Total weight = 4.00
Moving 1366 slots from 192.168.65.3:7001 to 192.168.65.2:7006
######################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################
Moving 1365 slots from 192.168.65.4:7002 to 192.168.65.2:7006
#####################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################
Moving 1354 slots from 192.168.65.2:7000 to 192.168.65.2:7006
##########################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################
[redis@test2 redis02]$ redis-cli --cluster check 192.168.65.4:7002
192.168.65.4:7002 (c666b7a7...) -> 2 keys | 4096 slots | 1 slaves.
192.168.65.2:7006 (4fbe96b7...) -> 1 keys | 4096 slots | 0 slaves.
192.168.65.3:7001 (dd7d7a57...) -> 1 keys | 4096 slots | 1 slaves.
192.168.65.2:7000 (7c32d265...) -> 1 keys | 4096 slots | 1 slaves.
[OK] 5 keys in 4 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.65.4:7002)
M: c666b7a75543419960f50fefaeab9f28796520cd 192.168.65.4:7002
   slots:[12288-16383] (4096 slots) master
   1 additional replica(s)
M: 4fbe96b7a785b5b6377631a980cb420ffba9ed23 192.168.65.2:7006
   slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master
M: dd7d7a572c6faccc72b87e81e0bd73b08573f50b 192.168.65.3:7001
   slots:[6827-10922] (4096 slots) master
   1 additional replica(s)
S: e0425d6872fc7891e409f4cd70203bb01aef122a 192.168.65.2:7003
   slots: (0 slots) slave
   replicates c666b7a75543419960f50fefaeab9f28796520cd
S: 2b70eaead74013f9b1277064e9f4131ca8a68899 192.168.65.3:7004
   slots: (0 slots) slave
   replicates 7c32d265ffb53922ef7a0a795d61845c8bd20594
S: eed011474b29cefcaf753418eb4efadd844339b7 192.168.65.4:7005
   slots: (0 slots) slave
   replicates dd7d7a572c6faccc72b87e81e0bd73b08573f50b
M: 7c32d265ffb53922ef7a0a795d61845c8bd20594 192.168.65.2:7000
   slots:[1365-5460] (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.

发现 65.2:7006 端口被分配 4096 个 slot,说明均衡起作用了



2)根据集群中各个节点设置的权重等平衡slot数量(不执行,只模拟)
[redis@test2 redis02]$ redis-cli --cluster rebalance --cluster-weight c666b7a75543419960f50fefaeab9f28796520cd=5 4fbe96b7a785b5b6377631a980cb420ffba9ed23=2 7c32d265ffb53922ef7a0a795d61845c8bd20594=2 dd7d7a572c6faccc72b87e81e0bd73b08573f50b=1 --cluster-simulate 192.168.65.2:7000
>>> Performing Cluster Check (using node 192.168.65.2:7000)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Rebalancing across 4 nodes. Total weight = 10.00
Moving 2458 slots from 192.168.65.3:7001 to 192.168.65.4:7002
##########################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################
Moving 820 slots from 192.168.65.2:7006 to 192.168.65.4:7002
####################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################
Moving 820 slots from 192.168.65.2:7000 to 192.168.65.4:7002
####################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################

导入集群

redis-cli --cluster import 192.168.65.2:7000 --cluster-from 192.168.65.2:7008 --cluster-replace
说明:外部Redis实例(9021)导入到集群中的任意一节点。
注意:测试下来发现参数--cluster-replace没有用,如果集群中已经包含了某个key,在导入的时候会失败,不会覆盖,只有清空集群key才能导入。

*** Importing 97847 keys from DB 0
Migrating 9223372011174675807 to 192.168.65.2:7008: Source 192.168.65.2:7000 replied with error:
ERR Target instance replied with error: BUSYKEY Target key name already exists
并且发现如果集群设置了密码,也会导入失败,需要设置集群密码为空才能进行导入(call)。通过monitor(9021)的时候发现,在migrate的时候需要密码进行auth认证。 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ty_FFTQ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值