Redis系列(三):Redis集羣的水平擴展與伸縮

1、Redis集羣的水平擴展

  Redis3.0版本之後,有了集羣的功能,提供了比以前版本的哨兵模式更高的性能與可用性,可是集羣的水平擴展卻比較麻煩,接下來介紹下Redis高可用集羣如何作水平擴展,在原集羣的6個節點的基礎上新增2個節點,由原來的3主3從變成4主4從,原先的3主3從部署詳見Redis系列(二):Redis高可用集羣,以下圖:html

 2、水平擴展具體操做

   將redis-5.0.2文件夾拷貝到新的主機192.168.160.154上去,(1)scp -r /usr/local/redis-5.0.2 root@192.168.160.154:/usr/local/ 進去到192.168.160.154主機 (2)cd /usr/localnode

    新啓動2個redis實例,而後檢查是否啓動成功 (1)/usr/local/redis-5.0.2/bin/redis-server /usr/local/redis-5.0.2/redis-cluster/700*/redis.conf (2)ps -ef | grep redis 查看是否啓動成功redis

   ③ 查看redis集羣的命令幫助 (1)cd /usr/local/redis-5.0.2 (2)bin/redis-cli --cluster help性能

create:建立一個集羣host1:port1 ... hostN:portN
call:能夠執行redis命令
add-node:將一個節點添加到集羣裏,第一個參數爲新節點的ip:port,第二個參數爲集羣中任意一個已經存在的節點的ip:port
del-node:移除一個節點
reshard:從新分片
check:檢查集羣狀態

  ④ 使用add-node命令新增一個主節點192.168.160.154:7001(master),前面的ip:port爲新增節點,後面的ip:port爲集羣中已存在節點(1)/usr/local/redis-5.0.2/bin/redis-cli --cluster add-node 192.168.160.154:7001 192.168.160.146:7001spa

   (1)鏈接任意一個客戶端便可:./redis-cli -c -h -p (-a訪問服務端密碼,-c表示集羣模式,指定ip地址和端口號)如:/usr/local/redis-5.0.2/bin/redis-cli -c -h 192.168.160.146 -p 700* (2)進行驗證: cluster info(查看集羣信息)、cluster nodes(查看節點列表)3d

    使用redis-cli命令爲192.168.160.154:7001分配slots槽位,找到集羣中的任意一個主節點,對其進行從新分片工做。(1)/usr/local/redis-5.0.2/bin/redis-cli --cluster reshard 192.168.160.146:7001code

How many slots do you want to move (from 1 to 16384)? 4000
(ps:須要多少個槽移動到新的節點上,本身設置,好比4000個hash槽)
What is the receiving node ID? 44b0bd6cf056af7dbbfa0dd9497def1cfc21eb6d
(ps:把這4000個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爲從全部主節點中分別抽取相應的槽數指定到新節點中,抽取的總槽數爲4000個;或者輸入原節點ID而後輸入done,意思將輸入的節點ID,抽取的總槽數爲4000個)
Do you want to proceed with the proposed reshard plan (yes/no)? yes
(ps:輸入yes確認開始執行分片任務)

  ⑦ 查看下最新的集羣狀態server

 

    添加從節點192.168.160.154:7002到集羣中去並查看集羣狀態htm

  (1)/usr/local/redis-5.0.2/bin/redis-cli --cluster add-node 192.168.160.154:7002 192.168.160.146:7001blog

  (2)cluster nodes

  (3)如上圖所示,仍是一個master節點,沒有被分配任何的hash槽。咱們須要執行replicate命令來指定當前節點(從節點)的主節點id爲哪一個,首先須要鏈接新加的192.168.160.154:7002節點的客戶端,而後使用集羣命令進行操做,把當前的192.168.160.154:7002(slave)節點指定到一個主節點下(這裏使用以前建立的192.168.160.154:7001主節點)

  (3-1)/usr/local/redis-5.0.2/bin/redis-cli -c -h 192.168.160.154 -p 7002

  (3-2)cluster replicate 44b0bd6cf056af7dbbfa0dd9497def1cfc21eb6d #後面這串id爲192.168.160.154:7001的節點id

   (3-2)cluster nodes

 3、水平伸縮具體操做

  目的還原成原始集羣,以下圖:

    刪除192.168.160.154:7002從節點,用del-node刪除從節點192.168.160.154:7002,指定刪除節點ip和端口,以及節點id(192.168.160.154:7002節點id) 

  (1)/usr/local/redis-5.0.2/bin/redis-cli --cluster del-node 192.168.160.154:7002 564963541c243365cbb20aed69e98048d21d68fd

   (2)cluster nodes

    刪除192.168.160.154:7001主節點,這個步驟相對麻煩一些,由於主節點的裏面是有分配了slots槽位,因此必須先把192.168.160.154:7001裏的slots槽位放入到其餘的可用主節點中去,而後再進行移除節點操做,否則會出現數據丟失問題(目前只能把master的數據遷移到一個節點上,暫時作不了平均分配功能)

  (1)/usr/local/redis-5.0.2/bin/redis-cli --cluster reshard 192.168.160.154:7001

How many slots do you want to move (from 1 to 16384)? 4000
(ps:須要多少個槽移動到新的節點上,本身設置,好比4000個hash槽)
What is the receiving node ID? e7f80ba80749904838b6d779a0646e7f22313624
(ps:把這4000個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:44b0bd6cf056af7dbbfa0dd9497def1cfc21eb6d
Source node 1:done
(ps:輸入all爲從全部主節點中分別抽取相應的槽數指定到新節點中,抽取的總槽數爲4000個;或者輸入原節點ID而後輸入done,意思將輸入的節點ID,抽取的總槽數爲4000個)
Do you want to proceed with the proposed reshard plan (yes/no)? yes
(ps:輸入yes確認開始執行分片任務)

  (2)cluster nodes 已經成功的把192.168.160.154:7001主節點的數據遷移到192.168.160.146:7001上去了

    (3)最後咱們直接使用del-node命令刪除192.168.160.154:7001主節點便可

 

   (4)最後執行cluster nodes

相關文章
相關標籤/搜索