redis节点管理-新增从节点
原文:http://blog.sina.com.cn/s/blog_53b45c4d0102wg12.html
新增从节点
新增一个节点7008节点,使用add-node --slave命令。
- [root@localhost redis-cluster]# cp -r redis01/ redis08
- [root@localhost redis-cluster]# cd redis08/
- [root@localhost redis08]# sed -i "s/7001/7008/g" ./redis.conf
- [root@localhost redis08]# ./redis-server redis.conf
redis-trib增加从节点的命令为:
- ./redis-trib.rb add-node --slave --master-id $[nodeid] 127.0.0.1:7008 127.0.0.1:7000
nodeid为要加到master主节点的node id,127.0.0.1:7008为新增的从节点,127.0.0.1:7000为集群的一个节点(集群的任意节点都行),用来辨识是哪个集群;如果没有给定那个主节点--master-id的话,redis-trib将会将新增的从节点随机到从节点较少的主节点上。
现在我们添加一下7008,看是否会自动加到没有从节点的7007主节点上。
- [root@localhost redis-cluster]# ./redis-trib.rb add-node --slave 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: dd19221c404fb2fc4da37229de56bab755c76f2b 127.0.0.1:7001
- slots:1365-5460 (4096 slots) master
- 1 additional replica(s)
- M: ee3efb90e5ac0725f15238a64fc60a18a71205d7 127.0.0.1:7007
- slots:0-1364,5461-6826,10923-12287 (4096 slots) master
- 0 additional replica(s)
- M: a5db243087d8bd423b9285fa8513eddee9bb59a6 127.0.0.1:7005
- slots:6827-10922 (4096 slots) master
- 1 additional replica(s)
- S: 8bb3ede48319b46d0015440a91ab277da9353c8b 127.0.0.1:7006
- slots: (0 slots) slave
- replicates f9886c71e98a53270f7fda961e1c5f730382d48f
- M: f9886c71e98a53270f7fda961e1c5f730382d48f 127.0.0.1:7003
- slots:12288-16383 (4096 slots) master
- 1 additional replica(s)
- S: 1f07d76585bfab35f91ec711ac53ab4bc00f2d3a 127.0.0.1:7002
- slots: (0 slots) slave
- replicates a5db243087d8bd423b9285fa8513eddee9bb59a6
- S: 50ce1ea59106b4c2c6bc502593a6a7a7dabf5041 127.0.0.1:7004
- slots: (0 slots) slave
- replicates dd19221c404fb2fc4da37229de56bab755c76f2b
- [OK] All nodes agree about slots configuration.
- >>> Check for open slots...
- >>> Check slots coverage...
- [OK] All 16384 slots covered.
- Automatically selected master 127.0.0.1:7007
- >>> 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.
- [root@localhost redis-cluster]#
可以看到自动选择了127.0.0.1:7007为master主节点,并且添加成功。
可以check一下7008:
- [root@localhost redis-cluster]# ./redis-trib.rb check 127.0.0.1:7008
- >>> Performing Cluster Check (using node 127.0.0.1:7008)
- S: 2ab1b061c36f30ae35604e9a171ae3afdc3c87e5 127.0.0.1:7008
- slots: (0 slots) slave
- replicates ee3efb90e5ac0725f15238a64fc60a18a71205d7
- M: a5db243087d8bd423b9285fa8513eddee9bb59a6 127.0.0.1:7005
- slots:6827-10922 (4096 slots) master
- 1 additional replica(s)
- M: dd19221c404fb2fc4da37229de56bab755c76f2b 127.0.0.1:7001
- slots:1365-5460 (4096 slots) master
- 1 additional replica(s)
- S: 8bb3ede48319b46d0015440a91ab277da9353c8b 127.0.0.1:7006
- slots: (0 slots) slave
- replicates f9886c71e98a53270f7fda961e1c5f730382d48f
- M: ee3efb90e5ac0725f15238a64fc60a18a71205d7 127.0.0.1:7007
- slots:0-1364,5461-6826,10923-12287 (4096 slots) master
- 1 additional replica(s)
- S: 50ce1ea59106b4c2c6bc502593a6a7a7dabf5041 127.0.0.1:7004
- slots: (0 slots) slave
- replicates dd19221c404fb2fc4da37229de56bab755c76f2b
- M: f9886c71e98a53270f7fda961e1c5f730382d48f 127.0.0.1:7003
- slots:12288-16383 (4096 slots) master
- 1 additional replica(s)
- S: 1f07d76585bfab35f91ec711ac53ab4bc00f2d3a 127.0.0.1:7002
- slots: (0 slots) slave
- replicates a5db243087d8bd423b9285fa8513eddee9bb59a6
- [OK] All nodes agree about slots configuration.
- >>> Check for open slots...
- >>> Check slots coverage...
- [OK] All 16384 slots covered.
- [root@localhost redis-cluster]#
可以看到7008作为了7007的从节点。
再测试一下指定主节点添加从节点,给7007增加7009从节点。
- [root@localhost redis-cluster]# cp -r redis01/ redis09
- [root@localhost redis-cluster]# cd redis09
- [root@localhost redis09]# sed -i "s/7001/7009/g" ./redis.conf
- [root@localhost redis09]# ./redis-server redis.conf
添加7007主节点上
- [root@localhost redis-cluster]# ./redis-trib.rb add-node --slave --master-id ee3efb90e5ac0725f15238a64fc60a18a71205d7 127.0.0.1:7009 127.0.0.1:7001
- >>> Adding node 127.0.0.1:7009 to cluster 127.0.0.1:7001
- >>> Performing Cluster Check (using node 127.0.0.1:7001)
- M: dd19221c404fb2fc4da37229de56bab755c76f2b 127.0.0.1:7001
- slots:1365-5460 (4096 slots) master
- 1 additional replica(s)
- S: 2ab1b061c36f30ae35604e9a171ae3afdc3c87e5 127.0.0.1:7008
- slots: (0 slots) slave
- replicates ee3efb90e5ac0725f15238a64fc60a18a71205d7
- M: ee3efb90e5ac0725f15238a64fc60a18a71205d7 127.0.0.1:7007
- slots:0-1364,5461-6826,10923-12287 (4096 slots) master
- 1 additional replica(s)
- M: a5db243087d8bd423b9285fa8513eddee9bb59a6 127.0.0.1:7005
- slots:6827-10922 (4096 slots) master
- 1 additional replica(s)
- S: 8bb3ede48319b46d0015440a91ab277da9353c8b 127.0.0.1:7006
- slots: (0 slots) slave
- replicates f9886c71e98a53270f7fda961e1c5f730382d48f
- M: f9886c71e98a53270f7fda961e1c5f730382d48f 127.0.0.1:7003
- slots:12288-16383 (4096 slots) master
- 1 additional replica(s)
- S: 1f07d76585bfab35f91ec711ac53ab4bc00f2d3a 127.0.0.1:7002
- slots: (0 slots) slave
- replicates a5db243087d8bd423b9285fa8513eddee9bb59a6
- S: 50ce1ea59106b4c2c6bc502593a6a7a7dabf5041 127.0.0.1:7004
- slots: (0 slots) slave
- replicates dd19221c404fb2fc4da37229de56bab755c76f2b
- [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:7009 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.
- [root@localhost redis-cluster]#
显示从节点7009节点添加到7007主节点,可以看一下7007的从节点,如下:
- [root@localhost redis-cluster]# cd ./redis07
- [root@localhost redis07]# ./redis-cli -c -p 7007 cluster nodes | grep ee3efb90e5ac0725f15238a64fc60a18a71205d7
- 1f51443ede952b98724fea2a12f61fe710ab6cb1 127.0.0.1:7009 slave ee3efb90e5ac0725f15238a64fc60a18a71205d7 0 1462962710266 8 connected
- ee3efb90e5ac0725f15238a64fc60a18a71205d7 127.0.0.1:7007 myself,master - 0 0 8 connected 0-1364 5461-6826 10923-12287
- 2ab1b061c36f30ae35604e9a171ae3afdc3c87e5 127.0.0.1:7008 slave ee3efb90e5ac0725f15238a64fc60a18a71205d7 0 1462962711607 8 connected
- [root@localhost redis07]#
maser 7007有2个slave 7008,7009。
我们测试一下7007节点挂掉,看7008和7009那个成为主节点。
- [root@localhost redis-cluster]# ps -ef | grep redis
- root 7950 1 0 12:50 ? 00:02:05 ./redis-server 127.0.0.1:7001 [cluster]
- root 7956 1 0 12:50 ? 00:02:11 ./redis-server 127.0.0.1:7003 [cluster]
- root 7960 1 0 12:50 ? 00:01:47 ./redis-server 127.0.0.1:7004 [cluster]
- root 7964 1 0 12:50 ? 00:02:07 ./redis-server 127.0.0.1:7005 [cluster]
- root 7966 1 0 12:50 ? 00:01:46 ./redis-server 127.0.0.1:7006 [cluster]
- root 12070 1 0 15:14 ? 00:01:08 ./redis-server 127.0.0.1:7002 [cluster]
- root 13441 1 0 16:09 ? 00:01:25 ./redis-server 127.0.0.1:7007 [cluster]
- root 15939 1 0 17:41 ? 00:00:20 ./redis-server 127.0.0.1:7008 [cluster]
- root 16623 1 0 18:07 ? 00:00:10 ./redis-server 127.0.0.1:7009 [cluster]
- root 17295 10581 0 18:37 pts/2 00:00:00 grep --color=auto redis
- [root@localhost redis-cluster]# kill -9 13441
- [root@localhost redis-cluster]# cd ./redis08
- [root@localhost redis08]# ./redis-cli -c -p 7008
- 127.0.0.1:7008> get name
- -> Redirected to slot [5798] located at 127.0.0.1:7009
- "andy"
- 127.0.0.1:7009> cluster nodes
- ee3efb90e5ac0725f15238a64fc60a18a71205d7 127.0.0.1:7007 master,fail - 1462963082317 1462963080194 8 disconnected
- 50ce1ea59106b4c2c6bc502593a6a7a7dabf5041 127.0.0.1:7004 slave dd19221c404fb2fc4da37229de56bab755c76f2b 0 1462963170968 1 connected
- f9886c71e98a53270f7fda961e1c5f730382d48f 127.0.0.1:7003 master - 0 1462963168525 3 connected 12288-16383
- dd19221c404fb2fc4da37229de56bab755c76f2b 127.0.0.1:7001 master - 0 1462963164466 1 connected 1365-5460
- 2ab1b061c36f30ae35604e9a171ae3afdc3c87e5 127.0.0.1:7008 slave 1f51443ede952b98724fea2a12f61fe710ab6cb1 0 1462963167508 9 connected
- 1f51443ede952b98724fea2a12f61fe710ab6cb1 127.0.0.1:7009 myself,master - 0 0 9 connected 0-1364 5461-6826 10923-12287
- 1f07d76585bfab35f91ec711ac53ab4bc00f2d3a 127.0.0.1:7002 slave a5db243087d8bd423b9285fa8513eddee9bb59a6 0 1462963170564 7 connected
- 8bb3ede48319b46d0015440a91ab277da9353c8b 127.0.0.1:7006 slave f9886c71e98a53270f7fda961e1c5f730382d48f 0 1462963167915 3 connected
- a5db243087d8bd423b9285fa8513eddee9bb59a6 127.0.0.1:7005 master - 0 1462963169538 7 connected 6827-10922
- 127.0.0.1:7009>
可以看到7009代替7007成了主节点。
重启7007之后,会自动变成7009的从节点。
- [root@localhost redis-cluster]# cd redis07
- [root@localhost redis07]# ./redis-server redis.conf
- [root@localhost redis07]# cd ../
- [root@localhost redis-cluster]# ./redis-trib.rb check 127.0.0.1:7007
- >>> Performing Cluster Check (using node 127.0.0.1:7007)
- S: ee3efb90e5ac0725f15238a64fc60a18a71205d7 127.0.0.1:7007
- slots: (0 slots) slave
- replicates 1f51443ede952b98724fea2a12f61fe710ab6cb1
- S: 50ce1ea59106b4c2c6bc502593a6a7a7dabf5041 127.0.0.1:7004
- slots: (0 slots) slave
- replicates dd19221c404fb2fc4da37229de56bab755c76f2b
- M: 1f51443ede952b98724fea2a12f61fe710ab6cb1 127.0.0.1:7009
- slots:0-1364,5461-6826,10923-12287 (4096 slots) master
- 2 additional replica(s)
- S: 8bb3ede48319b46d0015440a91ab277da9353c8b 127.0.0.1:7006
- slots: (0 slots) slave
- replicates f9886c71e98a53270f7fda961e1c5f730382d48f
- M: dd19221c404fb2fc4da37229de56bab755c76f2b 127.0.0.1:7001
- slots:1365-5460 (4096 slots) master
- 1 additional replica(s)
- M: a5db243087d8bd423b9285fa8513eddee9bb59a6 127.0.0.1:7005
- slots:6827-10922 (4096 slots) master
- 1 additional replica(s)
- S: 1f07d76585bfab35f91ec711ac53ab4bc00f2d3a 127.0.0.1:7002
- slots: (0 slots) slave
- replicates a5db243087d8bd423b9285fa8513eddee9bb59a6
- M: f9886c71e98a53270f7fda961e1c5f730382d48f 127.0.0.1:7003
- slots:12288-16383 (4096 slots) master
- 1 additional replica(s)
- S: 2ab1b061c36f30ae35604e9a171ae3afdc3c87e5 127.0.0.1:7008
- slots: (0 slots) slave
- replicates 1f51443ede952b98724fea2a12f61fe710ab6cb1
- [OK] All nodes agree about slots configuration.
- >>> Check for open slots...
- >>> Check slots coverage...
- [OK] All 16384 slots covered.
- [root@localhost redis-cluster]#
看完打开支付宝扫一扫领个红包吧!
![](https://files.cnblogs.com/files/shihaiming/zhifubao_red.gif)