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

• 资源分配:合理分配主从节点和哨兵节点的资源,确保系统的高可用性和性能。
相关推荐
秋雨雁南飞1 小时前
c# 使用Memory实现Redis入队出队功能
redis·c#
星光一影2 小时前
悬赏任务平台/拉新地推系统源码
redis·mysql·小程序·php·uniapp·html5
-Xie-3 小时前
Redis(二)——数据类型二
数据库·redis·缓存
User_芊芊君子5 小时前
【LeetCode 经典题解】:队列与栈的双向模拟——从原理到代码详解
linux·redis·leetcode
JH307317 小时前
《Redis 经典应用场景(一):缓存、分布式锁与限流》
redis·分布式·缓存
苦学编程的谢17 小时前
Redis_4_常见命令(完)+认识数据类型和编码方式
数据库·redis·缓存
小坏讲微服务18 小时前
五分钟使用 Docker-compose搭建 Redis 8.0 中间件
运维·redis·docker·中间件·容器·kubernetes·k8s
JanelSirry20 小时前
Redis服务器的的内存是多大
服务器·redis·github
大G的笔记本21 小时前
高频 Redis 面试题答案解析
数据库·redis·缓存
陈果然DeepVersion1 天前
Java大厂面试真题:Spring Boot+微服务+AI智能客服三轮技术拷问实录(四)
spring boot·redis·微服务·kafka·spring security·智能客服·java面试