3 .redis集群增加主从节点和删除节点
3 .redis集群增加主从节点和删除节点
海蛎子大呲花 发表于8个月前
3 .redis集群增加主从节点和删除节点
  • 发表于 8个月前
  • 阅读 8
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云 技术升级10大核心产品年终让利>>>   

1. 准备两个redis实例

在centos中有redis8个,其中4个主节点,4个从节点,要加入一个主节点和从节点,首先要像搭建集群一样复制两份redis,分别是redis09和redis10:

Last login: Thu Apr  6 15:50:40 2017 from 10.6.129.1
[root@localhost ~]# cd /usr/local/redis-cluster/
[root@localhost redis-cluster]# ll
总用量 96
drwxr-xr-x. 2 root root  4096 4月   6 12:08 redis01
drwxr-xr-x. 2 root root  4096 4月   6 04:22 redis02
drwxr-xr-x. 2 root root  4096 4月   6 01:12 redis03
drwxr-xr-x. 2 root root  4096 4月   6 12:08 redis04
drwxr-xr-x. 2 root root  4096 4月   6 04:22 redis05
drwxr-xr-x. 2 root root  4096 4月   6 01:12 redis06
drwxr-xr-x. 2 root root  4096 4月   6 15:51 redis07
drwxr-xr-x. 2 root root  4096 4月   6 15:51 redis08
-rwxr-xr-x. 1 root root 60852 4月   5 19:41 redis-trib.rb
-rwxr-xr-x. 1 root root   263 4月   5 18:52 start-all.sh
[root@localhost redis-cluster]# cd redis01
[root@localhost redis01]# ./redis-cli -p 7001 cluster nodes
778121abaff573e97bf8eb4d2765d00a3a495488 10.6.129.245:7005 slave 1ffd86e3f5ed13c8c5ed11c3d928beda10e52536 0 1491465762702 5 connected
4e861f006713502ab9163811b1b729c6d0a14b2e 10.6.129.245:7008 slave ec719ef8b430ce27b88aede499eb591eceae456b 0 1491465761692 8 connected
ec719ef8b430ce27b88aede499eb591eceae456b 10.6.129.245:7007 master - 0 1491465757659 8 connected 0-332 5461-5794 10923-11255
9039f35bf3f6101db773ed54e16c12be83116ef9 10.6.129.245:7006 slave 56840146f8b2b94bbfa58addded014794d5f8994 0 1491465757154 6 connected
56840146f8b2b94bbfa58addded014794d5f8994 10.6.129.245:7003 master - 0 1491465760685 3 connected 11256-16383
1ffd86e3f5ed13c8c5ed11c3d928beda10e52536 10.6.129.245:7002 master - 0 1491465755639 2 connected 5795-10922
d081fae080f6ce819d5e0f38b1baa1ce12334a07 10.6.129.245:7004 slave ca8a2b3a67912f85853a21b5038eff2d106e9771 0 1491465761189 4 connected
ca8a2b3a67912f85853a21b5038eff2d106e9771 10.6.129.245:7001 myself,master - 0 0 1 connected 333-5460
[root@localhost redis01]# 
[root@localhost local]# cp -r redis/bin /usr/local/redis-cluster/redis09
[root@localhost local]# cp -r redis/bin /usr/local/redis-cluster/redis10
[root@localhost local]# cd redis-cluster/
[root@localhost redis-cluster]# ll
总用量 104
drwxr-xr-x. 2 root root  4096 4月   6 12:08 redis01
drwxr-xr-x. 2 root root  4096 4月   6 04:22 redis02
drwxr-xr-x. 2 root root  4096 4月   6 01:12 redis03
drwxr-xr-x. 2 root root  4096 4月   6 12:08 redis04
drwxr-xr-x. 2 root root  4096 4月   6 04:22 redis05
drwxr-xr-x. 2 root root  4096 4月   6 01:12 redis06
drwxr-xr-x. 2 root root  4096 4月   6 15:51 redis07
drwxr-xr-x. 2 root root  4096 4月   6 15:51 redis08
drwxr-xr-x. 2 root root  4096 4月   6 16:06 redis09
drwxr-xr-x. 2 root root  4096 4月   6 16:06 redis10
-rwxr-xr-x. 1 root root 60852 4月   5 19:41 redis-trib.rb
-rwxr-xr-x. 1 root root   263 4月   5 18:52 start-all.sh

删除两个实例下的aof、rdb文件。确保两个实例是足够干净的。并通过redis.conf修改端口号,把cluster的注释放开。启动两个实例:

[root@localhost redis10]# cd ../redis09
[root@localhost redis09]# ./redis-server redis.conf 
[root@localhost redis09]# cd ../redis10
[root@localhost redis10]# ./redis-server redis.conf 
[root@localhost redis10]# ps -aux|grep redis
Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.8/FAQ
root       3372  0.1  0.9 137620  9808 ?        Ssl  Apr05   1:30 ./redis-server 0.0.0.0:7001 [cluster]
root       3376  0.1  0.9 137620  9828 ?        Ssl  Apr05   1:30 ./redis-server 0.0.0.0:7002 [cluster]
root       3380  0.1  0.9 137620  9820 ?        Ssl  Apr05   1:28 ./redis-server 0.0.0.0:7003 [cluster]
root       3384  0.1  0.7 137620  7760 ?        Ssl  Apr05   1:28 ./redis-server 0.0.0.0:7004 [cluster]
root       3388  0.1  0.7 137620  7788 ?        Ssl  Apr05   1:29 ./redis-server 0.0.0.0:7005 [cluster]
root       3392  0.1  0.7 137620  7780 ?        Ssl  Apr05   1:27 ./redis-server 0.0.0.0:7006 [cluster]
root      12332  0.1  0.9 135572  9792 ?        Ssl  15:40   0:02 ./redis-server 0.0.0.0:7007 [cluster]
root      12350  0.1  0.7 133524  7736 ?        Ssl  15:43   0:02 ./redis-server 0.0.0.0:7008 [cluster]
root      12550  0.5  0.7 133524  7588 ?        Ssl  16:10   0:00 ./redis-server 0.0.0.0:7009 [cluster]
root      12554  0.1  0.7 133524  7584 ?        Ssl  16:10   0:00 ./redis-server 0.0.0.0:7010 [cluster]
root      12559  0.0  0.0 103260   848 pts/2    S+   16:10   0:00 grep redis

2. 插入节点

./redis-trib.rb add-node 10.6.129.245:7009 10.6.129.245:7008

默认节点是加入主节点的。所以先将7009和7010都加进去。

[root@localhost redis-cluster]# ./redis-trib.rb add-node 10.6.129.245:7009 10.6.129.245:7008
>>> Adding node 10.6.129.245:7009 to cluster 10.6.129.245:7008
>>> Performing Cluster Check (using node 10.6.129.245:7008)
S: 4e861f006713502ab9163811b1b729c6d0a14b2e 10.6.129.245:7008
   slots: (0 slots) slave
   replicates ec719ef8b430ce27b88aede499eb591eceae456b
M: ca8a2b3a67912f85853a21b5038eff2d106e9771 10.6.129.245:7001
   slots:333-5460 (5128 slots) master
   1 additional replica(s)
M: 56840146f8b2b94bbfa58addded014794d5f8994 10.6.129.245:7003
   slots:11256-16383 (5128 slots) master
   1 additional replica(s)
S: 9039f35bf3f6101db773ed54e16c12be83116ef9 10.6.129.245:7006
   slots: (0 slots) slave
   replicates 56840146f8b2b94bbfa58addded014794d5f8994
S: 778121abaff573e97bf8eb4d2765d00a3a495488 10.6.129.245:7005
   slots: (0 slots) slave
   replicates 1ffd86e3f5ed13c8c5ed11c3d928beda10e52536
M: 1ffd86e3f5ed13c8c5ed11c3d928beda10e52536 10.6.129.245:7002
   slots:5795-10922 (5128 slots) master
   1 additional replica(s)
S: d081fae080f6ce819d5e0f38b1baa1ce12334a07 10.6.129.245:7004
   slots: (0 slots) slave
   replicates ca8a2b3a67912f85853a21b5038eff2d106e9771
M: ec719ef8b430ce27b88aede499eb591eceae456b 10.6.129.245:7007
   slots:0-332,5461-5794,10923-11255 (1000 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 10.6.129.245:7009 to make it join the cluster.
[OK] New node added correctly.

3. 分配hash槽

使用redis-trib程序,将集群中的某些哈希槽移动到新节点里面, 这个新节点就成为真正的主节点了。

执行下面的命令对集群中的哈希槽进行移动。

./redis-trib.rb reshard 10.6.129.245:7009

[root@localhost redis-cluster]# ./redis-trib.rb reshard 10.6.129.245:7009
>>> Performing Cluster Check (using node 10.6.129.245:7009)
M: 4ac2c30f1e04adadf92ca1c6113a89f2c292f141 10.6.129.245:7009
   slots:0-29,333-488,5795-5951,11256-11411 (499 slots) master
   0 additional replica(s)
M: 1ffd86e3f5ed13c8c5ed11c3d928beda10e52536 10.6.129.245:7002
   slots:5952-10922 (4971 slots) master
   1 additional replica(s)
S: 4e861f006713502ab9163811b1b729c6d0a14b2e 10.6.129.245:7008
   slots: (0 slots) slave
   replicates ec719ef8b430ce27b88aede499eb591eceae456b
S: 9039f35bf3f6101db773ed54e16c12be83116ef9 10.6.129.245:7006
   slots: (0 slots) slave
   replicates 56840146f8b2b94bbfa58addded014794d5f8994
S: 778121abaff573e97bf8eb4d2765d00a3a495488 10.6.129.245:7005
   slots: (0 slots) slave
   replicates 1ffd86e3f5ed13c8c5ed11c3d928beda10e52536
M: ca8a2b3a67912f85853a21b5038eff2d106e9771 10.6.129.245:7001
   slots:489-5460 (4972 slots) master
   1 additional replica(s)
M: 56840146f8b2b94bbfa58addded014794d5f8994 10.6.129.245:7003
   slots:11412-16383 (4972 slots) master
   1 additional replica(s)
S: d081fae080f6ce819d5e0f38b1baa1ce12334a07 10.6.129.245:7004
   slots: (0 slots) slave
   replicates ca8a2b3a67912f85853a21b5038eff2d106e9771
M: dadcd2c3ec798f6756057207a9c7f0ce31791938 10.6.129.245:7010
   slots: (0 slots) master
   0 additional replica(s)
M: ec719ef8b430ce27b88aede499eb591eceae456b 10.6.129.245:7007
   slots:30-332,5461-5794,10923-11255 (970 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)? 

移动500个槽,然后会提示“What is the receiving node ID?”把7001的ID输入进去。然后会提示是否

“source node”意思是那些node分配转移,输入all 表示从所有的主节点中随机转移,凑够500个哈希槽

然后再输入yes,redis集群就开始分配哈希槽了。

4. 加入从节点

7010的节点是主节点加入,但是7010的节点没有分配hash槽是没意义的。

[root@localhost redis01]# ./redis-cli -p 7001 cluster nodes
778121abaff573e97bf8eb4d2765d00a3a495488 10.6.129.245:7005 slave 1ffd86e3f5ed13c8c5ed11c3d928beda10e52536 0 1491466958627 5 connected
4e861f006713502ab9163811b1b729c6d0a14b2e 10.6.129.245:7008 slave ec719ef8b430ce27b88aede499eb591eceae456b 0 1491466954588 8 connected
ec719ef8b430ce27b88aede499eb591eceae456b 10.6.129.245:7007 master - 0 1491466954082 8 connected 30-332 5461-5794 10923-11255
d081fae080f6ce819d5e0f38b1baa1ce12334a07 10.6.129.245:7004 slave ca8a2b3a67912f85853a21b5038eff2d106e9771 0 1491466956607 4 connected
4ac2c30f1e04adadf92ca1c6113a89f2c292f141 10.6.129.245:7009 master - 0 1491466959637 9 connected 0-29 333-488 5795-5951 11256-11411
dadcd2c3ec798f6756057207a9c7f0ce31791938 10.6.129.245:7010 master - 0 1491466955597 0 connected
56840146f8b2b94bbfa58addded014794d5f8994 10.6.129.245:7003 master - 0 1491466957617 3 connected 11412-16383
9039f35bf3f6101db773ed54e16c12be83116ef9 10.6.129.245:7006 slave 56840146f8b2b94bbfa58addded014794d5f8994 0 1491466956607 6 connected
ca8a2b3a67912f85853a21b5038eff2d106e9771 10.6.129.245:7001 myself,master - 0 0 1 connected 489-5460
1ffd86e3f5ed13c8c5ed11c3d928beda10e52536 10.6.129.245:7002 master - 0 1491466958122 2 connected 5952-10922

可以看出7010是master的。

把7010节点作为7009从节点:

./redis-cli -p 7010 cluster replicate 4ac2c30f1e04adadf92ca1c6113a89f2c292f141

[root@localhost redis01]# ./redis-cli -p 7010 cluster replicate 4ac2c30f1e04adadf92ca1c6113a89f2c292f141
OK
[root@localhost redis01]# ./redis-cli -p 7001 cluster nodes
778121abaff573e97bf8eb4d2765d00a3a495488 10.6.129.245:7005 slave 1ffd86e3f5ed13c8c5ed11c3d928beda10e52536 0 1491467178093 5 connected
4e861f006713502ab9163811b1b729c6d0a14b2e 10.6.129.245:7008 slave ec719ef8b430ce27b88aede499eb591eceae456b 0 1491467171536 8 connected
ec719ef8b430ce27b88aede499eb591eceae456b 10.6.129.245:7007 master - 0 1491467173555 8 connected 30-332 5461-5794 10923-11255
d081fae080f6ce819d5e0f38b1baa1ce12334a07 10.6.129.245:7004 slave ca8a2b3a67912f85853a21b5038eff2d106e9771 0 1491467176579 4 connected
4ac2c30f1e04adadf92ca1c6113a89f2c292f141 10.6.129.245:7009 master - 0 1491467177588 9 connected 0-29 333-488 5795-5951 11256-11411
dadcd2c3ec798f6756057207a9c7f0ce31791938 10.6.129.245:7010 slave 4ac2c30f1e04adadf92ca1c6113a89f2c292f141 0 1491467172545 9 connected
56840146f8b2b94bbfa58addded014794d5f8994 10.6.129.245:7003 master - 0 1491467175570 3 connected 11412-16383
9039f35bf3f6101db773ed54e16c12be83116ef9 10.6.129.245:7006 slave 56840146f8b2b94bbfa58addded014794d5f8994 0 1491467174564 6 connected
ca8a2b3a67912f85853a21b5038eff2d106e9771 10.6.129.245:7001 myself,master - 0 0 1 connected 489-5460
1ffd86e3f5ed13c8c5ed11c3d928beda10e52536 10.6.129.245:7002 master - 0 1491467177083 2 connected 5952-10922

5.  删除主节点和从节点

删除主节点:先reshard + 后del-node(因为主节点存在hash槽,所以要先都移出来)

删除从节点:直接del-node(从节点没有hash槽)

共有 人打赏支持
粉丝 5
博文 44
码字总数 16098
×
海蛎子大呲花
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: