Redis 部署方式有哪些

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


1.Redis 主从复制部署

架构

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

部署步骤

  1. 安装 Redis

• 安装编译环境(如makegcc)。

• 下载并编译 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 的故障转移需要一定时间,可能会导致短暂的服务中断。

• 资源分配:合理分配主从节点和哨兵节点的资源,确保系统的高可用性和性能。
相关推荐
冰冰菜的扣jio3 小时前
Redis缓存中三大问题——穿透、击穿、雪崩
java·redis·缓存
阿里巴巴P8资深技术专家4 小时前
基于 Spring AI 和 Redis 向量库的智能对话系统实践
人工智能·redis·spring
oMcLin4 小时前
如何在 AlmaLinux 9 上配置并优化 Redis 集群,支持高并发的实时数据缓存与快速查询?
数据库·redis·缓存
洛阳纸贵5 小时前
Redis
数据库·redis·缓存
挺6的还6 小时前
13.持久化
redis
沙白猿7 小时前
Redis报错:A bean with that name has already been defined in class path resource
spring boot·redis·mybatis
IT 行者7 小时前
Spring Security 7 OAuth2 授权码分布式存储之Redis存储方案
redis·分布式·spring
花~盗8 小时前
redis笔记
redis
五阿哥永琪8 小时前
Spring Data Redis 实战避坑指南:从配置到缓存预热的全链路最佳实践
redis·spring·缓存
win x9 小时前
Redis 哨兵模式
数据库·redis·缓存