编辑
2023-04-16
数据库-NoSQL
00
请注意,本文编写于 1088 天前,最后修改于 0 天前,其中某些信息可能已经过时。

目录

CentOS7编译安装Redis5
Redis单主
Redis主从
Redis一主两从
Redis一主二从三哨兵
Redis三主三从集群

Redis 作为高性能的键值存储数据库,在生产环境中需要根据业务场景选择不同的部署架构。本文基于 CentOS 7 环境,从源码编译安装 Redis 5.0.0 开始,系统介绍四种主流部署模式的完整搭建流程。

核心内容包括:

  1. 单机模式:最简单的部署方式,适用于开发测试环境,存在单点故障风险

  2. 主从复制模式:一主一从或一主两从,数据实时同步,故障需手动切换,提供数据备份能力

  3. 哨兵模式(Sentinel):一主两从三哨兵架构,实现故障自动检测与主从切换,需应用端支持

  4. 集群模式(Cluster):三主三从架构,数据分片存储,支持水平扩展与高可用

每种模式均提供完整的配置文件示例、启动命令及关键参数说明(如 slaveofsentinel monitorcluster-enabled 等)。

本文适用于需要根据业务场景选择合适的 Redis 高可用架构的运维及开发人员。

CentOS7编译安装Redis5

shell
#!/bin/bash # 安装路径: /usr/local/redis cd /usr/local wget http://download.redis.io/releases/redis-5.0.0.tar.gz tar -xzvf redis-5.0.0.tar.gz cd redis-5.0.0 make make install PREFIX=/usr/local/redis cp redis.conf sentinel.conf /usr/local/redis/ cd /usr/local/redis mkdir data log var

Redis单主

万一db文件有损,直接凉凉

text
# 常用配置项 bind 0.0.0.0 port 6379 daemonize no pidfile /usr/local/redis/var/redis_6379.pid logfile "/usr/local/redis/log/redis_6379.log" dir "/usr/local/redis/data/" dbfilename dump_6379.rdb maxclients 30000 appendonly no #requirepass 访问密码
shell
# 启动脚本 /usr/local/redis/bin/redis-server /usr/local/redis/redis.conf &

Redis主从

故障手动切换,有数据备份

text
# Master 192.168.60.110 bind 0.0.0.0 port 6379 daemonize no pidfile /usr/local/redis/var/redis_6379.pid logfile "/usr/local/redis/log/redis_6379.log" dir "/usr/local/redis/data/" dbfilename dump_6379.rdb maxclients 30000 appendonly no #requirepass 访问密码 # Slave 192.168.60.111 bind 0.0.0.0 port 6379 daemonize no pidfile /usr/local/redis/var/redis_6379.pid logfile "/usr/local/redis/log/redis_6379.log" dir "/usr/local/redis/data/" dbfilename dump_6379.rdb maxclients 30000 appendonly no #masterauth 访问密码 slaveof 192.168.60.110 6379
shell
# 启动脚本 /usr/local/redis/bin/redis-server /usr/local/redis/redis.conf &

Redis一主两从

故障手动切换,有数据备份

text
# Master 192.168.60.110 bind 0.0.0.0 port 6379 daemonize no pidfile /usr/local/redis/var/redis_6379.pid logfile "/usr/local/redis/log/redis_6379.log" dir "/usr/local/redis/data/" dbfilename dump_6379.rdb maxclients 30000 appendonly no #requirepass 访问密码 # Slave1 192.168.60.111 bind 0.0.0.0 port 6379 daemonize no pidfile /usr/local/redis/var/redis_6379.pid logfile "/usr/local/redis/log/redis_6379.log" dir "/usr/local/redis/data/" dbfilename dump_6379.rdb maxclients 30000 appendonly no #masterauth 访问密码 slaveof 192.168.60.110 6379 # Slave2 192.168.60.112 bind 0.0.0.0 port 6379 daemonize no pidfile /usr/local/redis/var/redis_6379.pid logfile "/usr/local/redis/log/redis_6379.log" dir "/usr/local/redis/data/" dbfilename dump_6379.rdb maxclients 30000 appendonly no #masterauth 访问密码 slaveof 192.168.60.110 6379
shell
# 启动脚本 /usr/local/redis/bin/redis-server /usr/local/redis/redis.conf &

Redis一主二从三哨兵

有数据备份,自动漂移切换节点,需要代码支持

text
# Master 192.168.60.110 bind 0.0.0.0 port 6379 daemonize no pidfile /usr/local/redis/var/redis_6379.pid logfile "/usr/local/redis/log/redis_6379.log" dir "/usr/local/redis/data/" dbfilename dump_6379.rdb maxclients 30000 appendonly no #requirepass 访问密码 #masterauth 访问密码 # Slave1 192.168.60.111 bind 0.0.0.0 port 6379 daemonize no pidfile /usr/local/redis/var/redis_6379.pid logfile "/usr/local/redis/log/redis_6379.log" dir "/usr/local/redis/data/" dbfilename dump_6379.rdb maxclients 30000 appendonly no #requirepass 访问密码 #masterauth 访问密码 slaveof 192.168.60.110 6379 # Slave2 192.168.60.112 bind 0.0.0.0 port 6379 daemonize no pidfile /usr/local/redis/var/redis_6379.pid logfile "/usr/local/redis/log/redis_6379.log" dir "/usr/local/redis/data/" dbfilename dump_6379.rdb maxclients 30000 appendonly no #requirepass 访问密码 #masterauth 访问密码 slaveof 192.168.60.110 6379

sentinel.conf

text
# Master 192.168.60.110 bind 0.0.0.0 port 26379 protected-mode no ## 这个ID得不一样 sentinel myid a77c390022ef2aed3a25ec8cb46b81776a7d079a ## 监听master节点 sentinel monitor test-sentinel 192.168.60.110 6379 2 # 设定5秒内没有响应,说明服务器挂了 sentinel down-after-milliseconds test-sentinel 5000 # 设定15秒内master没有活起来,就重新选举主 sentinel failover-timeout test-sentinel 15000 sentinel parallel-syncs test-sentinel 2 ## sentinel auth-pass test-sentinel 访问密码 logfile "/usr/local/redis/log/sentinel_26379.log" dir "/usr/local/redis" # Slave1 192.168.60.111 bind 0.0.0.0 port 26379 protected-mode no ## 这个ID得不一样 sentinel myid a77c390022ef2aed3a25ec8cb46b81776a7d079b ## 监听master节点 sentinel monitor test-sentinel 192.168.60.110 6379 2 # 设定5秒内没有响应,说明服务器挂了 sentinel down-after-milliseconds test-sentinel 5000 # 设定15秒内master没有活起来,就重新选举主 sentinel failover-timeout test-sentinel 15000 sentinel parallel-syncs test-sentinel 2 ## sentinel auth-pass test-sentinel 访问密码 logfile "/usr/local/redis/log/sentinel_26379.log" dir "/usr/local/redis" # Slave2 192.168.60.112 bind 0.0.0.0 port 26379 protected-mode no ## 这个ID得不一样 sentinel myid a77c390022ef2aed3a25ec8cb46b81776a7d079c ## 监听master节点 sentinel monitor test-sentinel 192.168.60.110 6379 2 # 设定5秒内没有响应,说明服务器挂了 sentinel down-after-milliseconds test-sentinel 5000 # 设定15秒内master没有活起来,就重新选举主 sentinel failover-timeout test-sentinel 15000 sentinel parallel-syncs test-sentinel 2 ## sentinel auth-pass test-sentinel 访问密码 logfile "/usr/local/redis/log/sentinel_26379.log" dir "/usr/local/redis"

启动顺序

text
所有Master->所有Slave->所有Sentinel

启动脚本

shell
# redis-server通用 /usr/local/redis/bin/redis-server /usr/local/redis/conf/redis.conf & # redis-sentinel通用 /usr/local/redis/bin/redis-sentinel /usr/local/redis/conf/sentinel.conf &

Redis三主三从集群

text
# 请务必保证有6个节点,无论怎么弄,一定需要6个节点 # 如果在同一台机器上搞集群,请保证端口和相关文件名各不相同,请不要让我看扁你 # 通用配置 bind 0.0.0.0 port 6379 daemonize no pidfile /usr/local/redis/var/redis_6379.pid logfile "/usr/local/redis/log/redis_6379.log" dir "/usr/local/redis/data/" dbfilename dump_6379.rdb maxclients 30000 appendonly no # masterauth 访问密码 # 开启集群模式 cluster-enabled yes # 每一个节点需要有一个配置文件,需要6份。每个节点处于集群的角色都需要告知其他所有节点,彼此知道,这个文件用于存储集群模式下的集群状态等信息,这个文件是由redis自己维护,我们不用管。如果你要重新创建集群,那么把这个文件删了就行 cluster-config-file /usr/local/redis/nodes-6379.conf # 超时时间,超时则认为master宕机,随后主备切换 cluster-node-timeout 5000 # 开启AOF appendonly yes # aof文件名 appendfilename "appendonly_6379.aof" # 删除工作空间中rdb和aof文件 # 分别启动6台redis,然后再任意一台节点机上在执行以下命令,执行完会出现日志,6台的主从关系都会显示
shell
# 创建集群 /usr/local/redis/bin/redis-cli --cluster create ip1:port1 ip2:port2 ip3:port3 ip4:port4 ip5:port5 ip6:port6 --cluster-replicas 1 # 测试节点 /usr/local/redis/bin/redis-cli -a imooc --cluster check ip1:6379
如果对你有用的话,可以打赏哦
打赏
ali pay
wechat pay

本文作者:Odboy

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 CC 4.0 BY-SA 许可协议。转载请注明出处!