Redis學習總結第四章--Redis集羣水平擴展

Redis學習總結第三章--Redis集羣水平擴展

 

在這篇博客裏《Redis學習總結第二章--Redis 搭建高可用集羣》我們搭建的集羣由6個節點組成,6個節點分佈在三臺機器上,採用三主三從的模式

 

 

 

1、啓動集羣

# 啓動整個集羣

/usr/local/lanbing/redis-5.0.4/src/redis-server /usr/local/lanbing/redis-cluster/8001/redis-8001.conf

/usr/local/lanbing/redis-5.0.4/src/redis-server /usr/local/lanbing/redis-cluster/8004/redis-8004.conf

 

/usr/local/lanbing/redis-5.0.4/src/redis-server /usr/local/lanbing/redis-cluster/8002/redis-8002.conf

/usr/local/lanbing/redis-5.0.4/src/redis-server /usr/local/lanbing/redis-cluster/8005/redis-8005.conf

 

/usr/local/lanbing/redis-5.0.4/src/redis-server /usr/local/lanbing/redis-cluster/8003/redis-8003.conf

/usr/local/lanbing/redis-5.0.4/src/redis-server /usr/local/lanbing/redis-cluster/8006/redis-8006.conf

# 客戶端連接8001端口的redis實例

/usr/local/lanbing/redis-5.0.4/src/redis-cli -a lanbing -c -h 192.168.0.1 -p 8001

# 查看集羣狀態

192.168.01:8001> cluster  nodes

 

 

 

2、集羣操作

我們在原始集羣基礎上再增加一主(8007)一從(8008),增加節點後的集羣參見下圖,新增節點用虛線框表示

克隆虛擬機

此操作可以看《Redis學習總結第二章--Redis 搭建高可用集羣》這篇文章。

 

增加redis實例

# 在/usr/local/lanbing/redis-cluster下改8001和8004文件夾名爲8007和8008文件夾,並修改redis-8001.conf中8001--》8007;redis-8004.conf中8004--》8008;

 

# 修改8007文件夾下的redis-8001.conf配置文件

vim /usr/local/redis-cluster/8007/redis.conf

 

# 修改如下內容:

port:8007

dir /usr/local/lanbing/redis-cluster/8007/

cluster-config-file nodes-8007.conf

 

# 修改8008文件夾下的redis-8004.conf配置文件

vim /usr/local/lanbing/redis-cluster/8008/redis-8004.conf

修改內容如下:

port:8008

dir /usr/local/lanbing/redis-cluster/8008/

cluster-config-file nodes8008.conf

 

啓動redis:

/usr/local/lanbing/redis-5.0.4/src/redis-server /usr/local/lanbing/redis-cluster/8007/redis-8007.conf

/usr/local/lanbing/redis-5.0.4/src/redis-server /usr/local/lanbing/redis-cluster/8008/redis-8008.conf

 

查看redis集羣的命令幫助

cd /usr/local/lanbing/redis-5.0.4

src/redis-cli --cluster help

1.create:創建一個集羣環境host1:port1 ... hostN:portN

2.call:可以執行redis命令

3.add-node:將一個節點添加到集羣裏,第一個參數爲新節點的ip:port,第二個參數爲集羣中任意一個已經存在的節點的ip:port

4.del-node:移除一個節點

5.reshard:重新分片

6.check:檢查集羣狀態

 

增加8007主節點:

/usr/local/lanbing/redis-5.0.4/src/redis-cli -a lanbing --cluster add-node 節點Ip:8007  集羣中任意masterIp:8002

注意:當添加節點成功以後,新增的節點不會有任何數據,因爲它還沒有分配任何的slot(hash槽),我們需要爲新節點手工分配hash槽

 

# 使用redis-cli命令爲8007分配hash槽,找到集羣中的任意一個主節點(紅色位置表示集羣中的任意一個主節點),對其進行重新分片工作。

/usr/local/lanbing/redis-5.0.4/src/redis-cli -a lanbing --cluster reshard 192.168.0.61:8001

 

輸出如下:

... ...

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

(ps:需要多少個槽移動到新的節點上,自己設置,比如600個hash槽)

What is the receiving node ID? eb57a5700ee6f9ff099b3ce0d03b1a50ff247c3c

(ps:把這600個hash槽移動到哪個節點上去,需要指定節點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

(ps:輸入all爲從所有主節點(8001,8002,8003)中分別抽取相應的槽數指定到新節點中,抽取的總槽數爲600個)

 ... ...

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

(ps:輸入yes確認開始執行分片任務)

... ...

 

# 查看下最新的集羣狀態

 

 

配置8008爲8007的從節點

# 添加從節點8008到集羣中去並查看集羣狀態

/usr/local/lanbing/redis-5.0.4/src/redis-cli -a lanbing --cluster add-node 節點Ip:8008 集羣中任意masterIp:8003

如圖所示,還是一個master節點,沒有被分配任何的hash槽。

 

# 我們需要執行replicate命令來指定當前節點(從節點)的主節點id爲哪個,首先需要連接新加的8008節點的客戶端,然後使用集羣命令進行操作,把當前的8008(slave)節點指定到一個主節點下(這裏使用之前創建的8007主節點,紅色表示節點id)

/usr/local/redis-5.0.2/src/redis-cli -c -h 192.168.0.64 -p 8008

192.168.0.61:8008> cluster replicate eb57a5700ee6f9ff099b3ce0d03b1a50ff247c3c

# 查看集羣狀態,8008節點已成功添加爲8007節點的從節點

 

 

 

刪除8008從節點

# 用del-node刪除從節點8008,指定刪除節點ip和端口,以及節點id(紅色爲8008節點id)

/usr/local/redis-5.0.2/src/redis-cli --cluster del-node 192.168.0.64:8008 1805b6339d91b0e051f46845eebacb9bc43baefe

# 再次查看集羣狀態,如下圖所示,8008這個slave節點已經移除,並且該節點的redis服務也已被停止

 

 

刪除8007主節點

最後,我們嘗試刪除之前加入的主節點8007,這個步驟相對比較麻煩一些,因爲主節點的裏面是有分配了hash槽的,所以我們這裏必須先把8007裏的hash槽放入到其他的可用主節點中去,然後再進行移除節點操作,不然會出現數據丟失問題(目前只能把master的數據遷移到一個節點上,暫時做不了平均分配功能),執行命令如下:

 

/usr/local/lanbing/redis-5.0.4/src/redis-cli -a lanbing --cluster reshard 節點Ip:8007輸出如下:

 ... ...

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

What is the receiving node ID? deedad3c34e8437baa6ff013fd3d1461a0c2e761

(ps:這裏是需要把數據移動到哪?8001的主節點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:eb57a5700ee6f9ff099b3ce0d03b1a50ff247c3c

(ps:這裏是需要數據源,也就是我們的8007節點id)

Source node 2:done

(ps:這裏直接輸入done 開始生成遷移計劃)

 ... ...

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

(ps:這裏輸入yes開始遷移)

 

 

 

# 最後我們直接使用del-node命令刪除8007主節點即可(紅色表示8007的節點id)。

/usr/local/redis-5.0.2/src/redis-cli --cluster del-node 192.168.0.64:8007    eb57a5700ee6f9ff099b3ce0d03b1a50ff247c3c

# 查看集羣狀態,一切還原爲最初始狀態啦!大功告成!

 

 

 

 

 

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