Redis详解(十一)进阶:如何配置哨兵模式

哨兵模式

当主服务器宕机后,需要手动把一台从服务器切换为主服务器,这就需要人工干预,费事费力,还会造成一段时间内服务不可用。这不是一种推荐的方式,更多时候,我们优先考虑哨兵模式**。

Redis从2.8开始正式提供了Sentinel(哨兵) 架构来解决这个问题。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-WoM3ovaV-1667250597575)(Untitled.assets/1-1667250584761-9.png)]

假设主服务器宕机,哨兵1先检测到这个结果,系统并不会立马进行failover选举过程,仅仅是哨兵1主观的认为主服务器不可用,这个现象称为主观下线。当后面的哨兵也检测到主服务器不可用,并且达到一定数量,那么哨兵之间会进行一次投票,投票的结果由一个哨兵发起,进行failover(故障转移操作)。切换成功后,通过发布订阅模式,让各个哨兵把自己的监控的从服务器实现切换主机,这个过称为客观下线

测试

  • 先启动一主二从机制

    [root@hecs-66166 bin]# redis-server ./mconfig/redis6379.conf 
    [root@hecs-66166 bin]# redis-server ./mconfig/redis6380.conf 
    [root@hecs-66166 bin]# redis-server ./mconfig/redis6381.conf 
    [root@hecs-66166 bin]# ps -ef |grep redis
    root      250399       1  0 04:48 ?        00:00:00 redis-server 127.0.0.1:6379
    root      250405       1  0 04:48 ?        00:00:00 redis-server 127.0.0.1:6380
    root      250411       1  0 04:48 ?        00:00:00 redis-server 127.0.0.1:6381
    root      250417  249174  0 04:49 pts/1    00:00:00 grep --color=auto redis
    
    ###################配置6380从机################################
    [root@hecs-66166 ~]# redis-cli -p 6380
    127.0.0.1:6380> SLAVEOF 127.0.0.1 6379
    OK
    
    ###################配置6381从机################################
    [root@hecs-66166 ~]# redis-cli -p 6381
    127.0.0.1:6381> SLAVEOF 127.0.0.1 6379
    OK
    
    ###################配置6379主机################################
    [root@hecs-66166 bin]# redis-cli -p 6379
    127.0.0.1:6379> info replication
    # Replication
    role:master
    connected_slaves:2
    slave0:ip=127.0.0.1,port=6381,state=online,offset=8463,lag=0
    slave1:ip=127.0.0.1,port=6380,state=online,offset=8463,lag=0
    master_failover_state:no-failover
    master_replid:fdb45a7767a6961d3592332a6900a8fc6b66ca6c
    master_replid2:1893a9622491d130a17c0afb55e26829d2b89137
    master_repl_offset:8463
    second_repl_offset:1957
    repl_backlog_active:1
    repl_backlog_size:1048576
    repl_backlog_first_byte_offset:1957
    repl_backlog_histlen:6507
    
    
  • touch 创建一个名称为sentinel.conf的文件

    [root@hecs-66166 mconfig]# touch sentinel.conf
    [root@hecs-66166 mconfig]# ls
    6379  6380  6381  dump6379.rdb  dump6380.rdb  dump6381.rdb  redis6379.conf  redis6380.conf  redis6381.conf  redis.conf  sentinel.conf
    
  • 配置哨兵配置文件,名字不能错

    # sentinel monitor 被监控的名称 host port 1
    # 1 代表主机挂了以后,看谁当主机,投票后票数最多的当主机
    sentinel monitor myredis 127.0.0.1 6379 1
    
  • 配置完成,启动哨兵

    [root@hecs-66166 mconfig]# redis-sentinel sentinel.conf
    

    查看日志

    [root@hecs-66166 mconfig]# redis-sentinel sentinel.conf 
    250563:X 01 Nov 2022 04:56:28.345 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
    250563:X 01 Nov 2022 04:56:28.345 # Redis version=7.0.5, bits=64, commit=00000000, modified=0, pid=250563, just started
    250563:X 01 Nov 2022 04:56:28.345 # Configuration loaded
    250563:X 01 Nov 2022 04:56:28.345 * monotonic clock: POSIX clock_gettime
                    _._                                                  
               _.-``__ ''-._                                             
          _.-``    `.  `_.  ''-._           Redis 7.0.5 (00000000/0) 64 bit
      .-`` .-```.  ```\/    _.,_ ''-._                                  
     (    '      ,       .-`  | `,    )     Running in sentinel mode
     |`-._`-...-` __...-.``-._|'` _.-'|     Port: 26379
     |    `-._   `._    /     _.-'    |     PID: 250563
      `-._    `-._  `-./  _.-'    _.-'                                   
     |`-._`-._    `-.__.-'    _.-'_.-'|                                  
     |    `-._`-._        _.-'_.-'    |           https://redis.io       
      `-._    `-._`-.__.-'_.-'    _.-'                                   
     |`-._`-._    `-.__.-'    _.-'_.-'|                                  
     |    `-._`-._        _.-'_.-'    |                                  
      `-._    `-._`-.__.-'_.-'    _.-'                                   
          `-._    `-.__.-'    _.-'                                       
              `-._        _.-'                                           
                  `-.__.-'                                               
    
    250563:X 01 Nov 2022 04:56:28.346 # Sentinel ID is 3b4d4b397673461056b1823f0c5f6f9b6664bf96
    250563:X 01 Nov 2022 04:56:28.346 # +monitor master myredis 127.0.0.1 6379 quorum 1
    250563:X 01 Nov 2022 04:56:28.347 * +slave slave 127.0.0.1:6381 127.0.0.1 6381 @ myredis 127.0.0.1 6379
    250563:X 01 Nov 2022 04:56:28.351 * Sentinel new configuration saved on disk
    250563:X 01 Nov 2022 04:56:28.351 * +slave slave 127.0.0.1:6380 127.0.0.1 6380 @ myredis 127.0.0.1 6379
    250563:X 01 Nov 2022 04:56:28.355 * Sentinel new configuration saved on disk
    
    
  • 现在我们关闭6379主机

    127.0.0.1:6379> SHUTDOWN
    not connected> exit
    
  • 稍等一会后会发现哨兵日志

    250563:X 01 Nov 2022 04:58:31.000 # +odown master myredis 127.0.0.1 6379 #quorum 1/1
    250563:X 01 Nov 2022 04:58:31.000 # +new-epoch 1
    250563:X 01 Nov 2022 04:58:31.000 # +try-failover master myredis 127.0.0.1 6379
    250563:X 01 Nov 2022 04:58:31.004 * Sentinel new configuration saved on disk
    250563:X 01 Nov 2022 04:58:31.004 # +vote-for-leader 3b4d4b397673461056b1823f0c5f6f9b6664bf96 1
    250563:X 01 Nov 2022 04:58:31.004 # +elected-leader master myredis 127.0.0.1 6379
    250563:X 01 Nov 2022 04:58:31.004 # +failover-state-select-slave master myredis 127.0.0.1 6379
    250563:X 01 Nov 2022 04:58:31.094 # +selected-slave slave 127.0.0.1:6380 127.0.0.1 6380 @ myredis 127.0.0.1 6379
    250563:X 01 Nov 2022 04:58:31.094 * +failover-state-send-slaveof-noone slave 127.0.0.1:6380 127.0.0.1 6380 @ myredis 127.0.0.1 6379
    250563:X 01 Nov 2022 04:58:31.153 * +failover-state-wait-promotion slave 127.0.0.1:6380 127.0.0.1 6380 @ myredis 127.0.0.1 6379
    250563:X 01 Nov 2022 04:58:31.910 * Sentinel new configuration saved on disk
    250563:X 01 Nov 2022 04:58:31.910 # +promoted-slave slave 127.0.0.1:6380 127.0.0.1 6380 @ myredis 127.0.0.1 6379
    250563:X 01 Nov 2022 04:58:31.910 # +failover-state-reconf-slaves master myredis 127.0.0.1 6379
    250563:X 01 Nov 2022 04:58:31.972 * +slave-reconf-sent slave 127.0.0.1:6381 127.0.0.1 6381 @ myredis 127.0.0.1 6379
    250563:X 01 Nov 2022 04:58:32.973 * +slave-reconf-inprog slave 127.0.0.1:6381 127.0.0.1 6381 @ myredis 127.0.0.1 6379
    250563:X 01 Nov 2022 04:58:32.973 * +slave-reconf-done slave 127.0.0.1:6381 127.0.0.1 6381 @ myredis 127.0.0.1 6379
    250563:X 01 Nov 2022 04:58:33.035 # +failover-end master myredis 127.0.0.1 6379
    250563:X 01 Nov 2022 04:58:33.035 # +switch-master myredis 127.0.0.1 6379 127.0.0.1 6380    ###选取了新的主机
    250563:X 01 Nov 2022 04:58:33.035 * +slave slave 127.0.0.1:6381 127.0.0.1 6381 @ myredis 127.0.0.1 6380
    250563:X 01 Nov 2022 04:58:33.035 * +slave slave 127.0.0.1:6379 127.0.0.1 6379 @ myredis 127.0.0.1 6380  ###原来主机也被加入到了从机中,重启即可生效
    250563:X 01 Nov 2022 04:58:33.047 * Sentinel new configuration saved on disk
    250563:X 01 Nov 2022 04:59:03.076 # +sdown slave 127.0.0.1:6379 127.0.0.1 6379 @ myredis 127.0.0.1 6380
    

    我们发现,其中的一台从机(6380)已经自动变成了主机,原主机也被加入到了从机中,这就叫做哨兵模式

  • 重新启动原来的主机6379

    ##################################哨兵日志#########################################
    250563:X 01 Nov 2022 05:03:58.149 # -sdown slave 127.0.0.1:6379 127.0.0.1 6379 @ myredis 127.0.0.1 6380
    250563:X 01 Nov 2022 05:04:08.158 * +convert-to-slave slave 127.0.0.1:6379 127.0.0.1 6379 @ myredis 127.0.0.1 6380
    ##################################查看6380主机信息#########################################
    127.0.0.1:6380> info replication
    # Replication
    role:master
    connected_slaves:2
    slave0:ip=127.0.0.1,port=6381,state=online,offset=37716,lag=0
    slave1:ip=127.0.0.1,port=6379,state=online,offset=37716,lag=0
    master_failover_state:no-failover
    master_replid:b2b31e1f5763bb146123f28b609a03399c430c42
    master_replid2:fdb45a7767a6961d3592332a6900a8fc6b66ca6c
    master_repl_offset:37716
    second_repl_offset:14609
    repl_backlog_active:1
    repl_backlog_size:1048576
    repl_backlog_first_byte_offset:8268
    repl_backlog_histlen:29449
    
    

优缺点

  • 优点
    • 哨兵集群,基于主从复制模式,所有主从配置的优点他都有
    • 主从可以切换,故障可以转移,系统可用性会更好
    • 哨兵模式就是主从模式的升级,手动到自动,更加健壮
  • 缺点
    • redis不好在线扩容,集群容量一旦到达上限,在线扩容就十分麻烦
    • 实现哨兵模式的配置其实是很麻烦的,里面有很多选择

全部配置

# Example sentinel.conf
 
# 哨兵sentinel实例运行的端口 默认26379
port 26379
 
# 哨兵sentinel的工作目录
dir /tmp
 
# 哨兵sentinel监控的redis主节点的 ip port 
# master-name  可以自己命名的主节点名字 只能由字母A-z、数字0-9 、这三个字符".-_"组成。
# quorum 当这些quorum个数sentinel哨兵认为master主节点失联 那么这时 客观上认为主节点失联了
# sentinel monitor <master-name> <ip> <redis-port> <quorum>
sentinel monitor mymaster 127.0.0.1 6379 1
 
# 当在Redis实例中开启了requirepass foobared 授权密码 这样所有连接Redis实例的客户端都要提供密码
# 设置哨兵sentinel 连接主从的密码 注意必须为主从设置一样的验证密码
# sentinel auth-pass <master-name> <password>
sentinel auth-pass mymaster MySUPER--secret-0123passw0rd
 
 
# 指定多少毫秒之后 主节点没有应答哨兵sentinel 此时 哨兵主观上认为主节点下线 默认30秒
# sentinel down-after-milliseconds <master-name> <milliseconds>
sentinel down-after-milliseconds mymaster 30000
 
# 这个配置项指定了在发生failover主备切换时最多可以有多少个slave同时对新的master进行 同步,
这个数字越小,完成failover所需的时间就越长,
但是如果这个数字越大,就意味着越 多的slave因为replication而不可用。
可以通过将这个值设为 1 来保证每次只有一个slave 处于不能处理命令请求的状态。
# sentinel parallel-syncs <master-name> <numslaves>
sentinel parallel-syncs mymaster 1
 
 
 
# 故障转移的超时时间 failover-timeout 可以用在以下这些方面: 
#1. 同一个sentinel对同一个master两次failover之间的间隔时间。
#2. 当一个slave从一个错误的master那里同步数据开始计算时间。直到slave被纠正为向正确的master那里同步数据时。
#3.当想要取消一个正在进行的failover所需要的时间。  
#4.当进行failover时,配置所有slaves指向新的master所需的最大时间。不过,即使过了这个超时,slaves依然会被正确配置为指向master,但是就不按parallel-syncs所配置的规则来了
# 默认三分钟
# sentinel failover-timeout <master-name> <milliseconds>
sentinel failover-timeout mymaster 180000
 
# SCRIPTS EXECUTION
 
#配置当某一事件发生时所需要执行的脚本,可以通过脚本来通知管理员,例如当系统运行不正常时发邮件通知相关人员。
#对于脚本的运行结果有以下规则:
#若脚本执行后返回1,那么该脚本稍后将会被再次执行,重复次数目前默认为10
#若脚本执行后返回2,或者比2更高的一个返回值,脚本将不会重复执行。
#如果脚本在执行过程中由于收到系统中断信号被终止了,则同返回值为1时的行为相同。
#一个脚本的最大执行时间为60s,如果超过这个时间,脚本将会被一个SIGKILL信号终止,之后重新执行。
 
#通知型脚本:当sentinel有任何警告级别的事件发生时(比如说redis实例的主观失效和客观失效等等),将会去调用这个脚本,
#这时这个脚本应该通过邮件,SMS等方式去通知系统管理员关于系统不正常运行的信息。调用该脚本时,将传给脚本两个参数,
#一个是事件的类型,
#一个是事件的描述。
#如果sentinel.conf配置文件中配置了这个脚本路径,那么必须保证这个脚本存在于这个路径,并且是可执行的,否则sentinel无法正常启动成功。
#通知脚本
# sentinel notification-script <master-name> <script-path>
  sentinel notification-script mymaster /var/redis/notify.sh
 
# 客户端重新配置主节点参数脚本
# 当一个master由于failover而发生改变时,这个脚本将会被调用,通知相关的客户端关于master地址已经发生改变的信息。
# 以下参数将会在调用脚本时传给脚本:
# <master-name> <role> <state> <from-ip> <from-port> <to-ip> <to-port>
# 目前<state>总是“failover”,
# <role>是“leader”或者“observer”中的一个。 
# 参数 from-ip, from-port, to-ip, to-port是用来和旧的master和新的master(即旧的slave)通信的
# 这个脚本应该是通用的,能被多次调用,不是针对性的。
# sentinel client-reconfig-script <master-name> <script-path>
sentinel client-reconfig-script mymaster /var/redis/reconfig.sh
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当使用Docker配置Redis哨兵模式时,可以按照以下步骤进行操作: 1. 首先,确保已经安装了Docker和Docker Compose。 2. 创建一个新的目录,用于存放Redis配置文件和Docker Compose文件。 3. 在该目录下创建一个名为`docker-compose.yml`的文件,并在其中编写以下内容: ```yaml version: '3' services: redis-master: image: redis container_name: redis-master ports: - "6379:6379" volumes: - ./redis-master.conf:/usr/local/etc/redis/redis.conf command: redis-server /usr/local/etc/redis/redis.conf redis-slave1: image: redis container_name: redis-slave1 ports: - "6380:6379" volumes: - ./redis-slave1.conf:/usr/local/etc/redis/redis.conf command: redis-server /usr/local/etc/redis/redis.conf redis-slave2: image: redis container_name: redis-slave2 ports: - "6381:6379" volumes: - ./redis-slave2.conf:/usr/local/etc/redis/redis.conf command: redis-server /usr/local/etc/redis/redis.conf redis-sentinel1: image: redis container_name: redis-sentinel1 ports: - "26379:26379" volumes: - ./sentinel1.conf:/usr/local/etc/redis/sentinel.conf command: redis-sentinel /usr/local/etc/redis/sentinel.conf --sentinel redis-sentinel2: image: redis container_name: redis-sentinel2 ports: - "26380:26379" volumes: - ./sentinel2.conf:/usr/local/etc/redis/sentinel.conf command: redis-sentinel /usr/local/etc/redis/sentinel.conf --sentinel redis-sentinel3: image: redis container_name: redis-sentinel3 ports: - "26381:26379" volumes: - ./sentinel3.conf:/usr/local/etc/redis/sentinel.conf command: redis-sentinel /usr/local/etc/redis/sentinel.conf --sentinel ``` 4. 在同一目录下创建以下配置文件: - `redis-master.conf`:Redis主节点的配置文件,内容如下: ``` bind 0.0.0.0 port 6379 daemonize yes ``` - `redis-slave1.conf`:Redis从节点1的配置文件,内容如下: ``` bind 0.0.0.0 port 6379 daemonize yes slaveof redis-master 6379 ``` - `redis-slave2.conf`:Redis从节点2的配置文件,内容如下: ``` bind 0.0.0.0 port 6379 daemonize yes slaveof redis-master 6379 ``` - `sentinel1.conf`:Redis哨兵1的配置文件,内容如下: ``` bind 0.0.0.0 port 26379 sentinel monitor mymaster redis-master 6379 2 sentinel down-after-milliseconds mymaster 5000 sentinel failover-timeout mymaster 10000 sentinel parallel-syncs mymaster 1 ``` - `sentinel2.conf`:Redis哨兵2的配置文件,内容如下: ``` bind 0.0.0.0 port 26379 sentinel monitor mymaster redis-master 6379 2 sentinel down-after-milliseconds mymaster 5000 sentinel failover-timeout mymaster 10000 sentinel parallel-syncs mymaster 1 ``` - `sentinel3.conf`:Redis哨兵3的配置文件,内容如下: ``` bind 0.0.0.0 port 26379 sentinel monitor mymaster redis-master 6379 2 sentinel down-after-milliseconds mymaster 5000 sentinel failover-timeout mymaster 10000 sentinel parallel-syncs mymaster 1 ``` 5. 打开终端,进入到该目录,并执行以下命令启动Redis容器: ``` docker-compose up -d ``` 这将启动一个包含Redis主节点、两个从节点和三个哨兵节点的Docker容器。 现在,你已经成功配置Redis哨兵模式。你可以通过访问`localhost:6379`来访问Redis主节点,`localhost:6380`和`localhost:6381`来访问两个从节点,以及`localhost:26379`、`localhost:26380`和`localhost:26381`来访问三个哨兵节点。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值