redis 的一主二从三哨兵模式

redis 的一主二从三哨兵模式
最新回答
頖縌者丶菜鸟

2020-11-23 03:48:03

Redis一主二从三哨兵模式是一种高可用性的部署策略,旨在保障Redis在宕机时仍然可提供服务。通过配置一台主服务器以及两台从服务器,并部署三台哨兵实例,系统能够在主服务器故障时,自动将从服务器提升为主服务器,确保服务的连续性。


配置方案如下:



  • 主服务器IP地址:127.0.0.1 6001


  • 两台从服务器IP地址:127.0.0.1 6002 和 127.0.0.1 6003


  • 三台哨兵服务器IP地址:127.0.0.1 16001, 127.0.0.1 16002, 127.0.0.1 16003



配置文件修改涉及:



  • 将`redis.conf`复制为`redis1.conf`和`redis2.conf`

  • 编辑`redis.conf`文件,进行如下配置:


    • `bind 192.168.1.88 127.0.0.1`

    • `protected-mode no`

    • `daemonize yes`

    • `port 6001`

    • `pidfile "/var/run/redis_6001.pid"`


  • 编辑`redis1.conf`和`redis2.conf`文件,分别进行如下配置:


    • `bind 192.168.1.88 127.0.0.1`

    • `protected-mode no`

    • `daemonize yes`

    • `port 6002` 或 `6003`

    • `pidfile "/var/run/redis_6002.pid" 或 "/var/run/redis_6003.pid"`

    • `slaveof 127.0.0.1 6001`


  • 编辑哨兵配置文件,包括`sentinel.conf`、`sentinel1.conf`和`sentinel2.conf`,进行如下配置:


    • `port 16001`

    • `daemonize yes`

    • `sentinel monitor mymaster 127.0.0.1 6001 2`


  • 修改哨兵配置文件,分别进行如下配置:


    • `port 16002` 或 `16003`

    • `sentinel monitor mymaster 127.0.0.1 6001 2`



启动服务:



  • 使用`./bin/redis-server`命令启动`redis.conf`、`redis1.conf`和`redis2.conf`文件。

  • 使用`./bin/redis-sentinel`命令启动哨兵服务。


验证配置:



  • 通过命令`./bin/redis-cli -p 16001`连接哨兵,运行`sentinel master mymaster`以验证主从状态。

  • 手动关闭主服务器,观察从服务器是否能升级为主服务器。

  • 测试数据设置与获取,确认从服务器间的数据同步。


哨兵的作用包括:



  • 状态监控:监控主服务器状态。


  • 故障转移:主服务器异常时,自动提升从服务器为新主服务器。


  • 配置调整:主从切换后,相关配置文件(如`redis.conf`、`redis1.conf`和`redis2.conf`以及`sentinel.conf`)自动更新。



学习资源:



  • Redis Sentinel官方文档

  • Redis哨兵机制原理及配置