Redis 部署方式有哪些

以下是 Redis 主从复制和分布式部署的详细方法和步骤:


1.Redis 主从复制部署

架构

主从复制是 Redis 最基本的分布式机制,通过将数据从主节点(Master)复制到多个从节点(Slave),实现读写分离和数据备份。

部署步骤

  1. 安装 Redis

• 安装编译环境(如make和gcc)。

• 下载并编译 Redis:

bash 复制代码
     wget http://download.redis.io/releases/redis-7.2.5.tar.gz
     tar xvf redis-7.2.5.tar.gz
     cd redis-7.2.5/
     make && make install
     ```



2. 配置主从节点

• 主节点配置(`redis.conf`):

```conf
     port 6379
     bind 0.0.0.0
     ```


• 从节点配置(`redis.conf`):

```conf
     port 6380
     bind 0.0.0.0
     slaveof 127.0.0.1 6379
     ```



3. 启动 Redis 实例

• 启动主节点:

```bash
     redis-server /path/to/master_redis.conf
     ```


• 启动从节点:

```bash
     redis-server /path/to/slave_redis.conf
     ```



4. 验证主从复制

• 在主节点执行写操作,从节点会自动同步数据。


---



2\.Redis Sentinel(哨兵模式)部署


架构
Sentinel 是 Redis 的高可用性解决方案,基于主从复制,增加了哨兵节点用于监控和自动故障转移。


部署步骤

1. 配置主从复制(同上)。

2. 配置 Sentinel

• 创建`sentinel.conf`文件:

```conf
     port 26379
     daemonize yes
     pidfile /var/run/redis-sentinel.pid
     logfile "/var/log/redis-sentinel.log"
     dir /tmp
     sentinel myid 1
     sentinel monitor mymaster 127.0.0.1 6379 2
     sentinel auth-pass mymaster <password>
     sentinel down-after-milliseconds mymaster 5000
     sentinel parallel-syncs mymaster 1
     sentinel failover-timeout mymaster 60000
     ```



3. 启动 Sentinel 实例

• 启动多个 Sentinel 实例(至少 3 个):

```bash
     redis-sentinel /path/to/sentinel.conf
     ```



4. 验证故障转移

• 关闭主节点,Sentinel 会自动将从节点提升为主节点。


---



3\.Redis Cluster(集群模式)部署


架构
Redis Cluster 是 Redis 的原生分布式方案,支持数据分片和高可用性。


部署步骤

1. 安装 Redis

• 同主从复制的安装步骤。


2. 配置集群节点

• 创建多个节点配置文件(`redis.conf`):

```conf
     port <port>
     daemonize yes
     cluster-enabled yes
     cluster-config-file nodes-<port>.conf
     cluster-node-timeout 5000
     appendonly yes
     ```



3. 启动节点

• 启动每个节点的 Redis 实例:

```bash
     redis-server /path/to/redis.conf
     ```



4. 创建集群

• 使用`redis-cli`创建集群:

```bash
     redis-cli --cluster create <node1_ip>:<port1> <node2_ip>:<port2> ... --cluster-replicas 1
     ```


• 示例:

```bash
     redis-cli --cluster create 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:8000 127.0.0.1:8001 127.0.0.1:9000 127.0.0.1:9001 --cluster-replicas 1
     ```



5. 验证集群状态

• 连接到任意节点,执行`CLUSTER INFO`和`CLUSTER NODES`查看集群状态。


---



注意事项

• 数据一致性:主从复制和集群模式都可能存在数据同步延迟,导致短暂的数据不一致。

• 故障转移时间:Sentinel 和 Cluster 的故障转移需要一定时间,可能会导致短暂的服务中断。

• 资源分配:合理分配主从节点和哨兵节点的资源,确保系统的高可用性和性能。
相关推荐
imDwAaY15 小时前
操作系统如何管理程序运行?从线程、进程到内存隔离与用户态切换:CS162 Note2
android·java·redis
码途行者16 小时前
处理超时未支付订单:两套轻量化方案,附 PHP 极简实现
开发语言·redis·php
草莓熊Lotso20 小时前
【C++ 基于微服务的即时通讯系统】etcd + brpc 从安装到服务注册发现全链路实战
网络·数据库·c++·redis·微服务·etcd
我不会起名字3221 天前
Redis 突然只能读不能写:MISCONF 报错的 3 类触发条件与 4 步恢复
运维·redis·持久化·排查·misconf
我不会起名字3221 天前
Redis 从 0 到 1(九):主库宕机 30 秒自动切主——1 主 2 从 3 哨兵 + Go 客户端
redis·docker·sentinel·高可用·哨兵
毕业设计7031 天前
(免费领源码)SSM董家湾线上点餐系统04682- java、PHP、python、C#、小程序、大数据、单片机、网络工程等)
java·redis·mysql·tomcat·maven·mybatis·ssm
用户094248568032 天前
第1章:Redis 术语全景与单线程工作原理
redis·快应用·源码阅读
hflmwl82 天前
RedisDesktopManager安装包中文版官方下载,redis可视化客户端工具下载
数据库·redis·缓存·可视化客户端工具
用户84913717547162 天前
订单超时未支付自动取消实现方案 | 三种方案详解
redis·面试·rabbitmq
easyboot2 天前
Memurai Windows下的redis
redis