1. 程式人生 > >redis cluster 叢集配置示例: 建立, 新增節點, 重新分片, 刪除節點

redis cluster 叢集配置示例: 建立, 新增節點, 重新分片, 刪除節點

1 redis叢集搭建

Redis 3.0.0正式版開始官方支援叢集,  下面開始做一個叢集配置的示例.

[[email protected] ~]# tar xf redis-3.2.0.tar.gz

[[email protected] ~]# cd redis-3.2.0

[[email protected] redis-3.2.0]# make && make PREFIX=/usr/local/redis install

安裝之後複製 src/redis-trib.rb 檔案到/usr/local/redis/bin/ ,後續建立叢集的時候用的著.這是官方提供的一個用

ruby語言編寫的用於建立redis叢集的工具.

[[email protected] redis-3.2.0]# cp -a src/redis-trib.rb /usr/local/redis/bin/


接著我們建立6個目錄,對應6redis例項,在建立叢集時,將分三個主例項,三個從例項.(事實上,一個例項就可以建立叢集,但是redis-trib.rb工具預設至少需要三個例項方可建立叢集,這也是為了叢集的自動故障轉移功能可用,如果某個主節點掛了,會自動選舉一個它的從節點替代它作為主節點)

...# mkdir -p /data/redis-cluster/{7001,7002,7003,7004,7005,7006}

...# ls /data/redis-cluster/

7001  7002  7003  7004  7005  7006

複製redis包根目錄下的redis.conf配置檔案到各個節點目錄下

...# for dir in /data/redis-cluster/700* ; do cp -v redis.conf $dir ; done

 

然後修改配置檔案.因為是在一臺機器模擬多個節點,所以主要是修改各個例項的埠以及確保去掉了叢集的配置註釋.主要是以下三個配置項:

cluster-enabled yes

cluster-config-file nodes-6379.conf //這個檔案不能刪除也不能去修改

cluster-node-timeout 15000

[[email protected] redis-3.2.0]# for dir in /data/redis-cluster/700*; do

sed -r -i "

[email protected]^\s*port.*@port ${dir##*/}@ ;

[email protected]^\s*daemonize.*@daemonize [email protected] ;

[email protected]^\s*pidfile.*@pidfile /var/run/redis_${dir##*/}[email protected] ;

[email protected]^\s*logfile.*@logfile /var/log/redis_${dir##*/}[email protected];

[email protected]^\s*#?\s*cluster-enabled.*@cluster-enabled [email protected];

[email protected]^\s*#?\s*cluster-config-file.*@cluster-config-file $dir/nodes-${dir##*/}[email protected];

[email protected]^\s*#?\s*cluster-node-timeout.*@cluster-node-timeout [email protected];

[email protected]^\s*dir.*@dir [email protected];

" $dir/redis.conf

done

 

可以去看下配置是否修改正確, 確認修改無誤後啟動這6個例項.

... # for dir in /data/redis-cluster/700*; do /usr/local/redis/bin/redis-server $dir/redis.conf ; done

 

可以看到6個例項都以cluster的方式正常工作了,接下來就是配置叢集.

這就用到了 redis-trib.rb 這個工具了, 因為這個工具是ruby寫的,所以需要ruby環境的支援,我們需要安裝下ruby環境, 否則無法執行此工具.

[[email protected] redis-3.2.0]# yum -y install ruby rubygems

注意:請確保 ruby版本是 1.8.7+版本,  否則,執行 redis-trib.rb命令時可能會提示無法連線redis服務.

[[email protected] redis-3.2.0]# ruby --version

ruby 1.8.7 (2013-06-27 patchlevel 374) [x86_64-linux]

[[email protected] redis-3.2.0]#

接著安裝ruby環境的redis, 注意,  因天朝特殊的網路環境的原因,我們可能需要更換一下 gem的映象,  更換映象可以使用淘寶的,請參考:http://ruby.taobao.org/ 

[[email protected] redis-3.2.0]# gem install redis

 

好了,開始使用redis-trib.rb 這個工具建立叢集.可以先執行redis-trib.rb看看這個工具的用法.

[[email protected] redis-3.2.0]# /usr/local/redis/bin/redis-trib.rb

Usage: redis-trib <command> <options> <arguments ...>

  info            host:port

  reshard         host:port

                  --pipeline <arg>

                  --slots <arg>

                  --from <arg>

                  --yes

                  --timeout <arg>

                  --to <arg>

  rebalance       host:port

                  --pipeline <arg>

                  --auto-weights

                  --threshold <arg>

                  --use-empty-masters

                  --weight <arg>

                  --timeout <arg>

                  --simulate

  check           host:port

  fix             host:port

                  --timeout <arg>

  set-timeout     host:port milliseconds

  del-node        host:port node_id

  add-node        new_host:new_port existing_host:existing_port

                  --slave

                  --master-id <arg>

  import          host:port

                  --copy

                  --from <arg>

                  --replace

  create          host1:port1 ... hostN:portN

                  --replicas <arg>

  help            (show this help)

  call            host:port command arg arg .. arg

For check, fix, reshard, del-node, set-timeout you can specify the host and port of any working node in the cluster.

開始使用工具建立叢集.

[[email protected] redis-3.2.0]# /usr/local/redis/bin/redis-trib.rb create --replicas 1 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 127.0.0.1:7006

create //表示建立叢集功能

--replicas 1 //表示為每個主節點自動分配一個從節點.也就是自動分配三個主節點和三個從節點.

>>> Creating cluster

>>> Performing hash slots allocation on 6 nodes...

Using 3 masters:

127.0.0.1:7001

127.0.0.1:7002

127.0.0.1:7003

Adding replica 127.0.0.1:7004 to 127.0.0.1:7001

Adding replica 127.0.0.1:7005 to 127.0.0.1:7002

Adding replica 127.0.0.1:7006 to 127.0.0.1:7003

M: a2eee0ea546f2c3701b08981737c07938039857c 127.0.0.1:7001

   slots:0-5460 (5461 slots) master

M: 8ab3d14eba181c06dc8826bea0db1becdead2533 127.0.0.1:7002

   slots:5461-10922 (5462 slots) master

M: fecbca3b75adc5cf6e79175630ff59c5764962ae 127.0.0.1:7003

   slots:10923-16383 (5461 slots) master

S: 2b577338d01f6be1502c493cd80af719e719ddbd 127.0.0.1:7004

   replicates a2eee0ea546f2c3701b08981737c07938039857c

S: 590e206fe468991138e2fce92b6ccd10a9eddb2e 127.0.0.1:7005

   replicates 8ab3d14eba181c06dc8826bea0db1becdead2533

S: 888b802702cb9304913a0b25d7ff3daf953a4507 127.0.0.1:7006

   replicates fecbca3b75adc5cf6e79175630ff59c5764962ae

Can I set the above configuration? (type 'yes' to accept): yes

>>> Nodes configuration updated

>>> Assign a different config epoch to each node

>>> Sending CLUSTER MEET messages to join the cluster

Waiting for the cluster to join.....

>>> Performing Cluster Check (using node 127.0.0.1:7001)

M: a2eee0ea546f2c3701b08981737c07938039857c 127.0.0.1:7001

   slots:0-5460 (5461 slots) master

M: 8ab3d14eba181c06dc8826bea0db1becdead2533 127.0.0.1:7002

   slots:5461-10922 (5462 slots) master

M: fecbca3b75adc5cf6e79175630ff59c5764962ae 127.0.0.1:7003

   slots:10923-16383 (5461 slots) master

M: 2b577338d01f6be1502c493cd80af719e719ddbd 127.0.0.1:7004

   slots: (0 slots) master

   replicates a2eee0ea546f2c3701b08981737c07938039857c

M: 590e206fe468991138e2fce92b6ccd10a9eddb2e 127.0.0.1:7005

   slots: (0 slots) master

   replicates 8ab3d14eba181c06dc8826bea0db1becdead2533

M: 888b802702cb9304913a0b25d7ff3daf953a4507 127.0.0.1:7006

   slots: (0 slots) master

   replicates fecbca3b75adc5cf6e79175630ff59c5764962ae

[OK] All nodes agree about slots configuration.

>>> Check for open slots...

>>> Check slots coverage...

[OK] All 16384 slots covered.

至此, redis的叢集環境已經搭建完成了,我們可以看一下節點情況.

[[email protected] redis-3.2.0]# /usr/local/redis/bin/redis-cli -p 7002 cluster nodes

2b577338d01f6be1502c493cd80af719e719ddbd 127.0.0.1:7004 slave a2eee0ea546f2c3701b08981737c07938039857c 0 1463476480206 4 connected

fecbca3b75adc5cf6e79175630ff59c5764962ae 127.0.0.1:7003 master - 0 1463476479204 3 connected 10923-16383

a2eee0ea546f2c3701b08981737c07938039857c 127.0.0.1:7001 master - 0 1463476482210 1 connected 0-5460

8ab3d14eba181c06dc8826bea0db1becdead2533 127.0.0.1:7002 myself,master - 0 0 2 connected 5461-10922

590e206fe468991138e2fce92b6ccd10a9eddb2e 127.0.0.1:7005 slave 8ab3d14eba181c06dc8826bea0db1becdead2533 0 1463476481208 5 connected

888b802702cb9304913a0b25d7ff3daf953a4507 127.0.0.1:7006 slave fecbca3b75adc5cf6e79175630ff59c5764962ae 0 1463476477700 6 connected

[[email protected]lhost redis-3.2.0]#

2 新增新節點

當叢集各個節點扛不住業務的需要時,我們可能需要往叢集裡新增新的節點以供業務的正常使用,現在介紹如何新增新節點.

同上面一樣的, 我們先建立兩個例項目錄,一個例項做為新節點的主例項,一個例項做為新節點的從例項.

[[email protected] redis-3.2.0]# mkdir -p /data/redis-cluster/{7007,7008}                                     

[[email protected] redis-3.2.0]#

[[email protected] redis-3.2.0]# for dir in /data/redis-cluster/{7007,7008} ; do cp -v redis.conf $dir ; done

`redis.conf' -> `/data/redis-cluster/7007/redis.conf'

`redis.conf' -> `/data/redis-cluster/7008/redis.conf'

[[email protected] redis-3.2.0]#

[[email protected] redis-3.2.0]# for dir in /data/redis-cluster/{7007,7008}; do  sed -r -i "                                                     

[email protected]^\s*port.*@port ${dir##*/}@ ;

[email protected]^\s*daemonize.*@daemonize [email protected] ;

[email protected]^\s*pidfile.*@pidfile /var/run/redis_${dir##*/}[email protected] ;

[email protected]^\s*logfile.*@logfile /var/log/redis_${d##*/}[email protected];

[email protected]^\s*#?\s*cluster-enabled.*@cluster-enabled [email protected];

[email protected]^\s*#?\s*cluster-config-file.*@cluster-config-file $dir/nodes-${dir##*/}[email protected];

[email protected]^\s*#?\s*cluster-node-timeout.*@cluster-node-timeout [email protected];

[email protected]^\s*dir.*@dir [email protected];

" $dir/redis.conf; done

[[email protected] redis-3.2.0]#

 

接下來啟動這兩個例項.

[[email protected] redis-3.2.0]# for dir in /data/redis-cluster/{7007,7008} ; do /usr/local/redis/bin/redis-server $dir/redis.conf ; done

 

我們看到啟動成功了, 但是這兩個例項還沒有加入到叢集,我們現在要把它加入到叢集裡邊兒來,這又用到了redis-trib.rb這個工具.

[[email protected] redis-3.2.0]# /usr/local/redis/bin/redis-trib.rb add-node 127.0.0.1:7007127.0.0.1:7001

PS:這個IP:PORT可以是叢集裡邊兒任意一個主節點的IP和埠,下同

>>> Adding node 127.0.0.1:7007 to cluster 127.0.0.1:7001

>>> Performing Cluster Check (using node 127.0.0.1:7001)

M: a2eee0ea546f2c3701b08981737c07938039857c 127.0.0.1:7001

   slots:0-5460 (5461 slots) master

   1 additional replica(s)

S: 888b802702cb9304913a0b25d7ff3daf953a4507 127.0.0.1:7006

   slots: (0 slots) slave

   replicates fecbca3b75adc5cf6e79175630ff59c5764962ae

S: 2b577338d01f6be1502c493cd80af719e719ddbd 127.0.0.1:7004

   slots: (0 slots) slave

   replicates a2eee0ea546f2c3701b08981737c07938039857c

S: 590e206fe468991138e2fce92b6ccd10a9eddb2e 127.0.0.1:7005

   slots: (0 slots) slave

   replicates 8ab3d14eba181c06dc8826bea0db1becdead2533

M: fecbca3b75adc5cf6e79175630ff59c5764962ae 127.0.0.1:7003

   slots:10923-16383 (5461 slots) master

   1 additional replica(s)

M: 8ab3d14eba181c06dc8826bea0db1becdead2533 127.0.0.1:7002

   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 127.0.0.1:7007 to make it join the cluster.

[OK] New node added correctly.

[[email protected] redis-3.2.0]#

再看下叢集節點, 發現7007這個例項已經做為主節點加到叢集裡邊兒來了.

[[email protected] redis-3.2.0]# /usr/local/redis/bin/redis-cli -c -p 7002 cluster nodes                     

36d53c7f1896838249c0b4afdcf680bac2f4ec2e 127.0.0.1:7007 master - 0 1463476564369 0 connected

2b577338d01f6be1502c493cd80af719e719ddbd 127.0.0.1:7004 slave a2eee0ea546f2c3701b08981737c07938039857c 0 1463476564870 4 connected

fecbca3b75adc5cf6e79175630ff59c5764962ae 127.0.0.1:7003 master - 0 1463476567376 3 connected 10923-16383

a2eee0ea546f2c3701b08981737c07938039857c 127.0.0.1:7001 master - 0 1463476566374 1 connected 0-5460

8ab3d14eba181c06dc8826bea0db1becdead2533 127.0.0.1:7002 myself,master - 0 0 2 connected 5461-10922

590e206fe468991138e2fce92b6ccd10a9eddb2e 127.0.0.1:7005 slave 8ab3d14eba181c06dc8826bea0db1becdead2533 0 1463476565371 5 connected

888b802702cb9304913a0b25d7ff3daf953a4507 127.0.0.1:7006 slave fecbca3b75adc5cf6e79175630ff59c5764962ae 0 1463476568378 6 connected

[[email protected] redis-3.2.0]#

接下來還要把7008做為7007的從節點也加入到叢集裡邊兒來,這裡的話我們首先要記住7007這個主節點的節點id.從節點加入到叢集的時候要用到.

[[email protected] redis-3.2.0]# /usr/local/redis/bin/redis-trib.rb add-node --slave --master-id 36d53c7f1896838249c0b4afdcf680bac2f4ec2e 127.0.0.1:7008 127.0.0.1:7001

>>> Adding node 127.0.0.1:7008 to cluster 127.0.0.1:7001

>>> Performing Cluster Check (using node 127.0.0.1:7001)

M: a2eee0ea546f2c3701b08981737c07938039857c 127.0.0.1:7001

   slots:0-5460 (5461 slots) master

   1 additional replica(s)

M: 36d53c7f1896838249c0b4afdcf680bac2f4ec2e 127.0.0.1:7007

   slots: (0 slots) master

   0 additional replica(s)

S: 888b802702cb9304913a0b25d7ff3daf953a4507 127.0.0.1:7006

   slots: (0 slots) slave

   replicates fecbca3b75adc5cf6e79175630ff59c5764962ae

S: 2b577338d01f6be1502c493cd80af719e719ddbd 127.0.0.1:7004

   slots: (0 slots) slave

   replicates a2eee0ea546f2c3701b08981737c07938039857c

S: 590e206fe468991138e2fce92b6ccd10a9eddb2e 127.0.0.1:7005

   slots: (0 slots) slave

   replicates 8ab3d14eba181c06dc8826bea0db1becdead2533

M: fecbca3b75adc5cf6e79175630ff59c5764962ae 127.0.0.1:7003

   slots:10923-16383 (5461 slots) master

   1 additional replica(s)

M: 8ab3d14eba181c06dc8826bea0db1becdead2533 127.0.0.1:7002

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

Waiting for the cluster to join.

>>> Configure node as replica of 127.0.0.1:7007.

[OK] New node added correctly.

[[email protected] redis-3.2.0]#

[[email protected] redis-3.2.0]# /usr/local/redis/bin/redis-cli -c -p 7002 cluster nodes

36d53c7f1896838249c0b4afdcf680bac2f4ec2e 127.0.0.1:7007 master - 0 1463476686650 7 connected

2b577338d01f6be1502c493cd80af719e719ddbd 127.0.0.1:7004 slave a2eee0ea546f2c3701b08981737c07938039857c 0 1463476682641 4 connected

fecbca3b75adc5cf6e79175630ff59c5764962ae 127.0.0.1:7003 master - 0 1463476685647 3 connected 10923-16383

a2eee0ea546f2c3701b08981737c07938039857c 127.0.0.1:7001 master - 0 1463476684646 1 connected 0-5460

8ab3d14eba181c06dc8826bea0db1becdead2533 127.0.0.1:7002 myself,master - 0 0 2 connected 5461-10922

590e206fe468991138e2fce92b6ccd10a9eddb2e 127.0.0.1:7005 slave 8ab3d14eba181c06dc8826bea0db1becdead2533 0 1463476684645 5 connected

888b802702cb9304913a0b25d7ff3daf953a4507 127.0.0.1:7006 slave fecbca3b75adc5cf6e79175630ff59c5764962ae 0 1463476681639 6 connected

66c0169faa7a00da01ddb681dcc56cae730e2ced 127.0.0.1:7008 slave 36d53c7f1896838249c0b4afdcf680bac2f4ec2e 0 1463476683643 7 connected

[[email protected] redis-3.2.0]#

可以看到 7008 已經做為一個從節點加入到了叢集.

從圖中可以看到, 7007 這個節點例項並沒有分配slots.加入到叢集的新節點,我們還需要給它分配slots,才能真正用於叢集存取資料,否則加進來沒有任何意義.

3 重新分片

所謂重新分片,就是重新分配各個節點擁有的slots,這裡我們的主要目的是從老的節點中遷移一部分slots放到新節點中去,以便讓新節點真正成為叢集中的一員.

同樣, 還是利用redis-trib.rb 工具.

[[email protected] redis-3.2.0]# /usr/local/redis/bin/redis-trib.rb reshard 127.0.0.1:7001

//PS: 這條命令是互動的,按照提示操作即可.

>>> Performing Cluster Check (using node 127.0.0.1:7001)

M: a2eee0ea546f2c3701b08981737c07938039857c 127.0.0.1:7001

   slots:0-5460 (5461 slots) master

   1 additional replica(s)

M: 36d53c7f1896838249c0b4afdcf680bac2f4ec2e 127.0.0.1:7007

   slots: (0 slots) master

   1 additional replica(s)

S: 888b802702cb9304913a0b25d7ff3daf953a4507 127.0.0.1:7006

   slots: (0 slots) slave

   replicates fecbca3b75adc5cf6e79175630ff59c5764962ae

S: 2b577338d01f6be1502c493cd80af719e719ddbd 127.0.0.1:7004

   slots: (0 slots) slave

   replicates a2eee0ea546f2c3701b08981737c07938039857c

S: 66c0169faa7a00da01ddb681dcc56cae730e2ced 127.0.0.1:7008

   slots: (0 slots) slave

   replicates 36d53c7f1896838249c0b4afdcf680bac2f4ec2e

S: 590e206fe468991138e2fce92b6ccd10a9eddb2e 127.0.0.1:7005

   slots: (0 slots) slave

   replicates 8ab3d14eba181c06dc8826bea0db1becdead2533

M: fecbca3b75adc5cf6e79175630ff59c5764962ae 127.0.0.1:7003

   slots:10923-16383 (5461 slots) master

   1 additional replica(s)

M: 8ab3d14eba181c06dc8826bea0db1becdead2533 127.0.0.1:7002

   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.

How many slots do you want to move (from 1 to 16384)?4096  //輸入一個數,這個4096表示遷移多少個slots

What is the receiving node ID? 36d53c7f1896838249c0b4afdcf680bac2f4ec2e//輸入目標節點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 //輸入all表示從老的所有節點進行重分配,湊夠4096slots給到新節點.

也可以輸入源節點id, 可以輸入多個源節點id,最後輸入done.就開始從你輸入的源節點id的節點進行遷移了.

Ready to move 4096 slots.

  Source nodes:

    M: a2eee0ea546f2c3701b08981737c07938039857c 127.0.0.1:7001

   slots:0-5460 (5461 slots) master

   1 additional replica(s)

    M: fecbca3b75adc5cf6e79175630ff59c5764962ae 127.0.0.1:7003

   slots:10923-16383 (5461 slots) master

   1 additional replica(s)

    M: 8ab3d14eba181c06dc8826bea0db1becdead2533 127.0.0.1:7002

   slots:5461-10922 (5462 slots) master

   1 additional replica(s)

  Destination node:

    M: 36d53c7f1896838249c0b4afdcf680bac2f4ec2e 127.0.0.1:7007

   slots: (0 slots) master

   1 additional replica(s)

  Resharding plan:

    Moving slot 5461 from 8ab3d14eba181c06dc8826bea0db1becdead2533

    Moving slot 5462 from 8ab3d14eba181c06dc8826bea0db1becdead2533

    Moving slot 5463 from 8ab3d14eba181c06dc8826bea0db1becdead2533

    Moving slot 5464 from 8ab3d14eba181c06dc8826bea0db1becdead2533

    Moving slot 5465 from 8ab3d14eba181c06dc8826bea0db1becdead2533

    Moving slot 5466 from 8ab3d14eba181c06dc8826bea0db1becdead2533

......

當然也可以採用非互動自動的進行遷移, :

[[email protected] redis-3.2.0]# /usr/local/redis/bin/redis-trib.rb reshard --from a2eee0ea546f2c3701b08981737c07938039857c --to 36d53c7f1896838249c0b4afdcf680bac2f4ec2e --slots 45 --yes 127.0.0.1:7001

--from a2eee0ea546f2c3701b08981737c07938039857c //表示從哪個節點進行遷移,即源節點

--to 36d53c7f1896838249c0b4afdcf680bac2f4ec2e //遷移到哪個節點,即目標節點

--slots 45 //遷移多少slot

--yes //無需輸入yes確認

不過非互動的遷移就不能使用多個源節點進行遷移slots.

4 刪除節點

要刪除叢集中的某個節點(:這裡說的是叢集中的主節點),首先必須確保這個節點沒有擁有任何一個slots.我們現在來刪除7001 這個節點.

首先,我們要把7001這個節點上的slots全部遷移出去.

[[email protected] redis-3.2.0]# /usr/local/redis/bin/redis-cli -c -p 7002 cluster nodes

36d53c7f1896838249c0b4afdcf680bac2f4ec2e 127.0.0.1:7007 master - 0 1463477003339 7 connected 0-1364 5461-6826 10923-12287

2b577338d01f6be1502c493cd80af719e719ddbd 127.0.0.1:7004 slave a2eee0ea546f2c3701b08981737c07938039857c 0 1463477000332 4 connected

fecbca3b75adc5cf6e79175630ff59c5764962ae 127.0.0.1:7003 master - 0 1463477002336 3 connected 12288-16383

a2eee0ea546f2c3701b08981737c07938039857c 127.0.0.1:7001 master - 0 1463477001334 1 connected 1365-5460

8ab3d14eba181c06dc8826bea0db1becdead2533 127.0.0.1:7002 myself,master - 0 0 2 connected 6827-10922

590e206fe468991138e2fce92b6ccd10a9eddb2e 127.0.0.1:7005 slave 8ab3d14eba181c06dc8826bea0db1becdead2533 0 1463477004340 5 connected

888b802702cb9304913a0b25d7ff3daf953a4507 127.0.0.1:7006 slave fecbca3b75adc5cf6e79175630ff59c5764962ae 0 1463476999831 6 connected

66c0169faa7a00da01ddb681dcc56cae730e2ced 127.0.0.1:7008 slave 36d53c7f1896838249c0b4afdcf680bac2f4ec2e 0 1463477004841 7 connected

[[email protected] redis-3.2.0]#

[[email protected] redis-3.2.0]#

[[email protected] redis-3.2.0]#

我們直接執行命令刪除看看.

[[email protected] redis-3.2.0]# /usr/local/redis/bin/redis-trib.rb del-node 127.0.0.1:7001 a2eee0ea546f2c3701b08981737c07938039857c

>>> Removing node a2eee0ea546f2c3701b08981737c07938039857c from cluster 127.0.0.1:7001

[ERR] Node 127.0.0.1:7001 is not empty! Reshard data away and try again.

[[email protected] redis-3.2.0]#

由此可知,直接刪會提示不為空,不能刪除,分片後再刪吧.把這個節點擁有的slots全部遷移出去即可.

[[email protected] redis-3.2.0]# /usr/local/redis/bin/redis-trib.rb reshard 127.0.0.1:7001

>>> Performing Cluster Check (using node 127.0.0.1:7001)

M: a2eee0ea546f2c3701b08981737c07938039857c 127.0.0.1:7001

   slots:1365-5460 (4096 slots) master

   1 additional replica(s)

M: 36d53c7f1896838249c0b4afdcf680bac2f4ec2e 127.0.0.1:7007

   slots:0-1364,5461-6826,10923-12287 (4096 slots) master

   1 additional replica(s)

S: 888b802702cb9304913a0b25d7ff3daf953a4507 127.0.0.1:7006

   slots: (0 slots) slave

   replicates fecbca3b75adc5cf6e79175630ff59c5764962ae

S: 2b577338d01f6be1502c493cd80af719e719ddbd 127.0.0.1:7004

   slots: (0 slots) slave

   replicates a2eee0ea546f2c3701b08981737c07938039857c

S: 66c0169faa7a00da01ddb681dcc56cae730e2ced 127.0.0.1:7008

   slots: (0 slots) slave

   replicates 36d53c7f1896838249c0b4afdcf680bac2f4ec2e

S: 590e206fe468991138e2fce92b6ccd10a9eddb2e 127.0.0.1:7005

   slots: (0 slots) slave

   replicates 8ab3d14eba181c06dc8826bea0db1becdead2533

M: fecbca3b75adc5cf6e79175630ff59c5764962ae 127.0.0.1:7003

   slots:12288-16383 (4096 slots) master

   1 additional replica(s)

M: 8ab3d14eba181c06dc8826bea0db1becdead2533 127.0.0.1:7002

   slots:6827-10922 (4096 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.

How many slots do you want to move (from 1 to 16384)?16384 //輸入一個大於或等於7001節點所擁有的slots數的數即可.

What is the receiving node ID? 8ab3d14eba181c06dc8826bea0db1becdead2533 //接收這些slots的目標節點,這裡是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:a2eee0ea546f2c3701b08981737c07938039857c //因為我們要刪除7001這個節點,所以源節點的id就是7001的節點ID

Source node #2:done //輸入done,回車,就會開始從7001 這個節點遷移16384slot(沒有這麼多就遷移擁有的全部)7002節點中去.

Ready to move 16384 slots.

  Source nodes:

    M: a2eee0ea546f2c3701b08981737c07938039857c 127.0.0.1:7001

   slots:1365-5460 (4096 slots) master

   1 additional replica(s)

  Destination node:

    M: 8ab3d14eba181c06dc8826bea0db1becdead2533 127.0.0.1:7002

   slots:6827-10922 (4096 slots) master

   1 additional replica(s)

  Resharding plan:

    Moving slot 1365 from a2eee0ea546f2c3701b08981737c07938039857c

    Moving slot 1366 from a2eee0ea546f2c3701b08981737c07938039857c

    Moving slot 1367 from a2eee0ea546f2c3701b08981737c07938039857c

    Moving slot 1368 from a2eee0ea546f2c3701b08981737c07938039857c

    Moving slot 1369 from a2eee0ea546f2c3701b08981737c07938039857c

    Moving slot 1370 from a2eee0ea546f2c3701b08981737c07938039857c

....

.

.

Moving slot 5457 from 127.0.0.1:7001 to 127.0.0.1:7002:

Moving slot 5458 from 127.0.0.1:7001 to 127.0.0.1:7002:

Moving slot 5459 from 127.0.0.1:7001 to 127.0.0.1:7002:

Moving slot 5460 from 127.0.0.1:7001 to 127.0.0.1:7002:

再看各個節點的狀態:

[[email protected] redis-3.2.0]# /usr/local/redis/bin/redis-cli -c -p 7002 cluster nodes                                            

36d53c7f1896838249c0b4afdcf680bac2f4ec2e 127.0.0.1:7007 master - 0 1463477346180 7 connected 0-1364 5461-6826 10923-12287

2b577338d01f6be1502c493cd80af719e719ddbd 127.0.0.1:7004 slave 8ab3d14eba181c06dc8826bea0db1becdead2533 0 1463477345178 8 connected

fecbca3b75adc5cf6e79175630ff59c5764962ae 127.0.0.1:7003 master - 0 1463477350189 3 connected 12288-16383

a2eee0ea546f2c3701b08981737c07938039857c 127.0.0.1:7001 master - 0 1463477349186 1 connected

8ab3d14eba181c06dc8826bea0db1becdead2533 127.0.0.1:7002 myself,master - 0 0 8 connected 1365-5460 6827-10922

590e206fe468991138e2fce92b6ccd10a9eddb2e 127.0.0.1:7005 slave 8ab3d14eba181c06dc8826bea0db1becdead2533 0 1463477347182 8 connected

888b802702cb9304913a0b25d7ff3daf953a4507 127.0.0.1:7006 slave fecbca3b75adc5cf6e79175630ff59c5764962ae 0 1463477350189 6 connected

66c0169faa7a00da01ddb681dcc56cae730e2ced 127.0.0.1:7008 slave 36d53c7f1896838249c0b4afdcf680bac2f4ec2e 0 1463477348183 7 connected

[[email protected] redis-3.2.0]#

7001 已經沒有分配slots,可以從叢集中刪除了.

[[email protected] redis-3.2.0]# redis-trib.rb del-node 127.0.0.1:7002 a2eee0ea546f2c3701b08981737c07938039857c                                              

>>> Removing node a2eee0ea546f2c3701b08981737c07938039857c from cluster 127.0.0.1:7002

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

>>> SHUTDOWN the node. //同時停止了程序

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

root     34736 43075  0 05:32 pts/1    00:00:00 grep redis

root     42204     1  0 05:12 ?        00:00:06 redis-server 127.0.0.1:7002 [cluster]           

root     42206     1  0 05:12 ?        00:00:04 redis-server 127.0.0.1:7003 [cluster]           

root     42208     1  0 05:12 ?        00:00:00 redis-server 127.0.0.1:7004 [cluster]           

root     42210     1  0 05:12 ?        00:00:00 redis-server 127.0.0.1:7005 [cluster]           

root     42212     1  0 05:12 ?        00:00:00 redis-server 127.0.0.1:7006 [cluster]           

root     42214     1  0 05:12 ?        00:00:05 redis-server 127.0.0.1:7007 [cluster]           

root     42216     1  0 05:12 ?        00:00:00 redis-server 127.0.0.1:7008 [cluster]           

[[email protected] redis-3.2.0]#

另外, 經過觀察,這個主節點被刪除之後,它之前擁有的從節點會自動成為其他主節點的從節點.

以上就是利用官方提供的redis-trib.rb 工具來完成上述的各項工作,事實上這個工具也是利用cluster的內部命令進行的整合以方便我們使用和管理.如果想了解更多的細節,需要檢視官方的文件,可以完全脫離redis-trib.rb的便利性純手工使用cluster內部命令來完成叢集的搭建以及分配slots,新增節點,刪除節點等等.

參考文件: