重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
上一篇博客我们讲了如何安装配置redis Cluster,详情参考:Redis Cluster--安装配置,今天我们来学习一下Redis Cluster的日常运维操作
十多年的昭阳网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。成都全网营销推广的优势是能够根据用户设备显示端的尺寸不同,自动调整昭阳建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。成都创新互联从事“昭阳网站设计”,“昭阳网站推广”以来,每个客户项目都认真落实执行。
Cluster常用命令
cluster info 集群信息 cluster nodes 所有节点和slot分布 cluster slots 所有节点和slot分布 cluster slaves返回一个master节点的slaves 列表 cluster meet 添加指定的节点到集群,默认成为maser,相当于redis-trib.rb add-node cluster forget 删除指定的节点,相当于redis-trib.rb del-node cluster replicate 将当前节点设置为指定node-id的slave; cluster saveconfig 将节点信息保存在nodes-6379.conf文化中; cluster addslots [slot ...] 将一个或多个槽(slot)指派(assign)给当前节点。 cluster delslots [slot ...] 移除一个或多个槽对当前节点点。 cluster flushslots 移除指派给当前节点的所有槽,让当前节点变成一个没有指派任何槽的节点。 cluster setslot node 将槽 slot 指派给 node_id 指定的节点。 cluster setslot migrating 将本节点的槽 slot 迁移到 node_id 指定的节点中。 cluster setslot importing 从 node_id 指定的节点中导入槽 slot 到本节点。 cluster setslot stable 取消对槽 slot 的导入(import)或者迁移(migrate)。 cluster keyslot 获得key对应的槽 cluster countkeysinslot 返回slot目前包含的key数量。 cluster getkeysinslot 返回 count个slot 槽中的键。
例:
172.16.101.54:6379> cluster nodes 4fe26b83847e8d995230b8e7e5b3ca6a5fe99c19 172.16.101.66:6380 slave 58a08dff7169bb0ce289c19a6520c85131bb44da 0 1532789488602 9 connected b50ba506525ee547823b3d9a7e5a095933e3ff42 172.16.101.59:6379 slave 4c7348ac575a1a3bd4357e937a8f94f1654193cf 0 1532789484595 5 connected 610aa83831404be545b25cc7f7322e987da1dd33 172.16.101.54:6379 myself,master - 0 0 1 connected 866-5460 58a08dff7169bb0ce289c19a6520c85131bb44da 172.16.101.66:6379 master - 0 1532789489605 9 connected 0-865 5461-6826 10923-12181 28e8d5c8fd12533622d8110f8d262cb50120ca02 172.16.101.58:6379 slave 610aa83831404be545b25cc7f7322e987da1dd33 0 1532789484095 4 connected 4c7348ac575a1a3bd4357e937a8f94f1654193cf 172.16.101.55:6379 master - 0 1532789486598 2 connected 6827-10922 ece3a80f6989fa7f8316d73ee30ceea84340f24e 172.16.101.56:6379 master - 0 1532789487599 8 connected 12182-16383 c79de1092011ed395ee772969274c0567c2b5dd1 172.16.101.60:6379 slave ece3a80f6989fa7f8316d73ee30ceea84340f24e 0 1532789485597 8 connected
新建三个key,根据crc16算法分配到各个slot上
172.16.101.54:6379> set key1 value1 -> Redirected to slot [9189] located at 172.16.101.55:6379 OK 172.16.101.55:6379> set key2 value2 OK 172.16.101.54:6379> set key3 value3 OK
获得key的value
172.16.101.56:6379> get key1 -> Redirected to slot [9189] located at 172.16.101.55:6379 "value1" 172.16.101.55:6379> get key2 -> Redirected to slot [4998] located at 172.16.101.54:6379 "value2" 172.16.101.56:6379> get key3 -> Redirected to slot [935] located at 172.16.101.54:6379 "value3"
通过keyslot命令根据key名称获得key对应的slot号
172.16.101.54:6379> cluster keyslot key1 (integer) 9189 172.16.101.54:6379> cluster keyslot key2 (integer) 4998 172.16.101.54:6379> cluster keyslot key3 (integer) 935
返回槽 slot 上对应的key的数量
172.16.101.54:6379> cluster countkeysinslot 9189 (integer) 1 172.16.101.54:6379> cluster countkeysinslot 4998 (integer) 1 172.16.101.54:6379> cluster countkeysinslot 935 (integer) 1
根据slot号9189,返回其中count100个键
172.16.101.55:6379> cluster getkeysinslot 9189 100 1) "key1" 172.16.101.54:6379> cluster getkeysinslot 4998 100 1) "key2" 172.16.101.54:6379> cluster getkeysinslot 935 100 1) "key3"
写入key-values数据测试
可以发现数据是分片存放在这三个节点上的,虽然使用命令keys * 无法显示不存在本节点的key,但是我们仍可以使用get命令获得key内容,并直接切换到存放该key的数据节点上
[root@sht-sgmhadoopcm-01 redis]# src/redis-cli -c -h 172.16.101.54 -p 6379 172.16.101.54:6379> set foo bar -> Redirected to slot [12182] located at 172.16.101.56:6379 OK 172.16.101.56:6379> exit [root@sht-sgmhadoopcm-01 redis]# src/redis-cli -c -h 172.16.101.54 -p 6379 172.16.101.54:6379> keys * (empty list or set) 172.16.101.54:6379> get foo -> Redirected to slot [12182] located at 172.16.101.56:6379 "bar" 172.16.101.56:6379> keys * 1) "foo" [root@sht-sgmhadoopnn-02 redis]# src/redis-cli -c -h 172.16.101.56 -p 6379 172.16.101.56:6379> set hello world -> Redirected to slot [866] located at 172.16.101.54:6379 OK 172.16.101.54:6379> keys * 1) "hello"
宕机恢复测试
模拟一个master故障,此时这个master的slave会提成为新的master,当老的master修复好之后,会作为新的master的slave
[root@sht-sgmhadoopnn-02 redis]# src/redis-cli -c -h 172.16.101.56 -p 6379 debug segfault Error: Server closed the connection [root@sht-sgmhadoopnn-02 redis]# ps -ef|grep redis root 1764 1504 0 13:48 pts/2 00:00:00 grep --color=auto redis 172.16.101.54:6379> cluster nodes b50ba506525ee547823b3d9a7e5a095933e3ff42 172.16.101.59:6379 slave 4c7348ac575a1a3bd4357e937a8f94f1654193cf 0 1532497434709 5 connected 610aa83831404be545b25cc7f7322e987da1dd33 172.16.101.54:6379 myself,master - 0 0 1 connected 0-5460 28e8d5c8fd12533622d8110f8d262cb50120ca02 172.16.101.58:6379 slave 610aa83831404be545b25cc7f7322e987da1dd33 0 1532497438730 4 connected 4c7348ac575a1a3bd4357e937a8f94f1654193cf 172.16.101.55:6379 master - 0 1532497439732 2 connected 5461-10922 ece3a80f6989fa7f8316d73ee30ceea84340f24e 172.16.101.56:6379 master,fail - 1532497356109 1532497354406 3 disconnected c79de1092011ed395ee772969274c0567c2b5dd1 172.16.101.60:6379 master - 0 1532497437724 7 connected 10923-16383
重新启动故障的节点,此节点将作为新的master的slave
[root@sht-sgmhadoopnn-02 redis]# src/redis-server redis.conf [root@sht-sgmhadoopnn-02 redis]# src/redis-cli -c -h 172.16.101.56 -p 6379 172.16.101.56:6379> CLUSTER NODES 28e8d5c8fd12533622d8110f8d262cb50120ca02 172.16.101.58:6379 slave 610aa83831404be545b25cc7f7322e987da1dd33 0 1532497754924 4 connected c79de1092011ed395ee772969274c0567c2b5dd1 172.16.101.60:6379 master - 0 1532497757930 7 connected 10923-16383 ece3a80f6989fa7f8316d73ee30ceea84340f24e 172.16.101.56:6379 myself,slave c79de1092011ed395ee772969274c0567c2b5dd1 0 0 3 connected b50ba506525ee547823b3d9a7e5a095933e3ff42 172.16.101.59:6379 slave 4c7348ac575a1a3bd4357e937a8f94f1654193cf 0 1532497751918 5 connected 4c7348ac575a1a3bd4357e937a8f94f1654193cf 172.16.101.55:6379 master - 0 1532497756929 2 connected 5461-10922 610aa83831404be545b25cc7f7322e987da1dd33 172.16.101.54:6379 master - 0 1532497755927 1 connected 0-5460
手动Failover
在Slave上使用cluster failover,把Slave变成Master,这种手工故障转移没有任何数据丢失,用来升级redis是一种比较的安全方式
172.16.101.56:6379> cluster nodes 28e8d5c8fd12533622d8110f8d262cb50120ca02 172.16.101.58:6379 slave 610aa83831404be545b25cc7f7322e987da1dd33 0 1532674427835 4 connected c79de1092011ed395ee772969274c0567c2b5dd1 172.16.101.60:6379 master - 0 1532674425830 7 connected 10923-16383 ece3a80f6989fa7f8316d73ee30ceea84340f24e 172.16.101.56:6379 myself,slave c79de1092011ed395ee772969274c0567c2b5dd1 0 0 3 connected b50ba506525ee547823b3d9a7e5a095933e3ff42 172.16.101.59:6379 slave 4c7348ac575a1a3bd4357e937a8f94f1654193cf 0 1532674428837 5 connected 4c7348ac575a1a3bd4357e937a8f94f1654193cf 172.16.101.55:6379 master - 0 1532674429839 2 connected 5461-10922 610aa83831404be545b25cc7f7322e987da1dd33 172.16.101.54:6379 master - 0 1532674426832 1 connected 0-5460
172.16.101.56:6379> role 1) "slave" 2) "172.16.101.60" 3) (integer) 6379 4) "connected" 5) (integer) 246737 172.16.101.56:6379> 172.16.101.56:6379> 172.16.101.56:6379> CLUSTER FAILOVER OK 172.16.101.56:6379> role 1) "master" 2) (integer) 246808 3) 1) 1) "172.16.101.60" 2) "6379" 3) "246808"
通过日志查看failover过程:
[root@sht-sgmhadoopnn-02 redis]# vim redis.log 1766:S 27 Jul 14:54:56.863 # Manual failover user request accepted. 1766:S 27 Jul 14:54:56.912 # Received replication offset for paused master manual failover: 246807 1766:S 27 Jul 14:54:56.991 # All master replication stream processed, manual failover can start. 1766:S 27 Jul 14:54:56.991 # Start of election delayed for 0 milliseconds (rank #0, offset 246807). 1766:S 27 Jul 14:54:57.091 # Starting a failover election for epoch 8. 1766:S 27 Jul 14:54:57.122 # Failover election won: I'm the new master.
添加一个新的Master节点并分配slot
准备一个新的redis环境,版本保持一致,使用src/redis-trib.rb add-node命令添加一个新的节点,不能有数据,否则添加的时候会报错,节点添加成功后会自动成为master节点,没有分配任何的slot,但是我们可以通过redis-trib.rb reshard/rebalance重新分配slots,让数据分配更加合理
[root@sht-sgmhadoopcm-01 redis]# rsync -avz --progress /usr/local/redis sht-sgmhadoopdn-04:/usr/local/ [root@sht-sgmhadoopdn-04 redis]# src/redis-server redis.conf
(1) 172.16.101.66:6379是新节点的地址和端口,172.16.101.54:6379是已经存在的节点的IP和端口.
也可以使用cluster meet
[root@sht-sgmhadoopcm-01 redis]# src/redis-trib.rb add-node 172.16.101.66:6379 172.16.101.54:6379 >>> Adding node 172.16.101.66:6379 to cluster 172.16.101.54:6379 >>> Performing Cluster Check (using node 172.16.101.54:6379) M: 610aa83831404be545b25cc7f7322e987da1dd33 172.16.101.54:6379 slots:0-5460 (5461 slots) master 1 additional replica(s) S: b50ba506525ee547823b3d9a7e5a095933e3ff42 172.16.101.59:6379 slots: (0 slots) slave replicates 4c7348ac575a1a3bd4357e937a8f94f1654193cf S: 28e8d5c8fd12533622d8110f8d262cb50120ca02 172.16.101.58:6379 slots: (0 slots) slave replicates 610aa83831404be545b25cc7f7322e987da1dd33 M: 4c7348ac575a1a3bd4357e937a8f94f1654193cf 172.16.101.55:6379 slots:5461-10922 (5462 slots) master 1 additional replica(s) M: ece3a80f6989fa7f8316d73ee30ceea84340f24e 172.16.101.56:6379 slots:10923-16383 (5461 slots) master 1 additional replica(s) S: c79de1092011ed395ee772969274c0567c2b5dd1 172.16.101.60:6379 slots: (0 slots) slave replicates ece3a80f6989fa7f8316d73ee30ceea84340f24e [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.101.66:6379 to make it join the cluster. [OK] New node added correctly.
(2)重新分配slot到新的Master
[root@sht-sgmhadoopcm-01 redis]# src/redis-trib.rb info 172.16.101.54:6379 172.16.101.54:6379 (610aa838...) -> 3 keys | 8086 slots | 1 slaves. 172.16.101.66:6379 (58a08dff...) -> 0 keys | 0 slots | 0 slaves. 172.16.101.55:6379 (4c7348ac...) -> 1 keys | 4096 slots | 1 slaves. 172.16.101.56:6379 (ece3a80f...) -> 1 keys | 4202 slots | 1 slaves. [OK] 5 keys in 4 masters. 0.00 keys per slot on average.
[root@sht-sgmhadoopcm-01 redis]# src/redis-trib.rb reshard 172.16.101.54:6379 >>> Performing Cluster Check (using node 172.16.101.54:6379) M: 610aa83831404be545b25cc7f7322e987da1dd33 172.16.101.54:6379 slots:0-6826,10923-12181 (8086 slots) master 1 additional replica(s) S: b50ba506525ee547823b3d9a7e5a095933e3ff42 172.16.101.59:6379 slots: (0 slots) slave replicates 4c7348ac575a1a3bd4357e937a8f94f1654193cf M: 58a08dff7169bb0ce289c19a6520c85131bb44da 172.16.101.66:6379 slots: (0 slots) master 0 additional replica(s) S: 28e8d5c8fd12533622d8110f8d262cb50120ca02 172.16.101.58:6379 slots: (0 slots) slave replicates 610aa83831404be545b25cc7f7322e987da1dd33 M: 4c7348ac575a1a3bd4357e937a8f94f1654193cf 172.16.101.55:6379 slots:6827-10922 (4096 slots) master 1 additional replica(s) M: ece3a80f6989fa7f8316d73ee30ceea84340f24e 172.16.101.56:6379 slots:12182-16383 (4202 slots) master 1 additional replica(s) S: c79de1092011ed395ee772969274c0567c2b5dd1 172.16.101.60:6379 slots: (0 slots) slave replicates ece3a80f6989fa7f8316d73ee30ceea84340f24e [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)?866 What is the receiving node ID? 58a08dff7169bb0ce289c19a6520c85131bb44da 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 ............ Moving slot 855 from 4c7348ac575a1a3bd4357e937a8f94f1654193cf Moving slot 856 from 4c7348ac575a1a3bd4357e937a8f94f1654193cf Do you want to proceed with the proposed reshard plan (yes/no)?yes ............ Moving slot 854 from 172.16.101.54:6379 to 172.16.101.66:6379: Moving slot 855 from 172.16.101.54:6379 to 172.16.101.66:6379: Moving slot 856 from 172.16.101.54:6379 to 172.16.101.66:6379:
添加一个新的Slave节点
指定master-id创建一个Slave,第一个是新节点的IP和端口,第二个是任意一个已经存在的节点的IP和端口.
[root@sht-sgmhadoopcm-01 redis]# src/redis-trib.rb add-node --slave --master-id 3bfe06617ab8f682a6b55be38a529d9c7e1b50d0 172.16.101.66:6380 172.16.101.54:6379 >>> Adding node 172.16.101.66:6380 to cluster 172.16.101.54:6379 >>> Performing Cluster Check (using node 172.16.101.54:6379) M: 610aa83831404be545b25cc7f7322e987da1dd33 172.16.101.54:6379 slots:0-5460 (5461 slots) master 1 additional replica(s) S: b50ba506525ee547823b3d9a7e5a095933e3ff42 172.16.101.59:6379 slots: (0 slots) slave replicates 4c7348ac575a1a3bd4357e937a8f94f1654193cf M: 3bfe06617ab8f682a6b55be38a529d9c7e1b50d0 172.16.101.66:6379 slots: (0 slots) master 0 additional replica(s) S: 28e8d5c8fd12533622d8110f8d262cb50120ca02 172.16.101.58:6379 slots: (0 slots) slave replicates 610aa83831404be545b25cc7f7322e987da1dd33 M: 4c7348ac575a1a3bd4357e937a8f94f1654193cf 172.16.101.55:6379 slots:5461-10922 (5462 slots) master 1 additional replica(s) M: ece3a80f6989fa7f8316d73ee30ceea84340f24e 172.16.101.56:6379 slots:10923-16383 (5461 slots) master 1 additional replica(s) S: c79de1092011ed395ee772969274c0567c2b5dd1 172.16.101.60:6379 slots: (0 slots) slave replicates ece3a80f6989fa7f8316d73ee30ceea84340f24e [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.101.66:6380 to make it join the cluster. Waiting for the cluster to join. >>> Configure node as replica of 172.16.101.66:6379. [OK] New node added correctly.
移除一个节点
想要删除一个节点,这个节点不能有slot,如果含有slot是无法删除的,但是可以移除一个slave,如果你想移除一个含有slot的master,需要把该节点的所有slot移动到其他master上,然后删除;
[root@sht-sgmhadoopcm-01 redis]# src/redis-trib.rb del-node 172.16.101.54:6379 '58a08dff7169bb0ce289c19a6520c85131bb44da' >>> Removing node 58a08dff7169bb0ce289c19a6520c85131bb44da from cluster 172.16.101.54:6379 [ERR] Node 172.16.101.66:6379 is not empty! Reshard data away and try again.
(1)移除一个salve节点
第一个任意一个已经存在的节点的IP和端口,第二个节点是你想要移除的节点地址node-id
[root@sht-sgmhadoopcm-01 redis]# src/redis-trib.rb del-node 172.16.101.54:6379 '47a9c6cd1dd9aa9edd99890d380bcaee3a807a71' >>> Removing node 47a9c6cd1dd9aa9edd99890d380bcaee3a807a71 from cluster 172.16.101.54:6379 >>> Sending CLUSTER FORGET messages to the cluster... >>> SHUTDOWN the node.
(2)移除一个有slot的master节点
需要把该节点的所有slot移动到其他master上,然后删除;
[root@sht-sgmhadoopcm-01 redis]# src/redis-trib.rb info 172.16.101.54:6379 172.16.101.54:6379 (610aa838...) -> 3 keys | 7220 slots | 1 slaves. 172.16.101.66:6379 (58a08dff...) -> 0 keys | 866 slots | 0 slaves. 172.16.101.55:6379 (4c7348ac...) -> 1 keys | 4096 slots | 1 slaves. 172.16.101.56:6379 (ece3a80f...) -> 1 keys | 4202 slots | 1 slaves. [OK] 5 keys in 4 masters. 0.00 keys per slot on average. [root@sht-sgmhadoopcm-01 redis]# src/redis-trib.rb reshard 172.16.101.54:6379 >>> Performing Cluster Check (using node 172.16.101.54:6379) M: 610aa83831404be545b25cc7f7322e987da1dd33 172.16.101.54:6379 slots:866-6826,10923-12181 (7220 slots) master 1 additional replica(s) S: b50ba506525ee547823b3d9a7e5a095933e3ff42 172.16.101.59:6379 slots: (0 slots) slave replicates 4c7348ac575a1a3bd4357e937a8f94f1654193cf M: 58a08dff7169bb0ce289c19a6520c85131bb44da 172.16.101.66:6379 slots:0-865 (866 slots) master 0 additional replica(s) S: 28e8d5c8fd12533622d8110f8d262cb50120ca02 172.16.101.58:6379 slots: (0 slots) slave replicates 610aa83831404be545b25cc7f7322e987da1dd33 M: 4c7348ac575a1a3bd4357e937a8f94f1654193cf 172.16.101.55:6379 slots:6827-10922 (4096 slots) master 1 additional replica(s) M: ece3a80f6989fa7f8316d73ee30ceea84340f24e 172.16.101.56:6379 slots:12182-16383 (4202 slots) master 1 additional replica(s) S: c79de1092011ed395ee772969274c0567c2b5dd1 172.16.101.60:6379 slots: (0 slots) slave replicates ece3a80f6989fa7f8316d73ee30ceea84340f24e [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)? 866 What is the receiving node ID?610aa83831404be545b25cc7f7322e987da1dd33 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:58a08dff7169bb0ce289c19a6520c85131bb44da Source node #2:done ...... Moving slot 864 from 58a08dff7169bb0ce289c19a6520c85131bb44da Moving slot 865 from 58a08dff7169bb0ce289c19a6520c85131bb44da Do you want to proceed with the proposed reshard plan (yes/no)? Moving slot 0 from 172.16.101.66:6379 to 172.16.101.54:6379: Moving slot 1 from 172.16.101.66:6379 to 172.16.101.54:6379: Moving slot 2 from 172.16.101.66:6379 to 172.16.101.54:6379: ...... [root@sht-sgmhadoopcm-01 redis]# src/redis-trib.rb info 172.16.101.54:6379 172.16.101.54:6379 (610aa838...) -> 3 keys | 8086 slots | 1 slaves. 172.16.101.66:6379 (58a08dff...) -> 0 keys | 0 slots | 0 slaves. 172.16.101.55:6379 (4c7348ac...) -> 1 keys | 4096 slots | 1 slaves. 172.16.101.56:6379 (ece3a80f...) -> 1 keys | 4202 slots | 1 slaves. [OK] 5 keys in 4 masters. 0.00 keys per slot on average. [root@sht-sgmhadoopcm-01 redis]# src/redis-trib.rb del-node 172.16.101.54:6379 '58a08dff7169bb0ce289c19a6520c85131bb44da'
升级Redis节点
如果是Slave,你可以直接停掉,用一个高的版本直接替代;
如果是Master,可以通过cluster failover把主切换成从,然后再升级,最后再手动执行cluster failover;
迁移slot数据
(1) 创建三个属于一个槽的键,将这些键迁移到新的节点中,如果key名称中含有{ },那么计算hash值时,只计算{ }中包括的字符串,所以以下三个key属于同一个slot;
172.16.101.54:6379> SET key:{test}:555 value:test:555 -> Redirected to slot [6918] located at 172.16.101.66:6379 OK 172.16.101.66:6379> SET key:{test}:666 value:test:666 OK 172.16.101.66:6379> SET key:{test}:777 value:test:777 OK 172.16.101.54:6379> CLUSTER KEYSLOT key:{test}:555 (integer) 6918 172.16.101.54:6379> CLUSTER KEYSLOT key:{test}:666 (integer) 6918 172.16.101.54:6379> CLUSTER KEYSLOT key:{test}:777 (integer) 6918 172.16.101.55:6379> cluster getkeysinslot 6918 100 1) "key:{test}:555" 2) "key:{test}:666" 3) "key:{test}:777"
(2) 目的是要把IP172.16.101.55上面的slot6918移动到IP172.16.101.66节点上
172.16.101.56:6379> cluster nodes 1e905d0573c5c71b6b8e3cf2af92584da78d1be2 172.16.101.59:6379 slave eac2a7e96f8c4f03b8b2e44ec79b274153417951 0 1533464714919 5 connected 2fb7f7439771771f37f1e52add0bbaea5b196786 172.16.101.58:6379 slave 6f529643c9fb12075014ae9c9aced50f23bb3127 0 1533464715920 4 connected 6f529643c9fb12075014ae9c9aced50f23bb3127 172.16.101.54:6379 master - 0 1533464711913 1 connected 0-5460 eac2a7e96f8c4f03b8b2e44ec79b274153417951 172.16.101.55:6379 master - 0 1533464717926 2 connected 5461-10922 243b0c9ab7532427536bb5662db7c57030250798 172.16.101.60:6379 slave 64d6aac9b175066fbf8138dc99662778dda4084b 0 1533464716924 6 connected 64d6aac9b175066fbf8138dc99662778dda4084b 172.16.101.56:6379 myself,master - 0 0 3 connected 10923-16383
在目标节点172.16.101.56上导入slot 6918
172.16.101.56:6379> cluster setslot 6918 importing eac2a7e96f8c4f03b8b2e44ec79b274153417951 172.16.101.56:6379> cluster nodes 64d6aac9b175066fbf8138dc99662778dda4084b 172.16.101.56:6379 myself,master - 0 0 3 connected 10923-16383 [6918-<-eac2a7e96f8c4f03b8b2e44ec79b274153417951]
在源节点172.16.101.55迁移slot 6918
172.16.101.55:6379> cluster setslot 6918 migrating 64d6aac9b175066fbf8138dc99662778dda4084b 172.16.101.55:6379> cluster nodes eac2a7e96f8c4f03b8b2e44ec79b274153417951 172.16.101.55:6379 myself,master - 0 0 2 connected 5461-10922 [6918->-64d6aac9b175066fbf8138dc99662778dda4084b] 172.16.101.55:6379> migrate 172.16.101.56 6379 "" 0 1000 keys key:{test}:777 key:{test}:666 key:{test}:555 172.16.101.55:6379> MGET key:{test}:777 key:{test}:666 key:{test}:555 (error) ASK 6918 172.16.101.56:6379
在所有master上执行迁移命令
172.16.101.54:6379> cluster setslot 6918 node 64d6aac9b175066fbf8138dc99662778dda4084b 172.16.101.55:6379> cluster setslot 6918 node 64d6aac9b175066fbf8138dc99662778dda4084b 172.16.101.56:6379> cluster setslot 6918 node 64d6aac9b175066fbf8138dc99662778dda4084b 172.16.101.54:6379> cluster nodes eac2a7e96f8c4f03b8b2e44ec79b274153417951 172.16.101.55:6379 master - 0 1533466116633 2 connected 5461-6917 6919-10922 64d6aac9b175066fbf8138dc99662778dda4084b 172.16.101.56:6379 master - 0 1533466114629 3 connected 6918 10923-16383 1e905d0573c5c71b6b8e3cf2af92584da78d1be2 172.16.101.59:6379 slave eac2a7e96f8c4f03b8b2e44ec79b274153417951 0 1533466115630 5 connected 6f529643c9fb12075014ae9c9aced50f23bb3127 172.16.101.54:6379 myself,master - 0 0 1 connected 0-5460 2fb7f7439771771f37f1e52add0bbaea5b196786 172.16.101.58:6379 slave 6f529643c9fb12075014ae9c9aced50f23bb3127 0 1533466113626 4 connected 243b0c9ab7532427536bb5662db7c57030250798 172.16.101.60:6379 slave 64d6aac9b175066fbf8138dc99662778dda4084b 0 1533466112623 6 connected
最后可以通过check命令查看迁移是否成功
[root@sht-sgmhadoopcm-01 redis]# src/redis-trib.rb check 172.16.101.54:6379
FAQ
Error1:
[root@sht-sgmhadoopcm-01 redis]# src/redis-trib.rb check 172.16.101.54:6379
......
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
[WARNING] Node 172.16.101.54:6379 has slots in migrating state (866).
[WARNING] Node 172.16.101.66:6379 has slots in importing state (866).
[WARNING] The following slots are open: 866
>>> Check slots coverage...
[OK] All 16384 slots covered.
解决方法:
登录到对应的两个redis服务上执行:
172.16.101.54:6379> cluster setslot 866 stable
OK
172.16.101.66:6379> CLUSTER SETSLOT 866 stable
OK
参考链接
https://redis.io/topics/cluster-tutorial