Redis集羣——Redis4.0.1高可用集羣水平擴展

根據Redis4.0.1版本高可用集羣模式搭建(3.9.2版本適用)這篇文章已經可以成功搭建一個由三個小集羣構成的redis高可用集羣,現在來對這個集羣進行水平擴展,再次添加一個主節點和一個從節點。

PS:由於本人的電腦配置有限,就不再搞一個虛擬機了,仍然在192.168.1.12機器上添加這兩個節點。

擴展節點

現在192.168.1.12機器上有7003和7006兩個節點,添加7007和7008節點

第一步,創建文件夾

mkdir -p /opt/redis-cluster/{7007,7008}

第二步,複製配置文件到7007和7008文件夾下

cd /opt/redis-cluster
cp 7003/redis-7003.conf 7007/redis-7007.conf
cp 7003/redis-7003.conf 7008/redis-7008.conf

第三步,修改redis-7007.conf和redis-7008的conf,把端口修改爲對應的7007和7008,可以使用批量修改。

第四步,啓動這兩個節點

redis-server /opt/redis-cluster/7007/redis-7007.conf 
redis-server /opt/redis-cluster/7008/redis-7008.conf 

第五步,開放端口

firewall-cmd --zone=public --add-port=7007/tcp --permanent
firewall-cmd --zone=public --add-port=17007/tcp --permanent
firewall-cmd --zone=public --add-port=7008/tcp --permanent
firewall-cmd --zone=public --add-port=17008/tcp --permanent
firewall-cmd --reload

現在這兩個節點還沒有加入到集羣中,先把主節點7007加入到集羣中。仍然需要使用redis-trib.rb命令來操作,所以我們回到192.168.1.10服務器上操作。

添加主節點

通過add-node命令添加節點,192.168.1.12:7007是要添加的節點,192.168.1.10:7001是集羣已經存在的任意一個節點

[root@localhost src]# ./redis-trib.rb add-node 192.168.1.12:7007 192.168.1.10:7001

當看到[OK] New node added correctly.就是添加成功了。現在查看一下集羣的節點信息

集羣中已經有7007節點了並且是主節點,現在就算是直接使用集羣也不會向7007節點裏寫入數據,因爲7007節點還沒有分配任何的slot(hash槽),現在爲7007分配slot。

root@localhost src]# ./redis-trib.rb reshard 192.168.1.11:7002

reshard表示重新分配slot,後面跟的是集羣中任意一個主節點,回車命令

打印很多信息

How many slots do you want to move (from 1 to 16384)? 

這個是問你要分配多少slot到新的節點上,從1到16384選一個數字,(16384是可以配置的),這裏輸入600

What is the receiving node ID? 89b421f99a3c7f2d7afe09ceefb2f9449abfc209

要分配到哪個id的節點,這裏輸入7007節點的id

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選擇在當前所有主節點中取出slot分配給7007節點

輸入主節點的id則是選擇指定的主節點取出slot分配給7007節點

輸入all,回車

Do you want to proceed with the proposed reshard plan (yes/no)? 

是否開始分配,輸入yes,然後打印在哪個節點取了哪個slot分配到7007節點

到現在就分配slot結束,再次查看集羣節點信息。

發現7007節點已經有slot了,可以寫入數據了。到此爲止,7007已經加入到集羣中,並且是主節點。

添加從節點

現在開始添加從節點,和添加7007節點一樣使用add-node命令,出現[OK] New node added correctly.就是添加成功了

[root@localhost src]# ./redis-trib.rb add-node 192.168.1.12:7008 192.168.1.10:7001

查看節點信息

發現7008節點已經添加到集羣了,但是是一個主節點,現在把7008節點改成7007的從節點

通過redis-cli -c -h 192.168.1.12 -p 7008進入到7008節點

redis-cli -c -h 192.168.1.12 -p 7008

通過cluster replicate 89b421f99a3c7f2d7afe09ceefb2f9449abfc209命令將7008節點改爲7007的從節點

89b421f99a3c7f2d7afe09ceefb2f9449abfc209是7007節點的id

再次查看節點信息

7008節點已經改成7007的從節點了。

到此爲止已經完成了水平擴展,添加了一個主節點和一個從節點。

移除節點

移除從節點(7008節點)

通過./redis-trib.rb的del-node移除,192.168.1.12:7008是移除的節點ip和端口,5a1c5ec76126a1d0ce95b1f7ab70112777088c34

是移除的節點的id

./redis-trib.rb del-node 192.168.1.12:7008 5a1c5ec76126a1d0ce95b1f7ab70112777088c34

打印

>>> Removing node 5a1c5ec76126a1d0ce95b1f7ab70112777088c34 from cluster 192.168.1.12:7008
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.
而且去12服務器查看7008的服務也關掉了

[root@localhost redis-4.0.1]# ps -ef | grep redis
root       1227      1  0 15:14 ?        00:00:10 redis-server 192.168.1.12:7003 [cluster]
root       1232      1  0 15:14 ?        00:00:09 redis-server 192.168.1.12:7006 [cluster]
root       1283      1  0 16:37 ?        00:00:02 redis-server 192.168.1.12:7007 [cluster]
root       1375   1204  0 17:20 pts/0    00:00:00 grep --color=auto redis

查看節點信息,已經沒有7008節點了


移除主節點(7007節點)

在移除主節點前必須先移除它的slot,保證數據不被丟失還是通過reshard命令

[root@localhost src]# ./redis-trib.rb reshard 192.168.1.12:7007

How many slots do you want to move (from 1 to 16384)? 

這裏輸入599,是不能把600個都移走的

What is the receiving node ID? 927d10c90f1db9654a21ed6b9998e43fda64f6e4

要分配到哪個id的節點,這裏輸入7002節點的id,即移動7007的slot到7002節點(必須選主節點)

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:
89b421f99a3c7f2d7afe09ceefb2f9449abfc209
輸入7007節點的id,即把7007節點作爲數據源把slot移動到7002節點

Source node #2:done

Do you want to proceed with the proposed reshard plan (yes/no)? yes

開始打印移動slot的信息

再次查看節點信息,7007節點沒有slot了。

刪除7007節點

./redis-trib.rb del-node 192.168.1.12:7007 89b421f99a3c7f2d7afe09ceefb2f9449abfc209

[root@localhost src]# ./redis-trib.rb del-node 192.168.1.12:7007 89b421f99a3c7f2d7afe09ceefb2f9449abfc209
>>> Removing node 89b421f99a3c7f2d7afe09ceefb2f9449abfc209 from cluster 192.168.1.12:7007
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.

再次查看節點信息,發現7007節點已經移除,並且把slot移動到了7002節點

到此爲止就成功的移除了一個主節點一個從節點,

 

 

 

 

 

 

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章