1. 程式人生 > >Linux環境redis叢集搭建(二)叢集維護

Linux環境redis叢集搭建(二)叢集維護

叢集維護:

1>叢集新增節點

1、建立兩個redis服務70067007,配置檔案與前述一致,注意埠號即可

2、啟動兩個節點,並且檢查redis服務

[[email protected] r7007]# chmod 775 redis-server

[[email protected] r7007]# chmod 775 redis-cli

[[email protected] r7007]# ./redis-server redis.conf

[[email protected] r7007]# ps -ef|grep redis

root      5160     1  0 10

19 ?      00:23:58 ./redis-server 192.168.8.28:7000 [cluster]

root      5164     1  0 1019 ?      00:23:55 ./redis-server 192.168.8.28:7001 [cluster]

root      5168     1  0 1019 ?      00:24:12 ./redis-server 192.168.8.28:7002 [cluster]

root      5172     1  0 1019 ?      00:23:47 ./redis-server 192.168.8.28:7003 [cluster]

root      5176     1  0 1019 ?      00:23:48 ./redis-server 192.168.8.28:7004 [cluster]

root      5180     1  0 1019 ?      00:24:11 ./redis-server 192.168.8.28:7005 [cluster]

root     10997     1  0 10:34 ?        00:00:00 ./redis-server 192.168.8.28:7006 [cluster]

root     11007     1  0 10:35 ?        00:00:00 ./redis-server 192.168.8.28:7007 [cluster]

root     11011 10937  0 10:35 pts/0    00:00:00 grep --color=auto redis

[[email protected] r7007]#

3、新增一個主節點

[[email protected] src]# ./redis-trib.rb add-node 192.168.8.28:7006 192.168.8.28:7000

>>> Adding node 192.168.8.28:7006 to cluster 192.168.8.28:7000

>>> Performing Cluster Check (using node 192.168.8.28:7000)

M: 6bf319dbb78bc54b33b80ef9ed9c96b77497a90e 192.168.8.28:7000

   slots:0-5460 (5461 slots) master

   1 additional replica(s)

M: aeaa50b29990fd58335a5755564a71be364482d8 192.168.8.28:7002

   slots:10923-16383 (5461 slots) master

   1 additional replica(s)

S: 869178d46874074636763951ea59e81a2d124d04 192.168.8.28:7003

   slots: (0 slots) slave

   replicates 6bf319dbb78bc54b33b80ef9ed9c96b77497a90e

S: 9b2b28ce6db5d0dc4b3849c3e427a87d97d17d07 192.168.8.28:7001

   slots: (0 slots) slave

   replicates 77682c93fc6bf535a3bf2701777fcc0df73b1791

S: b715e624c719bac4507dfac49f6d2d62a51dd357 192.168.8.28:7005

   slots: (0 slots) slave

   replicates aeaa50b29990fd58335a5755564a71be364482d8

M: 77682c93fc6bf535a3bf2701777fcc0df73b1791 192.168.8.28:7004

   slots:5461-10922 (5462 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.

>>> Send CLUSTER MEET to node 192.168.8.28:7006 to make it join the cluster.

[OK] New node added correctly.

注意192.168.8.28:7006是要加的新節點,192.168.8.28:7000是叢集中已存在的任意節點

檢查叢集狀態

[[email protected] src]# ./redis-trib.rb check 192.168.8.28:7000

>>> Performing Cluster Check (using node 192.168.8.28:7000)

M: 6bf319dbb78bc54b33b80ef9ed9c96b77497a90e 192.168.8.28:7000

   slots:0-5460 (5461 slots) master

   1 additional replica(s)

M: aeaa50b29990fd58335a5755564a71be364482d8 192.168.8.28:7002

   slots:10923-16383 (5461 slots) master

   1 additional replica(s)

S: 869178d46874074636763951ea59e81a2d124d04 192.168.8.28:7003

   slots: (0 slots) slave

   replicates 6bf319dbb78bc54b33b80ef9ed9c96b77497a90e

M: 8c3f3f136094fa762c78f239428828e972871e05 192.168.8.28:7006

   slots: (0 slots) master

   0 additional replica(s)

S: 9b2b28ce6db5d0dc4b3849c3e427a87d97d17d07 192.168.8.28:7001

   slots: (0 slots) slave

   replicates 77682c93fc6bf535a3bf2701777fcc0df73b1791

S: b715e624c719bac4507dfac49f6d2d62a51dd357 192.168.8.28:7005

   slots: (0 slots) slave

   replicates aeaa50b29990fd58335a5755564a71be364482d8

M: 77682c93fc6bf535a3bf2701777fcc0df73b1791 192.168.8.28:7004

   slots:5461-10922 (5462 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.

3、給主節點新增從節點


[[email protected] src]# ./redis-trib.rb add-node --slave --master-id 8c3f3f136094fa762c78f239428828e972871e05 192.168.8.28:7007 192.168.8.28:7000

>>> Adding node 192.168.8.28:7007 to cluster 192.168.8.28:7000

>>> Performing Cluster Check (using node 192.168.8.28:7000)

M: 6bf319dbb78bc54b33b80ef9ed9c96b77497a90e 192.168.8.28:7000

   slots:0-5460 (5461 slots) master

   1 additional replica(s)

M: aeaa50b29990fd58335a5755564a71be364482d8 192.168.8.28:7002

   slots:10923-16383 (5461 slots) master

   1 additional replica(s)

S: 869178d46874074636763951ea59e81a2d124d04 192.168.8.28:7003

   slots: (0 slots) slave

   replicates 6bf319dbb78bc54b33b80ef9ed9c96b77497a90e

M: 8c3f3f136094fa762c78f239428828e972871e05 192.168.8.28:7006

   slots: (0 slots) master

   0 additional replica(s)

S: 9b2b28ce6db5d0dc4b3849c3e427a87d97d17d07 192.168.8.28:7001

   slots: (0 slots) slave

   replicates 77682c93fc6bf535a3bf2701777fcc0df73b1791

S: b715e624c719bac4507dfac49f6d2d62a51dd357 192.168.8.28:7005

   slots: (0 slots) slave

   replicates aeaa50b29990fd58335a5755564a71be364482d8

M: 77682c93fc6bf535a3bf2701777fcc0df73b1791 192.168.8.28:7004

   slots:5461-10922 (5462 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.

>>> Send CLUSTER MEET to node 192.168.8.28:7007 to make it join the cluster.

Waiting for the cluster to join.

>>> Configure node as replica of 192.168.8.28:7006.

[OK] New node added correctly.

注意--slave 指定要加的是從節點,--master-id 指定這個從節點的主節點ID10.10.2.85:6303是需要新加的從節點,10.10.2.70:6300是叢集已存在的任意節點

4、遷移一些slots給新節點

執行後顯示分配情況,然後根據提示填yes確認遷移

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

命令:

./redis-trib.rb reshard 192.168.8.28:7000

How many slots do you want to move (from 1 to 16384)? 4000  #設定需要把4000slot做移動

What is the receiving node ID? 8c3f3f136094fa762c78f239428828e972871e05  #設定接收這4000slot的節點ID,也就是剛才新加的192.168.8.28:7006ID

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:6bf319dbb78bc54b33b80ef9ed9c96b77497a90e   #設定這4000slot的來源ID,這裡我從叢集之前的3個節點分別去取一部分slot

Source node #2:aeaa50b29990fd58335a5755564a71be364482d8    #設定這4000slot的來源ID,這裡我從叢集之前的3個節點分別去取一部分slot

Source node #3:77682c93fc6bf535a3bf2701777fcc0df73b1791      #設定這4000slot的來源ID,這裡我從叢集之前的3個節點分別去取一部分slot

Source node #4:done   #輸入done開始做一些初始化操作

此處省略

Do you want to proceed with the proposed reshard plan (yes/no)? yes  # 輸入yes確認開始遷移slot

檢查新增節點情況

[[email protected] src]# ./redis-trib.rb check 192.168.8.28:7000

>>> Performing Cluster Check (using node 192.168.8.28:7000)

M: 6bf319dbb78bc54b33b80ef9ed9c96b77497a90e 192.168.8.28:7000

   slots:1333-5460 (4128 slots) master

   1 additional replica(s)

M: aeaa50b29990fd58335a5755564a71be364482d8 192.168.8.28:7002

   slots:12256-16383 (4128 slots) master

   1 additional replica(s)

S: 869178d46874074636763951ea59e81a2d124d04 192.168.8.28:7003

   slots: (0 slots) slave

   replicates 6bf319dbb78bc54b33b80ef9ed9c96b77497a90e

M: 8c3f3f136094fa762c78f239428828e972871e05 192.168.8.28:7006

   slots:0-1332,5461-6794,10923-12255 (4000 slots) master

   1 additional replica(s)

S: 9b2b28ce6db5d0dc4b3849c3e427a87d97d17d07 192.168.8.28:7001

   slots: (0 slots) slave

   replicates 77682c93fc6bf535a3bf2701777fcc0df73b1791

S: b715e624c719bac4507dfac49f6d2d62a51dd357 192.168.8.28:7005

   slots: (0 slots) slave

   replicates aeaa50b29990fd58335a5755564a71be364482d8

M: 77682c93fc6bf535a3bf2701777fcc0df73b1791 192.168.8.28:7004

   slots:6795-10922 (4128 slots) master

   1 additional replica(s)

S: 7784af78f9d04a2da3a9ac6ef48bc1506ca90e40 192.168.8.28:7007

   slots: (0 slots) slave

   replicates 8c3f3f136094fa762c78f239428828e972871e05

[OK] All nodes agree about slots configuration.

>>> Check for open slots...

>>> Check slots coverage...

[OK] All 16384 slots covered.

驗證

[[email protected] r7000]# ./redis-cli -c -h 192.168.8.28 -p 7000

192.168.8.28:7000> set a 1

-> Redirected to slot [15495] located at 192.168.8.28:7002

OK

192.168.8.28:7002> set b 2

-> Redirected to slot [3300] located at 192.168.8.28:7000

OK

192.168.8.28:7000> set c 3

-> Redirected to slot [7365] located at 192.168.8.28:7004

OK

192.168.8.28:7004> set d 4

-> Redirected to slot [11298] located at 192.168.8.28:7006

OK

192.168.8.28:7006>

2>刪除節點

[[email protected]_0_0 ~]# ./redis-trib.rb reshard 192.168.8.28:7000

 

[OK] All nodes agree about slots configuration.

>>> Check for open slots...

>>> Check slots coverage...

[OK] All 16384 slots covered.

How many slots do you want to move (from 1 to 16384)? 1334 #上文給這個節點遷入了4000slot,所以這裡還選擇遷出4000slot平均分配到三個主節點中

What is the receiving node ID? 77682c93fc6bf535a3bf2701777fcc0df73b1791 #接收這1334slot節點的主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:8c3f3f136094fa762c78f239428828e972871e05  #要下線節點的主ID

Source node #2:done

此處省略

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

2、下線從節點

[[email protected] src]# ./redis-trib.rb del-node 192.168.8.28:7007 7784af78f9d04a2da3a9ac6ef48bc1506ca90e40

>>> Removing node 7784af78f9d04a2da3a9ac6ef48bc1506ca90e40 from cluster 192.168.8.28:7007

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

>>> SHUTDOWN the node.

3、下線主節點

[[email protected] src]# ./redis-trib.rb del-node 192.168.8.28:7006 8c3f3f136094fa762c78f239428828e972871e05

>>> Removing node 8c3f3f136094fa762c78f239428828e972871e05 from cluster 192.168.8.28:7006

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

>>> SHUTDOWN the node.

3、檢查

[[email protected] src]# ./redis-trib.rb check 192.168.8.28:7000

>>> Performing Cluster Check (using node 192.168.8.28:7000)

M: 6bf319dbb78bc54b33b80ef9ed9c96b77497a90e 192.168.8.28:7000

   slots:0-5460 (5461 slots) master

   1 additional replica(s)

M: aeaa50b29990fd58335a5755564a71be364482d8 192.168.8.28:7002

   slots:5461-6793,12256-16383 (5461 slots) master

   1 additional replica(s)

S: 869178d46874074636763951ea59e81a2d124d04 192.168.8.28:7003

   slots: (0 slots) slave

   replicates 6bf319dbb78bc54b33b80ef9ed9c96b77497a90e

S: 9b2b28ce6db5d0dc4b3849c3e427a87d97d17d07 192.168.8.28:7001

   slots: (0 slots) slave

   replicates 77682c93fc6bf535a3bf2701777fcc0df73b1791

S: b715e624c719bac4507dfac49f6d2d62a51dd357 192.168.8.28:7005

   slots: (0 slots) slave

   replicates aeaa50b29990fd58335a5755564a71be364482d8

M: 77682c93fc6bf535a3bf2701777fcc0df73b1791 192.168.8.28:7004

   slots:6794-12255 (5462 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.

4、驗證

[[email protected] r7000]# ./redis-cli -c -h 192.168.8.28 -p 7000

192.168.8.28:7000> get a

-> Redirected to slot [15495] located at 192.168.8.28:7002

"1"

192.168.8.28:7002> get b

-> Redirected to slot [3300] located at 192.168.8.28:7000

"2"

192.168.8.28:7000> get c

-> Redirected to slot [7365] located at 192.168.8.28:7004

"3"

192.168.8.28:7004> get d

"4"