文章目录
- [1 编译安装](#1 编译安装)
- [2 主从复制](#2 主从复制)
-
- [2.1 配置文件修改](#2.1 配置文件修改)
- [2.2 状态检测](#2.2 状态检测)
- [2.3 数据同步性检测](#2.3 数据同步性检测)
- [3 哨兵](#3 哨兵)
-
- [3.1 配置文件](#3.1 配置文件)
- [3.2 哨兵状态](#3.2 哨兵状态)
- [3.3 故障测试](#3.3 故障测试)
- [4 cluster](#4 cluster)
-
- [4.1 配置文件](#4.1 配置文件)
- [4.2 创建集群](#4.2 创建集群)
- [4.3 集群扩、缩容](#4.3 集群扩、缩容)
-
- [4.3.1 克隆新虚拟机](#4.3.1 克隆新虚拟机)
- [4.3.2 增加slave](#4.3.2 增加slave)
- [4.3.3 增加master](#4.3.3 增加master)
- [4.3.4 集群缩容](#4.3.4 集群缩容)
1 编译安装
bash
[root@redis-node1 ~]# rpm -q make gcc initscripts
make-4.3-8.el9.x86_64
gcc-11.5.0-14.el9.x86_64
package initscripts is not installed
[root@redis-node1 ~]# dnf install -yq initscripts
Installed:
chkconfig-1.24-2.el9.x86_64 initscripts-10.11.8-4.el9.x86_64
[root@redis-node1 ~]# wget -P /tmp/ https://download.redis.io/releases/redis-7.4.8.tar.gz
[root@redis-node1 ~]# tar -zxf /tmp/redis-7.4.8.tar.gz -C .
[root@redis-node1 ~]# ls | grep redis-7.4.8
redis-7.4.8
[root@redis-node1 ~]# cd redis-7.4.8/
[root@redis-node1 redis-7.4.8]# make && make install
[root@redis-node1 redis-7.4.8]# cd utils/
[root@redis-node1 utils]# vim install_server.sh # 解决报错问题
/_pid
:77,83s /^/#/
76 #bail if this system is managed by systemd
77 #_pid_1_exe="$(readlink -f /proc/1/exe)"
78 #if [ "${_pid_1_exe##*/}" = systemd ]
79 #then
80 # echo "This systems seems to use systemd."
81 # echo "Please take a look at the provided example service unit files in this directory, and adapt and install the m. Sorry!"
82 # exit 1
83 #fi
[root@redis-node1 utils]# ./install_server.sh
Welcome to the redis service installer
This script will help you easily set up a running redis server
Please select the redis port for this instance: [6379]
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf] /etc/redis/redis.conf
Please select the redis log file name [/var/log/redis_6379.log]
Selected default - /var/log/redis_6379.log
Please select the data directory for this instance [/var/lib/redis/6379]
Selected default - /var/lib/redis/6379
Please select the redis executable path [/usr/local/bin/redis-server]
Selected config:
Port : 6379
Config file : /etc/redis/redis.conf
Log file : /var/log/redis_6379.log
Data dir : /var/lib/redis/6379
Executable : /usr/local/bin/redis-server
Cli Executable : /usr/local/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6379.conf => /etc/init.d/redis_6379
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
Starting Redis server...
Installation successful!
[root@redis-node1 utils]# systemctl daemon-reload
[root@redis-node1 utils]# systemctl status redis_6379.service | egrep -i "active|loaded"
Loaded: loaded (/etc/rc.d/init.d/redis_6379; generated)
Active: inactive (dead)
[root@redis-node1 utils]# systemctl start redis_6379.service
[root@redis-node1 utils]# netstat -antlpe | grep redis
tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 0 76897 46385/redis-server
tcp6 0 0 ::1:6379 :::* LISTEN 0 76898 46385/redis-server
[root@redis-node1 ~]# for i in {20,30};do
> scp /tmp/redis-7.4.8.tar.gz root@172.25.254.$i:/tmp/
> done
[root@redis-node1 ~]# for i in {20,30};do scp /tmp/redis-7.4.8.tar.gz root@172.25.254.$i:/tmp/; done
..................
2 主从复制
2.1 配置文件修改
bash
# master
[root@redis-node1 ~]# vim /etc/redis/redis.conf
89 #bind 127.0.0.1 -::1
90 bind * -::*
114 #protected-mode yes
115 protected-mode no # redis保护模式
[root@redis-node1 ~]# systemctl restart redis_6379.service
[root@redis-node2、3 ~]# vim /etc/redis/redis.conf
90 bind * -::*
112 protected-mode no
113 replicaof 172.25.254.10 6379
2.2 状态检测
bash
# 测试
[root@redis-node2、3 ~]# systemctl start redis_6379.service
[root@redis-node3 utils]# redis-cli
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:0
master_failover_state:no-failover
master_replid:b7fb57573055e2ebda2659f2a85c6a843996041f
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
127.0.0.1:6379> exit
[root@redis-node3 utils]# netstat -lntupa | grep redis
tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 38391/redis-server
tcp6 0 0 ::1:6379 :::* LISTEN 38391/redis-server
[root@redis-node3 utils]# systemctl restart redis_6379.service
[root@redis-node3 utils]# netstat -lntupa | grep redis
tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 38510/redis-server
tcp6 0 0 :::6379 :::* LISTEN 38510/redis-server
[root@redis-node3 utils]# redis-cli
127.0.0.1:6379> info replication
# Replication
role:slave
master_host:172.25.254.10
master_port:6379
master_link_status:down
master_last_io_seconds_ago:-1
master_sync_in_progress:0
slave_read_repl_offset:1
slave_repl_offset:1
master_link_down_since_seconds:-1
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:360b8864582f4dcc7eed18aff6ae36b1177e45f8
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
127.0.0.1:6379>
# 解决问题
[root@redis-node3 utils]# nc -zv 172.25.254.10 6379
Ncat: Version 7.92 ( https://nmap.org/ncat )
Ncat: Connected to 172.25.254.10:6379.
Ncat: 0 bytes sent, 0 bytes received in 0.01 seconds.
[root@redis-node1 ~]# egrep "^bind|^protected-mode" /etc/redis/redis.conf
bind * -::*
protected-mode no
protected-mode yes
[root@redis-node1、2、3 ~]# vim /etc/redis/redis.conf
587 #protected-mode yes
588 protected-mode no
[root@redis-node1 ~]# systemctl restart redis_6379.service
# 再次查看状态
[root@redis-node1 ~]# redis-cli
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=172.25.254.20,port=6379,state=online,offset=182,lag=1
slave1:ip=172.25.254.30,port=6379,state=online,offset=182,lag=0
master_failover_state:no-failover
master_replid:cbab9fd2d10b34f2b0a3c7ec912cb6afd783a94b
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:182
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:182
[root@redis-node2 ~]# redis-cli info replication
# Replication
role:slave
master_host:172.25.254.10
master_port:6379
master_link_status:up
master_last_io_seconds_ago:5
master_sync_in_progress:0
slave_read_repl_offset:210
slave_repl_offset:210
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:cbab9fd2d10b34f2b0a3c7ec912cb6afd783a94b
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:210
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:210
[root@redis-node3 utils]# redis-cli info replication
# Replication
role:slave
master_host:172.25.254.10
master_port:6379
master_link_status:up
master_last_io_seconds_ago:3
master_sync_in_progress:0
slave_read_repl_offset:224
slave_repl_offset:224
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:cbab9fd2d10b34f2b0a3c7ec912cb6afd783a94b
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:224
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:224
2.3 数据同步性检测
bash
[root@redis-node1 ~]# redis-cli set name xixi
OK
[root@redis-node1 ~]# redis-cli get name
"xixi"
[root@redis-node2 ~]# redis-cli get name
"xixi"
[root@redis-node3 ~]# redis-cli get name
"xixi"
3 哨兵
3.1 配置文件
bash
# master
[root@redis-node1 ~]# cp -p redis-7.4.8/sentinel.conf /etc/redis/
[root@redis-node1 ~]# vim /etc/redis/sentinel.conf
92 #sentinel monitor mymaster 127.0.0.1 6379 2
93 sentinel monitor mymaster 172.25.254.100 6379 2
[root@redis-node1 ~]# egrep "^protected-mode|^port|^daemonize|^pidfile|^loglevel|^sentinel" /etc/redis/sentinel.conf
protected-mode no # 关闭保护模式
port 26379 # 监听端口
daemonize no # 进入不打入后台
pidfile /var/run/redis-sentinel.pid # sentinel进程pid文件
loglevel notice # 日志级别
sentinel monitor mymaster 172.25.254.10 6379 2 # 创建sentinel监控监控master主机,2表示必须得到2票
sentinel down-after-milliseconds mymaster 30000 # master中断时长,30秒连不上视为master下线
sentinel parallel-syncs mymaster 1 # 发生故障转移后,同时开始同步新master数据的slave数量
sentinel failover-timeout mymaster 180000 # 整个故障切换的超时时间为3分钟
sentinel deny-scripts-reconfig yes
[root@redis-node1 ~]# for i in {20,30};do scp /etc/redis/sentinel.conf root@172.25.254.$i:/etc/redis/ ;done
# 从节点
[root@redis-node3 ~]# grep protected-mode /etc/redis/redis.conf
protected-mode no
[root@redis-node2 ~]# grep protected-mode /etc/redis/redis.conf
protected-mode no
# 所有节点开启哨兵
[root@redis-node1 ~]# systemctl restart redis_6379.service
[root@redis-node1 ~]# redis-sentinel /etc/redis/sentinel.conf
3.2 哨兵状态
bash
[root@redis-node3 ~]# redis-sentinel /etc/redis/sentinel.conf
38723:X 28 Mar 2026 19:22:26.801 # WARNING Memory overcommit must be enabled! Without it, a background save or replication may fail under low memory condition. Being disabled, it can also cause failures without low memory condition, see https://github.com/jemalloc/jemalloc/issues/1328. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
38723:X 28 Mar 2026 19:22:26.801 * oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
38723:X 28 Mar 2026 19:22:26.801 * Redis version=7.4.8, bits=64, commit=00000000, modified=0, pid=38723, just started
38723:X 28 Mar 2026 19:22:26.801 * Configuration loaded
38723:X 28 Mar 2026 19:22:26.801 * Increased maximum number of open files to 10032 (it was originally set to 1024).
38723:X 28 Mar 2026 19:22:26.801 * monotonic clock: POSIX clock_gettime
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis Community Edition
.-`` .-```. ```\/ _.,_ ''-._ 7.4.8 (00000000/0) 64 bit
( ' , .-` | `, ) Running in sentinel mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 26379
| `-._ `._ / _.-' | PID: 38723
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | https://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
38723:X 28 Mar 2026 19:22:26.803 * Sentinel new configuration saved on disk
38723:X 28 Mar 2026 19:22:26.803 * Sentinel ID is ba06897c971e936a89922ac77bbc89f5ea13ccde
38723:X 28 Mar 2026 19:22:26.803 # +monitor master mymaster 172.25.254.10 6379 quorum 2
38723:X 28 Mar 2026 19:22:27.058 * +sentinel sentinel b775988b27f82351cc1d9367777cc6e9840e90d1 172.25.254.10 26379 @ mymaster 172.25.254.10 6379
38723:X 28 Mar 2026 19:22:27.060 * Sentinel new configuration saved on disk
38723:X 28 Mar 2026 19:22:28.748 * +sentinel sentinel 949f4e81aefce8d8b0d1279e8491bd867c5b971d 172.25.254.20 26379 @ mymaster 172.25.254.10 6379
38723:X 28 Mar 2026 19:22:28.749 * Sentinel new configuration saved on disk
# 不想另起终端可以在配置文件中把daemonize yes
[root@redis-node1 ~]# redis-cli -p 26379 info sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_tilt_since_seconds:-1
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=172.25.254.10:6379,slaves=2,sentinels=3
3.3 故障测试
bash
[root@redis-node1 ~]# redis-cli shutdown
# 等一下30秒
[root@redis-node1 ~]# redis-cli -h 172.25.254.20 info replication
# Replication
role:slave
master_host:172.25.254.30
master_port:6379
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_read_repl_offset:262180
slave_repl_offset:262180
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:c288f3444d0d00425004d1549437cdabd1e8af09
master_replid2:954e3febfde9a863f361f176aba17bab06744a2a
master_repl_offset:262180
second_repl_offset:260170
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:3839
repl_backlog_histlen:258342
[root@redis-node1 ~]# redis-cli -h 172.25.254.30 info replication
# Replication
role:master
connected_slaves:1
slave0:ip=172.25.254.20,port=6379,state=online,offset=264309,lag=1
master_failover_state:no-failover
master_replid:c288f3444d0d00425004d1549437cdabd1e8af09
master_replid2:954e3febfde9a863f361f176aba17bab06744a2a
master_repl_offset:264309
second_repl_offset:260170
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2787
repl_backlog_histlen:261523
# 现象
[root@redis-node1 ~]# systemctl status redis_6379.service
● redis_6379.service - LSB: start and stop redis_6379
Loaded: loaded (/etc/rc.d/init.d/redis_6379; generated)
Active: active (exited) since Sat 2026-03-28 18:58:17 CST; 28min ago
Docs: man:systemd-sysv-generator(8)
Process: 47066 ExecStart=/etc/rc.d/init.d/redis_6379 start (code=exited, status=0/SUCCESS)
CPU: 1.818s
Mar 28 18:58:17 redis-node1 systemd[1]: Starting LSB: start and stop redis_6379...
Mar 28 18:58:17 redis-node1 redis_6379[47066]: Starting Redis server...
Mar 28 18:58:17 redis-node1 systemd[1]: Started LSB: start and stop redis_6379.
[root@redis-node1 ~]# ps -aux | grep redis
root 47102 0.3 0.3 134148 12704 pts/0 Sl+ 19:01 0:05 redis-sentinel *:26379 [sentinel]
root 47198 0.0 0.0 6416 2304 pts/1 S+ 19:27 0:00 grep --color=auto redis
[root@redis-node1 ~]# netstat -lntupa | grep 6379
tcp 0 0 0.0.0.0:26379 0.0.0.0:* LISTEN 47102/redis-sentine
tcp 0 0 172.25.254.10:55666 172.25.254.30:6379 ESTABLISHED 47102/redis-sentine
tcp 0 0 172.25.254.10:40474 172.25.254.20:6379 ESTABLISHED 47102/redis-sentine
tcp 0 0 172.25.254.10:26379 172.25.254.30:55360 ESTABLISHED 47102/redis-sentine
tcp 0 0 172.25.254.10:55840 172.25.254.30:26379 ESTABLISHED 47102/redis-sentine
tcp 0 0 172.25.254.10:41808 172.25.254.20:26379 ESTABLISHED 47102/redis-sentine
tcp 0 0 172.25.254.10:40462 172.25.254.20:6379 ESTABLISHED 47102/redis-sentine
tcp 0 0 172.25.254.10:26379 172.25.254.20:46084 ESTABLISHED 47102/redis-sentine
tcp 0 0 172.25.254.10:55680 172.25.254.30:6379 ESTABLISHED 47102/redis-sentine
tcp6 0 0 :::26379 :::* LISTEN 47102/redis-sentine
# [root@redis-node1 ~]# /etc/init.d/redis_6379 start
[root@redis-node1 ~]# redis-server /etc/redis/redis.conf
[root@redis-node1 ~]# ps -aux | grep redis
root 47102 0.3 0.3 134148 12704 pts/0 Sl+ 19:01 0:06 redis-sentinel *:26379 [sentinel]
root 47202 0.3 0.3 138756 11716 ? Ssl 19:28 0:00 redis-server *:6379
root 47215 0.0 0.0 6416 2304 pts/1 S+ 19:30 0:00 grep --color=auto redis
[root@redis-node1 ~]# redis-cli -h 172.25.254.30 info replication
# Replication
role:master
connected_slaves:2
slave0:ip=172.25.254.20,port=6379,state=online,offset=311861,lag=0
slave1:ip=172.25.254.10,port=6379,state=online,offset=311861,lag=0
master_failover_state:no-failover
master_replid:c288f3444d0d00425004d1549437cdabd1e8af09
master_replid2:954e3febfde9a863f361f176aba17bab06744a2a
master_repl_offset:311861
second_repl_offset:260170
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2787
repl_backlog_histlen:309075
[root@redis-node1 ~]# redis-cli info replication
# Replication
role:slave
master_host:172.25.254.30
master_port:6379
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_read_repl_offset:314554
slave_repl_offset:314554
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:c288f3444d0d00425004d1549437cdabd1e8af09
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:314554
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:300066
repl_backlog_histlen:14489
4 cluster
4.1 配置文件
bash
[root@redis-node1 ~]# systemctl stop redis_6379.service
[root@redis-node1 ~]# rm -rf /var/lib/redis/6379/* # 把redis进程的工作目录删了,环境保持干净
[root@redis-node1 ~]# vim /etc/redis/redis.conf
[root@redis-node1 ~]# egrep -i "^masterauth|^requirepass|^cluster|^replicaof|^protected" /etc/redis/redis.conf
masterauth "123"
requirepass "123"
cluster-enabled yes
cluster-config-file cluster-node-6379.conf
cluster-node-timeout 15000
[root@redis-node1 ~]# tail -n10 /etc/redis/redis.conf
#
# ignore-warnings ARM64-COW-BUG
# Generated by CONFIG REWRITE
latency-tracking-info-percentiles 50 99 99.9
save 3600 1
save 300 100
save 60 10000
#replicaof 172.25.254.30 6379
#user default on nopass sanitize-payload ~* &* +@all #清理冲突的ACL配置
[root@redis-node1 ~]# systemctl start redis_6379.service
[root@redis-node1 ~]# ll /var/lib/redis/6379/
total 4
-rw-r--r-- 1 root root 176 Mar 29 15:03 cluster-node-6379.conf
[root@redis-node1 ~]# journalctl -eu redis_6379.service | tail -n5
Mar 29 15:02:13 redis-node1 systemd[1]: redis_6379.service: Deactivated successfully.
Mar 29 15:02:13 redis-node1 systemd[1]: Stopped LSB: start and stop redis_6379.
Mar 29 15:03:26 redis-node1 systemd[1]: Starting LSB: start and stop redis_6379...
Mar 29 15:03:26 redis-node1 redis_6379[2296]: Starting Redis server...
Mar 29 15:03:26 redis-node1 systemd[1]: Started LSB: start and stop redis_6379.
[root@redis-node1 ~]# for i in {20,30};do scp /etc/redis/redis.conf root@172.25.254.$i:/etc/redis/ ;done
# 其它节点
[root@redis-node2 ~]# systemctl stop redis_6379.service
[root@redis-node2 ~]# rm -rf /var/lib/redis/6379/*
[root@redis-node2 ~]# systemctl start redis_6379.service
[root@redis-node2 ~]# ll /var/lib/redis/6379/*
-rw-r--r-- 1 root root 176 Mar 29 15:10 /var/lib/redis/6379/cluster-node-6379.conf
# 验证
[root@redis-node1 ~]# redis-cli -a 123 ping
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
PONG
[root@redis-node1 ~]# redis-cli -h 172.25.254.20 -a 123 ping
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
PONG
[root@redis-node1 ~]# redis-cli -h 172.25.254.30 -a 123 ping
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
PONG
4.2 创建集群
create集群时的最少节点3个-->3主,然后后续需要扩容时再慢慢添加master、slave;也可以直接create搞六个节点-->三主三从。
bashredis-cli -a 123 --cluster create 172.25.254.10:6379 172.25.254.20:6379 172.25.254.30:6379 节点4:端口 节点5:端口 节点6:端口 --cluster-replicas 1 # --cluster-replicas 1 表示每个主节点分配一个从节点,Redis 会自动决定哪个节点是主、哪个是从。适合一主二从:主从复制 + 哨兵。
bash
[root@redis-node1 ~]# redis-cli --cluster create 172.25.254.10:6379 172.25.254.20:6379 172.25.254.30:6379 --cluster-replicas 0
[ERR] Node 172.25.254.10:6379 NOAUTH Authentication required.
[root@redis-node1 ~]# redis-cli -a 123 --cluster create 172.25.254.10:6379 172.25.254.20:6379 172.25.254.30:6379 --cluster-replicas 0
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing hash slots allocation on 3 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
M: 28d77f22ce4a290692954cb722b76583a0643d1f 172.25.254.10:6379
slots:[0-5460] (5461 slots) master
M: b938263a21f9473f8decd8ad14ec183a5f201954 172.25.254.20:6379
slots:[5461-10922] (5462 slots) master
M: 35af6ab3185ef51d23464d0917adcfc3a90bf917 172.25.254.30:6379
slots:[10923-16383] (5461 slots) master
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 172.25.254.10:6379)
M: 28d77f22ce4a290692954cb722b76583a0643d1f 172.25.254.10:6379
slots:[0-5460] (5461 slots) master
M: b938263a21f9473f8decd8ad14ec183a5f201954 172.25.254.20:6379
slots:[5461-10922] (5462 slots) master
M: 35af6ab3185ef51d23464d0917adcfc3a90bf917 172.25.254.30:6379
slots:[10923-16383] (5461 slots) master
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
# 检测集群状态
[root@redis-node1 ~]# redis-cli -a 123 cluster nodes
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
b938263a21f9473f8decd8ad14ec183a5f201954 172.25.254.20:6379@16379 master - 0 1774770701730 2 connected 5461-10922
28d77f22ce4a290692954cb722b76583a0643d1f 172.25.254.10:6379@16379 myself,master - 0 0 1 connected 0-5460
35af6ab3185ef51d23464d0917adcfc3a90bf917 172.25.254.30:6379@16379 master - 0 1774770700663 3 connected 10923-16383
[root@redis-node1 ~]# redis-cli -a 123 --cluster info 172.25.254.10:6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
172.25.254.10:6379 (28d77f22...) -> 0 keys | 5461 slots | 0 slaves.
172.25.254.20:6379 (b938263a...) -> 0 keys | 5462 slots | 0 slaves.
172.25.254.30:6379 (35af6ab3...) -> 0 keys | 5461 slots | 0 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.
# 查看集群信息
[root@redis-node1 ~]# redis-cli -a 123 cluster info
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:3
cluster_size:3
cluster_current_epoch:3
cluster_my_epoch:1
cluster_stats_messages_ping_sent:400
cluster_stats_messages_pong_sent:380
cluster_stats_messages_sent:780
cluster_stats_messages_ping_received:378
cluster_stats_messages_pong_received:400
cluster_stats_messages_meet_received:2
cluster_stats_messages_received:780
total_cluster_links_buffer_limit_exceeded:0
# 查看集群健康状态
[root@redis-node1 ~]# redis-cli -a 123 --cluster check 172.25.254.10:6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
172.25.254.10:6379 (28d77f22...) -> 0 keys | 5461 slots | 0 slaves.
172.25.254.20:6379 (b938263a...) -> 0 keys | 5462 slots | 0 slaves.
172.25.254.30:6379 (35af6ab3...) -> 0 keys | 5461 slots | 0 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 172.25.254.10:6379)
M: 28d77f22ce4a290692954cb722b76583a0643d1f 172.25.254.10:6379
slots:[0-5460] (5461 slots) master
M: b938263a21f9473f8decd8ad14ec183a5f201954 172.25.254.20:6379
slots:[5461-10922] (5462 slots) master
M: 35af6ab3185ef51d23464d0917adcfc3a90bf917 172.25.254.30:6379
slots:[10923-16383] (5461 slots) master
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
4.3 集群扩、缩容
使用了
--cluster-replicas 0,因此每个主节点没有从节点 。这意味着:
- 如果任意一个节点宕机,它负责的槽位数据将不可用,集群会进入
fail状态(部分槽位无法访问)。- 如果需要高可用,每个主节点至少添加一个从节点(至少需要 6 个节点,或通过
--cluster add-node添加从节点)。
4.3.1 克隆新虚拟机
为了同时开启多台虚拟机,可以减低CPU、内存来,直接把配置文件、编译后的二进制文件、init脚本scp过去就行不用再编译耗时间。
bash
[root@redis-node1 ~]# ll /usr/local/bin/ | grep redis.*
-rwxr-xr-x 1 root root 6381992 Mar 28 17:22 redis-benchmark
lrwxrwxrwx 1 root root 12 Mar 28 17:22 redis-check-aof -> redis-server
lrwxrwxrwx 1 root root 12 Mar 28 17:22 redis-check-rdb -> redis-server
-rwxr-xr-x 1 root root 7223064 Mar 28 17:22 redis-cli
lrwxrwxrwx 1 root root 12 Mar 28 17:22 redis-sentinel -> redis-server
-rwxr-xr-x 1 root root 16532528 Mar 28 17:22 redis-server
[root@redis-node1 ~]# vim new-redis-cluster.sh
[root@redis-node1 ~]# cat new-redis-cluster.sh
#!/bin/bash
for IP in {40,50,60,70,80};do
ssh root@172.25.254.$IP "mkdir -p /etc/redis /var/lib/redis/6379 /var/log/redis"
scp /etc/redis/redis.conf root@172.25.254.$IP:/etc/redis/
scp /usr/local/bin/redis* root@172.25.254.$IP:/usr/local/bin/
scp /etc/rc.d/init.d/redis_6379 root@172.25.254.$IP:/etc/rc.d/init.d/
ssh root@172.25.254.$IP "systemctl daemon-reload && systemctl start redis_6379.service"
done
[root@redis-node1 ~]# sh new-redis-cluster.sh >/dev/null
Warning: Permanently added '172.25.254.40' (ED25519) to the list of known hosts.
Warning: Permanently added '172.25.254.50' (ED25519) to the list of known hosts.
Warning: Permanently added '172.25.254.60' (ED25519) to the list of known hosts.
Warning: Permanently added '172.25.254.70' (ED25519) to the list of known hosts.
Warning: Permanently added '172.25.254.80' (ED25519) to the list of known hosts.
[root@redis-node1 ~]# for IP in {40,50,60,70,80};do redis-cli -h 172.25.254.$IP -a 123 ping ;done
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
PONG
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
PONG
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
PONG
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
PONG
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
PONG
4.3.2 增加slave
为每个master添加slave实现高可用。
注意事项:
- 新节点必须为空 (没有数据,没有分配槽位),且未加入任何集群 。如果之前启动过,建议清空
cluster-config-file指定的文件(如cluster-node-6379.conf)并重启 Redis。- 网络互通:确保新节点能与集群中所有节点通信(端口 6379 和 16379 开放)。
- 密码一致 :新节点的
redis.conf中也要设置相同的requirepass和masterauth(否则无法通过认证)。- 集群状态 :执行命令前,集群必须是正常的(
cluster_state:ok),否则 add-node 可能失败。
bash
[root@redis-node1 ~]# redis-cli -a 123 cluster nodes
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
b938263a21f9473f8decd8ad14ec183a5f201954 172.25.254.20:6379@16379 master - 0 1774777295498 2 connected 5461-10922
28d77f22ce4a290692954cb722b76583a0643d1f 172.25.254.10:6379@16379 myself,master - 0 0 1 connected 0-5460
35af6ab3185ef51d23464d0917adcfc3a90bf917 172.25.254.30:6379@16379 master - 0 1774777294430 3 connected 10923-16383
[root@redis-node1 ~]#
[root@redis-node1 ~]# redis-cli -a 123 --cluster add-node 172.25.254.40:6379 172.25.254.10:6379 --cluster-slave --cluster-master-id 28d77f22ce4a290692954cb722b76583a0643d1f
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Adding node 172.25.254.40:6379 to cluster 172.25.254.10:6379
>>> Performing Cluster Check (using node 172.25.254.10:6379)
M: 28d77f22ce4a290692954cb722b76583a0643d1f 172.25.254.10:6379
slots:[0-5460] (5461 slots) master
M: b938263a21f9473f8decd8ad14ec183a5f201954 172.25.254.20:6379
slots:[5461-10922] (5462 slots) master
M: 35af6ab3185ef51d23464d0917adcfc3a90bf917 172.25.254.30:6379
slots:[10923-16383] (5461 slots) master
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 172.25.254.40:6379 to make it join the cluster.
Waiting for the cluster to join
>>> Configure node as replica of 172.25.254.10:6379.
[OK] New node added correctly.
[root@redis-node1 ~]# redis-cli -a 123 --cluster add-node 172.25.254.50:6379 172.25.254.20:6379 --cluster-slave --cluster-master-id b938263a21f9473f8decd8ad14ec183a5f201954
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Adding node 172.25.254.50:6379 to cluster 172.25.254.20:6379
>>> Performing Cluster Check (using node 172.25.254.20:6379)
M: b938263a21f9473f8decd8ad14ec183a5f201954 172.25.254.20:6379
slots:[5461-10922] (5462 slots) master
M: 28d77f22ce4a290692954cb722b76583a0643d1f 172.25.254.10:6379
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: 35af6ab3185ef51d23464d0917adcfc3a90bf917 172.25.254.30:6379
slots:[10923-16383] (5461 slots) master
S: 9a8d60e5da540524825c1b81762a6264e169df6c 172.25.254.40:6379
slots: (0 slots) slave
replicates 28d77f22ce4a290692954cb722b76583a0643d1f
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 172.25.254.50:6379 to make it join the cluster.
Waiting for the cluster to join
>>> Configure node as replica of 172.25.254.20:6379.
[OK] New node added correctly.
[root@redis-node1 ~]# redis-cli -a 123 --cluster add-node 172.25.254.60:6379 172.25.254.30:6379 --cluster-slave --cluster-master-id 35af6ab3185ef51d23464d0917adcfc3a90bf917
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Adding node 172.25.254.60:6379 to cluster 172.25.254.30:6379
>>> Performing Cluster Check (using node 172.25.254.30:6379)
M: 35af6ab3185ef51d23464d0917adcfc3a90bf917 172.25.254.30:6379
slots:[10923-16383] (5461 slots) master
M: 28d77f22ce4a290692954cb722b76583a0643d1f 172.25.254.10:6379
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: 53a188d30f6ac7495fda2db5acf7f2b2028bb9f1 172.25.254.50:6379
slots: (0 slots) slave
replicates b938263a21f9473f8decd8ad14ec183a5f201954
M: b938263a21f9473f8decd8ad14ec183a5f201954 172.25.254.20:6379
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: 9a8d60e5da540524825c1b81762a6264e169df6c 172.25.254.40:6379
slots: (0 slots) slave
replicates 28d77f22ce4a290692954cb722b76583a0643d1f
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 172.25.254.60:6379 to make it join the cluster.
Waiting for the cluster to join
>>> Configure node as replica of 172.25.254.30:6379.
[OK] New node added correctly.
# 检测集群状态
[root@redis-node1 ~]# redis-cli -a 123 --cluster check 172.25.254.10:6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
172.25.254.10:6379 (28d77f22...) -> 0 keys | 5461 slots | 1 slaves.
172.25.254.20:6379 (b938263a...) -> 0 keys | 5462 slots | 1 slaves.
172.25.254.30:6379 (35af6ab3...) -> 0 keys | 5461 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 172.25.254.10:6379)
M: 28d77f22ce4a290692954cb722b76583a0643d1f 172.25.254.10:6379
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: b938263a21f9473f8decd8ad14ec183a5f201954 172.25.254.20:6379
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: 6fd9f66753dc5a07043ed59f2084fedd4bcee473 172.25.254.60:6379
slots: (0 slots) slave
replicates 35af6ab3185ef51d23464d0917adcfc3a90bf917
M: 35af6ab3185ef51d23464d0917adcfc3a90bf917 172.25.254.30:6379
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 9a8d60e5da540524825c1b81762a6264e169df6c 172.25.254.40:6379
slots: (0 slots) slave
replicates 28d77f22ce4a290692954cb722b76583a0643d1f
S: 53a188d30f6ac7495fda2db5acf7f2b2028bb9f1 172.25.254.50:6379
slots: (0 slots) slave
replicates b938263a21f9473f8decd8ad14ec183a5f201954
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
4.3.3 增加master
执行命令后,
redis-cli会进入一个交互式的问答流程,需要根据提示输入几个参数,具体可以参考下面的说明:
- How many slots do you want to move (from 1 to 16384)?
- 输入你想迁移的槽位数量。目前集群有 3 个
master节点,为了让数据均匀分布,可以让新节点也承担大约16384 ÷ 4 = 4096个槽位,所以这里可以输入4096。- What is the receiving node ID?
- 输入目标节点的 ID,也就是新节点
70的 ID。- 可以在另一个终端通过
redis-cli -a 123 -h 172.25.254.70 cluster nodes | grep myself命令获取。- Please enter all the source node IDs.
- 输入源节点的 ID,即要从哪些节点上移出槽位。
- 这里可以依次输入现有三个
master节点的 ID(10,20,30),每个 ID 输完后按回车确认。如果想从所有现有的master节点平均迁移,可以简单地输入all然后回车。- Do you want to proceed with the proposed reshard plan (yes/no)?
- 仔细检查上面输入的迁移计划,确认无误后输入
yes并回车,Redis 就会开始槽位的迁移。
bash
[root@redis-node1 ~]# redis-cli -a 123 --cluster add-node 172.25.254.70:6379 172.25.254.10:6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Adding node 172.25.254.70:6379 to cluster 172.25.254.10:6379
>>> Performing Cluster Check (using node 172.25.254.10:6379)
M: 28d77f22ce4a290692954cb722b76583a0643d1f 172.25.254.10:6379
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: b938263a21f9473f8decd8ad14ec183a5f201954 172.25.254.20:6379
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: 6fd9f66753dc5a07043ed59f2084fedd4bcee473 172.25.254.60:6379
slots: (0 slots) slave
replicates 35af6ab3185ef51d23464d0917adcfc3a90bf917
M: 35af6ab3185ef51d23464d0917adcfc3a90bf917 172.25.254.30:6379
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 9a8d60e5da540524825c1b81762a6264e169df6c 172.25.254.40:6379
slots: (0 slots) slave
replicates 28d77f22ce4a290692954cb722b76583a0643d1f
S: 53a188d30f6ac7495fda2db5acf7f2b2028bb9f1 172.25.254.50:6379
slots: (0 slots) slave
replicates b938263a21f9473f8decd8ad14ec183a5f201954
[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 172.25.254.70:6379 to verify there is no functions in it
>>> Send FUNCTION RESTORE to 172.25.254.70:6379
>>> Send CLUSTER MEET to node 172.25.254.70:6379 to make it join the cluster.
[OK] New node added correctly.
[root@redis-node1 ~]# redis-cli -a 123 --cluster reshard 172.25.254.10:6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing Cluster Check (using node 172.25.254.10:6379)
M: 28d77f22ce4a290692954cb722b76583a0643d1f 172.25.254.10:6379
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: b938263a21f9473f8decd8ad14ec183a5f201954 172.25.254.20:6379
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: 6fd9f66753dc5a07043ed59f2084fedd4bcee473 172.25.254.60:6379
slots: (0 slots) slave
replicates 35af6ab3185ef51d23464d0917adcfc3a90bf917
M: 35af6ab3185ef51d23464d0917adcfc3a90bf917 172.25.254.30:6379
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 9a8d60e5da540524825c1b81762a6264e169df6c 172.25.254.40:6379
slots: (0 slots) slave
replicates 28d77f22ce4a290692954cb722b76583a0643d1f
S: 53a188d30f6ac7495fda2db5acf7f2b2028bb9f1 172.25.254.50:6379
slots: (0 slots) slave
replicates b938263a21f9473f8decd8ad14ec183a5f201954
M: 0c6beaaa90c141a0aa5dba2ed7c04697b5d6956f 172.25.254.70:6379
slots: (0 slots) master
[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? 0c6beaaa90c141a0aa5dba2ed7c04697b5d6956f
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
..............................
# 检测集群状态
[root@redis-node1 ~]# redis-cli -a 123 --cluster check 172.25.254.10:6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
172.25.254.10:6379 (28d77f22...) -> 0 keys | 4096 slots | 1 slaves.
172.25.254.20:6379 (b938263a...) -> 0 keys | 4096 slots | 1 slaves.
172.25.254.30:6379 (35af6ab3...) -> 0 keys | 4096 slots | 1 slaves.
172.25.254.70:6379 (0c6beaaa...) -> 0 keys | 4096 slots | 0 slaves.
[OK] 0 keys in 4 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 172.25.254.10:6379)
M: 28d77f22ce4a290692954cb722b76583a0643d1f 172.25.254.10:6379
slots:[1365-5460] (4096 slots) master
1 additional replica(s)
M: b938263a21f9473f8decd8ad14ec183a5f201954 172.25.254.20:6379
slots:[6827-10922] (4096 slots) master
1 additional replica(s)
S: 6fd9f66753dc5a07043ed59f2084fedd4bcee473 172.25.254.60:6379
slots: (0 slots) slave
replicates 35af6ab3185ef51d23464d0917adcfc3a90bf917
M: 35af6ab3185ef51d23464d0917adcfc3a90bf917 172.25.254.30:6379
slots:[12288-16383] (4096 slots) master
1 additional replica(s)
S: 9a8d60e5da540524825c1b81762a6264e169df6c 172.25.254.40:6379
slots: (0 slots) slave
replicates 28d77f22ce4a290692954cb722b76583a0643d1f
S: 53a188d30f6ac7495fda2db5acf7f2b2028bb9f1 172.25.254.50:6379
slots: (0 slots) slave
replicates b938263a21f9473f8decd8ad14ec183a5f201954
M: 0c6beaaa90c141a0aa5dba2ed7c04697b5d6956f 172.25.254.70:6379
slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master # 分到slots说明成功了
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
# 给新master添加slave
[root@redis-node1 ~]# redis-cli -a 123 --cluster add-node 172.25.254.80:6379 172.25.254.70:6379 --cluster-slave --cluster-master-id 0c6beaaa90c141a0aa5dba2ed7c04697b5d6956f
..................
[root@redis-node1 ~]# redis-cli -a 123 --cluster check 172.25.254.10:6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
172.25.254.10:6379 (28d77f22...) -> 0 keys | 4096 slots | 1 slaves.
172.25.254.20:6379 (b938263a...) -> 0 keys | 4096 slots | 1 slaves.
172.25.254.30:6379 (35af6ab3...) -> 0 keys | 4096 slots | 1 slaves.
172.25.254.70:6379 (0c6beaaa...) -> 0 keys | 4096 slots | 1 slaves.
[OK] 0 keys in 4 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 172.25.254.10:6379)
M: 28d77f22ce4a290692954cb722b76583a0643d1f 172.25.254.10:6379
slots:[1365-5460] (4096 slots) master
1 additional replica(s)
M: b938263a21f9473f8decd8ad14ec183a5f201954 172.25.254.20:6379
slots:[6827-10922] (4096 slots) master
1 additional replica(s)
S: 6fd9f66753dc5a07043ed59f2084fedd4bcee473 172.25.254.60:6379
slots: (0 slots) slave
replicates 35af6ab3185ef51d23464d0917adcfc3a90bf917
M: 35af6ab3185ef51d23464d0917adcfc3a90bf917 172.25.254.30:6379
slots:[12288-16383] (4096 slots) master
1 additional replica(s)
S: 559c0d27ab940322d0fe777f44a9bbd315f65a81 172.25.254.80:6379
slots: (0 slots) slave
replicates 0c6beaaa90c141a0aa5dba2ed7c04697b5d6956f
S: 9a8d60e5da540524825c1b81762a6264e169df6c 172.25.254.40:6379
slots: (0 slots) slave
replicates 28d77f22ce4a290692954cb722b76583a0643d1f
S: 53a188d30f6ac7495fda2db5acf7f2b2028bb9f1 172.25.254.50:6379
slots: (0 slots) slave
replicates b938263a21f9473f8decd8ad14ec183a5f201954
M: 0c6beaaa90c141a0aa5dba2ed7c04697b5d6956f 172.25.254.70:6379
slots:[0-1364],[5461-6826],[10923-12287] (4096 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.
4.3.4 集群缩容
使用
--cluster reshard命令,将 master30 上的所有槽位(4096 个)平均或按需分配给10、20、70三个 master。
bashredis-cli -a 123 --cluster reshard 172.25.254.10:6379交互式输入(以平均分配为例):
- How many slots do you want to move?
输入1365(master30 拥有的槽位数分为三份:1365、1365、1366)。- What is the receiving node ID?
先输入第一个目标 master(例如10的 ID:28d77f22ce4a290692954cb722b76583a0643d1f),回车。- Please enter all the source node IDs.
输入源节点 ID,即 master30 的 ID:35af6ab3185ef51d23464d0917adcfc3a90bf917,回车。
然后输入done回车。- Do you want to proceed with the proposed reshard plan?
输入yes。当然,也可以直接全都把30的槽位4096都给10,然后10不执行
--cluster reshard命令分给其它的master,但是这样存在明显的负载不均衡
master10承担了 50% 的请求,而其他两个 master 只各承担 25%。- 如果业务请求分布均匀,
master10会成为性能瓶颈。- 后续如果希望重新均衡,还需要再次执行
reshard来调整。
bash
# 上面说的是交互式方式,竟然已经知道了30的槽位数和所有节点的ID,可以直接加上一些参数实现非交互式
[root@redis-node1 ~]# redis-cli -a 123 cluster nodes |egrep "172.25.254.30|172.25.254.60"
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
6fd9f66753dc5a07043ed59f2084fedd4bcee473 172.25.254.60:6379@16379 slave 35af6ab3185ef51d23464d0917adcfc3a90bf917 0 1774780629000 3 connected
35af6ab3185ef51d23464d0917adcfc3a90bf917 172.25.254.30:6379@16379 master - 0 1774780628000 3 connected 12288-16383
# 迁移 1366 槽位到 master10
[root@redis-node1 ~]# redis-cli -a 123 --cluster reshard 172.25.254.10:6379 --cluster-from 35af6ab3185ef51d23464d0917adcfc3a90bf917 --cluster-to 28d77f22ce4a290692954cb722b76583a0643d1f --cluster-slots 1366 --cluster-yes
..................
# 迁移 1365 槽位到 master20
[root@redis-node1 ~]# redis-cli -a 123 --cluster reshard 172.25.254.10:6379 --cluster-from 35af6ab3185ef51d23464d0917adcfc3a90bf917 --cluster-to b938263a21f9473f8decd8ad14ec183a5f201954 --cluster-slots 1365 --cluster-yes
..................
# 迁移 1365 槽位到 master70
[root@redis-node1 ~]# redis-cli -a 123 --cluster reshard 172.25.254.10:6379 --cluster-from 35af6ab3185ef51d23464d0917adcfc3a90bf917 --cluster-to 0c6beaaa90c141a0aa5dba2ed7c04697b5d6956f --cluster-slots 1365 --cluster-yes
..................
[root@redis-node1 ~]# redis-cli -a 123 --cluster check 172.25.254.10:6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
172.25.254.10:6379 (28d77f22...) -> 0 keys | 5462 slots | 1 slaves.
172.25.254.20:6379 (b938263a...) -> 0 keys | 5461 slots | 1 slaves.
172.25.254.70:6379 (0c6beaaa...) -> 0 keys | 5461 slots | 3 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 172.25.254.10:6379)
M: 28d77f22ce4a290692954cb722b76583a0643d1f 172.25.254.10:6379
slots:[1365-5460],[12288-13653] (5462 slots) master
1 additional replica(s)
M: b938263a21f9473f8decd8ad14ec183a5f201954 172.25.254.20:6379
slots:[6827-10922],[13654-15018] (5461 slots) master
1 additional replica(s)
S: 6fd9f66753dc5a07043ed59f2084fedd4bcee473 172.25.254.60:6379
slots: (0 slots) slave
replicates 0c6beaaa90c141a0aa5dba2ed7c04697b5d6956f
S: 35af6ab3185ef51d23464d0917adcfc3a90bf917 172.25.254.30:6379
slots: (0 slots) slave
replicates 0c6beaaa90c141a0aa5dba2ed7c04697b5d6956f
S: 559c0d27ab940322d0fe777f44a9bbd315f65a81 172.25.254.80:6379
slots: (0 slots) slave
replicates 0c6beaaa90c141a0aa5dba2ed7c04697b5d6956f
S: 9a8d60e5da540524825c1b81762a6264e169df6c 172.25.254.40:6379
slots: (0 slots) slave
replicates 28d77f22ce4a290692954cb722b76583a0643d1f
S: 53a188d30f6ac7495fda2db5acf7f2b2028bb9f1 172.25.254.50:6379
slots: (0 slots) slave
replicates b938263a21f9473f8decd8ad14ec183a5f201954
M: 0c6beaaa90c141a0aa5dba2ed7c04697b5d6956f 172.25.254.70:6379
slots:[0-1364],[5461-6826],[10923-12287],[15019-16383] (5461 slots) master
3 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
# 两个多余的slave(30、60)不仅浪费资源,还可能在某些故障场景下导致不必要的选举复杂化。
# 使用 redis-cli --cluster del-node 命令,将 30 和 60 这两个节点从集群中移除。
[root@redis-node1 ~]# redis-cli -a 123 --cluster del-node 172.25.254.10:6379 35af6ab3185ef51d23464d0917adcfc3a90bf917
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Removing node 35af6ab3185ef51d23464d0917adcfc3a90bf917 from cluster 172.25.254.10:6379
>>> Sending CLUSTER FORGET messages to the cluster...
>>> Sending CLUSTER RESET SOFT to the deleted node.
[root@redis-node1 ~]# redis-cli -a 123 --cluster check 172.25.254.10:6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
172.25.254.10:6379 (28d77f22...) -> 0 keys | 5462 slots | 1 slaves.
172.25.254.20:6379 (b938263a...) -> 0 keys | 5461 slots | 1 slaves.
172.25.254.70:6379 (0c6beaaa...) -> 0 keys | 5461 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 172.25.254.10:6379)
M: 28d77f22ce4a290692954cb722b76583a0643d1f 172.25.254.10:6379
slots:[1365-5460],[12288-13653] (5462 slots) master
1 additional replica(s)
M: b938263a21f9473f8decd8ad14ec183a5f201954 172.25.254.20:6379
slots:[6827-10922],[13654-15018] (5461 slots) master
1 additional replica(s)
S: 559c0d27ab940322d0fe777f44a9bbd315f65a81 172.25.254.80:6379
slots: (0 slots) slave
replicates 0c6beaaa90c141a0aa5dba2ed7c04697b5d6956f
S: 9a8d60e5da540524825c1b81762a6264e169df6c 172.25.254.40:6379
slots: (0 slots) slave
replicates 28d77f22ce4a290692954cb722b76583a0643d1f
S: 53a188d30f6ac7495fda2db5acf7f2b2028bb9f1 172.25.254.50:6379
slots: (0 slots) slave
replicates b938263a21f9473f8decd8ad14ec183a5f201954
M: 0c6beaaa90c141a0aa5dba2ed7c04697b5d6956f 172.25.254.70:6379
slots:[0-1364],[5461-6826],[10923-12287],[15019-16383] (5461 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.