Fork me on GitHub

redis集群维护之横向扩容

  一、集群维护之动态添加节点:

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

不影响数据业务使用,不会照成数据丢失,添加办法如下:

redis-trib.rb add-node 192.168.1.4:6380 192.168.1.4:6379

 ## add-node表示添加节点 ,前面的IP地址和端口是现在要添加的到集群的主机,后面的IP地址选择集群中任意一台IP:端口即可 
 ## 如果添加节点失败的话,可能是创建集群之前设置了密码奥(我就是这样)

  返回如下则表示添加集群成功
>>> Adding node 192.168.1.4:6380 to cluster 192.168.1.4:6379
>>> Performing Cluster Check (using node 192.168.1.4:6379)
S: 2feed5ccb782f6bc7c424f4892aafa19a57d1820 192.168.1.4:6379
   slots: (0 slots) slave
   replicates 286a966ff47d0aafa482bf121912027edf312157
S: ddcc033179a02497200b5c87e2a5ee0bd1f4e2e8 192.168.1.7:6379
   slots: (0 slots) slave
   replicates eba2a74975496f8710f91b62c89afb28022df5b5
M: 286a966ff47d0aafa482bf121912027edf312157 192.168.1.8:6379
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: cf090357c5adf3b887197623c3d378a7719d44f2 192.168.1.9:6379
   slots: (0 slots) slave
   replicates 50a2fc694407fd464c201daa0e7c72146fba5507
M: eba2a74975496f8710f91b62c89afb28022df5b5 192.168.1.6:6379
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
M: 50a2fc694407fd464c201daa0e7c72146fba5507 192.168.1.5:6379
   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 192.168.1.4:6380 to make it join the cluster.
[OK] New node added correctly.
  二、检查redis是否添加成功

redis-trib.rb check 192.168.1.4:6380

 返回如下:

M: 94e5fb4877b18f1bc1c22b4ff52e224aba6d0195 192.168.1.4:6380
   slots: (0 slots) master
   0 additional replica(s)
M: 50a2fc694407fd464c201daa0e7c72146fba5507 192.168.1.5:6379
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
M: eba2a74975496f8710f91b62c89afb28022df5b5 192.168.1.6:6379
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
M: 286a966ff47d0aafa482bf121912027edf312157 192.168.1.8:6379
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: 2feed5ccb782f6bc7c424f4892aafa19a57d1820 192.168.1.4:6379
   slots: (0 slots) slave
   replicates 286a966ff47d0aafa482bf121912027edf312157
S: cf090357c5adf3b887197623c3d378a7719d44f2 192.168.1.9:6379
   slots: (0 slots) slave
   replicates 50a2fc694407fd464c201daa0e7c72146fba5507
S: ddcc033179a02497200b5c87e2a5ee0bd1f4e2e8 192.168.1.7:6379
   slots: (0 slots) slave
   replicates eba2a74975496f8710f91b62c89afb28022df5b5
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
##刚加进去的机器是master ,没有任何槽位,没有slave节点

  三、查看槽位信息

redis-trib.rb info 192.168.1.4:6380
192.168.1.4:6380 (94e5fb48...) -> 0 keys | 0 slots | 0 slaves.
192.168.1.5:6379 (50a2fc69...) -> 0 keys | 5462 slots | 1 slaves.
192.168.1.6:6379 (eba2a749...) -> 0 keys | 5461 slots | 1 slaves.
192.168.1.8:6379 (286a966f...) -> 0 keys | 5461 slots | 1 slaves.
[OK] 0 keys in 4 masters.
0.00 keys per slot on average.

   四、更改新节点的为slave (默认添加的都是master)

      1、进入redis命令行的交互模式,指定连接主机!!!(要设置哪台机器为从节点就连接哪个节点)

 redis-cli -h 192.168.1.4 -p 6380

      2、输入密码

auth 123456

      3、查看当前节点ID

192.168.1.4:6380> cluster nodes

ID如下:

1 94e5fb4877b18f1bc1c22b4ff52e224aba6d0195 192.168.1.4:6380@16380 master - 0 1627539652000 8 connected
2 c1d534ff8edbfeec70778859b8cd289ec7258de6 192.168.1.5:6380@16380 master - 0 1627539654676 0 connected

      4、设置192.168.1.4:6380为192.168.1.5的slave

192.168.1.4:6380> cluster replicate c1d534ff8edbfeec70778859b8cd289ec7258de6 
OK

  ## cluster replicate后面跟的是要设置为主机器的ID

   五、重新分配槽位

  1、

redis-trib.rb reshard 192.168.1.9:6379

返回信息如下:

How many slots do you want to move (from 1 to 16384)? 

  ##你想移动多少个槽位,从1-16384(16384可以放在同一个主机上)

需要指定槽位往哪分1-16384,总共16384个槽位,只需要指定集群中的任意一台机器

  2、指定4096个槽位

How many slots do you want to move (from 1 to 16384)? 4096

  3、接收节点ID是什么?

What is the receiving node ID? 
What is the receiving node ID? c1d534ff8edbfeec70778859b8cd289ec7258de6

##ID要选择master节点的ID,slave节点是无效的

  4、选择从哪个节点分配

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表示从所有节点上抽出一部分槽位给新的master机器,也可以一个节点ID一个节点ID写

Source node #1:all

  5、是否确认

Do you want to proceed with the proposed reshard plan (yes/no)? yes

分配可能会失败,因为重新分配的要求里面不能有数据

  6、分配失败对应的节点里的数据需要清除对应节点的数据

使用:
FLUSHDB 清空当前数据库里所有信息
FLUSHALL 清空redis所有库所有数据

  7、重新分配失败,会提示你之前出现的问题,选择修复

redis-trib.rb fix 192.168.1.9:6279

  8、在试一下修复,没报错就可以继续分配了但这里不能直接分配4096个了,得去掉之前分配好的 4096-分配好的=现在多少

redis-trib.rb reshard 192.168.1.9:6379

  9、分配ID选择all

Source node #1:all

 

posted @ 2021-07-29 15:39  Alex-Lzy  阅读(437)  评论(0编辑  收藏  举报