2017年7月3日 星期一

Redis 新增 node (Master,Slave)



在原本Redis 三主三備 Cluster架構中 新增一組 Master Slave


1.啟動 新的 Redis

/redis/software/redis-3.2.9/src/redis-server /redis/software/8000/redis.conf
/redis/software/redis-3.2.9/src/redis-server /redis/software/8001/redis.conf

[root@localhost 8000]# ps -ef|grep redis
root      9360     1  0 12:17 ?        00:00:03 /redis/software/redis-3.2.9/src/redis-server *:8000 [cluster]
root      9366     1  0 12:17 ?        00:00:01 /redis/software/redis-3.2.9/src/redis-server *:8001 [cluster]


2.把新的Redis node 加進去 Redis Cluster 中 
#前面是新的node , 後面是原有的node

redis-trib.rb add-node 10.22.22.174:8000 10.22.22.174:7000
redis-trib.rb add-node 10.22.22.174:8001 10.22.22.174:7000



成功畫面, 要是出現 [ERR] Node 10.22.22.174:8001 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0. 錯誤

請先刪除原有的 node.log 檔案, 再次加入就可以

加入完成會出現 兩個沒有分配 Slot 的Master 的node

以下是 8000及8001 Port 



3.分配Redis Port 8000 3000個 Slot

redis-trib.rb reshard 10.22.22.174:8000

[root@localhost 8001]# redis-trib.rb reshard 10.22.22.174:8000
>>> Performing Cluster Check (using node 10.22.22.174:8000)
M: 086e40ce9139e625cea92da35619094e2c4a059f 10.22.22.174:8000
   slots: (0 slots) master
   0 additional replica(s)
S: 661f01b51c3aa508b9a990d123e24e6e1378ff61 10.22.22.174:7001
   slots: (0 slots) slave
   replicates f4eb29d1f76b0a29bec4c529377277b6e86a326e
M: 0d09b3e355be6b528c902af6a217a369fc4f4ef1 10.22.22.174:8001
   slots: (0 slots) master
   0 additional replica(s)
M: 493d6e53aa3e263812eb72bbd858784d4d9e88ba 10.22.22.167:7000
   slots:999-5460 (4462 slots) master
   1 additional replica(s)
S: 221ffb33e53faabc8805d79ed2cc39ca720d3698 10.22.22.186:7001
   slots: (0 slots) slave
   replicates 493d6e53aa3e263812eb72bbd858784d4d9e88ba
M: f4eb29d1f76b0a29bec4c529377277b6e86a326e 10.22.22.174:7000
   slots:0-998,5461-6461,10923-16383 (7461 slots) master
   1 additional replica(s)
S: 9b01d87f22fa6131adf8e3f136f4d63680e63892 10.22.22.167:7001
   slots: (0 slots) slave
   replicates af0061956f918d9d1cdb512465e26e3f8600b5c7
M: af0061956f918d9d1cdb512465e26e3f8600b5c7 10.22.22.186:7000
   slots:6462-10922 (4461 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.
### 這邊輸入 要分配的 Slot 的數, 這邊分配 3000個
How many slots do you want to move (from 1 to 16384)? 3000
### 這邊輸入哪一個要接收這3000個的node
What is the receiving node ID? 086e40ce9139e625cea92da35619094e2c4a059f
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.
##這邊輸入 all 代表 其他節點要自己分配掏出3000個, 也可以從單個節點索取
Source node #1:all




輸入完 Yes , 它就會自動分配了


查看一下 目前Cluster 狀態, 可以看到Port  8000 已經被分配了 Slot




4.配置 Slave

為剛剛的Port 8000 配置一個 Slave

直接登入 8001 輸入cluster replicate  Master id
redis-cli -p 8001 cluster replicate 086e40ce9139e625cea92da35619094e2c4a059f



這邊就可以看到 8001已經是 8000的Slave 了

4個Master 4個Slave


---------------
刪除 Master node

1.先把該Master node 上的 Slot 移去別的Slot

redis-trib.rb reshard 10.22.22.174:8000

[root@localhost 8000]# redis-trib.rb reshard 10.22.22.174:8000
>>> Performing Cluster Check (using node 10.22.22.174:8000)
M: 086e40ce9139e625cea92da35619094e2c4a059f 10.22.22.174:8000
   slots:0-998,5461-6461,10923-11921 (2999 slots) master
   1 additional replica(s)
S: 661f01b51c3aa508b9a990d123e24e6e1378ff61 10.22.22.174:7001
   slots: (0 slots) slave
   replicates f4eb29d1f76b0a29bec4c529377277b6e86a326e
M: 493d6e53aa3e263812eb72bbd858784d4d9e88ba 10.22.22.167:7000
   slots:999-5460 (4462 slots) master
   1 additional replica(s)
S: 221ffb33e53faabc8805d79ed2cc39ca720d3698 10.22.22.186:7001
   slots: (0 slots) slave
   replicates 493d6e53aa3e263812eb72bbd858784d4d9e88ba
M: f4eb29d1f76b0a29bec4c529377277b6e86a326e 10.22.22.174:7000
   slots:11922-16383 (4462 slots) master
   1 additional replica(s)
S: 3c09e774bee37e3c4843b791f3a51bb79bd867a5 10.22.22.174:8001
   slots: (0 slots) slave
   replicates 086e40ce9139e625cea92da35619094e2c4a059f
S: 9b01d87f22fa6131adf8e3f136f4d63680e63892 10.22.22.167:7001
   slots: (0 slots) slave
   replicates af0061956f918d9d1cdb512465e26e3f8600b5c7
M: af0061956f918d9d1cdb512465e26e3f8600b5c7 10.22.22.186:7000
   slots:6462-10922 (4461 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.
### 這邊輸入該Master 擁有的Slot 數
How many slots do you want to move (from 1 to 16384)? 3000
### 這邊輸入誰要接收這3000個Slot
What is the receiving node ID? f4eb29d1f76b0a29bec4c529377277b6e86a326e
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.
###這Slot來源是哪一個Master 這邊輸入要移除的Master id
Source node #1:086e40ce9139e625cea92da35619094e2c4a059f
Source node #2:done

2.移除節點

redis-trib.rb del-node 10.22.22.174:8000 '086e40ce9139e625cea92da35619094e2c4a059f'  

3.移除Salve

redis-trib.rb del-node 10.22.22.174:8001 '3c09e774bee37e3c4843b791f3a51bb79bd867a5'  

>>> Removing node 3c09e774bee37e3c4843b791f3a51bb79bd867a5 from cluster 10.22.22.174:8001
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.



2 則留言:

  1. 刪除節點 -- 需再master 節點上執行
    redis-cli -p 7000 cluster forget 661f01b51c3aa508b9a990d123e24e6e1378ff61

    回覆刪除
  2. 为什么我添加master和slave时,在有数据的情况下reshard报错,如果清空了就没问题?
    那么如何迁移有大量数据的slots呢?

    回覆刪除