Redis搭建主从哨兵模式集群环境搭建, 一主二从三哨兵

  1. 说明

    Redis 2.8开始就增加了Redis Sentinel 哨兵机制,而哨兵模式的集群部署到Linux需要至少6台机器:即一个Redis主节点,两个Redis从节点,三个sentenel哨兵节点。
  2. 准备

    1. 首先准备Redis安装包
      1. window:  Release 3.2.100 · microsoftarchive/redis · GitHub
      2. linux:Release 3.2.100 · microsoftarchive/redis · GitHub
  3. 搭建

    这里以windows为例:首先新建三个conf文件,直接从解压的文件里面直接复制三个出来,修改端口号

        将redis.windows.conf多复制成两个redis.windows2.conf,redis.windows3.conf

                1)在redis.windows2.conf的port改成6380,
                2)redis.windows3.conf的port改成6381
                3)分别在最后面加上slaveof 127.0.0.1 6379
                注:有slaveof配置的说明是从节点

  1. 新建一个sentinel.conf配置文件
    (直接复制下面的内容,修改sentinel monitor这一行)

        

# 当前Sentinel服务运行的端口
port 26379
# 保护模式 no
protected-mode no

# Sentinel去监视一个名为mymaster的主redis实例,
# 这个主实例的IP地址为本机地址127.0.0.1,端口号为6379,
# 而将这个主实例判断为失效至少需要2个Sentinel进程的同意,只要同意Sentinel的数量不达标,自动failover就不会执行

# down-after-milliseconds指定了Sentinel认为Redis实例已经失效所需的毫秒数。
# 当实例超过该时间没有返回PING,或者直接返回错误,那么Sentinel将这个实例标记为主观下线。
# 只有一个Sentinel进程将实例标记为主观下线并不一定会引起实例的自动故障迁移:只有在足够数量的Sentinel都将一个实例标记为主观下线之后,实例才会被标记为客观下线。
# 这时自动故障迁移才会执行

# parallel-syncs指定了在执行故障转移时,最多可以有多少个从Redis实例在同步新的主实例,
# 在从Redis实例较多的情况下这个数字越小,同步的时间越长,完成故障转移所需的时间就越长
sentinel monitor mymaster 127.0.0.1 6379 2

# 如果在failover-timeout该时间(ms)内未能完成failover操作,则认为该failover失败
sentinel down-after-milliseconds mymaster 5000

注:这里主要说一下“sentinel monitor mymaster 127.0.0.1 6379 2”的配置
Sentinel去监视一个master名称为mymaster的主redis实例,redis主节点的IP地址127.0.0.1,端口号为6379,2表示判定redis失效至少需要2个 Sentinel进程的同意,只要同意Sentinel的数量不达标,自动failover就不会执行

把刚刚的sentinel复制两份,重命名为sentinel2.conf,sentinel3.conf
修改sentinel2.conf的port为26380
修改sentinel2.conf的port为26381

6.分别在cmd用命令启动这6个节点就可以了
redis-server.exe redis.windows.conf
redis-server.exe sentinel.conf --sentinel
不过每次都要启动6个,挺麻烦的,写一个windos的脚本启动
新建一个start.bat(把redis的路径修改成自己redis解压的路径)

@echo off
start cmd /k "cd/d E:\redis\Redis-x64-3.2.100&&redis-server.exe redis.windows.conf"
start cmd /k "cd/d E:\redis\Redis-x64-3.2.100&&redis-server.exe redis.windows2.conf"
start cmd /k "cd/d E:\redis\Redis-x64-3.2.100&&redis-server.exe redis.windows3.conf"
start cmd /k "cd/d E:\redis\Redis-x64-3.2.100&&redis-server.exe sentinel.conf --sentinel"
start cmd /k "cd/d E:\redis\Redis-x64-3.2.100&&redis-server.exe sentinel2.conf --sentinel"
start cmd /k "cd/d E:\redis\Redis-x64-3.2.100&&redis-server.exe sentinel3.conf --sentinel"

注:如果报错 slaveof directive not allowed in cluster mode,将redis.windows.conf里的 cluster-enabled yes 设置为 no重启即可

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

杨章隐

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值