redis 扩容和收缩

Scroll Down

第十章:redis 扩容

01.目标节点准备导入槽
02.源节点准备导出槽
03.获取slot下槽总数
04.批量迁移相关键的数据
05.循环迁移
06.通知槽分配给目标节点

01. 创建新的节点

# 创建数据目录
mkdir -p /opt/redis_{6390,6391}/{conf,logs,pid}
mkdir -p /data/redis_{6390,6391}

# 复制配置并修改端口
cd /opt/
cp redis_6380/conf/redis_6380.conf redis_6390/conf/redis_6390.conf
cp redis_6380/conf/redis_6380.conf redis_6391/conf/redis_6391.conf
sed -i 's#6380#6390#g' redis_6390/conf/redis_6390.conf
sed -i 's#6380#6391#g' redis_6391/conf/redis_6391.conf

# 启动
redis-server /opt/redis_6390/conf/redis_6390.conf
redis-server /opt/redis_6391/conf/redis_6391.conf

# 手动发现节点
redis-cli -c -h 10.0.0.51 -p 6380 cluster meet 10.0.0.51 6390
redis-cli -c -h 10.0.0.51 -p 6380 cluster meet 10.0.0.51 6391

# 查看集群
redis-cli -c -h 10.0.0.51 -p 6380 cluster nodes

02. 扩容步骤

# 01 重新分配槽位
redis-cli --cluster reshard 10.0.0.51:6380

#第⼀次交互:每个节点最终分配多少个槽
How many slots do you want to move (from 1 to 16384)? 4096

#第三次交互:哪些节点需要导出
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

#第四次交互:确认信息
Do you want to proceed with the proposed reshard plan (yes/no)? yes

第十一章:redis 缩容

01. 缩容命令

# 重新分配槽
redis-cli --cluster reshard 10.0.0.51:6380

# 第一次交互:需要迁移多少个槽
How many slots do you want to move (from 1 to 16384)? 1365

#第三次交互:接受节点ID是多少
What is the receiving node ID? db01的6380的ID

#第三次交互:哪些节点需要导出
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: 6390的ID
Source node #2: done

#第四次交互:确认信息
Do you want to proceed with the proposed reshard plan (yes/no)? yes

#重复上述操作,知道6390所有的槽都被分配完毕

02. 检查命令

redis-cli --cluster info 10.0.0.51:6380