Redis cluster 平常操做命令

本文主要講解cluster添加節點,刪除節點的操做node

 

1、如下命令是Redis Cluster集羣所獨有的,執行下面命令須要先登陸redis:redis

[root@ceshi ~]# redis-cli -h 192.168.110.66 -c -p 6379測試

//集羣(cluster) 3d

CLUSTER INFO 打印集羣的信息 orm

CLUSTER NODES 列出集羣當前已知的全部節點(node),以及這些節點的相關信息。  server

   

//節點(node) ip

CLUSTER MEET <ip> <port> 將 ip 和 port 所指定的節點添加到集羣當中,讓它成爲集羣的一份子。 ci

CLUSTER FORGET <node_id> 從集羣中移除 node_id 指定的節點。 hash

CLUSTER REPLICATE <node_id> 將當前節點設置爲 node_id 指定的節點的從節點。 it

CLUSTER SAVECONFIG 將節點的配置文件保存到硬盤裏面。  

   

//槽(slot) 

CLUSTER ADDSLOTS <slot> [slot ...] 將一個或多個槽(slot)指派(assign)給當前節點。 

CLUSTER DELSLOTS <slot> [slot ...] 移除一個或多個槽對當前節點的指派。 

CLUSTER FLUSHSLOTS 移除指派給當前節點的全部槽,讓當前節點變成一個沒有指派任何槽的節點。 

CLUSTER SETSLOT <slot> NODE <node_id> 將槽 slot 指派給 node_id 指定的節點,若是槽已經指派給另外一個節點,那麼先讓另外一個節點刪除該槽>,而後再進行指派。 

CLUSTER SETSLOT <slot> MIGRATING <node_id> 將本節點的槽 slot 遷移到 node_id 指定的節點中。 

CLUSTER SETSLOT <slot> IMPORTING <node_id> 從 node_id 指定的節點中導入槽 slot 到本節點。 

CLUSTER SETSLOT <slot> STABLE 取消對槽 slot 的導入(import)或者遷移(migrate)。  

   

//鍵 (key) 

CLUSTER KEYSLOT <key> 計算鍵 key 應該被放置在哪一個槽上。 

CLUSTER COUNTKEYSINSLOT <slot> 返回槽 slot 目前包含的鍵值對數量。 

CLUSTER GETKEYSINSLOT <slot> <count> 返回 count 個 slot 槽中的鍵。

2、添加節點

一、新配置二個測試節點

[root@localhost ~]# cd /etc/redis/

 

//新增配置 

[root@localhost redis]# cp redis_7007.conf redis_7008.conf &&  sed -i "s/7007/7008/g" redis_7008.conf

[root@localhost redis]# cp redis_7007.conf redis_7009.conf &&  sed -i "s/7007/7009/g" redis_7009.conf

   

//啓動 

[root@localhost redis]# /usr/bin/redis-server /etc/redis/redis_7008.conf

[root@localhost redis]# /usr/bin/redis-server /etc/redis/redis_7009.conf

 二、添加主節點

[root@localhost ~]# redis-trib.rb add-node 192.168.110.63:7008 192.168.110.63:7007

>>> Adding node 192.168.110.63:7008 to cluster 192.168.110.63:7007

>>> Performing Cluster Check (using node 192.168.110.63:7007)

M: 740e86d0ba25e231dad2f4a360dbd2eaf245dfcd 192.168.110.63:7007

   slots:8192-12287 (4096 slots) master

   1 additional replica(s)

S: 8c7a6ef1bd97b1f4f73ba379d30b1a3947bd049b 192.168.110.56:7003

   slots: (0 slots) slave

   replicates 25fe0699abf9b5d713fde30af06889a3000bc45a

S: 949607fc0e1ff1f3ca6f6e037c17c655e225cc59 192.168.110.56:7001

   slots: (0 slots) slave

   replicates 740e86d0ba25e231dad2f4a360dbd2eaf245dfcd

M: 153dca4555605c8a9ccbc5bdf968727c43d3e5a6 192.168.110.63:7004

   slots:4096-8191 (4096 slots) master

   1 additional replica(s)

M: 72afb98122d923d3c4e16027947ec34c33df80b3 192.168.110.63:7006

   slots:0-4095 (4096 slots) master

   0 additional replica(s)

M: 25fe0699abf9b5d713fde30af06889a3000bc45a 192.168.110.63:7005

   slots:12288-16383 (4096 slots) master

   1 additional replica(s)

S: d1a11f47c26fea4df4f86a0e1fdde253246c2465 192.168.110.56:7002

   slots: (0 slots) slave

   replicates 153dca4555605c8a9ccbc5bdf968727c43d3e5a6

[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.110.63:7008 to make it join the cluster.

[OK] New node added correctly.

[root@localhost ~]#

註釋:

192.168.110.63:7007是集羣中任意一箇舊節點

192.168.110.63:7008是新增的主節點

三、給192.168.110.63:7008添加從節點

[root@localhost ~]# redis-trib.rb add-node --slave --master-id 777c9d6ef3faa82e9b5c75f6ea266a484b39e6b0 192.168.110.63:7009 192.168.110.63:7007

>>> Adding node 192.168.110.63:7009 to cluster 192.168.110.63:7007

>>> Performing Cluster Check (using node 192.168.110.63:7007)

M: 740e86d0ba25e231dad2f4a360dbd2eaf245dfcd 192.168.110.63:7007

   slots:8192-12287 (4096 slots) master

   1 additional replica(s)

S: 8c7a6ef1bd97b1f4f73ba379d30b1a3947bd049b 192.168.110.56:7003

   slots: (0 slots) slave

   replicates 25fe0699abf9b5d713fde30af06889a3000bc45a

M: 153dca4555605c8a9ccbc5bdf968727c43d3e5a6 192.168.110.63:7004

   slots:4096-8191 (4096 slots) master

   1 additional replica(s)

M: 777c9d6ef3faa82e9b5c75f6ea266a484b39e6b0 192.168.110.63:7008

   slots: (0 slots) master

   0 additional replica(s)

M: 25fe0699abf9b5d713fde30af06889a3000bc45a 192.168.110.63:7005

   slots:12288-16383 (4096 slots) master

   1 additional replica(s)

S: 949607fc0e1ff1f3ca6f6e037c17c655e225cc59 192.168.110.56:7001

   slots: (0 slots) slave

   replicates 740e86d0ba25e231dad2f4a360dbd2eaf245dfcd

M: 72afb98122d923d3c4e16027947ec34c33df80b3 192.168.110.63:7006

   slots:0-4095 (4096 slots) master

   0 additional replica(s)

S: d1a11f47c26fea4df4f86a0e1fdde253246c2465 192.168.110.56:7002

   slots: (0 slots) slave

   replicates 153dca4555605c8a9ccbc5bdf968727c43d3e5a6

[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.110.63:7009 to make it join the cluster.

Waiting for the cluster to join.

>>> Configure node as replica of 192.168.110.63:7008.

[OK] New node added correctly.

[root@localhost ~]#

註釋:

--slave,表示添加的是從節點

--master-id 777c9d6ef3faa82e9b5c75f6ea266a484b39e6b0,主節點的node id,在這裏是前面新添加的7008的node id

192.168.110.63:7009,新節點

192.168.110.63:7007,集羣任一個舊節點

 

 

四、給新加的主節點從新分配slot

[root@localhost ~]# # redis-trib.rb reshard 192.168.110.63:7008               //下面是主要過程 

How many slots do you want to move (from 1 to 16384)? 1000 //設置slot數1000 

What is the receiving node ID? 777c9d6ef3faa82e9b5c75f6ea266a484b39e6b0 //新節點7008的node 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                                      //表示所有節點從新洗牌 

Do you want to proceed with the proposed reshard plan (yes/no)? yes //確認從新分 

註釋: 

新增長的主節點,是沒有slots的,

M: 777c9d6ef3faa82e9b5c75f6ea266a484b39e6b0 192.168.110.63:7008

   slots: (0 slots) master

主節點若是沒有slots的話,存取數據就都不會被選中。

能夠把分配的過程理解成打撲克牌,all表示你們從新洗牌;輸入某個主節點的node id,而後在輸入done的話,就比如從某個節點,抽牌。

 

五、查看一下,集羣狀況

[root@localhost ~]# redis-trib.rb check 192.168.110.63:7007

>>> Performing Cluster Check (using node 192.168.110.63:7007)

M: 740e86d0ba25e231dad2f4a360dbd2eaf245dfcd 192.168.110.63:7007

   slots:8442-12287 (3846 slots) master

   1 additional replica(s)

S: 8c7a6ef1bd97b1f4f73ba379d30b1a3947bd049b 192.168.110.56:7003

   slots: (0 slots) slave

   replicates 25fe0699abf9b5d713fde30af06889a3000bc45a

M: 54c94223ef55a24082740ce3659215c619120b82 192.168.110.56:7000

   slots: (0 slots) master

   0 additional replica(s)

M: 153dca4555605c8a9ccbc5bdf968727c43d3e5a6 192.168.110.63:7004

   slots:4346-8191 (3846 slots) master

   1 additional replica(s)

M: 777c9d6ef3faa82e9b5c75f6ea266a484b39e6b0 192.168.110.63:7008

   slots:0-249,4096-4345,8192-8441,12288-12537 (1000 slots) master

   1 additional replica(s)

M: 25fe0699abf9b5d713fde30af06889a3000bc45a 192.168.110.63:7005

   slots:12538-16383 (3846 slots) master

   1 additional replica(s)

S: 949607fc0e1ff1f3ca6f6e037c17c655e225cc59 192.168.110.56:7001

   slots: (0 slots) slave

   replicates 740e86d0ba25e231dad2f4a360dbd2eaf245dfcd

M: 72afb98122d923d3c4e16027947ec34c33df80b3 192.168.110.63:7006

   slots:250-4095 (3846 slots) master

   0 additional replica(s)

S: 5653e8301abb5356a3a32d56da8512becb6d4409 192.168.110.63:7009

   slots: (0 slots) slave

   replicates 777c9d6ef3faa82e9b5c75f6ea266a484b39e6b0

S: d1a11f47c26fea4df4f86a0e1fdde253246c2465 192.168.110.56:7002

   slots: (0 slots) slave

   replicates 153dca4555605c8a9ccbc5bdf968727c43d3e5a6

[OK] All nodes agree about slots configuration.

>>> Check for open slots...

>>> Check slots coverage...

[OK] All 16384 slots covered.

[root@localhost ~]#

3、改變從節點的master

一、查看下7008的從節點

[root@ceshi ~]# redis-cli -h 192.168.110.63 -p 7008 cluster nodes|grep slave

5653e8301abb5356a3a32d56da8512becb6d4409 192.168.110.63:7009 slave 777c9d6ef3faa82e9b5c75f6ea266a484b39e6b0 0 1521105048702 13 connected

二、將7009加入新的master 7007

[root@ceshi ~]# redis-cli -c -p 7009 -h 192.168.110.63

192.168.110.63:7009> CLUSTER REPLICATE 740e86d0ba25e231dad2f4a360dbd2eaf245dfcd

OK

192.168.110.63:7009> exit

[root@ceshi ~]#

三、查看7007matser的slave

[root@ceshi ~]#  redis-cli -h 192.168.110.63 -p 7007 cluster nodes|grep slave

5653e8301abb5356a3a32d56da8512becb6d4409 192.168.110.63:7009 slave 777c9d6ef3faa82e9b5c75f6ea266a484b39e6b0 0 1521105048702 13 connected

 

4、刪除節點

一、刪除從節點

(1)查看目前存在的全部節點

[root@localhost ~]# redis-cli -h 192.168.110.63 -p 7007 cluster nodes

8c7a6ef1bd97b1f4f73ba379d30b1a3947bd049b 192.168.110.56:7003 slave 25fe0699abf9b5d713fde30af06889a3000bc45a 0 1521106589269 6 connected

54c94223ef55a24082740ce3659215c619120b82 192.168.110.56:7000 master - 0 1521106585742 12 connected

153dca4555605c8a9ccbc5bdf968727c43d3e5a6 192.168.110.63:7004 master - 0 1521106584233 5 connected 4346-8191

777c9d6ef3faa82e9b5c75f6ea266a484b39e6b0 192.168.110.63:7008 master - 0 1521106588262 13 connected 0-249 4096-4345 8192-8441 12288-12537

25fe0699abf9b5d713fde30af06889a3000bc45a 192.168.110.63:7005 master - 0 1521106587759 6 connected 12538-16383

740e86d0ba25e231dad2f4a360dbd2eaf245dfcd 192.168.110.63:7007 myself,master - 0 0 9 connected 8442-12287

33571cb37ef6b868b8413d570c2c212572694204 :0 slave,fail,noaddr 72afb98122d923d3c4e16027947ec34c33df80b3 1521093508336 1521093508235 10 disconnected

949607fc0e1ff1f3ca6f6e037c17c655e225cc59 192.168.110.56:7001 slave 740e86d0ba25e231dad2f4a360dbd2eaf245dfcd 0 1521106585240 9 connected

72afb98122d923d3c4e16027947ec34c33df80b3 192.168.110.63:7006 master - 0 1521106587254 10 connected 250-4095

5653e8301abb5356a3a32d56da8512becb6d4409 192.168.110.63:7009 slave 740e86d0ba25e231dad2f4a360dbd2eaf245dfcd 0 1521106583729 13 connected

d1a11f47c26fea4df4f86a0e1fdde253246c2465 192.168.110.56:7002 slave 153dca4555605c8a9ccbc5bdf968727c43d3e5a6 0 1521106590274 5 connected

[root@localhost ~]#

(2)刪除剛纔建立的7009的從節點

[root@ceshi ~]# redis-trib.rb del-node 192.168.110.63:7009 '5653e8301abb5356a3a32d56da8512becb6d4409'

>>> Removing node 5653e8301abb5356a3a32d56da8512becb6d4409 from cluster 192.168.110.63:7009

>>> Sending CLUSTER FORGET messages to the cluster...

>>> SHUTDOWN the node.

[root@localhost ~]#

(3)從新查看發現7009不存在了

[root@localhost ~]# redis-cli -h 192.168.110.63 -p 7007 cluster nodes

8c7a6ef1bd97b1f4f73ba379d30b1a3947bd049b 192.168.110.56:7003 slave 25fe0699abf9b5d713fde30af06889a3000bc45a 0 1521106810771 6 connected

54c94223ef55a24082740ce3659215c619120b82 192.168.110.56:7000 master - 0 1521106806741 12 connected

153dca4555605c8a9ccbc5bdf968727c43d3e5a6 192.168.110.63:7004 master - 0 1521106812788 5 connected 4346-8191

777c9d6ef3faa82e9b5c75f6ea266a484b39e6b0 192.168.110.63:7008 master - 0 1521106809763 13 connected 0-249 4096-4345 8192-8441 12288-12537

25fe0699abf9b5d713fde30af06889a3000bc45a 192.168.110.63:7005 master - 0 1521106809763 6 connected 12538-16383

740e86d0ba25e231dad2f4a360dbd2eaf245dfcd 192.168.110.63:7007 myself,master - 0 0 9 connected 8442-12287

33571cb37ef6b868b8413d570c2c212572694204 :0 slave,fail,noaddr 72afb98122d923d3c4e16027947ec34c33df80b3 1521093508336 1521093508235 10 disconnected

949607fc0e1ff1f3ca6f6e037c17c655e225cc59 192.168.110.56:7001 slave 740e86d0ba25e231dad2f4a360dbd2eaf245dfcd 0 1521106806239 9 connected

72afb98122d923d3c4e16027947ec34c33df80b3 192.168.110.63:7006 master - 0 1521106811779 10 connected 250-4095

d1a11f47c26fea4df4f86a0e1fdde253246c2465 192.168.110.56:7002 slave 153dca4555605c8a9ccbc5bdf968727c43d3e5a6 0 1521106808756 5 connected

(4)刪除從節點成功

二、刪除主節點

(1)查看當前存在的全部節點

[root@localhost ~]# redis-cli -h 192.168.110.63 -p 7007 cluster nodes

8c7a6ef1bd97b1f4f73ba379d30b1a3947bd049b 192.168.110.56:7003 slave 25fe0699abf9b5d713fde30af06889a3000bc45a 0 1521106810771 6 connected

54c94223ef55a24082740ce3659215c619120b82 192.168.110.56:7000 master - 0 1521106806741 12 connected

153dca4555605c8a9ccbc5bdf968727c43d3e5a6 192.168.110.63:7004 master - 0 1521106812788 5 connected 4346-8191

777c9d6ef3faa82e9b5c75f6ea266a484b39e6b0 192.168.110.63:7008 master - 0 1521106809763 13 connected 0-249 4096-4345 8192-8441 12288-12537

25fe0699abf9b5d713fde30af06889a3000bc45a 192.168.110.63:7005 master - 0 1521106809763 6 connected 12538-16383

740e86d0ba25e231dad2f4a360dbd2eaf245dfcd 192.168.110.63:7007 myself,master - 0 0 9 connected 8442-12287

33571cb37ef6b868b8413d570c2c212572694204 :0 slave,fail,noaddr 72afb98122d923d3c4e16027947ec34c33df80b3 1521093508336 1521093508235 10 disconnected

949607fc0e1ff1f3ca6f6e037c17c655e225cc59 192.168.110.56:7001 slave 740e86d0ba25e231dad2f4a360dbd2eaf245dfcd 0 1521106806239 9 connected

72afb98122d923d3c4e16027947ec34c33df80b3 192.168.110.63:7006 master - 0 1521106811779 10 connected 250-4095

d1a11f47c26fea4df4f86a0e1fdde253246c2465 192.168.110.56:7002 slave 153dca4555605c8a9ccbc5bdf968727c43d3e5a6 0 1521106808756 5 connected

(2)刪除剛纔建立的主節點7008

注意:

若是主節點有從節點,將從節點轉移到其餘主節點

若是主節點有slot,去掉分配的slot,而後在刪除主節點

 

a:去掉slot

[root@localhost ~]# redis-trib.rb reshard 192.168.110.63:7008                                       //取消分配的slot,下面是主要過程 

How many slots do you want to move (from 1 to 16384)? 1000          //被刪除master7008的全部slot數量

What is the receiving node ID? 740e86d0ba25e231dad2f4a360dbd2eaf245dfcd     //接收7008節點slot的master,我這裏使用的是7007

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:777c9d6ef3faa82e9b5c75f6ea266a484b39e6b0              //被刪除master的node-id,我這裏7008

Source node #2:done  

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

 

新增master節點後,也進行了這一步操做,當時是分配,如今去掉。反着的。

b:刪除主節點

[root@localhost ~]# redis-trib.rb del-node 192.168.110.63:7008 '777c9d6ef3faa82e9b5c75f6ea266a484b39e6b0'

>>> Removing node 777c9d6ef3faa82e9b5c75f6ea266a484b39e6b0 from cluster 192.168.110.63:7008

>>> Sending CLUSTER FORGET messages to the cluster...

>>> SHUTDOWN the node.

[root@ceshi ~]#

c:查看當前存在的節點

[root@localhost ~]# redis-cli -h 192.168.110.63 -p 7007 cluster nodes

8c7a6ef1bd97b1f4f73ba379d30b1a3947bd049b 192.168.110.56:7003 slave 25fe0699abf9b5d713fde30af06889a3000bc45a 0 1521108471111 6 connected

54c94223ef55a24082740ce3659215c619120b82 192.168.110.56:7000 master - 0 1521108475138 12 connected

153dca4555605c8a9ccbc5bdf968727c43d3e5a6 192.168.110.63:7004 master - 0 1521108476142 5 connected 4346-8191

25fe0699abf9b5d713fde30af06889a3000bc45a 192.168.110.63:7005 master - 0 1521108474636 6 connected 12538-16383

740e86d0ba25e231dad2f4a360dbd2eaf245dfcd 192.168.110.63:7007 myself,master - 0 0 14 connected 0-249 4096-4345 8192-12537

33571cb37ef6b868b8413d570c2c212572694204 :0 slave,fail,noaddr 72afb98122d923d3c4e16027947ec34c33df80b3 1521093508336 1521093508235 10 disconnected

949607fc0e1ff1f3ca6f6e037c17c655e225cc59 192.168.110.56:7001 slave 740e86d0ba25e231dad2f4a360dbd2eaf245dfcd 0 1521108474133 14 connected

72afb98122d923d3c4e16027947ec34c33df80b3 192.168.110.63:7006 master - 0 1521108473126 10 connected 250-4095

d1a11f47c26fea4df4f86a0e1fdde253246c2465 192.168.110.56:7002 slave 153dca4555605c8a9ccbc5bdf968727c43d3e5a6 0 1521108472120 5 connected

[root@localhost ~]#

d:至此節點刪除完畢

相關文章
相關標籤/搜索