1. 程式人生 > >Redis Cluster--運維管理

Redis Cluster--運維管理

tro line lease 是否 src 升級 通過 integer 集群

上一篇博客我們講了如何安裝配置Redis Cluster,詳情參考:Redis Cluster--安裝配置,今天我們來學習一下Redis Cluster的日常運維操作


Cluster常用命令

cluster info  集群信息
cluster nodes 所有節點和slot分布
cluster slots 所有節點和slot分布
cluster slaves <node_id> 返回一個master節點的slaves 列表

cluster meet <ip> <port> 添加指定的節點到集群,默認成為maser,相當於redis-trib.rb add-node
cluster forget <node-id> 刪除指定的節點,相當於redis-trib.rb del-node
cluster replicate <node-id>  將當前節點設置為指定node-id的slave;
cluster saveconfig 將節點信息保存在nodes-6379.conf文化中;
cluster addslots <slot> [slot ...] 將一個或多個槽(slot)指派(assign)給當前節點。
cluster delslots  <slot> [slot ...] 移除一個或多個槽對當前節點點。
cluster flushslots 移除指派給當前節點的所有槽,讓當前節點變成一個沒有指派任何槽的節點。

cluster setslot <slot> node <node_id> 將槽 slot 指派給 node_id 指定的節點。
cluster setslot <slot> migrating <node_id> 將本節點的槽 slot 遷移到 node_id 指定的節點中。
cluster setslot <slot> importing <node_id> 從 node_id 指定的節點中導入槽 slot 到本節點。
cluster setslot <slot> stable  取消對槽 slot 的導入(import)或者遷移(migrate)。

cluster keyslot <key> 獲得key對應的槽
cluster countkeysinslot<slot> 返回slot目前包含的key數量。
cluster getkeysinslot <slot> <count> 返回 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 <ip> <port>命令添加節點

[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


  1. 參考鏈接

https://redis.io/topics/cluster-tutorial


Redis Cluster--運維管理