Redis 部署方式有哪些

发布于:2025-03-13 ⋅ 阅读:(15) ⋅ 点赞:(0)

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


1.Redis 主从复制部署

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

部署步骤

  1. 安装 Redis

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

• 下载并编译 Redis:

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

• 资源分配:合理分配主从节点和哨兵节点的资源,确保系统的高可用性和性能。

网站公告

今日签到

点亮在社区的每一天
去签到