当前位置: 首页 > news >正文

Redis 实战指南:数据库选型 + 高可用(主从 / 哨兵)+ 集群搭建

1 关系型数据库和 NoSQL 数据库

1.1 数据库主要分为两大类:关系型数据库与 NoSQL 数据库

关系型数据库,是建立在关系模型基础上的数据库,其借助于集合代数等数学概念和方法来处理数据库
中的数据主流的 MySQLOracleMS SQL Server DB2 都属于这类传统数据库。
NoSQL 数据库,全称为 Not Only SQL,意思就是适用关系型数据库的时候就使用关系型数据库,不适
用的时候也没有必要非使用关系型数据库不可,可以考虑使用更加合适的数据存储。主要分为临时性键
值存储(memcachedRedis)、永久性键值存储(ROMARedis)、面向文档的数据库
MongoDBCouchDB)、面向列的数据库(CassandraHBase),每种 NoSQL 都有其特有的使用
场景及优点。

1.2 为什么还要用 NoSQL 数据库呢?

主要是由于随着互联网发展,数据量越来越大,对性能要求越来越高,传统数据库存在着先天性的缺
陷,即单机(单库)性能瓶颈,并且扩展困难。这样既有单机单库瓶颈,却又扩展困难,自然无法满足
日益增长的海量数据存储及其性能要求,所以才会出现了各种不同的 NoSQL 产品,NoSQL 根本性的优
势在于在云计算时代,简单、易于大规模分布式扩展,并且读写性能非常高

1.3 RDBMSNOSQL的特点及优缺点:

2 redis简介

2.1 什么是redis

Redis (Remote Dictionary Server)
2009年发布,开发者是意大利的萨尔瓦多·桑菲利波普(Salvatore Sanfilippo),他本想为自己的公司
开发一个用于替换MySQL的产品Redis,但是没有想到他把Redis开源后大受欢迎,短短几年,Redis就有
了很大的用户群体,目前国内外使用的公司众多,比如:阿里,百度,新浪微博,知乎网,GitHub,Twitter
Redis是一个开源的、遵循BSD协议的、基于内存的而且目前比较流行的键值数据库(key-value
database),是一个非关系型数据库,redis 提供将内存通过网络远程共享的一种服务,提供类似功能的
还有memcached,但相比memcachedredis还提供了易扩展、高性能、具备数据持久性等功能。
Redis 在高并发、低延迟环境要求比较高的环境使用量非常广泛

2.2 Redis特性

速度快: 10W QPS,基于内存,C语言实现
单线程
持久化
支持多种数据结构
支持多种编程语言
功能丰富: 支持Lua脚本,发布订阅,事务,pipeline等功能
简单: 代码短小精悍(单机核心代码只有23000行左右),单线程开发容易,不依赖外部库,使用简单
主从复制
支持高可用和分布式

单线程为何如此快?
纯内存
非阻塞
避免线程切换和竞态消耗
2.3 Redis应用场景
Session 共享:常见于web集群中的Tomcat或者PHP中多web服务器session共享
缓存:数据查询、电商网站商品信息、新闻内容
计数器:访问排行榜、商品浏览数等和次数相关的数值统计场景
微博/微信社交场合:共同好友,粉丝数,关注,点赞评论等
消息队列:ELK的日志缓存、部分业务的订阅发布系统
地理位置: 基于GEO(地理信息定位),实现摇一摇,附近的人,外卖等功能

2.4 缓存的实现流程
数据更新操作流程

数据读操作流程

3 Redis的安装

官方下载地址:http://download.redis.io/releases/

3.1 rpm包方式安装

dnf install redis -y

3.2 源码安装

3.2.1 对于redis-a

解压源码包

tar zxf redis-7.4.0.tar.gz

安装编译工具:

dnf install make gcc initscripts -y

执行编译:

make
make install

注释检测步骤,跳过检测:

cd utils/
vim install_server.sh

 将原本的内容注释掉。

启动redis:

./install_server.sh 

 启动过程一路回车:

[root@redis-a utils]# ./install_server.sh 
Welcome to the redis service installer
This script will help you easily set up a running redis serverThis systems seems to use systemd.
Please take a look at the provided example service unit files in this directory, and adapt and install them. Sorry!
[root@redis-a utils]# vim install_server.sh 
[root@redis-a utils]# ./install_server.sh 
Welcome to the redis service installer
This script will help you easily set up a running redis serverPlease select the redis port for this instance: [6379] 
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf] 
Selected default - /etc/redis/6379.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/6379.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!

3.2.2 对于redis-a、redis-b

对于另外两台的redis安装:
将以及编译配置好的redis文件夹复制过去:

scp -r redis-7.4.0 root@172.25.254.20:/mnt
scp -r redis-7.4.0 root@172.25.254.30:/mnt

复制配置文件:

rsync -a /usr/local/bin/ root@172.25.254.20:/usr/local/bin/
rsync -a /usr/local/bin/ root@172.25.254.30:/usr/local/bin/

安装依赖软件:

dnf install initscripts -y

来到redis目录下的utils路径下执行安装脚本:

cd /mnt/redis-7.4.0/utils/
./install_server.sh

 一路回车完成安装:

[root@redis-c utils]# ./install_server.sh 
Welcome to the redis service installer
This script will help you easily set up a running redis serverPlease select the redis port for this instance: [6379] 
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf] 
Selected default - /etc/redis/6379.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/6379.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!

4 Redis的基本操作

5 Redis 主从复制

5.1 环境配置

redis-node1 master
redis-node2 slave
redis-node3 slave
Note
在配置多台redis时建议用复制的方式节省编译时间

5.2 配置主从同步

5.2.1 修改mastser节点的配置文件

关闭protected模式:

a\b\c三台都要做

vim /etc/redis/6379.conf


重启服务:

/etc/init.d/redis_6379 restart

关闭火墙:

systemctl stop firewalld.service

5.2.2 配置slave节点

配置b\c节点:

vim /etc/redis/6379.conf

重启服务:

/etc/init.d/redis_6379 restart

5.2.3 测试效果

在master上进行配置:

[root@redis-a ~]# redis-cli 
127.0.0.1:6379> set name rin
OK
127.0.0.1:6379> set age 20
OK

在slave上测试:

[root@redis-b utils]# redis-cli 
127.0.0.1:6379> GET name
"rin"
127.0.0.1:6379> GET age
"20"

5.3 主从同步过程

  • slave节点发送同步亲求到master节点
  • slave节点通过master节点的认证开始进行同步
  • master节点会开启bgsave进程发送内存rbdslave节点,在此过程中是异步操作,也就是说
  • master节点仍然可以进行写入动作
  • slave节点收到rdb后首先清空自己的所有数据
  • slave节点加载rdb并进行数据恢复
  • masterslave同步过程中master还会开启新的bgsave进程把没有同步的数据进行缓存
  • 然后通过自有的replactionfeedslave函数把未通过内存快照发动到slave的数据一条一条写入到 slave

6 Redis的哨兵(高可用)

基于前面做的实验,

实验环境:我们用主两从来实现Redis的高可用架构

6.1 Redis哨兵

Sentinel 进程是用于监控redis集群中Master主服务器工作的状态,在Master主服务器发生故障的时候,
可以实现MasterSlave服务器的切换,保证系统的高可用,此功能在redis2.6+的版本已引用,Redis
哨兵模式到了2.8版本之后就稳定了下来。一般在生产环境也建议使用Redis2.8版本的以后版本
每个哨兵(Sentinel)进程会向其它哨兵(Sentinel)MasterSlave定时发送消息,以确认对方是否
着,如果发现对方在指定配置时间(此项可配置)内未得到回应,则暂时认为对方已离线,也就是所谓的
主观认为宕机” (主观:是每个成员都具有的独自的而且可能相同也可能不同的意识),英文名称:
Subjective Down,简称SDOWN
有主观宕机,对应的有客观宕机。当哨兵群中的多数Sentinel进程在对Master主服务器做出SDOWN
判断,并且通过 SENTINEL is-master-down-by-addr 命令互相交流之后,得出的Master Server下线判
断,这种方式就是客观宕机”(客观:是不依赖于某种意识而已经实际存在的一切事物),英文名称是:
Objectively Down, 简称 ODOWN
通过一定的vote算法,从剩下的slave从服务器节点中,选一台提升为Master服务器节点,然后自动修改
相关配置,并开启故障转移(failover
Sentinel 机制可以解决masterslave角色的自动切换问题,但单个 Master 的性能瓶颈问题无法解决,
似于MySQL中的MHA功能
Redis Sentinel中的Sentinel节点个数应该为大于等于3且最好为奇数
sentinel中的三个定时任务
10秒每个sentinelmasterslave执行info
发现slave节点
确认主从关系
2秒每个sentinel通过master节点的channel交换信息(pub/sub)
通过sentinel__:hello频道交互
交互对节点的看法和自身信息
1秒每个sentinel对其他sentinelredis执行pi

6.2 哨兵的实验过程

在所有阶段中关闭 protected-mode no
1.master节点中
#编辑配置文件
cd redis-7.4.0/
cp sentinel.conf  /etc/redis/
vim /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.100 6379 2        #创建sentinel监控监控master主机,2表示必须得到2票
sentinel down-after-milliseconds mymaster 10000        #master中断时长,10秒连不上视为master下线
sentinel parallel-syncs mymaster 1                    #发生故障转移后,同时开始同步新master数据的slave数量
sentinel failover-timeout mymaster 180000            #整个故障切换的超时时间为3分钟

#复制配置文件到其他阶段

scp /etc/redis/sentinel.conf root@172.25.254.20:/etc/redis/
scp /etc/redis/sentinel.conf root@172.25.254.30:/etc/redis/

2.启动服务:

redis-sentinel  /etc/redis/sentinel.conf

测试:
在20上开监控,将10的redis关闭查看master迁移过程

[root@redis-a redis-7.4.0]# 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=12419,lag=1
slave1:ip=172.25.254.30,port=6379,state=online,offset=12419,lag=1
master_failover_state:no-failover
master_replid:7ed3007f2fa39105d235091c9bbb58d7acec8beb
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:12560
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:12560127.0.0.1:6379> shutdown
(1.01s)

在20上查看状况:

[root@redis-b 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:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_read_repl_offset:22359
slave_repl_offset:22359
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:7ed3007f2fa39105d235091c9bbb58d7acec8beb
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:22359
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:813
repl_backlog_histlen:21547

等待一段时间后在20上再次查看:

not connected> 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:64858
slave_repl_offset:64858
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:f9f691ac35b13fc3f57075c49633880cca8abf24
master_replid2:7ed3007f2fa39105d235091c9bbb58d7acec8beb
master_repl_offset:64858
second_repl_offset:27938
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:813
repl_backlog_histlen:64046

发现master成功实现迁移,再将10上线

/etc/init.d/redis_6379 start

在30上查看主从状况:

[root@redis-c utils]# 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=108885,lag=0
slave1:ip=172.25.254.10,port=6379,state=online,offset=108885,lag=0
master_failover_state:no-failover
master_replid:f9f691ac35b13fc3f57075c49633880cca8abf24
master_replid2:7ed3007f2fa39105d235091c9bbb58d7acec8beb
master_repl_offset:108885
second_repl_offset:27938
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:883
repl_backlog_histlen:108003

监控过程:

redis-a(172.25.254.10):

[root@redis-a redis-7.4.0]# redis-sentinel  /etc/redis/sentinel.conf
36914:X 04 Aug 2025 04:17:49.521 # 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.
36914:X 04 Aug 2025 04:17:49.522 * oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
36914:X 04 Aug 2025 04:17:49.523 * Redis version=7.4.0, bits=64, commit=00000000, modified=0, pid=36914, just started
36914:X 04 Aug 2025 04:17:49.523 * Configuration loaded
36914:X 04 Aug 2025 04:17:49.524 * Increased maximum number of open files to 10032 (it was originally set to 1024).
36914:X 04 Aug 2025 04:17:49.524 * monotonic clock: POSIX clock_gettime_._                                                  _.-``__ ''-._                                             _.-``    `.  `_.  ''-._           Redis Community Edition      .-`` .-```.  ```\/    _.,_ ''-._     7.4.0 (00000000/0) 64 bit(    '      ,       .-`  | `,    )     Running in sentinel mode|`-._`-...-` __...-.``-._|'` _.-'|     Port: 26379|    `-._   `._    /     _.-'    |     PID: 36914`-._    `-._  `-./  _.-'    _.-'                                   |`-._`-._    `-.__.-'    _.-'_.-'|                                  |    `-._`-._        _.-'_.-'    |           https://redis.io       `-._    `-._`-.__.-'_.-'    _.-'                                   |`-._`-._    `-.__.-'    _.-'_.-'|                                  |    `-._`-._        _.-'_.-'    |                                  `-._    `-._`-.__.-'_.-'    _.-'                                   `-._    `-.__.-'    _.-'                                       `-._        _.-'                                           `-.__.-'                                               36914:X 04 Aug 2025 04:17:49.534 * Sentinel new configuration saved on disk
36914:X 04 Aug 2025 04:17:49.535 * Sentinel ID is 0a0b7beafe7731638bdc92e327264b2ba1b53a03
36914:X 04 Aug 2025 04:17:49.535 # +monitor master mymaster 172.25.254.10 6379 quorum 2
36914:X 04 Aug 2025 04:17:49.539 * +slave slave 172.25.254.20:6379 172.25.254.20 6379 @ mymaster 172.25.254.10 6379
36914:X 04 Aug 2025 04:17:49.542 * Sentinel new configuration saved on disk
36914:X 04 Aug 2025 04:17:49.543 * +slave slave 172.25.254.30:6379 172.25.254.30 6379 @ mymaster 172.25.254.10 6379
36914:X 04 Aug 2025 04:17:49.545 * Sentinel new configuration saved on disk
36914:X 04 Aug 2025 04:18:24.344 * +sentinel sentinel d57395b3f684e73c45d78dc06a7700526983d4c8 172.25.254.20 26379 @ mymaster 172.25.254.10 6379
36914:X 04 Aug 2025 04:18:24.365 * Sentinel new configuration saved on disk
36914:X 04 Aug 2025 04:18:27.367 * +sentinel sentinel a9b8c30bd2d52203076408ac7bf02192931ae679 172.25.254.30 26379 @ mymaster 172.25.254.10 6379
36914:X 04 Aug 2025 04:18:27.371 * Sentinel new configuration saved on disk
36914:X 04 Aug 2025 04:20:40.865 # +sdown master mymaster 172.25.254.10 6379
36914:X 04 Aug 2025 04:20:40.944 * Sentinel new configuration saved on disk
36914:X 04 Aug 2025 04:20:40.944 # +new-epoch 1
36914:X 04 Aug 2025 04:20:40.947 * Sentinel new configuration saved on disk
36914:X 04 Aug 2025 04:20:40.947 # +vote-for-leader d57395b3f684e73c45d78dc06a7700526983d4c8 1
36914:X 04 Aug 2025 04:20:41.256 # +config-update-from sentinel d57395b3f684e73c45d78dc06a7700526983d4c8 172.25.254.20 26379 @ mymaster 172.25.254.10 6379
36914:X 04 Aug 2025 04:20:41.257 # +switch-master mymaster 172.25.254.10 6379 172.25.254.30 6379
36914:X 04 Aug 2025 04:20:41.257 * +slave slave 172.25.254.20:6379 172.25.254.20 6379 @ mymaster 172.25.254.30 6379
36914:X 04 Aug 2025 04:20:41.258 * +slave slave 172.25.254.10:6379 172.25.254.10 6379 @ mymaster 172.25.254.30 6379
36914:X 04 Aug 2025 04:20:41.262 * Sentinel new configuration saved on disk
36914:X 04 Aug 2025 04:21:11.307 # +sdown slave 172.25.254.10:6379 172.25.254.10 6379 @ mymaster 172.25.254.30 6379
36914:X 04 Aug 2025 04:24:38.756 # -sdown slave 172.25.254.10:6379 172.25.254.10 6379 @ mymaster 172.25.254.30 6379

redis-b(172.25.254.20):

[root@redis-b utils]# redis-sentinel  /etc/redis/sentinel.conf
31549:X 04 Aug 2025 04:18:22.337 # 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.
31549:X 04 Aug 2025 04:18:22.337 * oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
31549:X 04 Aug 2025 04:18:22.338 * Redis version=7.4.0, bits=64, commit=00000000, modified=0, pid=31549, just started
31549:X 04 Aug 2025 04:18:22.338 * Configuration loaded
31549:X 04 Aug 2025 04:18:22.339 * Increased maximum number of open files to 10032 (it was originally set to 1024).
31549:X 04 Aug 2025 04:18:22.339 * monotonic clock: POSIX clock_gettime_._                                                  _.-``__ ''-._                                             _.-``    `.  `_.  ''-._           Redis Community Edition      .-`` .-```.  ```\/    _.,_ ''-._     7.4.0 (00000000/0) 64 bit(    '      ,       .-`  | `,    )     Running in sentinel mode|`-._`-...-` __...-.``-._|'` _.-'|     Port: 26379|    `-._   `._    /     _.-'    |     PID: 31549`-._    `-._  `-./  _.-'    _.-'                                   |`-._`-._    `-.__.-'    _.-'_.-'|                                  |    `-._`-._        _.-'_.-'    |           https://redis.io       `-._    `-._`-.__.-'_.-'    _.-'                                   |`-._`-._    `-.__.-'    _.-'_.-'|                                  |    `-._`-._        _.-'_.-'    |                                  `-._    `-._`-.__.-'_.-'    _.-'                                   `-._    `-.__.-'    _.-'                                       `-._        _.-'                                           `-.__.-'                                               31549:X 04 Aug 2025 04:18:22.346 * Sentinel new configuration saved on disk
31549:X 04 Aug 2025 04:18:22.347 * Sentinel ID is d57395b3f684e73c45d78dc06a7700526983d4c8
31549:X 04 Aug 2025 04:18:22.347 # +monitor master mymaster 172.25.254.10 6379 quorum 2
31549:X 04 Aug 2025 04:18:22.350 * +slave slave 172.25.254.20:6379 172.25.254.20 6379 @ mymaster 172.25.254.10 6379
31549:X 04 Aug 2025 04:18:22.352 * Sentinel new configuration saved on disk
31549:X 04 Aug 2025 04:18:22.353 * +slave slave 172.25.254.30:6379 172.25.254.30 6379 @ mymaster 172.25.254.10 6379
31549:X 04 Aug 2025 04:18:22.354 * Sentinel new configuration saved on disk
31549:X 04 Aug 2025 04:18:24.214 * +sentinel sentinel 0a0b7beafe7731638bdc92e327264b2ba1b53a03 172.25.254.10 26379 @ mymaster 172.25.254.10 6379
31549:X 04 Aug 2025 04:18:24.217 * Sentinel new configuration saved on disk
31549:X 04 Aug 2025 04:18:27.370 * +sentinel sentinel a9b8c30bd2d52203076408ac7bf02192931ae679 172.25.254.30 26379 @ mymaster 172.25.254.10 6379
31549:X 04 Aug 2025 04:18:27.377 * Sentinel new configuration saved on disk
31549:X 04 Aug 2025 04:20:40.877 # +sdown master mymaster 172.25.254.10 6379
31549:X 04 Aug 2025 04:20:40.945 # +odown master mymaster 172.25.254.10 6379 #quorum 2/2
31549:X 04 Aug 2025 04:20:40.946 # +new-epoch 1
31549:X 04 Aug 2025 04:20:40.946 # +try-failover master mymaster 172.25.254.10 6379
31549:X 04 Aug 2025 04:20:40.948 * Sentinel new configuration saved on disk
31549:X 04 Aug 2025 04:20:40.948 # +vote-for-leader d57395b3f684e73c45d78dc06a7700526983d4c8 1
31549:X 04 Aug 2025 04:20:40.955 * 0a0b7beafe7731638bdc92e327264b2ba1b53a03 voted for d57395b3f684e73c45d78dc06a7700526983d4c8 1
31549:X 04 Aug 2025 04:20:40.956 * a9b8c30bd2d52203076408ac7bf02192931ae679 voted for d57395b3f684e73c45d78dc06a7700526983d4c8 1
31549:X 04 Aug 2025 04:20:41.005 # +elected-leader master mymaster 172.25.254.10 6379
31549:X 04 Aug 2025 04:20:41.005 # +failover-state-select-slave master mymaster 172.25.254.10 6379
31549:X 04 Aug 2025 04:20:41.078 # +selected-slave slave 172.25.254.30:6379 172.25.254.30 6379 @ mymaster 172.25.254.10 6379
31549:X 04 Aug 2025 04:20:41.078 * +failover-state-send-slaveof-noone slave 172.25.254.30:6379 172.25.254.30 6379 @ mymaster 172.25.254.10 6379
31549:X 04 Aug 2025 04:20:41.181 * +failover-state-wait-promotion slave 172.25.254.30:6379 172.25.254.30 6379 @ mymaster 172.25.254.10 6379
31549:X 04 Aug 2025 04:20:41.196 * Sentinel new configuration saved on disk
31549:X 04 Aug 2025 04:20:41.196 # +promoted-slave slave 172.25.254.30:6379 172.25.254.30 6379 @ mymaster 172.25.254.10 6379
31549:X 04 Aug 2025 04:20:41.196 # +failover-state-reconf-slaves master mymaster 172.25.254.10 6379
31549:X 04 Aug 2025 04:20:41.258 * +slave-reconf-sent slave 172.25.254.20:6379 172.25.254.20 6379 @ mymaster 172.25.254.10 6379
31549:X 04 Aug 2025 04:20:42.137 # -odown master mymaster 172.25.254.10 6379
31549:X 04 Aug 2025 04:20:42.194 * +slave-reconf-inprog slave 172.25.254.20:6379 172.25.254.20 6379 @ mymaster 172.25.254.10 6379
31549:X 04 Aug 2025 04:20:42.194 * +slave-reconf-done slave 172.25.254.20:6379 172.25.254.20 6379 @ mymaster 172.25.254.10 6379
31549:X 04 Aug 2025 04:20:42.284 # +failover-end master mymaster 172.25.254.10 6379
31549:X 04 Aug 2025 04:20:42.285 # +switch-master mymaster 172.25.254.10 6379 172.25.254.30 6379
31549:X 04 Aug 2025 04:20:42.285 * +slave slave 172.25.254.20:6379 172.25.254.20 6379 @ mymaster 172.25.254.30 6379
31549:X 04 Aug 2025 04:20:42.286 * +slave slave 172.25.254.10:6379 172.25.254.10 6379 @ mymaster 172.25.254.30 6379
31549:X 04 Aug 2025 04:20:42.288 * Sentinel new configuration saved on disk
31549:X 04 Aug 2025 04:21:12.318 # +sdown slave 172.25.254.10:6379 172.25.254.10 6379 @ mymaster 172.25.254.30 6379
31549:X 04 Aug 2025 04:24:38.971 # -sdown slave 172.25.254.10:6379 172.25.254.10 6379 @ mymaster 172.25.254.30 6379
31549:X 04 Aug 2025 04:28:41.912 # +sdown sentinel 0a0b7beafe7731638bdc92e327264b2ba1b53a03 172.25.254.10 26379 @ mymaster 172.25.254.30 6379

redis-c(172.25.254.30):

[root@redis-c utils]# redis-sentinel  /etc/redis/sentinel.conf
31428:X 04 Aug 2025 04:18:25.314 # 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.
31428:X 04 Aug 2025 04:18:25.315 * oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
31428:X 04 Aug 2025 04:18:25.315 * Redis version=7.4.0, bits=64, commit=00000000, modified=0, pid=31428, just started
31428:X 04 Aug 2025 04:18:25.316 * Configuration loaded
31428:X 04 Aug 2025 04:18:25.316 * Increased maximum number of open files to 10032 (it was originally set to 1024).
31428:X 04 Aug 2025 04:18:25.317 * monotonic clock: POSIX clock_gettime_._                                                  _.-``__ ''-._                                             _.-``    `.  `_.  ''-._           Redis Community Edition      .-`` .-```.  ```\/    _.,_ ''-._     7.4.0 (00000000/0) 64 bit(    '      ,       .-`  | `,    )     Running in sentinel mode|`-._`-...-` __...-.``-._|'` _.-'|     Port: 26379|    `-._   `._    /     _.-'    |     PID: 31428`-._    `-._  `-./  _.-'    _.-'                                   |`-._`-._    `-.__.-'    _.-'_.-'|                                  |    `-._`-._        _.-'_.-'    |           https://redis.io       `-._    `-._`-.__.-'_.-'    _.-'                                   |`-._`-._    `-.__.-'    _.-'_.-'|                                  |    `-._`-._        _.-'_.-'    |                                  `-._    `-._`-.__.-'_.-'    _.-'                                   `-._    `-.__.-'    _.-'                                       `-._        _.-'                                           `-.__.-'                                               31428:X 04 Aug 2025 04:18:25.323 * Sentinel new configuration saved on disk
31428:X 04 Aug 2025 04:18:25.324 * Sentinel ID is a9b8c30bd2d52203076408ac7bf02192931ae679
31428:X 04 Aug 2025 04:18:25.324 # +monitor master mymaster 172.25.254.10 6379 quorum 2
31428:X 04 Aug 2025 04:18:25.326 * +slave slave 172.25.254.20:6379 172.25.254.20 6379 @ mymaster 172.25.254.10 6379
31428:X 04 Aug 2025 04:18:25.329 * Sentinel new configuration saved on disk
31428:X 04 Aug 2025 04:18:25.330 * +slave slave 172.25.254.30:6379 172.25.254.30 6379 @ mymaster 172.25.254.10 6379
31428:X 04 Aug 2025 04:18:25.332 * Sentinel new configuration saved on disk
31428:X 04 Aug 2025 04:18:26.222 * +sentinel sentinel 0a0b7beafe7731638bdc92e327264b2ba1b53a03 172.25.254.10 26379 @ mymaster 172.25.254.10 6379
31428:X 04 Aug 2025 04:18:26.225 * Sentinel new configuration saved on disk
31428:X 04 Aug 2025 04:18:26.342 * +sentinel sentinel d57395b3f684e73c45d78dc06a7700526983d4c8 172.25.254.20 26379 @ mymaster 172.25.254.10 6379
31428:X 04 Aug 2025 04:18:26.345 * Sentinel new configuration saved on disk
31428:X 04 Aug 2025 04:20:40.909 # +sdown master mymaster 172.25.254.10 6379
31428:X 04 Aug 2025 04:20:40.936 * Sentinel new configuration saved on disk
31428:X 04 Aug 2025 04:20:40.936 # +new-epoch 1
31428:X 04 Aug 2025 04:20:40.940 * Sentinel new configuration saved on disk
31428:X 04 Aug 2025 04:20:40.940 # +vote-for-leader d57395b3f684e73c45d78dc06a7700526983d4c8 1
31428:X 04 Aug 2025 04:20:40.978 # +odown master mymaster 172.25.254.10 6379 #quorum 3/2
31428:X 04 Aug 2025 04:20:40.978 * Next failover delay: I will not start a failover before Mon Aug  4 04:26:40 2025
31428:X 04 Aug 2025 04:20:41.248 # +config-update-from sentinel d57395b3f684e73c45d78dc06a7700526983d4c8 172.25.254.20 26379 @ mymaster 172.25.254.10 6379
31428:X 04 Aug 2025 04:20:41.249 # +switch-master mymaster 172.25.254.10 6379 172.25.254.30 6379
31428:X 04 Aug 2025 04:20:41.250 * +slave slave 172.25.254.20:6379 172.25.254.20 6379 @ mymaster 172.25.254.30 6379
31428:X 04 Aug 2025 04:20:41.250 * +slave slave 172.25.254.10:6379 172.25.254.10 6379 @ mymaster 172.25.254.30 6379
31428:X 04 Aug 2025 04:20:41.252 * Sentinel new configuration saved on disk
31428:X 04 Aug 2025 04:21:11.338 # +sdown slave 172.25.254.10:6379 172.25.254.10 6379 @ mymaster 172.25.254.30 6379
31428:X 04 Aug 2025 04:24:38.586 # -sdown slave 172.25.254.10:6379 172.25.254.10 6379 @ mymaster 172.25.254.30 6379
31428:X 04 Aug 2025 04:24:48.538 * +convert-to-slave slave 172.25.254.10:6379 172.25.254.10 6379 @ mymaster 172.25.254.30 6379
31428:X 04 Aug 2025 04:28:41.927 # +sdown sentinel 0a0b7beafe7731638bdc92e327264b2ba1b53a03 172.25.254.10 26379 @ mymaster 172.25.254.30 6379

6.3. 在整个架构中可能会出现的问题

问题:
在生产环境中如果masterslave中的网络出现故障,由于哨兵的存在会把master提出去
当网络恢复后,master发现环境发生改变,master就会把自己的身份转换成slave
master变成slave后会把网络故障那段时间写入自己中的数据清掉,这样数据就丢失了。
解决:
master在被写入数据时会持续连接slavemater确保有2slave可以写入我才允许写入
如果slave数量少于2个便拒绝写入
#matster中设定
[root@redis-node2 ~]# redis-cli
127.0.0.1:6379> CONFIG GET min-slaves-to-write
1) "min-slaves-to-write"
2) "0"
127.0.0.1:6379> CONFIG set min-slaves-to-write 2
OK
127.0.0.1:6379> CONFIG GET min-slaves-to-write
1) "min-slaves-to-write"
2) "2"
#如果要永久保存写到配置文件中/etc/redis/6379.conf

7 Redis Cluster(无中心化设计)

7.1 Redis Cluster 工作原理

在哨兵sentinel机制中,可以解决redis高可用问题,即当master故障后可以自动将slave提升为master
从而可以保证redis服务的正常使用,但是无法解决redis单机写入的瓶颈问题,即单机redis写入性能受
限于单机的内存大小、并发数量、网卡速率等因素。
redis 3.0版本之后推出了无中心架构的redis cluster机制,在无中心的redis集群当中,其每个节点保存
当前节点数据和整个集群状态,每个节点都和其他所有节点连接
Redis Cluster特点如下
1. 所有Redis节点使用(PING机制)互联
2. 集群中某个节点的是否失效,是由整个集群中超过半数的节点监测都失效,才能算真正的失效
3. 客户端不需要proxy即可直接连接redis,应用程序中需要配置有全部的redis服务器IP
4. redis cluster把所有的redis node 平均映射到 0-16383个槽位(slot)上,读写需要到指定的redis
node上进行操作,因此有多少个redis node相当于redis 并发扩展了多少倍,每个redis node 承担
16384/N个槽位
5. Redis cluster预先分配16384(slot)槽位,当需要在redis集群中写入一个key -value的时候,会使
CRC16(key) mod 16384之后的值,决定将key写入值哪一个槽位从而决定写入哪一个Redis节点
上,从而有效解决单机瓶颈。
Redis cluster 架构

假如三个主节点分别是:A, B, C 三个节点,采用哈希槽 (hash slot)的方式来分配16384slot 的话它们
三个节点分别承担的slot 区间可以是:
节点A覆盖 05460
节点B覆盖 546110922
节点C覆盖 1092316383

Redis cluster 主从架构
Redis cluster的架构虽然解决了并发的问题,但是又引入了一个新的问题,每个Redis master的高可用
如何解决?
那就是对每个master 节点都实现主从复制,从而实现 redis 高可用性

Redis Cluster 部署架构说明

7.2 创建redis cluster的前提

1.每个redis node节点采用相同的硬件配置、相同的密码、相同的redis版本。
2.每个节点必须开启的参数
cluster-enabled yes
#必须开启集群状态,开启后redis进程会有cluster显示
cluster-config-file nodes-6380.conf #此文件有redis cluster集群自动创建和维护,不需要任何手
动操作
3.所有redis服务器必须没有任何数据
4.先启动为单机redis且没有任何key value

7.3 部署redis cluster

在所有redis主机中

为方便同时操作多台主机,可以设置免密登录:

ssh-keygen -t ed25519 -N "" -f ~/.ssh/id_ed25519
for i in 40 50 60 70 80 90 100 110; 
do ssh-copy-id -o StrictHostKeyChecking=no root@172.25.254.$i;
done

安装redis:

for i in 40 50 60 70 80 90 100 110 ;
do ssh root@172.25.254.$i "dnf install redis -y";
done

编辑配置文件:

vim /etc/redis/redis.conf
masterauth "123456"                    #集群主从认证
requirepass "123456"        #redis登陆密码    redis-cli 命令连接redis后要用“auth 密码”进行认证
cluster-enabled yes                    #开启cluster集群功能
cluster-config-file nodes-6379.conf    #指定集群配置文件
cluster-node-timeout 15000            #节点加入集群的超时时间单位是ms
bind * -::*              # 允许所有IP访问(IPv4和IPv6)
protected-mode no         # 关闭保护模式(集群环境需要)

将配置好的redis配置文件,覆盖分发到每一台redis节点:

for i in 50 60 70 80 90 100 110 ;
do scp /etc/redis/redis.conf root@172.25.254.$i:/etc/redis/redis.conf;
done

接着重启每个节点的redis服务:

for i in 40 50 60 70 80 90 100 110; 
do ssh root@172.25.254.$i "systemctl restart redis.service"; 
done

创建一个 Redis 集群,其中redis-cli --cluster create是创建集群的核心指令;-a 123456指定了集群的访问密码,所有节点需预先配置相同的登录密码和主从认证密码;后续列出的172.25.254.40:6379至172.25.254.90:6379是参与集群的 6 个节点地址及端口;--cluster-replicas 1则设定每个主节点配备 1 个从节点,最终会形成 3 主 3 从的集群结构,系统会自动完成主从分配和 16384 个哈希槽的平均分配,使集群具备数据:

redis-cli--cluster create -a 123456 
172.25.254.40:6379 172.25.254.50:6379 
172.25.254.60:6379 172.25.254.70:6379 
172.25.254.80:6379 172.25.254.90:6379 
--cluster-replicas 1

 7.4 redis-cli --cluster 参数说明

[root@redis-master1 ~]# redis-cli --cluster help
Cluster Manager Commands:
create host1:port1 ... hostN:portN #创建集群
--cluster-replicas <arg> #指定master的副本数
check <host:port> or <host> <port> #检测集群信息
info <host:port> or <host> <port> #查看集群信息
fix <host:port> or <host> <port> #修复集群
reshard <host:port> or <host> <port> #在线热迁移集群指定主机的slots数据
rebalance <host:port> or <host> <port> #平衡各集群主机的slot数量
add-node new_host:new_port existing_host:existing_port #添加主机
del-node host:port node_id #删除主机
import host:port #导入外部redis服务器的数据到
当前集群

7.5. 创建redis-cluster

配置过程:

[root@redis-1 ~]# redis-cli --cluster create -a 123456 172.25.254.40:6379 172.25.254.50:6379 172.25.254.60:6379 172.25.254.70:6379 172.25.254.80:6379 172.25.254.90:6379 --cluster-replicas 1
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460       #哈希槽分配
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 172.25.254.80:6379 to 172.25.254.40:6379  #主从分配情况
Adding replica 172.25.254.90:6379 to 172.25.254.50:6379
Adding replica 172.25.254.70:6379 to 172.25.254.60:6379
M: 736dad0d25cc64d6dc27ac115f53cf8d19b463d9 172.25.254.40:6379slots:[0-5460] (5461 slots) master
M: ff6744abfb3dbb98931e351d79676a3eb809988d 172.25.254.50:6379slots:[5461-10922] (5462 slots) master
M: d2839da5cd1527d148c2cf502e2a4fbc8748ad1b 172.25.254.60:6379slots:[10923-16383] (5461 slots) master
S: 55ac140374c3af3ddb6977996e5555dc12b7bac1 172.25.254.70:6379replicates d2839da5cd1527d148c2cf502e2a4fbc8748ad1b
S: 787dc954997b41010941ef80435194b6b1af0423 172.25.254.80:6379replicates 736dad0d25cc64d6dc27ac115f53cf8d19b463d9
S: 9fc1ce6fbc3b3019a81c92884b759883147be85b 172.25.254.90:6379replicates ff6744abfb3dbb98931e351d79676a3eb809988d
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.40:6379)
M: 736dad0d25cc64d6dc27ac115f53cf8d19b463d9 172.25.254.40:6379slots:[0-5460] (5461 slots) master1 additional replica(s)
S: 55ac140374c3af3ddb6977996e5555dc12b7bac1 172.25.254.70:6379slots: (0 slots) slavereplicates d2839da5cd1527d148c2cf502e2a4fbc8748ad1b
S: 787dc954997b41010941ef80435194b6b1af0423 172.25.254.80:6379slots: (0 slots) slavereplicates 736dad0d25cc64d6dc27ac115f53cf8d19b463d9
M: d2839da5cd1527d148c2cf502e2a4fbc8748ad1b 172.25.254.60:6379slots:[10923-16383] (5461 slots) master1 additional replica(s)
S: 9fc1ce6fbc3b3019a81c92884b759883147be85b 172.25.254.90:6379slots: (0 slots) slavereplicates ff6744abfb3dbb98931e351d79676a3eb809988d
M: ff6744abfb3dbb98931e351d79676a3eb809988d 172.25.254.50:6379slots:[5461-10922] (5462 slots) master1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...    #检查打开的哈希槽位
>>> Check slots coverage...    #检查槽位覆盖范围
[OK] All 16384 slots covered.  #所有槽位分配完成#配置文件位置
[root@redis-master1 ~]# ll /var/lib/redis/nodes-6379.conf

检测redis集群状态:

redis-cli  -a 123456 --cluster info  172.25.254.40:6379
redis-cli  -a 123456 --cluster check 172.25.254.10:6379	redis-cli -a 123456  cluster info
[root@redis-1 ~]# redis-cli  -a 123456 --cluster info  172.25.254.40:6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
172.25.254.40:6379 (736dad0d...) -> 0 keys | 5461 slots | 1 slaves.
172.25.254.60:6379 (d2839da5...) -> 0 keys | 5461 slots | 1 slaves.
172.25.254.50:6379 (ff6744ab...) -> 0 keys | 5462 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.[root@redis-1 ~]# redis-cli -a 123456  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:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:68
cluster_stats_messages_pong_sent:69
cluster_stats_messages_sent:137
cluster_stats_messages_ping_received:64
cluster_stats_messages_pong_received:68
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:137[root@redis-1 ~]#  redis-cli  -a 123456 --cluster check 172.25.254.40:6379  #检测集群     
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
172.25.254.40:6379 (736dad0d...) -> 0 keys | 5461 slots | 1 slaves.
172.25.254.60:6379 (d2839da5...) -> 0 keys | 5461 slots | 1 slaves.
172.25.254.50:6379 (ff6744ab...) -> 0 keys | 5462 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 172.25.254.40:6379)
M: 736dad0d25cc64d6dc27ac115f53cf8d19b463d9 172.25.254.40:6379slots:[0-5460] (5461 slots) master1 additional replica(s)
S: 55ac140374c3af3ddb6977996e5555dc12b7bac1 172.25.254.70:6379slots: (0 slots) slavereplicates d2839da5cd1527d148c2cf502e2a4fbc8748ad1b
S: 787dc954997b41010941ef80435194b6b1af0423 172.25.254.80:6379slots: (0 slots) slavereplicates 736dad0d25cc64d6dc27ac115f53cf8d19b463d9
M: d2839da5cd1527d148c2cf502e2a4fbc8748ad1b 172.25.254.60:6379slots:[10923-16383] (5461 slots) master1 additional replica(s)
S: 9fc1ce6fbc3b3019a81c92884b759883147be85b 172.25.254.90:6379slots: (0 slots) slavereplicates ff6744abfb3dbb98931e351d79676a3eb809988d
M: ff6744abfb3dbb98931e351d79676a3eb809988d 172.25.254.50:6379slots:[5461-10922] (5462 slots) master1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
写入数据
[root@redis-1 ~]# redis-cli -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> set key1 valuel1     
(error) MOVED 9189 172.25.254.50:6379        #被分配到50的hash槽位上#来到50节点:
[root@redis-2 ~]# redis-cli -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> set key1 value1
OK

7.6 集群扩容:

添加新master:

添加新节点100:

[root@redis-1 ~]# redis-cli -a 123456 --cluster add-node 172.25.254.100:6379 172.25.254.40:6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Adding node 172.25.254.100:6379 to cluster 172.25.254.40:6379
>>> Performing Cluster Check (using node 172.25.254.40:6379)
M: 736dad0d25cc64d6dc27ac115f53cf8d19b463d9 172.25.254.40:6379slots:[0-6826],[10923-12287] (8192 slots) master1 additional replica(s)
M: ff6744abfb3dbb98931e351d79676a3eb809988d 172.25.254.50:6379slots:[6827-10922] (4096 slots) master1 additional replica(s)
S: 787dc954997b41010941ef80435194b6b1af0423 172.25.254.80:6379slots: (0 slots) slavereplicates 736dad0d25cc64d6dc27ac115f53cf8d19b463d9
S: 9fc1ce6fbc3b3019a81c92884b759883147be85b 172.25.254.90:6379slots: (0 slots) slavereplicates ff6744abfb3dbb98931e351d79676a3eb809988d
S: 55ac140374c3af3ddb6977996e5555dc12b7bac1 172.25.254.70:6379slots: (0 slots) slavereplicates d2839da5cd1527d148c2cf502e2a4fbc8748ad1b
M: d2839da5cd1527d148c2cf502e2a4fbc8748ad1b 172.25.254.60:6379slots:[12288-16383] (4096 slots) master1 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 172.25.254.100:6379 to make it join the cluster.
[OK] New node added correctly.

查看槽位情况:

redis-cli  -a 123456 --cluster info 172.25.254.40:6379

 可以看到新添加的master-172.25.254.100,没有任何槽位:

[root@redis-1 ~]# redis-cli -a 123456 --cluster info 172.25.254.40:6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
172.25.254.40:6379 (736dad0d...) -> 0 keys | 8192 slots | 1 slaves.
172.25.254.50:6379 (ff6744ab...) -> 1 keys | 4096 slots | 1 slaves.
172.25.254.100:6379 (2529f76d...) -> 0 keys | 0 slots | 0 slaves.
172.25.254.60:6379 (d2839da5...) -> 0 keys | 4096 slots | 1 slaves.
[OK] 1 keys in 4 masters.
0.00 keys per slot on average.

##分配槽位

[root@redis-1 ~]# redis-cli -a 123456 --cluster reshard 172.25.254.40: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.40:6379)
M: 736dad0d25cc64d6dc27ac115f53cf8d19b463d9 172.25.254.40:6379slots:[0-6826],[10923-12287] (8192 slots) master1 additional replica(s)
M: ff6744abfb3dbb98931e351d79676a3eb809988d 172.25.254.50:6379slots:[6827-10922] (4096 slots) master1 additional replica(s)
S: 787dc954997b41010941ef80435194b6b1af0423 172.25.254.80:6379slots: (0 slots) slavereplicates 736dad0d25cc64d6dc27ac115f53cf8d19b463d9
S: 9fc1ce6fbc3b3019a81c92884b759883147be85b 172.25.254.90:6379slots: (0 slots) slavereplicates ff6744abfb3dbb98931e351d79676a3eb809988d
M: 2529f76dbe2b4a4bbb50bbe5b53635e34d35510a 172.25.254.100:6379slots: (0 slots) master
S: 55ac140374c3af3ddb6977996e5555dc12b7bac1 172.25.254.70:6379slots: (0 slots) slavereplicates d2839da5cd1527d148c2cf502e2a4fbc8748ad1b
M: d2839da5cd1527d148c2cf502e2a4fbc8748ad1b 172.25.254.60:6379slots:[12288-16383] (4096 slots) master1 additional replica(s)
[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? 2529f76dbe2b4a4bbb50bbe5b53635e34d35510a   #谁来接收槽位,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    #表示将集群中所有节点作为这 4096 个槽位的迁移来源Do you want to proceed with the proposed reshard plan (yes/no)? yes    #是否执行分片计划
查看添加状况:
[root@redis-1 ~]# redis-cli -a 123456 --cluster check 172.25.254.40:6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
172.25.254.40:6379 (736dad0d...) -> 0 keys | 6144 slots | 1 slaves.
172.25.254.50:6379 (ff6744ab...) -> 1 keys | 3072 slots | 1 slaves.
172.25.254.100:6379 (2529f76d...) -> 0 keys | 4096 slots | 0 slaves.
172.25.254.60:6379 (d2839da5...) -> 0 keys | 3072 slots | 1 slaves.
[OK] 1 keys in 4 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 172.25.254.40:6379)
M: 736dad0d25cc64d6dc27ac115f53cf8d19b463d9 172.25.254.40:6379slots:[2048-6826],[10923-12287] (6144 slots) master1 additional replica(s)
M: ff6744abfb3dbb98931e351d79676a3eb809988d 172.25.254.50:6379slots:[7851-10922] (3072 slots) master1 additional replica(s)
S: 787dc954997b41010941ef80435194b6b1af0423 172.25.254.80:6379slots: (0 slots) slavereplicates 736dad0d25cc64d6dc27ac115f53cf8d19b463d9
S: 9fc1ce6fbc3b3019a81c92884b759883147be85b 172.25.254.90:6379slots: (0 slots) slavereplicates ff6744abfb3dbb98931e351d79676a3eb809988d
M: 2529f76dbe2b4a4bbb50bbe5b53635e34d35510a 172.25.254.100:6379slots:[0-2047],[6827-7850],[12288-13311] (4096 slots) master
S: 55ac140374c3af3ddb6977996e5555dc12b7bac1 172.25.254.70:6379slots: (0 slots) slavereplicates d2839da5cd1527d148c2cf502e2a4fbc8748ad1b
M: d2839da5cd1527d148c2cf502e2a4fbc8748ad1b 172.25.254.60:6379slots:[13312-16383] (3072 slots) master1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

#添加salve

添加前先找号对应的主节点id:

[root@redis-1 ~]# redis-cli -a 123456 --cluster check 172.25.254.40:6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
172.25.254.40:6379 (736dad0d...) -> 0 keys | 6144 slots | 1 slaves.
172.25.254.50:6379 (ff6744ab...) -> 1 keys | 3072 slots | 1 slaves.
172.25.254.100:6379 (2529f76d...) -> 0 keys | 4096 slots | 0 slaves.
172.25.254.60:6379 (d2839da5...) -> 0 keys | 3072 slots | 1 slaves.
[OK] 1 keys in 4 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 172.25.254.40:6379)
M: 736dad0d25cc64d6dc27ac115f53cf8d19b463d9 172.25.254.40:6379slots:[2048-6826],[10923-12287] (6144 slots) master1 additional replica(s)
M: ff6744abfb3dbb98931e351d79676a3eb809988d 172.25.254.50:6379slots:[7851-10922] (3072 slots) master1 additional replica(s)
S: 787dc954997b41010941ef80435194b6b1af0423 172.25.254.80:6379slots: (0 slots) slavereplicates 736dad0d25cc64d6dc27ac115f53cf8d19b463d9
S: 9fc1ce6fbc3b3019a81c92884b759883147be85b 172.25.254.90:6379slots: (0 slots) slavereplicates ff6744abfb3dbb98931e351d79676a3eb809988d
M: 2529f76dbe2b4a4bbb50bbe5b53635e34d35510a 172.25.254.100:6379slots:[0-2047],[6827-7850],[12288-13311] (4096 slots) master
S: 55ac140374c3af3ddb6977996e5555dc12b7bac1 172.25.254.70:6379slots: (0 slots) slavereplicates d2839da5cd1527d148c2cf502e2a4fbc8748ad1b
M: d2839da5cd1527d148c2cf502e2a4fbc8748ad1b 172.25.254.60:6379slots:[13312-16383] (3072 slots) master1 additional replica(s)

找到对应的主节点id为:2529f76dbe2b4a4bbb50bbe5b53635e34d35510a

进行添加:

[root@redis-1 ~]# redis-cli -a 123456 --cluster add-node 172.25.254.110:6379 172.25.254.40:6379 --cluster-slave --cluster-master-id 2529f76dbe2b4a4bbb50bbe5b53635e34d35510a
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Adding node 172.25.254.110:6379 to cluster 172.25.254.40:6379
>>> Performing Cluster Check (using node 172.25.254.40:6379)
M: 736dad0d25cc64d6dc27ac115f53cf8d19b463d9 172.25.254.40:6379slots:[2048-6826],[10923-12287] (6144 slots) master1 additional replica(s)
M: ff6744abfb3dbb98931e351d79676a3eb809988d 172.25.254.50:6379slots:[7851-10922] (3072 slots) master1 additional replica(s)
S: 787dc954997b41010941ef80435194b6b1af0423 172.25.254.80:6379slots: (0 slots) slavereplicates 736dad0d25cc64d6dc27ac115f53cf8d19b463d9
S: 9fc1ce6fbc3b3019a81c92884b759883147be85b 172.25.254.90:6379slots: (0 slots) slavereplicates ff6744abfb3dbb98931e351d79676a3eb809988d
M: 2529f76dbe2b4a4bbb50bbe5b53635e34d35510a 172.25.254.100:6379slots:[0-2047],[6827-7850],[12288-13311] (4096 slots) master
S: 55ac140374c3af3ddb6977996e5555dc12b7bac1 172.25.254.70:6379slots: (0 slots) slavereplicates d2839da5cd1527d148c2cf502e2a4fbc8748ad1b
M: d2839da5cd1527d148c2cf502e2a4fbc8748ad1b 172.25.254.60:6379slots:[13312-16383] (3072 slots) master1 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 172.25.254.110:6379 to make it join the cluster.
Waiting for the cluster to join>>> Configure node as replica of 172.25.254.100:6379.
[OK] New node added correctly.

命令参数说明:

  • -a 123456:指定 Redis 的访问密码(如果你的集群没有设置密码,可以省略此参数)
  • --cluster add-node:集群模式下添加节点的指令
  • 172.25.254.110:6379:待添加的新从节点地址和端口
  • 172.25.254.40:6379:集群中已存在的任意节点地址和端口(用于新节点加入集群)
  • --cluster-slave:声明添加的是从节点
  • --cluster-master-id 2529f76dbe2b4a4bbb50bbe5b53635e34d35510a:指定该从节点对应的主节点 ID

#检查当前槽位情况:

[root@redis-1 ~]# redis-cli -a 123456 --cluster check 172.25.254.40:6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
172.25.254.40:6379 (736dad0d...) -> 0 keys | 6144 slots | 1 slaves.
172.25.254.50:6379 (ff6744ab...) -> 1 keys | 3072 slots | 1 slaves.
172.25.254.100:6379 (2529f76d...) -> 0 keys | 4096 slots | 1 slaves.
172.25.254.60:6379 (d2839da5...) -> 0 keys | 3072 slots | 1 slaves.
[OK] 1 keys in 4 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 172.25.254.40:6379)
M: 736dad0d25cc64d6dc27ac115f53cf8d19b463d9 172.25.254.40:6379slots:[2048-6826],[10923-12287] (6144 slots) master1 additional replica(s)
M: ff6744abfb3dbb98931e351d79676a3eb809988d 172.25.254.50:6379slots:[7851-10922] (3072 slots) master1 additional replica(s)
S: 787dc954997b41010941ef80435194b6b1af0423 172.25.254.80:6379slots: (0 slots) slavereplicates 736dad0d25cc64d6dc27ac115f53cf8d19b463d9
S: 27f2e2da0310cf1cd41c6aaac6fa7b8d2eb4746d 172.25.254.110:6379slots: (0 slots) slavereplicates 2529f76dbe2b4a4bbb50bbe5b53635e34d35510a
S: 9fc1ce6fbc3b3019a81c92884b759883147be85b 172.25.254.90:6379slots: (0 slots) slavereplicates ff6744abfb3dbb98931e351d79676a3eb809988d
M: 2529f76dbe2b4a4bbb50bbe5b53635e34d35510a 172.25.254.100:6379slots:[0-2047],[6827-7850],[12288-13311] (4096 slots) master1 additional replica(s)
S: 55ac140374c3af3ddb6977996e5555dc12b7bac1 172.25.254.70:6379slots: (0 slots) slavereplicates d2839da5cd1527d148c2cf502e2a4fbc8748ad1b
M: d2839da5cd1527d148c2cf502e2a4fbc8748ad1b 172.25.254.60:6379slots:[13312-16383] (3072 slots) master1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

7.7 clsuter集群维护

添加节点的时候是先添加node节点到集群,然后分配槽位,删除节点的操作与添加节点的操作正好相 反,是先将被删除的Redis node上的槽位迁移到集群中的其他Redis node节点上,然后再将其删除,如 果一个Redis node节点上的槽位没有被完全迁移,删除该node的时候会提示有数据且无法删除。
#移除要下线主机的哈希槽位
[root@redis-1 ~]# redis-cli -a 123456 --cluster reshard 172.25.254.40: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.40:6379)
M: 736dad0d25cc64d6dc27ac115f53cf8d19b463d9 172.25.254.40:6379slots:[2048-6826],[10923-12287] (6144 slots) master1 additional replica(s)
M: ff6744abfb3dbb98931e351d79676a3eb809988d 172.25.254.50:6379slots:[7851-10922] (3072 slots) master1 additional replica(s)
S: 787dc954997b41010941ef80435194b6b1af0423 172.25.254.80:6379slots: (0 slots) slavereplicates 736dad0d25cc64d6dc27ac115f53cf8d19b463d9
S: 27f2e2da0310cf1cd41c6aaac6fa7b8d2eb4746d 172.25.254.110:6379slots: (0 slots) slavereplicates 2529f76dbe2b4a4bbb50bbe5b53635e34d35510a
S: 9fc1ce6fbc3b3019a81c92884b759883147be85b 172.25.254.90:6379slots: (0 slots) slavereplicates ff6744abfb3dbb98931e351d79676a3eb809988d
M: 2529f76dbe2b4a4bbb50bbe5b53635e34d35510a 172.25.254.100:6379slots:[0-2047],[6827-7850],[12288-13311] (4096 slots) master1 additional replica(s)
S: 55ac140374c3af3ddb6977996e5555dc12b7bac1 172.25.254.70:6379slots: (0 slots) slavereplicates d2839da5cd1527d148c2cf502e2a4fbc8748ad1b
M: d2839da5cd1527d148c2cf502e2a4fbc8748ad1b 172.25.254.60:6379slots:[13312-16383] (3072 slots) master1 additional replica(s)
[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#指定接收节点 ID(例如先迁移到 172.25.254.50:6379)
What is the receiving node ID? ff6744abfb3dbb98931e351d79676a3eb809988d#指定源节点 ID
Source node #1: 2529f76dbe2b4a4bbb50bbe5b53635e34d35510a
Source node #2: done#进行槽位分配:
Do you want to proceed with the proposed reshard plan (yes/no)? yes
#删除master
[root@redis-1 ~]# redis-cli -a 123456 --cluster del-node 172.25.254.100:6379 2529f76dbe2b4a4bbb50bbe5b53635e34d35510a
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Removing node 2529f76dbe2b4a4bbb50bbe5b53635e34d35510a from cluster 172.25.254.100:6379
>>> Sending CLUSTER FORGET messages to the cluster...
>>> Sending CLUSTER RESET SOFT to the deleted node.


文章转载自:

http://agMrcUI0.zrgdd.cn
http://1PlJZOM1.zrgdd.cn
http://s80yib3e.zrgdd.cn
http://l5R2JhKM.zrgdd.cn
http://TEa7m49a.zrgdd.cn
http://3iz1CbpL.zrgdd.cn
http://KhPXz77q.zrgdd.cn
http://5wvn3L71.zrgdd.cn
http://xjCIYCf5.zrgdd.cn
http://IySheph8.zrgdd.cn
http://uaP75YR2.zrgdd.cn
http://kRzmdjlI.zrgdd.cn
http://1ubfXq5u.zrgdd.cn
http://cK9gJorK.zrgdd.cn
http://sc3gUaa3.zrgdd.cn
http://Xth45kaN.zrgdd.cn
http://l57J3KdC.zrgdd.cn
http://jrdGu4ZH.zrgdd.cn
http://L0cW9XVM.zrgdd.cn
http://YzqDkxer.zrgdd.cn
http://622rKf1M.zrgdd.cn
http://BDp4cGrY.zrgdd.cn
http://Mj3hd6P2.zrgdd.cn
http://HcMnuYH0.zrgdd.cn
http://vACKpKLE.zrgdd.cn
http://x2Cx51Fy.zrgdd.cn
http://hdh6NwR7.zrgdd.cn
http://DQlwoKS4.zrgdd.cn
http://jrb40e2A.zrgdd.cn
http://mi6UOakX.zrgdd.cn
http://www.dtcms.com/a/384861.html

相关文章:

  • 进程与线程:从入门到精通
  • Android 项目:画图白板APP开发(八)——Matrix位移放大缩小(附demo)
  • 【大前端++】【混合开发】【node】express 文件服务器本地搭建-模拟加载图片使用
  • 如何启动Greenplum中的某个segment
  • 校验用户身份是否过期,是否存在等等JWT
  • Docker 多阶段镜像构建与缓存利用性能优化实践指南
  • Jenkinsfile配置【1】
  • 2025年渗透测试面试题总结-72(题目+回答)
  • 网络安全相关搜索引擎
  • 【Unity性能优化——Stats面板】
  • 【05】AI辅助编程完整的安卓二次商业实战-消息页面媒体对象(Media Object)布局实战调整-按钮样式调整实践-优雅草伊凡
  • AI如何赋能跨境支付,亚马逊云科技与PayerMax的联合探索
  • PAT乙级_1125 子串与子列_Python_AC解法_含疑难点
  • 华清远见25072班网络编程学习day6
  • 国标GB28181视频平台EasyGBS国标GB28181软件与公安数字化安防技术衔接方案
  • 我的Web开发实践笔记:从编码设置到项目运营
  • Regression Trees|回归树
  • [数据结构——Lesson14.快速排序]
  • 城乡供水一体化智慧水务管理系统方案——推动供水高质量发展的御控工业物联网解决方案
  • 云上安全的第一道门槛:身份与访问控制
  • Blender MCP—基于AI代理的智能三维建模协同框架
  • 从零开始打造复杂动作网页:现代CSS3动画与JavaScript交互完全指南
  • 基于 OpenCV 实现实时文档扫描:从轮廓检测到透视变换全流程解析
  • Qt 系统相关 - 事件2
  • iTwinjs GeoLocation
  • 【氮化镓】C缺陷络合物导致的GaN黄光发射
  • Docker 下部署 Elasticsearch 8 并集成 Kibana 和 IK 分词器
  • 机器学习-第一章
  • 【Java EE进阶 --- SpringBoot】SpringBoot配置文件
  • 安装gemini-fullstack-langgraph-quickstart