redis叢集節點新增、刪除、重新分配slot實戰
1、搭建好redis叢集
參考我的博文 http://blog.csdn.net/nuli888/article/details/52134117
下面操作都在同臺機子server1 192.168.1.198上操作
2、檢視現有節點
[[email protected] ~]# cd /usr/local/redis-3.0.6/src/
[[email protected] src]# redis-cli -c -p 7000 cluster nodes
6c4f18b9e8729c3ab5d43b00b0bc1e2ee976f299 192.168.1.199:7004 slave 26101db06b5c2d4431ca8308cf43d51f6939b4fc 0 1470480300217 7 connected
ebb27bd0a48b67a4f4e0584be27c1c909944e935 192.168.1.199:7005 slave 2f70e9f2b4a06a846e46d7034a54e0fe6971beea 0 1470480302228 9 connected
e60f49920cf8620927b200b0001892d08067d065 192.168.1.198:7001 master - 0 1470480302226 8 connected 5000-5460
02f1958bd5032caca2fd47a56362c8d562d7e621 192.168.1.199:7003 slave e60f49920cf8620927b200b0001892d08067d065 0 1470480301220 8 connected
2f70e9f2b4a06a846e46d7034a54e0fe6971beea 192.168.1.198:7000 myself,master - 0 0 9 connected 0-4999 5461-15922
26101db06b5c2d4431ca8308cf43d51f6939b4fc 192.168.1.198:7002 master - 0 1470480300217 7 connected 15923-16383
3、增加主節點192.168.1.198:7006
[
[[email protected] cluster]# mkdir 7006
#複製redis配置檔案並把埠改成mkdir 7006
[[email protected] cluster]# cp redis.conf 7006/redis.conf && sed -i "s/7000/7006/g" 7006/redis.conf
[[email protected] cluster]# cd 7006
#啟動redis
[[email protected] 7006]# redis-server redis.conf
[
root 2741 1 0 09:39 ? 00:03:09 redis-server *:7000 [cluster]
root 2747 1 0 09:40 ? 00:03:08 redis-server *:7001 [cluster]
root 2751 1 0 09:40 ? 00:03:07 redis-server *:7002 [cluster]
root 3482 1 0 18:11 ? 00:00:00 redis-server *:7006 [cluster]
root 3486 2687 0 18:11 pts/0 00:00:00 grep redis
[
#增加主節點操作192.168.1.198:7000是任意的舊節點
[[email protected] src]# ./redis-trib.rb add-node 192.168.1.198:7006 192.168.1.198:7000
>>> Adding node 192.168.1.198:7006 to cluster 192.168.1.198:7000
>>> Performing Cluster Check (using node 192.168.1.198:7000)
M: 2f70e9f2b4a06a846e46d7034a54e0fe6971beea 192.168.1.198:7000
slots:5461-10922 (5462 slots) master
1 additional replica(s)
S: 6c4f18b9e8729c3ab5d43b00b0bc1e2ee976f299 192.168.1.199:7004
slots: (0 slots) slave
replicates 26101db06b5c2d4431ca8308cf43d51f6939b4fc
S: ebb27bd0a48b67a4f4e0584be27c1c909944e935 192.168.1.199:7005
slots: (0 slots) slave
replicates 2f70e9f2b4a06a846e46d7034a54e0fe6971beea
M: e60f49920cf8620927b200b0001892d08067d065 192.168.1.198:7001
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: 02f1958bd5032caca2fd47a56362c8d562d7e621 192.168.1.199:7003
slots: (0 slots) slave
replicates e60f49920cf8620927b200b0001892d08067d065
M: 26101db06b5c2d4431ca8308cf43d51f6939b4fc 192.168.1.198:7002
slots:10923-16383 (5461 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.1.198:7006 to make it join the cluster.
[OK] New node added correctly.
#檢視叢集節點情況
[[email protected] src]# redis-cli -c -p 7000 cluster nodes
6c4f18b9e8729c3ab5d43b00b0bc1e2ee976f299 192.168.1.199:7004 slave 26101db06b5c2d4431ca8308cf43d51f6939b4fc 0 1470478484964 7 connected
ebb27bd0a48b67a4f4e0584be27c1c909944e935 192.168.1.199:7005 slave 2f70e9f2b4a06a846e46d7034a54e0fe6971beea 0 1470478486472 6 connected
e60f49920cf8620927b200b0001892d08067d065 192.168.1.198:7001 master - 0 1470478484961 8 connected 0-5460
02f1958bd5032caca2fd47a56362c8d562d7e621 192.168.1.199:7003 slave e60f49920cf8620927b200b0001892d08067d065 0 1470478485466 8 connected
4458ef594b310e61bf5f249e8efd707d0da880b0 192.168.1.198:7006 master - 0 1470478485968 0 connected
2f70e9f2b4a06a846e46d7034a54e0fe6971beea 192.168.1.198:7000 myself,master - 0 0 1 connected 5461-10922
26101db06b5c2d4431ca8308cf43d51f6939b4fc 192.168.1.198:7002 master - 0 1470478485968 7 connected 10923-16383
4、增加從節點192.168.1.198:7007
[[email protected] cluster]# cd /usr/local/cluster/
[[email protected] cluster]# mkdir 7007
[[email protected] cluster]# cp redis.conf 7007/redis.conf && sed -i "s/7000/7007/g" 7007/redis.conf
[[email protected] cluster]# cd 7007
[[email protected] 7007]# redis-server redis.conf
[[email protected] 7007]# ps -ef | grep redis
root 2741 1 0 09:39 ? 00:03:11 redis-server *:7000 [cluster]
root 2747 1 0 09:40 ? 00:03:10 redis-server *:7001 [cluster]
root 2751 1 0 09:40 ? 00:03:09 redis-server *:7002 [cluster]
root 3482 1 0 18:11 ? 00:00:01 redis-server *:7006 [cluster]
root 3495 1 0 18:17 ? 00:00:00 redis-server *:7007 [cluster]
root 3499 2687 0 18:17 pts/0 00:00:00 grep redis
[[email protected] 7007]# cd /usr/local/redis-3.0.6/src/
#--slave,表示新增的是從節點
#--master-id 2f70e9f2b4a06a846e46d7034a54e0fe6971beea,主節點的node id,在這裡是7000的node id
#192.168.1.198:7007,新節點
#192.168.1.198:7000叢集任一個舊節點
[[email protected] src]# ./redis-trib.rb add-node --slave --master-id 2f70e9f2b4a06a846e46d7034a54e0fe6971beea 192.168.1.198:7007 192.168.1.198:7000
>>> Adding node 192.168.1.198:7007 to cluster 192.168.1.198:7000
>>> Performing Cluster Check (using node 192.168.1.198:7000)
M: 2f70e9f2b4a06a846e46d7034a54e0fe6971beea 192.168.1.198:7000
slots:0-4999,5461-15922 (15462 slots) master
1 additional replica(s)
S: 6c4f18b9e8729c3ab5d43b00b0bc1e2ee976f299 192.168.1.199:7004
slots: (0 slots) slave
replicates 26101db06b5c2d4431ca8308cf43d51f6939b4fc
S: ebb27bd0a48b67a4f4e0584be27c1c909944e935 192.168.1.199:7005
slots: (0 slots) slave
replicates 2f70e9f2b4a06a846e46d7034a54e0fe6971beea
M: e60f49920cf8620927b200b0001892d08067d065 192.168.1.198:7001
slots:5000-5460 (461 slots) master
1 additional replica(s)
S: 02f1958bd5032caca2fd47a56362c8d562d7e621 192.168.1.199:7003
slots: (0 slots) slave
replicates e60f49920cf8620927b200b0001892d08067d065
M: 4458ef594b310e61bf5f249e8efd707d0da880b0 192.168.1.198:7006
slots: (0 slots) master
0 additional replica(s)
M: 26101db06b5c2d4431ca8308cf43d51f6939b4fc 192.168.1.198:7002
slots:15923-16383 (461 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.1.198:7007 to make it join the cluster.
Waiting for the cluster to join.
>>> Configure node as replica of 192.168.1.198:7000.
[OK] New node added correctly.
#檢視叢集節點
[[email protected] src]# redis-cli -c -p 7000 cluster nodes
6c4f18b9e8729c3ab5d43b00b0bc1e2ee976f299 192.168.1.199:7004 slave 26101db06b5c2d4431ca8308cf43d51f6939b4fc 0 1470483081269 7 connected
ebb27bd0a48b67a4f4e0584be27c1c909944e935 192.168.1.199:7005 slave 2f70e9f2b4a06a846e46d7034a54e0fe6971beea 0 1470483079858 9 connected
e60f49920cf8620927b200b0001892d08067d065 192.168.1.198:7001 master - 0 1470483080765 8 connected 5000-5460
9e5422dac0c715ef6d81bf82994fe909f97ccbff 192.168.1.198:7007 slave 2f70e9f2b4a06a846e46d7034a54e0fe6971beea 0 1470483081771 9 connected
02f1958bd5032caca2fd47a56362c8d562d7e621 192.168.1.199:7003 slave e60f49920cf8620927b200b0001892d08067d065 0 1470483080261 8 connected
2f70e9f2b4a06a846e46d7034a54e0fe6971beea 192.168.1.198:7000 myself,master - 0 0 9 connected 0-4999 5461-15922
4458ef594b310e61bf5f249e8efd707d0da880b0 192.168.1.198:7006 master - 0 1470483080765 0 connected
26101db06b5c2d4431ca8308cf43d51f6939b4fc 192.168.1.198:7002 master - 0 1470483080260 7 connected 15923-16383
5、對主節點重新分配slot
新增加的主節點,是沒有slots的,主節點如果沒有slots的話,存取資料就都不會被選中
可以把分配的過程理解成打撲克牌,all表示大家重新洗牌;輸入某個主節點的node id,然後在輸入done的話,就好比從某個節點,抽牌
[[email protected] src]# ./redis-trib.rb reshard 192.168.1.198:7006
>>> Performing Cluster Check (using node 192.168.1.198:7006)
M: 4458ef594b310e61bf5f249e8efd707d0da880b0 192.168.1.198:7006
slots:0-943,5000-5027,15923-15950 (1000 slots) master
0 additional replica(s)
S: 9e5422dac0c715ef6d81bf82994fe909f97ccbff 192.168.1.198:7007
slots: (0 slots) slave
replicates 2f70e9f2b4a06a846e46d7034a54e0fe6971beea
S: ebb27bd0a48b67a4f4e0584be27c1c909944e935 192.168.1.199:7005
slots: (0 slots) slave
replicates 2f70e9f2b4a06a846e46d7034a54e0fe6971beea
S: 6c4f18b9e8729c3ab5d43b00b0bc1e2ee976f299 192.168.1.199:7004
slots: (0 slots) slave
replicates 26101db06b5c2d4431ca8308cf43d51f6939b4fc
M: 2f70e9f2b4a06a846e46d7034a54e0fe6971beea 192.168.1.198:7000
slots:944-4999,5461-15922 (14518 slots) master
2 additional replica(s)
M: 26101db06b5c2d4431ca8308cf43d51f6939b4fc 192.168.1.198:7002
slots:15951-16383 (433 slots) master
1 additional replica(s)
S: 02f1958bd5032caca2fd47a56362c8d562d7e621 192.168.1.199:7003
slots: (0 slots) slave
replicates e60f49920cf8620927b200b0001892d08067d065
M: e60f49920cf8620927b200b0001892d08067d065 192.168.1.198:7001
slots:5028-5460 (433 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)? 1000
What is the receiving node ID? 4458ef594b310e61bf5f249e8efd707d0da880b0
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 表示從所有的主節點中隨機轉移,湊夠1000個雜湊槽
......
Do you want to proceed with the proposed reshard plan (yes/no)? yes
#輸入yes,redis叢集就開始分配雜湊槽了
......
6、刪除節點
1)192.168.1.198:7007 節點ip和埠
2)9e5422dac0c715ef6d81bf82994fe909f97ccbff 節點的id值
[[email protected] src]# ./redis-trib.rb del-node 192.168.1.198:7007 '9e5422dac0c715ef6d81bf82994fe909f97ccbff'
>>> Removing node 9e5422dac0c715ef6d81bf82994fe909f97ccbff from cluster 192.168.1.198:7007
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.