Redis 集群搭建

Redis集群搭建 7.2.5

文章目录

  • [1. 环境介绍](#1. 环境介绍)
  • [2. 环境前准备工作(所有机器)](#2. 环境前准备工作(所有机器))
  • [3. 安装 Redis 7.2.5](#3. 安装 Redis 7.2.5)
  • [4. redis 配置修改](#4. redis 配置修改)
    • [4.1 node-01主机添加配置信息](#4.1 node-01主机添加配置信息)
    • [4.2 node-02主机添加配置信息](#4.2 node-02主机添加配置信息)
    • [4.3 node-03主机添加配置信息](#4.3 node-03主机添加配置信息)
  • [5. 制作脚本](#5. 制作脚本)
    • [5.1 编写脚本](#5.1 编写脚本)
    • [5.2 测试脚本](#5.2 测试脚本)
  • [6. 启动Redis 集群](#6. 启动Redis 集群)
    • [6.1 启动集群](#6.1 启动集群)
    • [6.2 查看集群状态](#6.2 查看集群状态)
    • [6.3 查看集群节点](#6.3 查看集群节点)
    • [6.4 测试用例](#6.4 测试用例)
  • [7. 集群扩容节点](#7. 集群扩容节点)
    • [7.1 node-02](#7.1 node-02)
    • [7.2 node-03](#7.2 node-03)
    • [7.3 添加主节点](#7.3 添加主节点)
    • [7.4 添加从节点](#7.4 添加从节点)
    • [7.5 集群缩容节点](#7.5 集群缩容节点)

1. 环境介绍

使用 Redis 集群进行扩展 |文档

Note that the minimal cluster that works as expected must contain at least three master nodes. For deployment, we strongly recommend a six-node cluster, with three masters and three replicas.

请注意,按预期工作的小集群必须包含 至少三个主节点。对于部署,我们强烈建议 一个六节点集群,包含三个主节点和三个副本。

三台服务器各部署一个主节点、一个从节点,同一台服务器上不是直接主从关系。

操作系统 Centos 7 Centos 7 Centos 7
内核版本 Linux 3.10.0-957.el7.x86_64 Linux 3.10.0-957.el7.x86_64 Linux 3.10.0-957.el7.x86_64
主机名称 node-01 node-02 node-03
角色 master1/slave2 master2/slave3 master3/slave1
IP地址 192.168.1.100:7000/192.168.1.100:7001 192.168.1.200:7000/192.168.1.200:7001 192.168.1.250:7000/192.168.1.250:7001
Redis 7.2.5 7.2.5 7.2.5

2. 环境前准备工作(所有机器)

sh 复制代码
# 关闭防火墙
systemctl stop firewalld
systemctl disable firewalld

# 修改 hostname
hostnamectl set-hostname xxxx  # 修改后退出当前终端重新连接即可

# 更新下软件源
# 地址 https://developer.aliyun.com/mirror/

# 时区调整,时间校准
date -R
timedatectl set-timezone Asia/Shanghai
yum -y install ntp
ntpdate ntp1.aliyun.com

# 关闭 selinux: 
sed -i 's/enforcing/disabled/' /etc/selinux/config
setenforce 0

3. 安装 Redis 7.2.5

sh 复制代码
# 下载地址
http://download.redis.io/releases/

# 下载
wget http://download.redis.io/releases/redis-7.2.5.tar.gz
# 解压
tar -xf redis-7.2.5.tar.gz -C /opt/redis

# 编译
make MALLOC=libc

# 安装 注意如果安装提示python3不存在 执行 yum install -y python3
make install PREFIX=/usr/local/redis

# 配置环境变量
vi /etc/profile.d/redis.sh

#!/bin/bash

export REDIS_HOME=/usr/local/redis
export PATH=$PATH:$REDIS_HOME/bin 

# 使其配置生效
source /etc/profile.d/redis.sh

4. redis 配置修改

4.1 node-01主机添加配置信息

sh 复制代码
# 创建对应的文件夹
mkdir -p /usr/local/redis/run
mkdir -p /usr/local/redis/log
mkdir -p /usr/local/redis/data/7000
mkdir -p /usr/local/redis/data/7001
mkdir -p /usr/local/redis/conf

# cp 配置文件
cp /opt/redis/redis-7.2.5/redis.conf /usr/local/redis/conf/redis_7000.conf

# 可能需要清空配置文件信息
echo > /usr/local/redis/conf/redis_7000.conf

# 添加配置信息 master1
vi /usr/local/redis/conf/redis_7000.conf  

# 关闭保护模式
protected-mode no
# 添加本机的ip
bind 192.168.1.100   
# 端口  
port 7000
# pid存储目录
pidfile /usr/local/redis/run/redis_7000.pid   
# 日志存储目录
logfile /usr/local/redis/log/redis_7000.log 
# 数据存储目录,目录要提前创建好
dir /usr/local/redis/data/7000
# 开启集群 
cluster-enabled yes  
# 集群节点配置文件,这个文件是不能手动编辑的。确保每一个集群节点的配置文件不通
cluster-config-file nodes-7000.conf
# 集群节点的超时时间,单位:ms,超时后集群会认为该节点失败
cluster-node-timeout 15000 
# 持久化 
appendonly yes 
# 守护进程
daemonize yes   


cp /opt/redis/redis-7.2.5/redis.conf /usr/local/redis/conf/redis_7001.conf

# 可能需要清空配置文件信息
echo > /usr/local/redis/conf/redis_7001.conf

# 添加配置信息 sa
vi /usr/local/redis/conf/redis_7001.conf

# 关闭保护模式
protected-mode no
# 添加本机的ip
bind 192.168.1.100
# 端口
port 7001
# pid存储目录
pidfile /usr/local/redis/run/redis_7001.pid
# 日志存储目录
logfile /usr/local/redis/log/redis_7001.log
# 数据存储目录,目录要提前创建好
dir /usr/local/redis/data/7001
# 开启集群 
cluster-enabled yes  
# 集群节点配置文件,这个文件是不能手动编辑的。确保每一个集群节点的配置文件不通
cluster-config-file nodes-7001.conf   
# 持久化 
appendonly yes 
# 守护进程
daemonize yes 

4.2 node-02主机添加配置信息

sh 复制代码
# 创建对应的文件夹
mkdir -p /usr/local/redis/run
mkdir -p /usr/local/redis/log
mkdir -p /usr/local/redis/data/7000
mkdir -p /usr/local/redis/data/7001
mkdir -p /usr/local/redis/conf

# cp 配置文件
cp /opt/redis/redis-7.2.5/redis.conf /usr/local/redis/conf/redis_7000.conf

# 可能需要清空配置文件信息
echo > /usr/local/redis/conf/redis_7000.conf

# 添加配置信息 master1
vi /usr/local/redis/conf/redis_7000.conf  

# 关闭保护模式
protected-mode no
# 添加本机的ip
bind 192.168.1.200
# 端口
port 7000
# pid存储目录
pidfile /usr/local/redis/run/redis_7000.pid
# 日志存储目录
logfile /usr/local/redis/log/redis_7000.log
# 数据存储目录,目录要提前创建好
dir /usr/local/redis/data/7000
# 开启集群 
cluster-enabled yes  
# 集群节点配置文件,这个文件是不能手动编辑的。确保每一个集群节点的配置文件不通
cluster-config-file nodes-7000.conf   
# 持久化 
appendonly yes 
# 守护进程
daemonize yes 

cp /opt/redis/redis-7.2.5/redis.conf /usr/local/redis/conf/redis_7001.conf

# 可能需要清空配置文件信息
echo > /usr/local/redis/conf/redis_7001.conf

# 添加配置信息 sa
vi /usr/local/redis/conf/redis_7001.conf

# 关闭保护模式
protected-mode no
# 添加本机的ip
bind 192.168.1.200
# 端口
port 7001
# pid存储目录
pidfile /usr/local/redis/run/redis_7001.pid
# 日志存储目录
logfile /usr/local/redis/log/redis_7001.log
# 数据存储目录,目录要提前创建好
dir /usr/local/redis/data/7001
# 开启集群 
cluster-enabled yes  
# 集群节点配置文件,这个文件是不能手动编辑的。确保每一个集群节点的配置文件不通
cluster-config-file nodes-7001.conf   
# 持久化 
appendonly yes 
# 守护进程
daemonize yes 

4.3 node-03主机添加配置信息

sh 复制代码
# 创建对应的文件夹
mkdir -p /usr/local/redis/run
mkdir -p /usr/local/redis/log
mkdir -p /usr/local/redis/data/7000
mkdir -p /usr/local/redis/data/7001
mkdir -p /usr/local/redis/conf

# cp 配置文件
cp /opt/redis/redis-7.2.5/redis.conf /usr/local/redis/conf/redis_7000.conf

# 可能需要清空配置文件信息
echo > /usr/local/redis/conf/redis_7000.conf

# 添加配置信息 master1
vi /usr/local/redis/conf/redis_7000.conf  

# 关闭保护模式
protected-mode no
# 添加本机的ip
bind 192.168.1.250
# 端口
port 7000
# pid存储目录
pidfile /usr/local/redis/run/redis_7000.pid
# 日志存储目录
logfile /usr/local/redis/log/redis_7000.log
# 数据存储目录,目录要提前创建好
dir /usr/local/redis/data/7000
# 开启集群 
cluster-enabled yes  
# 集群节点配置文件,这个文件是不能手动编辑的。确保每一个集群节点的配置文件不通
cluster-config-file nodes-7000.conf 
# 持久化 
appendonly yes 
# 守护进程
daemonize yes 


cp /opt/redis/redis-7.2.5/redis.conf /usr/local/redis/conf/redis_7001.conf

# 可能需要清空配置文件信息
echo > /usr/local/redis/conf/redis_7001.conf

# 添加配置信息 sa
vi /usr/local/redis/conf/redis_7001.conf

# 关闭保护模式
protected-mode no
# 添加本机的ip
bind 192.168.1.250
# 端口
port 7001
# pid存储目录
pidfile /usr/local/redis/run/redis_7001.pid
# 日志存储目录
logfile /usr/local/redis/log/redis_7001.log
# 数据存储目录,目录要提前创建好
dir /usr/local/redis/data/7001
# 开启集群 
cluster-enabled yes  
# 集群节点配置文件,这个文件是不能手动编辑的。确保每一个集群节点的配置文件不通
cluster-config-file nodes-7001.conf
# 持久化 
appendonly yes 
# 守护进程
daemonize yes 

5. 制作脚本

5.1 编写脚本

sh 复制代码
# 启动脚本
vi /usr/local/redis/bin/cluster_start.sh

#!/bin/bash

redis-server /usr/local/redis/conf/redis_7000.conf
redis-server /usr/local/redis/conf/redis_7001.conf

# 添加可执行权限
chmod +x /usr/local/redis/bin/cluster_start.sh

# 关闭脚本
vi /usr/local/redis/bin/cluster_shutdown.sh

#!/bin/bash

pgrep redis-server | xargs -exec kill -9

# 添加可执行权限
chmod +x /usr/local/redis/bin/cluster_shutdown.sh

5.2 测试脚本

sh 复制代码
sh /usr/local/redis/bin/cluster_start.sh

[root@node-01 ~]# ps -ef | grep redis
root      28167      1  0 20:46 ?        00:00:00 redis-server 192.168.1.100:7001
root      28218      1  0 20:49 ?        00:00:00 redis-server 192.168.1.100:7000
root      28227   9872  0 20:50 pts/0    00:00:00 grep --color=auto redis


sh /usr/local/redis/bin/cluster_shutdown.sh

[root@node-01 ~]# ps -ef | grep redis
root      28234   9872  0 20:51 pts/0    00:00:00 grep --color=auto redis

6. 启动Redis 集群

6.1 启动集群

sh 复制代码
# 任意一台机器上都可以   得保证每个节点 redis服务是可用状态
redis-cli --cluster create 192.168.1.100:7000 192.168.1.100:7001 192.168.1.200:7000 192.168.1.200:7001 192.168.1.250:7000 192.168.1.250:7001 --cluster-replicas 1

[root@node-01 redis]# redis-cli --cluster create 192.168.1.100:7000 192.168.1.100:7001 192.168.1.200:7000 192.168.1.200:7001 192.168.1.250:7000 192.168.1.250:7001 --cluster-replicas 1
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 192.168.1.200:7001 to 192.168.1.100:7000
Adding replica 192.168.1.250:7001 to 192.168.1.200:7000
Adding replica 192.168.1.100:7001 to 192.168.1.250:7000
M: a866e2efff44ddaf8b3553037d72d247f35c5b95 192.168.1.100:7000
   slots:[0-5460] (5461 slots) master
S: 6d4411b05daa67d521eff4128e7ff4dbf1429279 192.168.1.100:7001
   replicates 9b12bbf161e6166153a448ea400e02bfbf2a7f3b
M: 99e6ca5ed6cd7d15096d553cb0c66ce595c492e3 192.168.1.200:7000
   slots:[5461-10922] (5462 slots) master
S: 74ad52e17d645ebe3b0b1976be4b6f9091659a88 192.168.1.200:7001
   replicates a866e2efff44ddaf8b3553037d72d247f35c5b95
M: 9b12bbf161e6166153a448ea400e02bfbf2a7f3b 192.168.1.250:7000
   slots:[10923-16383] (5461 slots) master
S: 04910d12340a2a982876eaed68dd87a02539c416 192.168.1.250:7001
   replicates 99e6ca5ed6cd7d15096d553cb0c66ce595c492e3
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join

>>> Performing Cluster Check (using node 192.168.1.100:7000)
M: a866e2efff44ddaf8b3553037d72d247f35c5b95 192.168.1.100:7000
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: 99e6ca5ed6cd7d15096d553cb0c66ce595c492e3 192.168.1.200:7000
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
M: 9b12bbf161e6166153a448ea400e02bfbf2a7f3b 192.168.1.250:7000
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 04910d12340a2a982876eaed68dd87a02539c416 192.168.1.250:7001
   slots: (0 slots) slave
   replicates 99e6ca5ed6cd7d15096d553cb0c66ce595c492e3
S: 6d4411b05daa67d521eff4128e7ff4dbf1429279 192.168.1.100:7001
   slots: (0 slots) slave
   replicates 9b12bbf161e6166153a448ea400e02bfbf2a7f3b
S: 74ad52e17d645ebe3b0b1976be4b6f9091659a88 192.168.1.200:7001
   slots: (0 slots) slave
   replicates a866e2efff44ddaf8b3553037d72d247f35c5b95
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.


# 如果出现Node xxxx is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.   请删除 appendonly.aof、dump.rdb、node_xxx.conf 这些文件 重新启动redis服务 再执行集群创建命令

# 如果出现 [ERR] Node xxxx is not configured as a cluster node.   请检查配置文件是否设置cluster-enabled yes  这个配置项

6.2 查看集群状态

sh 复制代码
# -c 参数表示以集群模式,否则报 (error) MOVED 9421 192.168.1.100:7000
redis-cli -c -h 192.168.1.100 -p 7000 cluster info

[root@node-01 redis]# redis-cli -c  -h 192.168.1.100 -p 7000 cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:737
cluster_stats_messages_pong_sent:671
cluster_stats_messages_sent:1408
cluster_stats_messages_ping_received:666
cluster_stats_messages_pong_received:737
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:1408
total_cluster_links_buffer_limit_exceeded:0

6.3 查看集群节点

sh 复制代码
# -c 参数表示以集群模式,否则报 (error) MOVED 9421 192.168.1.100:7000
redis-cli -c -h 192.168.1.100 -p 7000 cluster nodes

[root@node-01 redis]# redis-cli -c -h 192.168.1.100 -p 7000 cluster nodes
99e6ca5ed6cd7d15096d553cb0c66ce595c492e3 192.168.1.200:7000@17000 master - 0 1721181315000 3 connected 5461-10922
9b12bbf161e6166153a448ea400e02bfbf2a7f3b 192.168.1.250:7000@17000 master - 0 1721181316761 5 connected 10923-16383
04910d12340a2a982876eaed68dd87a02539c416 192.168.1.250:7001@17001 slave 99e6ca5ed6cd7d15096d553cb0c66ce595c492e3 0 1721181315736 3 connected
a866e2efff44ddaf8b3553037d72d247f35c5b95 192.168.1.100:7000@17000 myself,master - 0 1721181314000 1 connected 0-5460
6d4411b05daa67d521eff4128e7ff4dbf1429279 192.168.1.100:7001@17001 slave 9b12bbf161e6166153a448ea400e02bfbf2a7f3b 0 1721181312000 5 connected
74ad52e17d645ebe3b0b1976be4b6f9091659a88 192.168.1.200:7001@17001 slave a866e2efff44ddaf8b3553037d72d247f35c5b95 0 1721181316000 1 connected

6.4 测试用例

sh 复制代码
[root@node-01 redis]# redis-cli -c -h 192.168.1.100 -p 7000
192.168.1.100:7000> set name dongdong
-> Redirected to slot [5798] located at 192.168.1.200:7000
OK


[root@node-02 redis]# redis-cli -c -h 192.168.1.200 -p 7000
192.168.1.200:7000> get name
"dongdong"

[root@node-03 redis]#  redis-cli -c -h 192.168.1.250 -p 7000
192.168.1.250:7000> get name
-> Redirected to slot [5798] located at 192.168.1.200:7000
"dongdong"
192.168.1.250:7000> del name
-> Redirected to slot [5798] located at 192.168.1.200:7000
(integer) 1

7. 集群扩容节点

扩容时,先增加主节点,再增加从节点

可以在同一个节点 ,也可以在不同节点 ,推荐在不同节点

7.1 node-02

sh 复制代码
mkdir -p /usr/local/redis/data/7002

#  端口 7002
vi /usr/local/redis/conf/redis_7002.conf

# 关闭保护模式
protected-mode no
# 添加本机的ip
bind 192.168.1.200
# 端口
port 7002
# pid存储目录
pidfile /usr/local/redis/run/redis_7002.pid
# 日志存储目录
logfile /usr/local/redis/log/redis_7002.log
# 数据存储目录,目录要提前创建好
dir /usr/local/redis/data/7002
# 开启集群 
cluster-enabled yes  
# 集群节点配置文件,这个文件是不能手动编辑的。确保每一个集群节点的配置文件不通
cluster-config-file nodes-7002.conf
# 持久化 
appendonly yes 
# 守护进程
daemonize yes 


# 启动redis
redis-server /usr/local/redis/conf/redis_7002.conf

7.2 node-03

sh 复制代码
mkdir -p /usr/local/redis/data/7002

#  端口 7002
vi /usr/local/redis/conf/redis_7002.conf

# 关闭保护模式
protected-mode no
# 添加本机的ip
bind 192.168.1.250
# 端口
port 7002
# pid存储目录
pidfile /usr/local/redis/run/redis_7002.pid
# 日志存储目录
logfile /usr/local/redis/log/redis_7002.log
# 数据存储目录,目录要提前创建好
dir /usr/local/redis/data/7002
# 开启集群 
cluster-enabled yes  
# 集群节点配置文件,这个文件是不能手动编辑的。确保每一个集群节点的配置文件不通
cluster-config-file nodes-7002.conf
# 持久化 
appendonly yes 
# 守护进程
daemonize yes 


# 启动redis
redis-server /usr/local/redis/conf/redis_7002.conf

7.3 添加主节点

sh 复制代码
# 使用redis-cli的add-node命令新增一个主节点192.168.1.200:7002(master),前面的ip:port为新增节点,后面的ip:port为集群中已存在的节点。

redis-cli --cluster add-node 192.168.1.200:7002 192.168.1.100:7000

root@node-01 redis]# redis-cli --cluster add-node 192.168.1.200:7002 192.168.1.100:7000
>>> Adding node 192.168.1.200:7002 to cluster 192.168.1.100:7000
>>> Performing Cluster Check (using node 192.168.1.100:7000)
M: 9037a70744f728d436a7c37e5e841f3b1ae3f099 192.168.1.100:7000
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: a10c431ac4acb90c1da27fd76fd8c3479b32a902 192.168.1.200:7000
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 844876c169666de7665c51b5bdad01807ee62ae4 192.168.1.200:7001
   slots: (0 slots) slave
   replicates 9037a70744f728d436a7c37e5e841f3b1ae3f099
M: cf6df070f4102a3c5cc8d4577ab30e9dee5906b4 192.168.1.250:7000
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 55792b63bc0b23b1900b5c9ee03a488f8ed76988 192.168.1.100:7001
   slots: (0 slots) slave
   replicates cf6df070f4102a3c5cc8d4577ab30e9dee5906b4
S: 90f1e06b26cbdcbe884f22aba7fc37e98ca5d58f 192.168.1.250:7001
   slots: (0 slots) slave
   replicates a10c431ac4acb90c1da27fd76fd8c3479b32a902
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Getting functions from cluster
>>> Send FUNCTION LIST to 192.168.1.200:7002 to verify there is no functions in it
>>> Send FUNCTION RESTORE to 192.168.1.200:7002
>>> Send CLUSTER MEET to node 192.168.1.200:7002 to make it join the cluster.
[OK] New node added correctly.

# 查看集群状态
redis-cli --cluster check 192.168.1.100:7000

[root@node-01 redis]# redis-cli --cluster check 192.168.1.100:7000
192.168.1.100:7000 (9037a707...) -> 0 keys | 5461 slots | 1 slaves.
192.168.1.200:7000 (a10c431a...) -> 0 keys | 5462 slots | 1 slaves.
192.168.1.250:7000 (cf6df070...) -> 0 keys | 5461 slots | 1 slaves.
192.168.1.200:7002 (990bb934...) -> 0 keys | 0 slots | 0 slaves.
[OK] 0 keys in 4 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.1.100:7000)
M: 9037a70744f728d436a7c37e5e841f3b1ae3f099 192.168.1.100:7000
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: a10c431ac4acb90c1da27fd76fd8c3479b32a902 192.168.1.200:7000
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 844876c169666de7665c51b5bdad01807ee62ae4 192.168.1.200:7001
   slots: (0 slots) slave
   replicates 9037a70744f728d436a7c37e5e841f3b1ae3f099
M: cf6df070f4102a3c5cc8d4577ab30e9dee5906b4 192.168.1.250:7000
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 55792b63bc0b23b1900b5c9ee03a488f8ed76988 192.168.1.100:7001
   slots: (0 slots) slave
   replicates cf6df070f4102a3c5cc8d4577ab30e9dee5906b4
S: 90f1e06b26cbdcbe884f22aba7fc37e98ca5d58f 192.168.1.250:7001
   slots: (0 slots) slave
   replicates a10c431ac4acb90c1da27fd76fd8c3479b32a902
M: 990bb934a52a74ec9ebb2b73e9dc6016244293f6 192.168.1.200:7002
   slots: (0 slots) master
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

# 注意:当添加节点成功以后,新增的节点不会有任何数据,因为它还没有分配任何的slot(hash槽),我们需要为新节点手工分配hash槽。
# 重新分配槽位
# 注意:槽位迁移后,对应槽位中的数据也会迁移!
redis-cli --cluster reshard 192.168.1.100:7000

... ...
How many slots do you want to move (from 1 to 16384)? 800
(ps:需要多少个槽移动到新的节点上,自己设置,比如600个hash槽)
What is the receiving node ID? 990bb934a52a74ec9ebb2b73e9dc6016244293f6  # 192.168.1.200:7002 节点id
(ps:把这600个hash槽移动到哪个节点上去,需要指定节点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:all
(ps:输入all为从所有主节点(7000)中分别抽取相应的槽数指定到新节点中,抽取的总槽数为800个)
 ... ...
Do you want to proceed with the proposed reshard plan (yes/no)? yes
(ps:输入yes确认开始执行分片任务)
... ...

7.4 添加从节点

sh 复制代码
redis-cli --cluster add-node 192.168.1.250:7002 192.168.1.200:7002

[root@node-01 data]# redis-cli --cluster add-node 192.168.1.250:7002 192.168.1.200:7002
>>> Adding node 192.168.1.250:7002 to cluster 192.168.1.200:7002
>>> Performing Cluster Check (using node 192.168.1.200:7002)
M: 990bb934a52a74ec9ebb2b73e9dc6016244293f6 192.168.1.200:7002
   slots:[0-265],[5461-5727],[10923-11188] (799 slots) master
M: cf6df070f4102a3c5cc8d4577ab30e9dee5906b4 192.168.1.250:7000
   slots:[11189-16383] (5195 slots) master
   1 additional replica(s)
S: 90f1e06b26cbdcbe884f22aba7fc37e98ca5d58f 192.168.1.250:7001
   slots: (0 slots) slave
   replicates a10c431ac4acb90c1da27fd76fd8c3479b32a902
S: 55792b63bc0b23b1900b5c9ee03a488f8ed76988 192.168.1.100:7001
   slots: (0 slots) slave
   replicates cf6df070f4102a3c5cc8d4577ab30e9dee5906b4
S: 844876c169666de7665c51b5bdad01807ee62ae4 192.168.1.200:7001
   slots: (0 slots) slave
   replicates 9037a70744f728d436a7c37e5e841f3b1ae3f099
M: a10c431ac4acb90c1da27fd76fd8c3479b32a902 192.168.1.200:7000
   slots:[5728-10922] (5195 slots) master
   1 additional replica(s)
M: 9037a70744f728d436a7c37e5e841f3b1ae3f099 192.168.1.100:7000
   slots:[266-5460] (5195 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.
>>> Getting functions from cluster
>>> Send FUNCTION LIST to 192.168.1.250:7002 to verify there is no functions in it
>>> Send FUNCTION RESTORE to 192.168.1.250:7002
>>> Send CLUSTER MEET to node 192.168.1.250:7002 to make it join the cluster.
[OK] New node added correctly.

# 挂载节点 在挂在节点上执行
# 990bb934a52a74ec9ebb2b73e9dc6016244293f6 id  192.168.1.200:7002 主节点id 
redis-cli -c -h 192.168.1.250 -p 7002 cluster replicate  990bb934a52a74ec9ebb2b73e9dc6016244293f6

[root@node-03 data]# redis-cli -c -h 192.168.1.250 -p 7002 cluster replicate  990bb934a52a74ec9ebb2b73e9dc6016244293f6
OK

# 此时查看集群节点数
redis-cli -c -h 192.168.1.100 -p 7000 cluster nodes

[root@node-01 redis]# redis-cli -c -h 192.168.1.100 -p 7000 cluster nodes
a10c431ac4acb90c1da27fd76fd8c3479b32a902 192.168.1.200:7000@17000 master - 0 1721207538000 3 connected 5728-10922
844876c169666de7665c51b5bdad01807ee62ae4 192.168.1.200:7001@17001 slave 9037a70744f728d436a7c37e5e841f3b1ae3f099 0 1721207539000 1 connected
cf6df070f4102a3c5cc8d4577ab30e9dee5906b4 192.168.1.250:7000@17000 master - 0 1721207542440 5 connected 11189-16383
9037a70744f728d436a7c37e5e841f3b1ae3f099 192.168.1.100:7000@17000 myself,master - 0 1721207541000 1 connected 266-5460
55792b63bc0b23b1900b5c9ee03a488f8ed76988 192.168.1.100:7001@17001 slave cf6df070f4102a3c5cc8d4577ab30e9dee5906b4 0 1721207541000 5 connected
90f1e06b26cbdcbe884f22aba7fc37e98ca5d58f 192.168.1.250:7001@17001 slave a10c431ac4acb90c1da27fd76fd8c3479b32a902 0 1721207540400 3 connected
6c15b61a14a10fdc8b122708b6240b9fe3cbb2f6 192.168.1.250:7002@17002 master - 0 1721207541422 0 connected
990bb934a52a74ec9ebb2b73e9dc6016244293f6 192.168.1.200:7002@17002 master - 0 1721207540000 7 connected 0-265 5461-5727 10923-11188
[root@node-01 redis]# redis-cli -c -h 192.168.1.100 -p 7000 cluster nodes
a10c431ac4acb90c1da27fd76fd8c3479b32a902 192.168.1.200:7000@17000 master - 0 1721207800010 3 connected 5728-10922
844876c169666de7665c51b5bdad01807ee62ae4 192.168.1.200:7001@17001 slave 9037a70744f728d436a7c37e5e841f3b1ae3f099 0 1721207800000 1 connected
cf6df070f4102a3c5cc8d4577ab30e9dee5906b4 192.168.1.250:7000@17000 master - 0 1721207796000 5 connected 11189-16383
9037a70744f728d436a7c37e5e841f3b1ae3f099 192.168.1.100:7000@17000 myself,master - 0 1721207799000 1 connected 266-5460
55792b63bc0b23b1900b5c9ee03a488f8ed76988 192.168.1.100:7001@17001 slave cf6df070f4102a3c5cc8d4577ab30e9dee5906b4 0 1721207801000 5 connected
90f1e06b26cbdcbe884f22aba7fc37e98ca5d58f 192.168.1.250:7001@17001 slave a10c431ac4acb90c1da27fd76fd8c3479b32a902 0 1721207800000 3 connected
6c15b61a14a10fdc8b122708b6240b9fe3cbb2f6 192.168.1.250:7002@17002 slave 990bb934a52a74ec9ebb2b73e9dc6016244293f6 0 1721207798981 7 connected
990bb934a52a74ec9ebb2b73e9dc6016244293f6 192.168.1.200:7002@17002 master - 0 1721207801033 7 connected 0-265 5461-5727 10923-11188
# 至此集群扩容完毕

7.5 集群缩容节点

注意:先将缩容节点槽位信息移动到其他master 如果不这么操作 可能会导致槽位数据丢失

sh 复制代码
# 删除信息之前查询集群中的节点数量
redis-cli -c -h 192.168.1.100 -p 7000 cluster nodes
[root@node-01 redis]# redis-cli -c -h 192.168.1.100 -p 7000 cluster nodes
a10c431ac4acb90c1da27fd76fd8c3479b32a902 192.168.1.200:7000@17000 master - 0 1721208952176 3 connected 5728-10922
844876c169666de7665c51b5bdad01807ee62ae4 192.168.1.200:7001@17001 slave 9037a70744f728d436a7c37e5e841f3b1ae3f099 0 1721208951000 1 connected
cf6df070f4102a3c5cc8d4577ab30e9dee5906b4 192.168.1.250:7000@17000 master - 0 1721208953000 5 connected 11189-16383
9037a70744f728d436a7c37e5e841f3b1ae3f099 192.168.1.100:7000@17000 myself,master - 0 1721208949000 1 connected 266-5460
55792b63bc0b23b1900b5c9ee03a488f8ed76988 192.168.1.100:7001@17001 slave cf6df070f4102a3c5cc8d4577ab30e9dee5906b4 0 1721208953199 5 connected
90f1e06b26cbdcbe884f22aba7fc37e98ca5d58f 192.168.1.250:7001@17001 slave a10c431ac4acb90c1da27fd76fd8c3479b32a902 0 1721208953000 3 connected
6c15b61a14a10fdc8b122708b6240b9fe3cbb2f6 192.168.1.250:7002@17002 slave 990bb934a52a74ec9ebb2b73e9dc6016244293f6 0 1721208954216 7 connected
990bb934a52a74ec9ebb2b73e9dc6016244293f6 192.168.1.200:7002@17002 master - 0 1721208950131 7 connected 0-265 5461-5727 10923-11188

# 把192.168.1.200:7002的槽位信息分配给192.168.1.100:7000
redis-cli --cluster reshard 192.168.1.100:7000

[OK] All 16384 slots covered.
How many slots do you want to move (from 1 to 16384)? 800
What is the receiving node ID? 9037a70744f728d436a7c37e5e841f3b1ae3f099 #192.168.1.100:7000 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: 990bb934a52a74ec9ebb2b73e9dc6016244293f6
(ps:这里是需要数据源,也就是我们的192.168.1.200:7002节点id)
Source node 2:done
(ps:这里直接输入done 开始生成迁移计划)
Do you want to proceed with the proposed reshard plan (yes/no)? Yes
(ps:这里输入yes开始迁移)

# 删除 192.168.1.200:7002 	主节点
redis-cli --cluster del-node 192.168.1.200:7002 990bb934a52a74ec9ebb2b73e9dc6016244293f6

[root@node-01 redis]# redis-cli --cluster del-node 192.168.1.200:7002 990bb934a52a74ec9ebb2b73e9dc6016244293f6
>>> Removing node 990bb934a52a74ec9ebb2b73e9dc6016244293f6 from cluster 192.168.1.200:7002
>>> Sending CLUSTER FORGET messages to the cluster...
>>> Sending CLUSTER RESET SOFT to the deleted node.


# 删除 192.168.1.250:7002   从节点
redis-cli --cluster del-node 192.168.1.250:7002 6c15b61a14a10fdc8b122708b6240b9fe3cbb2f6

[root@node-01 redis]# redis-cli --cluster del-node 192.168.1.250:7002 6c15b61a14a10fdc8b122708b6240b9fe3cbb2f6
>>> Removing node 6c15b61a14a10fdc8b122708b6240b9fe3cbb2f6 from cluster 192.168.1.250:7002
>>> Sending CLUSTER FORGET messages to the cluster...
>>> Sending CLUSTER RESET SOFT to the deleted node.


# 再次查看节点数量
redis-cli -c -h 192.168.1.100 -p 7000 cluster nodes
[root@node-01 redis]# redis-cli -c -h 192.168.1.100 -p 7000 cluster nodes
a10c431ac4acb90c1da27fd76fd8c3479b32a902 192.168.1.200:7000@17000 master - 0 1721209888104 3 connected 5728-10922
844876c169666de7665c51b5bdad01807ee62ae4 192.168.1.200:7001@17001 slave 9037a70744f728d436a7c37e5e841f3b1ae3f099 0 1721209889128 8 connected
cf6df070f4102a3c5cc8d4577ab30e9dee5906b4 192.168.1.250:7000@17000 master - 0 1721209887085 5 connected 11189-16383
9037a70744f728d436a7c37e5e841f3b1ae3f099 192.168.1.100:7000@17000 myself,master - 0 1721209887000 8 connected 0-5727 10923-11188
55792b63bc0b23b1900b5c9ee03a488f8ed76988 192.168.1.100:7001@17001 slave cf6df070f4102a3c5cc8d4577ab30e9dee5906b4 0 1721209890146 5 connected
90f1e06b26cbdcbe884f22aba7fc37e98ca5d58f 192.168.1.250:7001@17001 slave a10c431ac4acb90c1da27fd76fd8c3479b32a902 0 1721209889000 3 connected

# 已经成功移除节点
相关推荐
C++忠实粉丝9 分钟前
Redis 介绍和安装
数据库·redis·缓存
wmd1316430671224 分钟前
将微信配置信息存到数据库并进行调用
数据库·微信
丰云26 分钟前
一个简单封装的的nodejs缓存对象
缓存·node.js
Oneforlove_twoforjob28 分钟前
【Java基础面试题025】什么是Java的Integer缓存池?
java·开发语言·缓存
泰伦闲鱼31 分钟前
nestjs:GET REQUEST 缓存问题
服务器·前端·缓存·node.js·nestjs
是阿建吖!38 分钟前
【Linux】基础IO(磁盘文件)
linux·服务器·数据库
凡人的AI工具箱1 小时前
每天40分玩转Django:Django国际化
数据库·人工智能·后端·python·django·sqlite
ClouGence1 小时前
Redis 到 Redis 数据迁移同步
数据库·redis·缓存
m0_748236581 小时前
《Web 应用项目开发:从构思到上线的全过程》
服务器·前端·数据库
苏三说技术1 小时前
Redis 性能优化的18招
数据库·redis·性能优化