Redis集群管理:添加节点

动态添加节点

前提要求

  • 增加 Redis node 节点,需要与之前的 Redis node 版本相同、配置一致,然后分别启动两台 Redis node,因为一主一从。

案例:

  • 因公司业务发展迅猛,现有的三主三从 redis cluster 架构可能无法满足现有业务的并发写入需求,因此公司紧急采购一台服务器 192.168.40.13,需要将其动态添加到集群当中其不能影响业务使用和数据丢失

为新节点安装redis安装方法

我这里是直接将之前安装好的redis打了个包。解压就好了,步骤如下:

[root@centos_13 ~]# mkdir /apps

[root@centos_13 ~]# cd /apps

[root@centos_13 apps]# ll
-rw-r--r-- 1 root  root  4595354 Mar 12 13:38 redis.tar.gz

[root@centos_13 apps]# tar xf redis.tar.gz 

[root@centos_13 apps]# ll
total 4488
drwxr-xr-x 7 redis redis      63 Mar 12 13:40 redis
-rw-r--r-- 1 root  root  4595354 Mar 12 13:38 redis.tar.gz

[root@centos_13 redis]# chown  redis.redis /apps/redis -R   
[root@centos_13 redis]# ll
total 0
drwxr-xr-x 2 redis redis 134 Mar 11 13:06 bin
drwxr-xr-x 2 redis redis  29 Mar 12 13:43 data
drwxr-xr-x 2 redis redis  24 Mar 12 13:43 etc
drwxr-xr-x 2 redis redis  28 Mar 12 13:43 logs
drwxr-xr-x 2 redis redis   6 Mar 11 13:07 run

[root@centos_13 redis]# ln -sv /apps/redis/bin/redis-* /usr/sbin/
。。。
# 启动脚本在安装redis的章节中有
[root@centos_13 redis]# systemctl daemon-reload
[root@centos_13 redis]# systemctl start redis 

切换至管理节点,就是创建集群时使用的那台服务器,我这里是192.168.40.7
使用 redis-trib.rb工具向集群中添加节点,直接回车可以看到帮助用法,使用add-node选项添加

[root@centos_7 ~]# redis-trib.rb 
Usage: redis-trib <command> <options> <arguments ...>
	add-node        new_host:new_port existing_host:existing_port
            --slave
            --master-id <arg>

格式为:redis-trib.rb add-node 新服务器IP:端口 集群中现有的服务器IP:端口
执行:

[root@centos_7 ~]# redis-trib.rb add-node 192.168.40.13:6379 192.168.40.7:6379
>>> Adding node 192.168.40.13:6379 to cluster 192.168.40.7:6379
>>> Performing Cluster Check (using node 192.168.40.7:6379)
M: 84414bb35278fe4608956f35f7d9b79211752a41 192.168.40.7:6379
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
M: 1ab9dba3b9aaeeed396ca9fd875cda0072006347 192.168.40.9:6379
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
M: b617fef3c13eaf16a37c851d8748bdc3d2f97e67 192.168.40.8:6379
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: 1476165d38606139382a2a576ea85648af439e3c 192.168.40.12:6379
   slots: (0 slots) slave
   replicates b617fef3c13eaf16a37c851d8748bdc3d2f97e67
S: 970d1e4ad835f6068f9cbc6152e6879692de4726 192.168.40.10:6379
   slots: (0 slots) slave
   replicates 1ab9dba3b9aaeeed396ca9fd875cda0072006347
S: dfe2c4bf4413b99374c52a4478e15f26a5b7ba30 192.168.40.11:6379
   slots: (0 slots) slave
   replicates 84414bb35278fe4608956f35f7d9b79211752a41
[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.40.13:6379 to make it join the cluster.
[OK] New node added correctly.

添加成功!!!

查看信息:
 可以看到已经添加成功,只是没有槽位

[root@centos_7 ~]# redis-trib.rb info 192.168.40.7:6379
192.168.40.7:6379 (84414bb3...) -> 0 keys | 5461 slots | 1 slaves.
192.168.40.9:6379 (1ab9dba3...) -> 0 keys | 5461 slots | 1 slaves.
192.168.40.13:6379 (2b0f3d3a...) -> 0 keys | 0 slots | 0 slaves.	# 可以看到已经添加成功,只是没有槽位
192.168.40.8:6379 (b617fef3...) -> 1 keys | 5462 slots | 1 slaves.
[OK] 1 keys in 4 masters.
0.00 keys per slot on average.

还需要手动分配槽位,依然使用redis-trib.rb
还是看一下帮助用法,使用 reshard工具为新节点分配槽位

[root@centos_7 ~]# redis-trib.rb 
Usage: redis-trib <command> <options> <arguments ...>
  reshard         host:port
                  --from <arg>
                  --to <arg>
                  --slots <arg>
                  --yes
                  --timeout <arg>
                  --pipeline <arg>

分配时我们要计算清楚,新添加一个节点那么就是四个主节点,redis总共16384个槽位,所以16384除4

[root@centos_9 ~]# echo 16384/4 | bc
4096

开始分配

扫描二维码关注公众号,回复: 9994971 查看本文章
[root@centos_7 ~]# redis-trib.rb reshard 192.168.40.13:6379
>>> Performing Cluster Check (using node 192.168.40.13:6379)
M: 2b0f3d3a03feea11f5fc0798f3e5f2988e7a57a1 192.168.40.13:6379
   slots: (0 slots) master
   0 additional replica(s)
S: 1476165d38606139382a2a576ea85648af439e3c 192.168.40.12:6379
   slots: (0 slots) slave
   replicates b617fef3c13eaf16a37c851d8748bdc3d2f97e67
M: b617fef3c13eaf16a37c851d8748bdc3d2f97e67 192.168.40.8:6379
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
M: 1ab9dba3b9aaeeed396ca9fd875cda0072006347 192.168.40.9:6379
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
M: 84414bb35278fe4608956f35f7d9b79211752a41 192.168.40.7:6379
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: dfe2c4bf4413b99374c52a4478e15f26a5b7ba30 192.168.40.11:6379
   slots: (0 slots) slave
   replicates 84414bb35278fe4608956f35f7d9b79211752a41
S: 970d1e4ad835f6068f9cbc6152e6879692de4726 192.168.40.10:6379
   slots: (0 slots) slave
   replicates 1ab9dba3b9aaeeed396ca9fd875cda0072006347
[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	# 分配的槽位数量
What is the receiving node ID? 2b0f3d3a03feea11f5fc0798f3e5f2988e7a57a1	为哪台设备分配槽位,取他的 ID

Type 'all' to use all the nodes as source nodes for the hash slots.	
# all表示从所有现有的redis设备中提取槽位

Type 'done' once you entered all the source nodes IDs.	
# done表示:在从中挑选出几台后,最后需要输入done表示选择完成

 Source node #1: all	# 输入all
  Moving slot 830 from 84414bb35278fe4608956f35f7d9b79211752a41
  Moving slot 831 from 84414bb35278fe4608956f35f7d9b79211752a41
  Moving slot 832 from 84414bb35278fe4608956f35f7d9b79211752a41
  Moving slot 833 from 84414bb35278fe4608956f35f7d9b79211752a41
  Moving slot 834 from 84414bb35278fe4608956f35f7d9b79211752a41
  Moving slot 835 from 84414bb35278fe4608956f35f7d9b79211752a41
  ...# 省略信息

Do you want to proceed with the proposed reshard plan (yes/no)? yes
.....# 省略信息
Moving slot 5798 from 192.168.40.8:6379 to 192.168.40.13:6379: 
[ERR] Calling MIGRATE: ERR Syntax error, try CLIENT (LIST | KILL | GETNAME | SETNAME | PAUSE | REPLY)

这个报错表示这个5798槽位有数据,无法转移!没关系,我们修复以下集群再创建!!,查看集群信息就可知道这个槽位分在了哪个设备上,然后到那台设备上清除数据

修复操作:清理数据,清楚前可以先备份,注意:redis在添加节点时不能有数据,会添加失败

[root@centos_7 ~]# redis-cli -h 192.168.40.8 -p 6379
192.168.40.8:6379> FLUSHALL
OK
创建前修复以下集群!使用redis-trib.rb fix 192.168.40.8:6379 

[root@centos_7 ~]# redis-trib.rb fix 192.168.40.8:6379     
>>> Performing Cluster Check (using node 192.168.40.8:6379)
M: b617fef3c13eaf16a37c851d8748bdc3d2f97e67 192.168.40.8:6379
   slots:5798-10922 (5125 slots) master
   1 additional replica(s)
S: dfe2c4bf4413b99374c52a4478e15f26a5b7ba30 192.168.40.11:6379
   slots: (0 slots) slave
   replicates 84414bb35278fe4608956f35f7d9b79211752a41
S: 970d1e4ad835f6068f9cbc6152e6879692de4726 192.168.40.10:6379
   slots: (0 slots) slave
   replicates 1ab9dba3b9aaeeed396ca9fd875cda0072006347
M: 84414bb35278fe4608956f35f7d9b79211752a41 192.168.40.7:6379
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
M: 1ab9dba3b9aaeeed396ca9fd875cda0072006347 192.168.40.9:6379
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
M: 2b0f3d3a03feea11f5fc0798f3e5f2988e7a57a1 192.168.40.13:6379
   slots:5461-5797 (337 slots) master
   0 additional replica(s)
S: 1476165d38606139382a2a576ea85648af439e3c 192.168.40.12:6379
   slots: (0 slots) slave
   replicates b617fef3c13eaf16a37c851d8748bdc3d2f97e67
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
[WARNING] Node 192.168.40.8:6379 has slots in migrating state (5798).
[WARNING] Node 192.168.40.13:6379 has slots in importing state (5798).
[WARNING] The following slots are open: 5798
>>> Fixing open slot 5798
Set as migrating in: 192.168.40.8:6379
Set as importing in: 192.168.40.13:6379
Moving slot 5798 from 192.168.40.8:6379 to 192.168.40.13:6379: 
>>> Check slots coverage...
[OK] All 16384 slots covered.

再次添加

[root@centos_7 ~]# redis-trib.rb reshard 192.168.40.13:6379
>>> Performing Cluster Check (using node 192.168.40.13:6379)
M: 2b0f3d3a03feea11f5fc0798f3e5f2988e7a57a1 192.168.40.13:6379
   slots:5461-5798 (338 slots) master			
### 请注意这里!!虽然之前添加失败,但是还是有一部分槽位时分配成功的,最初计划的是每台4096个槽位,那么就需要4096减去这338槽位,得出的数值就是这次添加的槽位数量
   0 additional replica(s)
S: 1476165d38606139382a2a576ea85648af439e3c 192.168.40.12:6379
   slots: (0 slots) slave
   replicates b617fef3c13eaf16a37c851d8748bdc3d2f97e67
M: b617fef3c13eaf16a37c851d8748bdc3d2f97e67 192.168.40.8:6379
   slots:5799-10922 (5124 slots) master
   1 additional replica(s)
M: 1ab9dba3b9aaeeed396ca9fd875cda0072006347 192.168.40.9:6379
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
M: 84414bb35278fe4608956f35f7d9b79211752a41 192.168.40.7:6379
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: dfe2c4bf4413b99374c52a4478e15f26a5b7ba30 192.168.40.11:6379
   slots: (0 slots) slave
   replicates 84414bb35278fe4608956f35f7d9b79211752a41
S: 970d1e4ad835f6068f9cbc6152e6879692de4726 192.168.40.10:6379
   slots: (0 slots) slave
   replicates 1ab9dba3b9aaeeed396ca9fd875cda0072006347
[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)? 3758
What is the receiving node ID? 2b0f3d3a03feea11f5fc0798f3e5f2988e7a57a1
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
  Moving slot 830 from 84414bb35278fe4608956f35f7d9b79211752a41
  Moving slot 831 from 84414bb35278fe4608956f35f7d9b79211752a41
  Moving slot 832 from 84414bb35278fe4608956f35f7d9b79211752a41
  Moving slot 833 from 84414bb35278fe4608956f35f7d9b79211752a41
  Moving slot 834 from 84414bb35278fe4608956f35f7d9b79211752a41
  Moving slot 835 from 84414bb35278fe4608956f35f7d9b79211752a41
  ...# 省略信息
Do you want to proceed with the proposed reshard plan (yes/no)? yes
Moving slot 6996 from 192.168.40.8:6379 to 192.168.40.13:6379: 
Moving slot 6997 from 192.168.40.8:6379 to 192.168.40.13:6379: 
Moving slot 6998 from 192.168.40.8:6379 to 192.168.40.13:6379: 
然后出现大量的分配槽位信息!!!
最后成功!!!

再次查看集群,看看是否为新节点分配好槽位了

[root@centos_7 ~]# redis-trib.rb info 192.168.40.7:6379 
192.168.40.7:6379 (84414bb3...) -> 0 keys | 4183 slots | 1 slaves.
192.168.40.9:6379 (1ab9dba3...) -> 0 keys | 4182 slots | 1 slaves.
192.168.40.13:6379 (2b0f3d3a...) -> 0 keys | 4095 slots | 0 slaves.		# 40.13已经分配了4095个槽位,成功!
192.168.40.8:6379 (b617fef3...) -> 0 keys | 3924 slots | 1 slaves.
[OK] 0 keys in 4 masters.
0.00 keys per slot on average.

虽然槽位分好了,但是新添加的节点没有slave,单节点设备又不能保证高可用,所以我们还需要为它加一个slave!

如果公司财大气粗,可以在买一台服务器那就最好不过了,我这里是在之前的六台设备中随意找一台再开启一个redis进程作为192.168.40.13的slave!

添加步骤:
在192.168.40.10设备上新开启一个redis进程,以端口作为区分

[root@centos_10 ~]# cd /apps/
[root@centos_10 apps]# ls
redis  redis.tar.gz
[root@centos_10 apps]# mkdir redis_6380
[root@centos_10 apps]# ls
redis  redis_6380  redis.tar.gz
[root@centos_10 apps]# mv redis.tar.gz redis_6380/
[root@centos_10 apps]# cd redis_6380/
[root@centos_10 redis_6380]# ls
redis.tar.gz
[root@centos_10 redis_6380]# tar xf redis.tar.gz 
[root@centos_10 redis_6380]# ls
redis  redis.tar.gz
[root@centos_10 redis_6380]# cd redis
# 编辑将文件中的所有6379改为6380
[root@centos_10 redis]# vim etc/redis.conf 
按冒号调出编辑模式,输入 :%s/6379/6380/g
修改完成后启动redis
[root@centos_10 redis]# redis-server /apps/redis_6380/redis/etc/redis.conf 
[root@centos_10 redis]# ss -nlt
State       Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN      0      100    127.0.0.1:25                       *:*                  
LISTEN      0      511          *:16379                    *:*                  
LISTEN      0      511          *:16380                    *:*                  
LISTEN      0      511          *:6379                     *:*                  
LISTEN      0      511          *:6380                     *:*        # 新端口已经开启
LISTEN      0      128          *:22                       *:*                  
LISTEN      0      100      [::1]:25                    [::]:*                  
LISTEN      0      128       [::]:22                    [::]:* 

然后将他添加进集群

[root@centos_7 ~]# redis-trib.rb add-node 192.168.40.10:6380 192.168.40.7:6379
>>> Adding node 192.168.40.10:6380 to cluster 192.168.40.7:6379
>>> Performing Cluster Check (using node 192.168.40.7:6379)
M: 84414bb35278fe4608956f35f7d9b79211752a41 192.168.40.7:6379
   slots:1278-5460 (4183 slots) master
   1 additional replica(s)
M: 1ab9dba3b9aaeeed396ca9fd875cda0072006347 192.168.40.9:6379
   slots:12202-16383 (4182 slots) master
   1 additional replica(s)
M: 2b0f3d3a03feea11f5fc0798f3e5f2988e7a57a1 192.168.40.13:6379
   slots:0-1277,5461-6998,10923-12201 (4095 slots) master
   0 additional replica(s)
M: b617fef3c13eaf16a37c851d8748bdc3d2f97e67 192.168.40.8:6379
   slots:6999-10922 (3924 slots) master
   1 additional replica(s)
S: 1476165d38606139382a2a576ea85648af439e3c 192.168.40.12:6379
   slots: (0 slots) slave
   replicates b617fef3c13eaf16a37c851d8748bdc3d2f97e67
S: 970d1e4ad835f6068f9cbc6152e6879692de4726 192.168.40.10:6379
   slots: (0 slots) slave
   replicates 1ab9dba3b9aaeeed396ca9fd875cda0072006347
S: dfe2c4bf4413b99374c52a4478e15f26a5b7ba30 192.168.40.11:6379
   slots: (0 slots) slave
   replicates 84414bb35278fe4608956f35f7d9b79211752a41
[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.40.10:6380 to make it join the cluster.
[OK] New node added correctly.

查看状态:

[root@centos_7 ~]# redis-trib.rb info 192.168.40.7:6379 
192.168.40.7:6379 (84414bb3...) -> 0 keys | 4183 slots | 1 slaves.
192.168.40.9:6379 (1ab9dba3...) -> 0 keys | 4182 slots | 1 slaves.
192.168.40.10:6380 (a782c0f4...) -> 0 keys | 0 slots | 0 slaves.	# 刚添加进来时还是master角色
192.168.40.13:6379 (2b0f3d3a...) -> 0 keys | 4095 slots | 0 slaves.
192.168.40.8:6379 (b617fef3...) -> 0 keys | 3924 slots | 1 slaves.
[OK] 0 keys in 5 masters.
0.00 keys per slot on average.

刚添加进来是master角色,我们需要进入到redis的命令行,手动将其指向作为192.168.40.13:6379的slave

登录192.168.40.10:6380,执行以下命令

先使用cluster nodes看一下集群中个主机的ID号,因为我们要为192.168.40.13:6379这台添加slave,就需要得到他的ID号

[root@centos_7 ~]# redis-cli -h 192.168.40.10 -p 6380
192.168.40.10:6380> CLUSTER NODES
1ab9dba3b9aaeeed396ca9fd875cda0072006347 192.168.40.9:6379@16379 master - 0 1583999513000 2 connected 12202-16383
1476165d38606139382a2a576ea85648af439e3c 192.168.40.12:6379@16379 slave b617fef3c13eaf16a37c851d8748bdc3d2f97e67 0 1583999513921 8 connected
b617fef3c13eaf16a37c851d8748bdc3d2f97e67 192.168.40.8:6379@16379 master - 0 1583999514927 8 connected 6999-10922
84414bb35278fe4608956f35f7d9b79211752a41 192.168.40.7:6379@16379 master - 0 1583999510000 1 connected 1278-5460
a782c0f4c9e4d3d7c4340a0d2b533d8e68d2d130 192.168.40.10:6380@16380 myself,master - 0 1583999509000 0 connected
2b0f3d3a03feea11f5fc0798f3e5f2988e7a57a1 192.168.40.13:6379@16379 master - 0 1583999510000 9 connected 0-1277 5461-6998 10923-12201
970d1e4ad835f6068f9cbc6152e6879692de4726 192.168.40.10:6379@16379 slave 1ab9dba3b9aaeeed396ca9fd875cda0072006347 0 1583999511904 2 connected
dfe2c4bf4413b99374c52a4478e15f26a5b7ba30 192.168.40.11:6379@16379 slave 84414bb35278fe4608956f35f7d9b79211752a41 0 1583999512000 1 connected

我这里的192.168.40.13的ID号为:2b0f3d3a03feea11f5fc0798f3e5f2988e7a57a1
执行cluster replicate ID号即可

192.168.40.10:6380> CLUSTER REPLICATE 2b0f3d3a03feea11f5fc0798f3e5f2988e7a57a1
OK

检查:

[root@centos_7 ~]# redis-cli -h 192.168.40.10 -p 6380 
192.168.40.10:6380> CLUSTER nodes
信息内容为,前面的ID号是自己的,后面的ID号是master的ID号,做一下对比就好
1ab9dba3b9aaeeed396ca9fd875cda0072006347 192.168.40.9:6379@16379 master - 0 1583999894000 2 connected 12202-16383
1476165d38606139382a2a576ea85648af439e3c 192.168.40.12:6379@16379 slave b617fef3c13eaf16a37c851d8748bdc3d2f97e67 0 1583999892000 8 connected
b617fef3c13eaf16a37c851d8748bdc3d2f97e67 192.168.40.8:6379@16379 master - 0 1583999893776 8 connected 6999-10922
84414bb35278fe4608956f35f7d9b79211752a41 192.168.40.7:6379@16379 master - 0 1583999893000 1 connected 1278-5460
a782c0f4c9e4d3d7c4340a0d2b533d8e68d2d130 192.168.40.10:6380@16380 myself,slave 2b0f3d3a03feea11f5fc0798f3e5f2988e7a57a1 0 1583999891000 0 connected
2b0f3d3a03feea11f5fc0798f3e5f2988e7a57a1 192.168.40.13:6379@16379 master - 0 1583999893000 9 connected 0-1277 5461-6998 10923-12201
970d1e4ad835f6068f9cbc6152e6879692de4726 192.168.40.10:6379@16379 slave 1ab9dba3b9aaeeed396ca9fd875cda0072006347 0 1583999891760 2 connected
dfe2c4bf4413b99374c52a4478e15f26a5b7ba30 192.168.40.11:6379@16379 slave 84414bb35278fe4608956f35f7d9b79211752a41 0 1583999894783 1 connected
[root@centos_7 ~]# redis-trib.rb info 192.168.40.7:6379
192.168.40.7:6379 (84414bb3...) -> 0 keys | 4183 slots | 1 slaves.
192.168.40.9:6379 (1ab9dba3...) -> 0 keys | 4182 slots | 1 slaves.
192.168.40.13:6379 (2b0f3d3a...) -> 0 keys | 4095 slots | 1 slaves.
192.168.40.8:6379 (b617fef3...) -> 0 keys | 3924 slots | 1 slaves.
[OK] 0 keys in 4 masters.
0.00 keys per slot on average.

完成!

发布了63 篇原创文章 · 获赞 0 · 访问量 2195

猜你喜欢

转载自blog.csdn.net/qq_43058911/article/details/104818335