1. 程式人生 > >redis--節點新增,切換,資料遷移

redis--節點新增,切換,資料遷移

新增redis節點, redis版本為redis-3.0.7   ruby-2.2.2

新增主節點到叢集命令: 

[xxxxxx src]# ./redis-trib.rb  add-node 192.168.76.116:6380 192.168.76.116:6379
>>> Adding node 192.168.76.116:6380 to cluster 192.168.76.116:6379
>>> Performing Cluster Check (using node 192.168.76.116:6379)
M: 0936588a1ea864fc020e40be4f22b557cf289471 192.168.76.116:6379
   slots: (0 slots) master
   0 additional replica(s)
M: ac0bed66e38ecb64e1fcf35363e773aab0a921ab 192.168.76.116:6382
   slots:0-16383 (16384 slots) master
   0 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 192.168.76.116:6380 to make it join the cluster.
[OK] New node added correctly.
You have new mail in /var/spool/mail/root

命令解析: /redis-trib.rb  add-node 192.168.76.116:6380 192.168.76.116:6379  其中192.168.76.116:6380  需要新增的主節點, 為叢集中已有的節點

新增從節點到叢集: 

[xxxxxx src]# ./redis-trib.rb add-node --slave --master-id ac0bed66e38ecb64e1fcf35363e773aab0a921ab 192.168.76.116:6381 192.168.76.116:6382 
>>> Adding node 192.168.76.116:6381 to cluster 192.168.76.116:6382
>>> Performing Cluster Check (using node 192.168.76.116:6382)
M: ac0bed66e38ecb64e1fcf35363e773aab0a921ab 192.168.76.116:6382
   slots:0-16383 (16384 slots) master
   1 additional replica(s)
S: c6a480d33b94eb5396ec0d936542490171d49666 192.168.76.116:6383
   slots: (0 slots) slave
   replicates ac0bed66e38ecb64e1fcf35363e773aab0a921ab
S: 0fcf9d2d4f331a33dd319e8ee40d25595c38325a 192.168.76.116:6380
   slots: (0 slots) slave
   replicates 0936588a1ea864fc020e40be4f22b557cf289471
M: 0936588a1ea864fc020e40be4f22b557cf289471 192.168.76.116:6379
   slots: (0 slots) master
   1 additional replica(s)
M: 4c075686000e3050b38429cf95717253b2623dc9 192.168.76.116:6384
   slots: (0 slots) master
   0 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 192.168.76.116:6381 to make it join the cluster.
Waiting for the cluster to join.
>>> Configure node as replica of 192.168.76.116:6382.
[OK] New node added correctly.
You have new mail in /var/spool/mail/root
[

[email protected] src]# 
[[email protected] src]# 

命令解析: /redis-trib.rb add-node --slave --master-id ac0bed66e38ecb64e1fcf35363e773aab0a921ab 192.168.76.116:6381 192.168.76.116:6382 

其中 add-node --slave 表示新增的為從節點。  --master-id ac0bed66e38ecb64e1fcf35363e773aab0a921ab 指定主節點。  192.168.76.116:6381需要新增的從節點。

192.168.76.116:6382  叢集中存在的任意節點

改變從節點的master 

[xxxxxx bin]# 
[[email protected] bin]# ./redis-cli  -c -p 6381
127.0.0.1:6381> cluster nodes
0fcf9d2d4f331a33dd319e8ee40d25595c38325a 192.168.76.116:6380 slave 0936588a1ea864fc020e40be4f22b557cf289471 0 1505381033845 1 connected
c6a480d33b94eb5396ec0d936542490171d49666 192.168.76.116:6383 slave ac0bed66e38ecb64e1fcf35363e773aab0a921ab 0 1505381028844 4 connected
95e9f52c3c0f3aad3b58191ffedb8ea6adf1db61 192.168.76.116:6381 myself,slave ac0bed66e38ecb64e1fcf35363e773aab0a921ab 0 0 0 connected
0936588a1ea864fc020e40be4f22b557cf289471 192.168.76.116:6379 master - 0 1505381030844 1 connected
ac0bed66e38ecb64e1fcf35363e773aab0a921ab 192.168.76.116:6382 master - 0 1505381032846 4 connected 0-16383
4c075686000e3050b38429cf95717253b2623dc9 192.168.76.116:6384 master - 0 1505381031844 0 connected
127.0.0.1:6381> 
127.0.0.1:6381> cluster replicate 4c075686000e3050b38429cf95717253b2623dc9
OK
127.0.0.1:6381> quit
You have new mail in /var/spool/mail/root
[[email protected] bin]# ./redis-cli  -c -p 6381
127.0.0.1:6381> cluster nodes
0fcf9d2d4f331a33dd319e8ee40d25595c38325a 192.168.76.116:6380 slave 0936588a1ea864fc020e40be4f22b557cf289471 0 1505381096899 1 connected
c6a480d33b94eb5396ec0d936542490171d49666 192.168.76.116:6383 slave ac0bed66e38ecb64e1fcf35363e773aab0a921ab 0 1505381094899 4 connected
95e9f52c3c0f3aad3b58191ffedb8ea6adf1db61 192.168.76.116:6381 myself,slave 4c075686000e3050b38429cf95717253b2623dc9 0 0 0 connected
0936588a1ea864fc020e40be4f22b557cf289471 192.168.76.116:6379 master - 0 1505381095899 1 connected
ac0bed66e38ecb64e1fcf35363e773aab0a921ab 192.168.76.116:6382 master - 0 1505381091899 4 connected 0-16383
4c075686000e3050b38429cf95717253b2623dc9 192.168.76.116:6384 master - 0 1505381093899 0 connected
127.0.0.1:6381> 

如上面展示

95e9f52c3c0f3aad3b58191ffedb8ea6adf1db61 192.168.76.116:6381 myself,slave ac0bed66e38ecb64e1fcf35363e773aab0a921ab 0 0 0 connected 切換之前是

192.168.76.116:6382 的從,切換之後是192.168.76.116:6384  的從。 

命令解析: 127.0.0.1:6381> cluster replicate 4c075686000e3050b38429cf95717253b2623dc9

其中 4c075686000e3050b38429cf95717253b2623dc9 為需要的主節點node id 

資料遷移,將192.168.76.116:6382 節點slot遷移10個槽位到192.168.76.116:6379

[xxxxxx src]# ./redis-trib.rb reshard 192.168.76.116:6379     ----最開始執行的命令 
>>> Performing Cluster Check (using node 192.168.76.116:6379)
M: 0936588a1ea864fc020e40be4f22b557cf289471 192.168.76.116:6379
   slots: (0 slots) master
   1 additional replica(s)
M: ac0bed66e38ecb64e1fcf35363e773aab0a921ab 192.168.76.116:6382
   slots:0-16383 (16384 slots) master
   1 additional replica(s)
S: c6a480d33b94eb5396ec0d936542490171d49666 192.168.76.116:6383
   slots: (0 slots) slave
   replicates ac0bed66e38ecb64e1fcf35363e773aab0a921ab
S: 0fcf9d2d4f331a33dd319e8ee40d25595c38325a 192.168.76.116:6380
   slots: (0 slots) slave
   replicates 0936588a1ea864fc020e40be4f22b557cf289471
S: 95e9f52c3c0f3aad3b58191ffedb8ea6adf1db61 192.168.76.116:6381
   slots: (0 slots) slave
   replicates 4c075686000e3050b38429cf95717253b2623dc9
M: 4c075686000e3050b38429cf95717253b2623dc9 192.168.76.116:6384
   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.
How many slots do you want to move (from 1 to 16384)? 10                        ---這個地方填寫需要遷移的槽位數
What is the receiving node ID? 0936588a1ea864fc020e40be4f22b557cf289471    ----這個地方填寫接受槽位的nodeid 
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:ac0bed66e38ecb64e1fcf35363e773aab0a921ab                    ---這個地方填寫需要分出槽位的nodeid 
Source node #2:done                                                 ---這個地方選擇done 代表著只從填寫的Source node 獲取slot 
Ready to move 10 slots.
  Source nodes:
    M: ac0bed66e38ecb64e1fcf35363e773aab0a921ab 192.168.76.116:6382
   slots:0-16383 (16384 slots) master
   1 additional replica(s)
  Destination node:
    M: 0936588a1ea864fc020e40be4f22b557cf289471 192.168.76.116:6379
   slots: (0 slots) master
   1 additional replica(s)
  Resharding plan:
    Moving slot 0 from ac0bed66e38ecb64e1fcf35363e773aab0a921ab
    Moving slot 1 from ac0bed66e38ecb64e1fcf35363e773aab0a921ab
    Moving slot 2 from ac0bed66e38ecb64e1fcf35363e773aab0a921ab
    Moving slot 3 from ac0bed66e38ecb64e1fcf35363e773aab0a921ab
    Moving slot 4 from ac0bed66e38ecb64e1fcf35363e773aab0a921ab
    Moving slot 5 from ac0bed66e38ecb64e1fcf35363e773aab0a921ab
    Moving slot 6 from ac0bed66e38ecb64e1fcf35363e773aab0a921ab
    Moving slot 7 from ac0bed66e38ecb64e1fcf35363e773aab0a921ab
    Moving slot 8 from ac0bed66e38ecb64e1fcf35363e773aab0a921ab
    Moving slot 9 from ac0bed66e38ecb64e1fcf35363e773aab0a921ab
Do you want to proceed with the proposed reshard plan (yes/no)? yes    --輸入yes 表示同意
Moving slot 0 from 192.168.76.116:6382 to 192.168.76.116:6379: 
Moving slot 1 from 192.168.76.116:6382 to 192.168.76.116:6379: 
Moving slot 2 from 192.168.76.116:6382 to 192.168.76.116:6379: 
Moving slot 3 from 192.168.76.116:6382 to 192.168.76.116:6379: 
Moving slot 4 from 192.168.76.116:6382 to 192.168.76.116:6379: 
Moving slot 5 from 192.168.76.116:6382 to 192.168.76.116:6379: 
Moving slot 6 from 192.168.76.116:6382 to 192.168.76.116:6379: 
Moving slot 7 from 192.168.76.116:6382 to 192.168.76.116:6379: 
Moving slot 8 from 192.168.76.116:6382 to 192.168.76.116:6379: 
Moving slot 9 from 192.168.76.116:6382 to 192.168.76.116:6379: 
You have new mail in /var/spool/mail/root
[[email protected] src]#  

遷移後檢測:

127.0.0.1:6381> cluster nodes 
0fcf9d2d4f331a33dd319e8ee40d25595c38325a 192.168.76.116:6380 slave 0936588a1ea864fc020e40be4f22b557cf289471 0 1505382099974 7 connected
c6a480d33b94eb5396ec0d936542490171d49666 192.168.76.116:6383 slave ac0bed66e38ecb64e1fcf35363e773aab0a921ab 0 1505382096973 4 connected
95e9f52c3c0f3aad3b58191ffedb8ea6adf1db61 192.168.76.116:6381 myself,slave 4c075686000e3050b38429cf95717253b2623dc9 0 0 0 connected
0936588a1ea864fc020e40be4f22b557cf289471 192.168.76.116:6379 master - 0 1505382100976 7 connected 0-9
ac0bed66e38ecb64e1fcf35363e773aab0a921ab 192.168.76.116:6382 master - 0 1505382097973 4 connected 10-16383
4c075686000e3050b38429cf95717253b2623dc9 192.168.76.116:6384 master - 0 1505382098974 0 connected
127.0.0.1:6381> 

 其中0936588a1ea864fc020e40be4f22b557cf289471 192.168.76.116:6379 master - 0 1505382100976 7 connected 0-9 表示遷移成功