Nosql之Redis群集扩容

文章目录

    • 搭建群集
    • 增加扩容IP
    • 查看集群slave、slot、key分布信息情况
    • redis节点扩容
      • 主备服务器安装Redis
      • 修改配置文件
      • 扩容主从Redis安装Ruby
      • 节点添加到集群
      • redis-cli启动自动迁移
      • 检测槽点的均衡性
      • 将扩容的主节点添加从节点
      • 平衡各节点槽数量

搭建群集

  • 略(原来博客介绍搭建群集)

增加扩容IP

Master D:192.168.18.110
Slave D1:192.168.18.120

查看集群slave、slot、key分布信息情况

redis-cli --cluster info 192.168.18.101:6379

在这里插入图片描述
Nosql之Redis群集扩容_第1张图片

  • 上述信息,在哪个节点上看集群的信息,效果都是一样的

redis节点扩容

  • 扩容流程:
    1、部署2台redis节点(主、备)
    2、将2台redis节点添加到集群中
    3、使用redis-cli启动自动迁移
    4、迁移后检测各个节点槽的均衡性
    5、将扩容的主节点添加从节点

主备服务器安装Redis

  • 与群集安装相同

修改配置文件

vim /etc/redis/6379.conf
#其他配置与前面群及配置相同,新增开启aof持久化
/etc/init.d/redis_6379 restart

在这里插入图片描述

扩容主从Redis安装Ruby

yum -y install ruby rubygems
gem install redis --version 3.2.0

节点添加到集群

#将2台redis节点添加到集群中、并确认集群状态信息(进入节点192.168.18.101)
redis-cli --cluster add-node 192.168.18.110:6379 192.168.18.101:6379
redis-cli --cluster add-node 192.168.18.120:6379 192.168.18.101:6379
redis-cli -h 192.168.18.101 -p 6379
192.168.18.101:6379> cluster nodes
11fbff6a6631e95891799d7498b5aedcd94027e4 192.168.18.110:6379@16379 master - 0 1584928230132
c9bd990ce61d98e51f5f782ab9a99e4c3c833b05 192.168.18.120:6379@16379 master - 0 1584928230000 
b04ab296bbf449e7171cb53859f049369eb1ecef 192.168.18.105:6379@16379 slave 682a50c7c03a3e8a093c90f5a1bf5b052c026881 0 1590339270037
c584af53b8af5692167cca419537a7d072f60829 192.168.18.106:6379@16379 slave a9c44ab344d72478b2cd6b8fb651667ab9ec6ceb 0 1590339271048 
a9c44ab344d72478b2cd6b8fb651667ab9ec6ceb 192.168.18.102:6379@16379 master - 0 1590339269027 2 connected 5461-10922
069576af79c4530dc16dbdffdceb265b9ad29dea 192.168.18.103:6379@16379 master - 0 1590339268020 3 connected 10923-16383
682a50c7c03a3e8a093c90f5a1bf5b052c026881 192.168.18.101:6379@16379 myself,master - 0 1590339266000 1 connected 0-5460
b3039a3f5f8ea17b5cc4294907525b8183c674e7 192.168.18.104:6379@16379 slave 069576af79c4530dc16dbdffdceb265b9ad29dea 0 1590339268000 

redis-cli启动自动迁移

  • 根据redis 槽位分析,一共有16384个槽位,如果分4个配套节点:每个节点是4096个槽位。
redis-cli --cluster reshard 192.168.18.101:6379
How many slots do you want to move (from 1 to 16384)? 4096  #要迁移多少个槽

What is the receiving node ID? 99521b7fd126b694bcb9a22ffa5a490f31f66543  #迁移到哪个节点

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.

#要求输入源节点的id,这里输入101,102,103三个节点,输入done表示结束

Source node #1: 682a50c7c03a3e8a093c90f5a1bf5b052c026881
Source node #2: a9c44ab344d72478b2cd6b8fb651667ab9ec6ceb
Source node #3: 069576af79c4530dc16dbdffdceb265b9ad29dea
Source node #4: done
  • 最后会有一个迁移方案,输入yes表示同意,迁移开始。输入no表示不同意,重新设置迁移方案。
  • 确认是否迁移成功
192.168.18.101:6379> cluster nodes
99521b7fd126b694bcb9a22ffa5a490f31f66543 192.168.18.110:6379@16379 master - 0 1590339270037 7 connected 0-1364 5461-6826 10923-12287 ##已加入新主节点

检测槽点的均衡性

redis-cli --cluster rebalance 192.168.18.101:6379
>>> Performing Cluster Check (using node 192.168.18.101:6379)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
*** No rebalancing needed! All nodes are within the 2.00% threshold.
#可以看出,节点负责的槽数据差异在2%以内,因此槽分配均衡

Nosql之Redis群集扩容_第2张图片

将扩容的主节点添加从节点

redis-cli -h 192.168.18.120 -p 6379
192.168.18.120:6379> cluster replicate 682a50c7c03a3e8a093c90f5a1bf5b052c026881
OK

平衡各节点槽数量

redis-cli --cluster rebalance --cluster-threshold 1 192.168.18.101:6379
>>> Performing Cluster Check (using node 192.168.18.101:6379)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
*** No rebalancing needed! All nodes are within the 1.00% threshold.

Nosql之Redis群集扩容_第3张图片

redis-cli -h 192.168.18.120 -p 6379
192.168.18.120:6379> cluster nodes
99521b7fd126b694bcb9a22ffa5a490f31f66543 192.168.18.110:6379@16379 master - 0 1590339270037 7 connected 0-1364 5461-6826 10923-12287
b04ab296bbf449e7171cb53859f049369eb1ecef 192.168.18.105:6379@16379 slave 682a50c7c03a3e8a093c90f5a1bf5b052c026881 0 1590339270037 5 connected
c584af53b8af5692167cca419537a7d072f60829 192.168.18.106:6379@16379 slave a9c44ab344d72478b2cd6b8fb651667ab9ec6ceb 0 1590339271048 6 connected
a9c44ab344d72478b2cd6b8fb651667ab9ec6ceb 192.168.18.102:6379@16379 master - 0 1590339269027 2 connected 6827-10922
069576af79c4530dc16dbdffdceb265b9ad29dea 192.168.18.103:6379@16379 master - 0 1590339268020 3 connected 12288-16383
682a50c7c03a3e8a093c90f5a1bf5b052c026881 192.168.18.101:6379@16379 master - 0 1590339266000 1 connected 1365-5460
5a5e20950e840393 192.168.18.120:6379@16379 myself,slave 99521b7fd126b694bcb9a22ffa5a490f31f66543 0 1590339271048 0 connected
b3039a3f5f8ea17b5cc4294907525b8183c674e7 192.168.18.104:6379@16379 slave 069576af79c4530dc16dbdffdceb265b9ad29dea 0 1590339268000 4 connected

你可能感兴趣的:(Redis,redis,分布式,运维,数据库)