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

• 资源分配:合理分配主从节点和哨兵节点的资源,确保系统的高可用性和性能。
相关推荐
时间的拾荒人9 小时前
Redis 主从复制详解:配置实操 + 原理机制
数据库·redis·面试
小林ixn17 小时前
别再全量塞历史了:用 Redis 给 Agent 装上「短期记忆」
redis·agent
我不会起名字3221 天前
会话缓存失效排查:区分 Redis 失效与节点广播
redis·后端·go·即时通讯·im·故障排查
aiks_yuan2 天前
Redis 大key怎么排查和删除:--bigkeys 每类只报一个,DEL 百万字段的 Hash 卡 0.3 秒(Redis 7.2 实测)
redis
羑悻的小杀马特2 天前
Redis持久化三剑客:RDB二进制快照、AOF文本日志、混合模式如何守护你的数据安全?
数据库·redis·缓存·aof·持久化存储·rdb
时间的拾荒人2 天前
Redis 进阶实战:渐进式遍历、持久化机制与事务详解
数据库·redis·缓存
码哥字节3 天前
缓存一致性:先删缓存还是先更新库?3 种方案对比 + 实战
redis
2501_933923253 天前
解读Redis:单线程模型
数据库·redis·缓存