013、Redis的高可用架构单哨兵(sentinel)模式部署实战
本文最后更新于 257 天前,其中的信息可能已经过时,如有错误请发送邮件到wuxianglongblog@163.com

Redis的高可用架构单哨兵(sentinel)模式部署实战

一.redis 哨兵(sentinel)模式工作原理概述

    redis 哨兵(sentinel)模式的优点:
        (1)监控master库,当master库宕机后会自动进行选主切换;
        (2)切换过程是应用透明的,因此应用程序无法感知;
        (3)自动处理故障节点;

    生产环境中,我们可以将多个Redis sentinel实例部署在K8S集群上,这样就可以借助K8S的一些工作机制来实现Redis的故障自愈功能哟~

二.部署redis 哨兵(sentinel)模式

1.准备3个Redis实例(可以直接基于上一节搭建的主从复制环境即可)

[root@redis201.oldboyedu.com ~]# ss -ntl
State       Recv-Q Send-Q                                                 Local Address:Port                                                                Peer Address:Port              
LISTEN      0      128                                                        127.0.0.1:16370                                                                          *:*                  
LISTEN      0      128                                                    172.200.1.201:16370                                                                          *:*                  
LISTEN      0      128                                                        127.0.0.1:16371                                                                          *:*                  
LISTEN      0      128                                                    172.200.1.201:16371                                                                          *:*                  
LISTEN      0      128                                                        127.0.0.1:16372                                                                          *:*                  
LISTEN      0      128                                                    172.200.1.201:16372                                                                          *:*                  
LISTEN      0      128                                                                *:22                                                                             *:*                  
LISTEN      0      128                                                             [::]:22                                                                          [::]:*                  
[root@redis201.oldboyedu.com ~]# 
[root@redis201.oldboyedu.com ~]# redis-cli -p 16370 -a oldboyedu INFO REPLICATION
# Replication
role:master
connected_slaves:2
slave0:ip=172.200.1.201,port=16371,state=online,offset=90616,lag=0
slave1:ip=172.200.1.201,port=16372,state=online,offset=90616,lag=0
master_repl_offset:90616
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:90615
[root@redis201.oldboyedu.com ~]# 

2.创建sentinel的配置文件,数据,日志存储目录

[root@redis201.oldboyedu.com ~]# mkdir -pv /oldboyedu/softwares/redis16379
mkdir: 已创建目录 "/oldboyedu/softwares/redis16379"
[root@redis201.oldboyedu.com ~]# 
[root@redis201.oldboyedu.com ~]# mkdir -pv /oldboyedu/data/redis16379
mkdir: 已创建目录 "/oldboyedu/data/redis16379"
[root@redis201.oldboyedu.com ~]# 
[root@redis201.oldboyedu.com ~]# mkdir -pv /oldboyedu/logs/redis16379
mkdir: 已创建目录 "/oldboyedu/logs/redis16379"
[root@redis201.oldboyedu.com ~]# 

3.创建sentinel的配置文件

[root@redis201.oldboyedu.com ~]# cat > /oldboyedu/softwares/redis16379/sentinel.conf <<EOF
> bind 172.200.1.201 127.0.0.1
> port 16379
> dir /oldboyedu/data/redis16379
> sentinel monitor oldboyedu_master 172.200.1.201 16370 1
> sentinel down-after-milliseconds oldboyedu_master 5000
> sentinel auth-pass oldboyedu_master oldboyedu
> EOF
[root@redis201.oldboyedu.com ~]# 
[root@redis201.oldboyedu.com ~]# cat /oldboyedu/softwares/redis16379/sentinel.conf 
bind 172.200.1.201 127.0.0.1
port 16379
dir /oldboyedu/data/redis16379
sentinel monitor oldboyedu_master 172.200.1.201 16370 1
sentinel down-after-milliseconds oldboyedu_master 5000
sentinel auth-pass oldboyedu_master oldboyedu
[root@redis201.oldboyedu.com ~]# 

配置文件参数说明如下:
    sentinel monitor oldboyedu_master 172.200.1.201 16370 1:
        指定Redis初始集群的master库节点并命名为"oldboyedu_master",注意这个"1"的含义,他表示sentinel需要最少的投票数。这在于多sentinel的场景下很有用,由于本案例只是用了1个sentinel,因此我就设置为1。
        在生产环境中,我建议大家配置多sentinel的模式,建议最少设置为3(建议大家设置奇数)个sentinel物理节点,而后将此处的"1"改为"2",表示3个sentinel中最少有2个节点认为master库宕机时,才会真正意义上认为master库宕机。       
        如果想要了解多sentinel如何配置可参考下一篇笔记中有详细案例哟~

    sentinel down-after-milliseconds oldboyedu_master 5000:
        监控sentinel集群时,如果超过了5000毫秒(即5秒)仍然没有响应,则sentinel会判定master库宕机了。

    sentinel auth-pass oldboyedu_master oldboyedu:
        由于sentinel需要访问Redis集群,因此我们要设置访问整个集群的密码,我这里指定的密码为"oldboyedu",这意味着所有的节点都使用相同的密码。

4.启动sentinel进程并查看日志

    (1)启动sentinel进程
        [root@redis201.oldboyedu.com ~]# redis-sentinel /oldboyedu/softwares/redis16379/sentinel.conf &> /oldboyedu/logs/redis16379/sentinel.log &
        [1] 14144
        [root@redis201.oldboyedu.com ~]# 

    (1)查看日志信息
        [root@redis201.oldboyedu.com ~]# tail -100f /oldboyedu/logs/redis16379/sentinel.log 
        14144:X 04 Mar 07:09:26.063 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                        _._                                                  
                   _.-``__ ''-._                                             
              _.-``    `.  `_.  ''-._           Redis 3.2.13 (00000000/0) 64 bit
          .-`` .-```.  ```\/    _.,_ ''-._                                   
         (    '      ,       .-`  | `,    )     Running in sentinel mode
         |`-._`-...-` __...-.``-._|'` _.-'|     Port: 16379
         |    `-._   `._    /     _.-'    |     PID: 14144
          `-._    `-._  `-./  _.-'    _.-'                                   
         |`-._`-._    `-.__.-'    _.-'_.-'|                                  
         |    `-._`-._        _.-'_.-'    |           http://redis.io        
          `-._    `-._`-.__.-'_.-'    _.-'                                   
         |`-._`-._    `-.__.-'    _.-'_.-'|                                  
         |    `-._`-._        _.-'_.-'    |                                  
          `-._    `-._`-.__.-'_.-'    _.-'                                   
              `-._    `-.__.-'    _.-'                                       
                  `-._        _.-'                                           
                      `-.__.-'                                               

        14144:X 04 Mar 07:09:26.063 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
        14144:X 04 Mar 07:09:26.064 # Sentinel ID is 13c5048514a67a5a66b0792cb3b64a3a85e10b4b
        14144:X 04 Mar 07:09:26.064 # +monitor master oldboyedu_master 172.200.1.201 16370 quorum 1
        14144:X 04 Mar 07:09:26.065 * +slave slave 172.200.1.201:16371 172.200.1.201 16371 @ oldboyedu_master 172.200.1.201 16370
        14144:X 04 Mar 07:09:26.066 * +slave slave 172.200.1.201:16372 172.200.1.201 16372 @ oldboyedu_master 172.200.1.201 16370

    (3)如下所示,我们可以登录到"sentinel"后在查看日子信息:
        [root@redis201.oldboyedu.com ~]# ss -ntl
        State       Recv-Q Send-Q                                                 Local Address:Port                                                                Peer Address:Port              
        LISTEN      0      128                                                        127.0.0.1:16370                                                                          *:*                  
        LISTEN      0      128                                                    172.200.1.201:16370                                                                          *:*                  
        LISTEN      0      128                                                        127.0.0.1:16371                                                                          *:*                  
        LISTEN      0      128                                                    172.200.1.201:16371                                                                          *:*                  
        LISTEN      0      128                                                        127.0.0.1:16372                                                                          *:*                  
        LISTEN      0      128                                                    172.200.1.201:16372                                                                          *:*                  
        LISTEN      0      128                                                                *:22                                                                             *:*                  
        LISTEN      0      128                                                        127.0.0.1:16379                                                                          *:*                  
        LISTEN      0      128                                                    172.200.1.201:16379                                                                          *:*                  
        LISTEN      0      128                                                             [::]:22                                                                          [::]:*                  
        [root@redis201.oldboyedu.com ~]# 
        [root@redis201.oldboyedu.com ~]# redis-cli -p 16379
        127.0.0.1:16379> PING
        PONG
        127.0.0.1:16379> 
        127.0.0.1:16379> SHUTDOWN
        not connected> 
        not connected> QUIT
        [1]+  完成                  redis-sentinel /oldboyedu/softwares/redis16379/sentinel.conf &>/oldboyedu/logs/redis16379/sentinel.log
        [root@redis201.oldboyedu.com ~]# 
        [root@redis201.oldboyedu.com ~]# ss -ntl
        State       Recv-Q Send-Q                                                 Local Address:Port                                                                Peer Address:Port              
        LISTEN      0      128                                                        127.0.0.1:16370                                                                          *:*                  
        LISTEN      0      128                                                    172.200.1.201:16370                                                                          *:*                  
        LISTEN      0      128                                                        127.0.0.1:16371                                                                          *:*                  
        LISTEN      0      128                                                    172.200.1.201:16371                                                                          *:*                  
        LISTEN      0      128                                                        127.0.0.1:16372                                                                          *:*                  
        LISTEN      0      128                                                    172.200.1.201:16372                                                                          *:*                  
        LISTEN      0      128                                                                *:22                                                                             *:*                  
        LISTEN      0      128                                                             [::]:22                                                                          [::]:*                  
        [root@redis201.oldboyedu.com ~]# 

5.手动停止主库运行,模拟主库宕机

    (1)切换前主从状态正常:
        [root@redis201.oldboyedu.com ~]# ss -ntl
        State       Recv-Q Send-Q                                                 Local Address:Port                                                                Peer Address:Port              
        LISTEN      0      128                                                        127.0.0.1:16370                                                                          *:*                  
        LISTEN      0      128                                                    172.200.1.201:16370                                                                          *:*                  
        LISTEN      0      128                                                        127.0.0.1:16371                                                                          *:*                  
        LISTEN      0      128                                                    172.200.1.201:16371                                                                          *:*                  
        LISTEN      0      128                                                        127.0.0.1:16372                                                                          *:*                  
        LISTEN      0      128                                                    172.200.1.201:16372                                                                          *:*                  
        LISTEN      0      128                                                                *:22                                                                             *:*                  
        LISTEN      0      128                                                        127.0.0.1:16379                                                                          *:*                  
        LISTEN      0      128                                                    172.200.1.201:16379                                                                          *:*                  
        LISTEN      0      128                                                             [::]:22                                                                          [::]:*                  
        [root@redis201.oldboyedu.com ~]# 
        [root@redis201.oldboyedu.com ~]# redis-cli -p 16370 -a oldboyedu INFO REPLICATION
        # Replication
        role:master
        connected_slaves:2
        slave0:ip=172.200.1.201,port=16371,state=online,offset=203023,lag=1
        slave1:ip=172.200.1.201,port=16372,state=online,offset=202881,lag=1
        master_repl_offset:203023
        repl_backlog_active:1
        repl_backlog_size:1048576
        repl_backlog_first_byte_offset:2
        repl_backlog_histlen:203022
        [root@redis201.oldboyedu.com ~]# 
        [root@redis201.oldboyedu.com ~]# redis-cli -p 16371 -a oldboyedu INFO REPLICATION
        # Replication
        role:slave
        master_host:172.200.1.201
        master_port:16370
        master_link_status:up
        master_last_io_seconds_ago:1
        master_sync_in_progress:0
        slave_repl_offset:379
        slave_priority:100
        slave_read_only:1
        connected_slaves:0
        master_repl_offset:0
        repl_backlog_active:0
        repl_backlog_size:1048576
        repl_backlog_first_byte_offset:0
        repl_backlog_histlen:0
        [root@redis201.oldboyedu.com ~]# 
        [root@redis201.oldboyedu.com ~]# redis-cli -p 16372 -a oldboyedu INFO REPLICATION
        # Replication
        role:slave
        master_host:172.200.1.201
        master_port:16370
        master_link_status:up
        master_last_io_seconds_ago:4
        master_sync_in_progress:0
        slave_repl_offset:379
        slave_priority:100
        slave_read_only:1
        connected_slaves:0
        master_repl_offset:0
        repl_backlog_active:0
        repl_backlog_size:1048576
        repl_backlog_first_byte_offset:0
        repl_backlog_histlen:0
        [root@redis201.oldboyedu.com ~]# 

    (2)手动将主库宕机,观察从库被sentinel实现了自动切换:
        [root@redis201.oldboyedu.com ~]# redis-cli -p 16370 -a oldboyedu SHUTDOWN
        [root@redis201.oldboyedu.com ~]# 
        [root@redis201.oldboyedu.com ~]# redis-cli -p 16370 -a oldboyedu INFO REPLICATION  # 由于我们将主库服务停了,因此无法再次连接主库了
        Could not connect to Redis at 127.0.0.1:16370: Connection refused
        [root@redis201.oldboyedu.com ~]# 
        [root@redis201.oldboyedu.com ~]# redis-cli -p 16371 -a oldboyedu INFO REPLICATION
        # Replication
        role:slave
        master_host:172.200.1.201
        master_port:16372
        master_link_status:up
        master_last_io_seconds_ago:2
        master_sync_in_progress:0
        slave_repl_offset:748
        slave_priority:100
        slave_read_only:1
        connected_slaves:0
        master_repl_offset:0
        repl_backlog_active:0
        repl_backlog_size:1048576
        repl_backlog_first_byte_offset:0
        repl_backlog_histlen:0
        [root@redis201.oldboyedu.com ~]# 
        [root@redis201.oldboyedu.com ~]# redis-cli -p 16372 -a oldboyedu INFO REPLICATION
        # Replication
        role:master
        connected_slaves:1
        slave0:ip=172.200.1.201,port=16371,state=online,offset=1032,lag=0
        master_repl_offset:1046
        repl_backlog_active:1
        repl_backlog_size:1048576
        repl_backlog_first_byte_offset:2
        repl_backlog_histlen:1045
        [root@redis201.oldboyedu.com ~]# 

    (3)查看sentinel的日志信息,不难发现有记录切换的过程,如下所示:
        [root@redis201.oldboyedu.com ~]# tail -100f /oldboyedu/logs/redis16379/sentinel.log 

        ...

        14823:X 04 Mar 07:35:30.162 # +new-epoch 1
        14823:X 04 Mar 07:35:30.162 # +try-failover master oldboyedu_master 172.200.1.201 16370
        14823:X 04 Mar 07:35:30.164 # +vote-for-leader 13c5048514a67a5a66b0792cb3b64a3a85e10b4b 1
        14823:X 04 Mar 07:35:30.164 # +elected-leader master oldboyedu_master 172.200.1.201 16370
        14823:X 04 Mar 07:35:30.164 # +failover-state-select-slave master oldboyedu_master 172.200.1.201 16370
        14823:X 04 Mar 07:35:30.236 # +selected-slave slave 172.200.1.201:16372 172.200.1.201 16372 @ oldboyedu_master 172.200.1.201 16370
        14823:X 04 Mar 07:35:30.236 * +failover-state-send-slaveof-noone slave 172.200.1.201:16372 172.200.1.201 16372 @ oldboyedu_master 172.200.1.201 16370
        14823:X 04 Mar 07:35:30.327 * +failover-state-wait-promotion slave 172.200.1.201:16372 172.200.1.201 16372 @ oldboyedu_master 172.200.1.201 16370
        14823:X 04 Mar 07:35:31.170 # +promoted-slave slave 172.200.1.201:16372 172.200.1.201 16372 @ oldboyedu_master 172.200.1.201 16370
        14823:X 04 Mar 07:35:31.170 # +failover-state-reconf-slaves master oldboyedu_master 172.200.1.201 16370
        14823:X 04 Mar 07:35:31.240 * +slave-reconf-sent slave 172.200.1.201:16371 172.200.1.201 16371 @ oldboyedu_master 172.200.1.201 16370
        14823:X 04 Mar 07:35:32.190 * +slave-reconf-inprog slave 172.200.1.201:16371 172.200.1.201 16371 @ oldboyedu_master 172.200.1.201 16370
        14823:X 04 Mar 07:35:32.190 * +slave-reconf-done slave 172.200.1.201:16371 172.200.1.201 16371 @ oldboyedu_master 172.200.1.201 16370
        14823:X 04 Mar 07:35:32.256 # +failover-end master oldboyedu_master 172.200.1.201 16370
        14823:X 04 Mar 07:35:32.256 # +switch-master oldboyedu_master 172.200.1.201 16370 172.200.1.201 16372
        14823:X 04 Mar 07:35:32.256 * +slave slave 172.200.1.201:16371 172.200.1.201 16371 @ oldboyedu_master 172.200.1.201 16372
        14823:X 04 Mar 07:35:32.256 * +slave slave 172.200.1.201:16370 172.200.1.201 16370 @ oldboyedu_master 172.200.1.201 16372
        14823:X 04 Mar 07:35:37.293 # +sdown slave 172.200.1.201:16370 172.200.1.201 16370 @ oldboyedu_master 172.200.1.201 16372

6.手动修复宕机的主库,sentinel会自动发现

    (1)修复之前,16370端口是连接不上的:
        [root@redis201.oldboyedu.com ~]# redis-cli -p 16370 -a oldboyedu INFO REPLICATION
        Could not connect to Redis at 127.0.0.1:16370: Connection refused
        [root@redis201.oldboyedu.com ~]# 
        [root@redis201.oldboyedu.com ~]# redis-cli -p 16371 -a oldboyedu INFO REPLICATION
        # Replication
        role:slave
        master_host:172.200.1.201
        master_port:16372
        master_link_status:up
        master_last_io_seconds_ago:2
        master_sync_in_progress:0
        slave_repl_offset:28558
        slave_priority:100
        slave_read_only:1
        connected_slaves:0
        master_repl_offset:0
        repl_backlog_active:0
        repl_backlog_size:1048576
        repl_backlog_first_byte_offset:0
        repl_backlog_histlen:0
        [root@redis201.oldboyedu.com ~]# 
        [root@redis201.oldboyedu.com ~]# redis-cli -p 16372 -a oldboyedu INFO REPLICATION
        # Replication
        role:master
        connected_slaves:1
        slave0:ip=172.200.1.201,port=16371,state=online,offset=28856,lag=1
        master_repl_offset:28856
        repl_backlog_active:1
        repl_backlog_size:1048576
        repl_backlog_first_byte_offset:2
        repl_backlog_histlen:28855
        [root@redis201.oldboyedu.com ~]# 

    (2)修复之后,之前宕机的主库会自动加入集群并成为slaves节点:
        [root@redis201.oldboyedu.com ~]# redis-server /oldboyedu/softwares/redis16370/redis.conf 
        [root@redis201.oldboyedu.com ~]# 
        [root@redis201.oldboyedu.com ~]# redis-cli -p 16370 -a oldboyedu INFO REPLICATION
        # Replication
        role:slave
        master_host:172.200.1.201
        master_port:16372
        master_link_status:up
        master_last_io_seconds_ago:1
        master_sync_in_progress:0
        slave_repl_offset:38731
        slave_priority:100
        slave_read_only:1
        connected_slaves:0
        master_repl_offset:0
        repl_backlog_active:0
        repl_backlog_size:1048576
        repl_backlog_first_byte_offset:0
        repl_backlog_histlen:0
        [root@redis201.oldboyedu.com ~]# 
        [root@redis201.oldboyedu.com ~]# redis-cli -p 16371 -a oldboyedu INFO REPLICATION
        # Replication
        role:slave
        master_host:172.200.1.201
        master_port:16372
        master_link_status:up
        master_last_io_seconds_ago:0
        master_sync_in_progress:0
        slave_repl_offset:39015
        slave_priority:100
        slave_read_only:1
        connected_slaves:0
        master_repl_offset:0
        repl_backlog_active:0
        repl_backlog_size:1048576
        repl_backlog_first_byte_offset:0
        repl_backlog_histlen:0
        [root@redis201.oldboyedu.com ~]# 
        [root@redis201.oldboyedu.com ~]# redis-cli -p 16372 -a oldboyedu INFO REPLICATION
        # Replication
        role:master
        connected_slaves:2
        slave0:ip=172.200.1.201,port=16371,state=online,offset=39157,lag=1
        slave1:ip=172.200.1.201,port=16370,state=online,offset=39157,lag=1
        master_repl_offset:39157
        repl_backlog_active:1
        repl_backlog_size:1048576
        repl_backlog_first_byte_offset:2
        repl_backlog_histlen:39156
        [root@redis201.oldboyedu.com ~]# 

    (3)注意观察sentinel日志哟,会有新日志记录:
        [root@redis201.oldboyedu.com ~]# tail -100f /oldboyedu/logs/redis16379/sentinel.log 

        ...

        14823:X 04 Mar 07:42:37.709 # -sdown slave 172.200.1.201:16370 172.200.1.201 16370 @ oldboyedu_master 172.200.1.201 16372
        14823:X 04 Mar 07:42:47.723 * +convert-to-slave slave 172.200.1.201:16370 172.200.1.201 16370 @ oldboyedu_master 172.200.1.201 16372

    温馨提示:
        除了提到上面的变化外,还有一点变化就是配置文件也修改了,每个"slave"节点都新增了以下两行:(本案例只有"redis16370"和"redis16371"这两个实例的配置文件是slave节点,因此这两个节点中是存在的!)
            [root@redis201.oldboyedu.com ~]# tail -2 /oldboyedu/softwares/redis16370/redis.conf 
            # Generated by CONFIG REWRITE
            slaveof 172.200.1.201 16372
            [root@redis201.oldboyedu.com ~]# 
            [root@redis201.oldboyedu.com ~]# tail -2 /oldboyedu/softwares/redis16371/redis.conf 
            # Generated by CONFIG REWRITE
            slaveof 172.200.1.201 16372
            [root@redis201.oldboyedu.com ~]# 

三.Sentinel管理命令

1.使用"PING"指令监测Redis或者sentinel实例是否正常工作,如果返回"PONG"说明是可以正常相应的!

[root@redis201.oldboyedu.com ~]# redis-cli -p 16379
127.0.0.1:16379> PING
PONG
127.0.0.1:16379> 
127.0.0.1:16379> QUIT
[root@redis201.oldboyedu.com ~]# 

2.使用"SENTINEL MASTERS"指令列出所有被监视的主服务器

[root@redis201.oldboyedu.com ~]# redis-cli -p 16379
127.0.0.1:16379> SENTINEL MASTERS
1)  1) "name"
    2) "oldboyedu_master"
    3) "ip"
    4) "172.200.1.201"
    5) "port"
    6) "16372"
    7) "runid"
    8) "197c7c9da1f90db94becbad0c4a06f7ebfdc58f0"
    9) "flags"
   10) "master"
   11) "link-pending-commands"
   12) "0"
   13) "link-refcount"
   14) "1"
   15) "last-ping-sent"
   16) "0"
   17) "last-ok-ping-reply"
   18) "841"
   19) "last-ping-reply"
   20) "841"
   21) "down-after-milliseconds"
   22) "5000"
   23) "info-refresh"
   24) "3770"
   25) "role-reported"
   26) "master"
   27) "role-reported-time"
   28) "1460019"
   29) "config-epoch"
   30) "1"
   31) "num-slaves"
   32) "2"
   33) "num-other-sentinels"
   34) "0"
   35) "quorum"
   36) "1"
   37) "failover-timeout"
   38) "180000"
   39) "parallel-syncs"
   40) "1"
127.0.0.1:16379> QUIT
[root@redis201.oldboyedu.com ~]# 

3.使用"SENTINEL slaves "指令列出所有被监视的从服务器

[root@redis201.oldboyedu.com ~]# redis-cli -p 16379
127.0.0.1:16379> SENTINEL SLAVES oldboyedu_master
1)  1) "name"
    2) "172.200.1.201:16370"
    3) "ip"
    4) "172.200.1.201"
    5) "port"
    6) "16370"
    7) "runid"
    8) "806728fde23bba8950da1b4dc6efde83ac9a5d10"
    9) "flags"
   10) "slave"
   11) "link-pending-commands"
   12) "0"
   13) "link-refcount"
   14) "1"
   15) "last-ping-sent"
   16) "0"
   17) "last-ok-ping-reply"
   18) "424"
   19) "last-ping-reply"
   20) "424"
   21) "down-after-milliseconds"
   22) "5000"
   23) "info-refresh"
   24) "5726"
   25) "role-reported"
   26) "slave"
   27) "role-reported-time"
   28) "1100125"
   29) "master-link-down-time"
   30) "0"
   31) "master-link-status"
   32) "ok"
   33) "master-host"
   34) "172.200.1.201"
   35) "master-port"
   36) "16372"
   37) "slave-priority"
   38) "100"
   39) "slave-repl-offset"
   40) "108121"
2)  1) "name"
    2) "172.200.1.201:16371"
    3) "ip"
    4) "172.200.1.201"
    5) "port"
    6) "16371"
    7) "runid"
    8) "cae263ec4a59df2a099a69be088748fe64b2f0fa"
    9) "flags"
   10) "slave"
   11) "link-pending-commands"
   12) "0"
   13) "link-refcount"
   14) "1"
   15) "last-ping-sent"
   16) "0"
   17) "last-ok-ping-reply"
   18) "334"
   19) "last-ping-reply"
   20) "334"
   21) "down-after-milliseconds"
   22) "5000"
   23) "info-refresh"
   24) "9472"
   25) "role-reported"
   26) "slave"
   27) "role-reported-time"
   28) "1545671"
   29) "master-link-down-time"
   30) "0"
   31) "master-link-status"
   32) "ok"
   33) "master-host"
   34) "172.200.1.201"
   35) "master-port"
   36) "16372"
   37) "slave-priority"
   38) "100"
   39) "slave-repl-offset"
   40) "107979"
127.0.0.1:16379> 
127.0.0.1:16379> QUIT
[root@redis201.oldboyedu.com ~]# 

4.使用"SENTINEL get-master-addr-by-name "指令返回给定名字的主服务器的IP地址和端口号

[root@redis201.oldboyedu.com ~]# redis-cli -p 16379
127.0.0.1:16379> SENTINEL get-master-addr-by-name oldboyedu_master
1) "172.200.1.201"
2) "16372"
127.0.0.1:16379> 
127.0.0.1:16379> QUIT
[root@redis201.oldboyedu.com ~]# 

5.使用"SENTINEL reset "指令重置所有名字和给定模式"pattern"相匹配的主服务器

[root@redis201.oldboyedu.com ~]# redis-cli -p 16379
127.0.0.1:16379> SENTINEL RESET my*
(integer) 1
127.0.0.1:16379> 
127.0.0.1:16379> QUIT
[root@redis201.oldboyedu.com ~]# 
[root@redis201.oldboyedu.com ~]# tail -100f /oldboyedu/logs/redis16379/sentinel.log

...

14823:X 04 Mar 08:14:21.191 # +reset-master master oldboyedu_master 172.200.1.201 16372
14823:X 04 Mar 08:14:21.672 * +slave slave 172.200.1.201:16371 172.200.1.201 16371 @ oldboyedu_master 172.200.1.201 16372
14823:X 04 Mar 08:14:21.675 * +slave slave 172.200.1.201:16370 172.200.1.201 16370 @ oldboyedu_master 172.200.1.201 16372

...

6.当主服务器失效时,使用"SENTINEL failover "指令在不询问其他Sentinel意见的情况下, 强制开始一次自动故障迁移。

    (1)手动"failover"操作前:
        [root@redis201.oldboyedu.com ~]# redis-cli -p 16370 -a oldboyedu INFO REPLICATION
        # Replication
        role:slave
        master_host:172.200.1.201
        master_port:16372
        master_link_status:up
        master_last_io_seconds_ago:1
        master_sync_in_progress:0
        slave_repl_offset:120585
        slave_priority:100
        slave_read_only:1
        connected_slaves:0
        master_repl_offset:0
        repl_backlog_active:0
        repl_backlog_size:1048576
        repl_backlog_first_byte_offset:0
        repl_backlog_histlen:0
        [root@redis201.oldboyedu.com ~]# 
        [root@redis201.oldboyedu.com ~]# redis-cli -p 16371 -a oldboyedu INFO REPLICATION
        # Replication
        role:slave
        master_host:172.200.1.201
        master_port:16372
        master_link_status:up
        master_last_io_seconds_ago:2
        master_sync_in_progress:0
        slave_repl_offset:120869
        slave_priority:100
        slave_read_only:1
        connected_slaves:0
        master_repl_offset:0
        repl_backlog_active:0
        repl_backlog_size:1048576
        repl_backlog_first_byte_offset:0
        repl_backlog_histlen:0
        [root@redis201.oldboyedu.com ~]# 
        [root@redis201.oldboyedu.com ~]# redis-cli -p 16372 -a oldboyedu INFO REPLICATION
        # Replication
        role:master
        connected_slaves:2
        slave0:ip=172.200.1.201,port=16371,state=online,offset=121153,lag=0
        slave1:ip=172.200.1.201,port=16370,state=online,offset=121153,lag=0
        master_repl_offset:121153
        repl_backlog_active:1
        repl_backlog_size:1048576
        repl_backlog_first_byte_offset:2
        repl_backlog_histlen:121152
        [root@redis201.oldboyedu.com ~]# 

    (2)手动触发故障转移
        [root@redis201.oldboyedu.com ~]# redis-cli -p 16379
        127.0.0.1:16379> SENTINEL failover oldboyedu_master
        OK
        127.0.0.1:16379> 
        127.0.0.1:16379> QUIT
        [root@redis201.oldboyedu.com ~]# 
        [root@redis201.oldboyedu.com ~]# tail -100f /oldboyedu/logs/redis16379/sentinel.log  # 注意观察日志的变化

        ...

        14823:X 04 Mar 08:06:16.688 # Executing user requested FAILOVER of 'oldboyedu_master'
        14823:X 04 Mar 08:06:16.688 # +new-epoch 2
        14823:X 04 Mar 08:06:16.688 # +try-failover master oldboyedu_master 172.200.1.201 16372
        14823:X 04 Mar 08:06:16.697 # +vote-for-leader 13c5048514a67a5a66b0792cb3b64a3a85e10b4b 2
        14823:X 04 Mar 08:06:16.697 # +elected-leader master oldboyedu_master 172.200.1.201 16372
        14823:X 04 Mar 08:06:16.697 # +failover-state-select-slave master oldboyedu_master 172.200.1.201 16372
        14823:X 04 Mar 08:06:16.753 # +selected-slave slave 172.200.1.201:16370 172.200.1.201 16370 @ oldboyedu_master 172.200.1.201 16372
        14823:X 04 Mar 08:06:16.753 * +failover-state-send-slaveof-noone slave 172.200.1.201:16370 172.200.1.201 16370 @ oldboyedu_master 172.200.1.201 16372
        14823:X 04 Mar 08:06:16.820 * +failover-state-wait-promotion slave 172.200.1.201:16370 172.200.1.201 16370 @ oldboyedu_master 172.200.1.201 16372
        14823:X 04 Mar 08:06:17.740 # +promoted-slave slave 172.200.1.201:16370 172.200.1.201 16370 @ oldboyedu_master 172.200.1.201 16372
        14823:X 04 Mar 08:06:17.740 # +failover-state-reconf-slaves master oldboyedu_master 172.200.1.201 16372
        14823:X 04 Mar 08:06:17.800 * +slave-reconf-sent slave 172.200.1.201:16371 172.200.1.201 16371 @ oldboyedu_master 172.200.1.201 16372
        14823:X 04 Mar 08:06:18.798 * +slave-reconf-inprog slave 172.200.1.201:16371 172.200.1.201 16371 @ oldboyedu_master 172.200.1.201 16372
        14823:X 04 Mar 08:06:18.798 * +slave-reconf-done slave 172.200.1.201:16371 172.200.1.201 16371 @ oldboyedu_master 172.200.1.201 16372
        14823:X 04 Mar 08:06:18.882 # +failover-end master oldboyedu_master 172.200.1.201 16372
        14823:X 04 Mar 08:06:18.882 # +switch-master oldboyedu_master 172.200.1.201 16372 172.200.1.201 16370
        14823:X 04 Mar 08:06:18.882 * +slave slave 172.200.1.201:16371 172.200.1.201 16371 @ oldboyedu_master 172.200.1.201 16370
        14823:X 04 Mar 08:06:18.882 * +slave slave 172.200.1.201:16372 172.200.1.201 16372 @ oldboyedu_master 172.200.1.201 16370
        14823:X 04 Mar 08:06:29.000 * +convert-to-slave slave 172.200.1.201:16372 172.200.1.201 16372 @ oldboyedu_master 172.200.1.201 16370

        ...

    (3)手动"failover"操作后:
        [root@redis201.oldboyedu.com ~]# redis-cli -p 16370 -a oldboyedu INFO REPLICATION
        # Replication
        role:master
        connected_slaves:2
        slave0:ip=172.200.1.201,port=16371,state=online,offset=135098,lag=1
        slave1:ip=172.200.1.201,port=16372,state=online,offset=135098,lag=0
        master_repl_offset:135098
        repl_backlog_active:1
        repl_backlog_size:1048576
        repl_backlog_first_byte_offset:129559
        repl_backlog_histlen:5540
        [root@redis201.oldboyedu.com ~]# 
        [root@redis201.oldboyedu.com ~]# redis-cli -p 16371 -a oldboyedu INFO REPLICATION
        # Replication
        role:slave
        master_host:172.200.1.201
        master_port:16370
        master_link_status:up
        master_last_io_seconds_ago:1
        master_sync_in_progress:0
        slave_repl_offset:135396
        slave_priority:100
        slave_read_only:1
        connected_slaves:0
        master_repl_offset:0
        repl_backlog_active:0
        repl_backlog_size:1048576
        repl_backlog_first_byte_offset:0
        repl_backlog_histlen:0
        [root@redis201.oldboyedu.com ~]# 
        [root@redis201.oldboyedu.com ~]# redis-cli -p 16372 -a oldboyedu INFO REPLICATION
        # Replication
        role:slave
        master_host:172.200.1.201
        master_port:16370
        master_link_status:up
        master_last_io_seconds_ago:1
        master_sync_in_progress:0
        slave_repl_offset:135538
        slave_priority:100
        slave_read_only:1
        connected_slaves:0
        master_repl_offset:0
        repl_backlog_active:0
        repl_backlog_size:1048576
        repl_backlog_first_byte_offset:2
        repl_backlog_histlen:130295
        [root@redis201.oldboyedu.com ~]# 

四.作业

(1)创建sentinel的配置文件
install -d /oldboyedu/{data,softwares}/sentinel
cat > /oldboyedu/softwares/sentinel/sentinel.conf <<EOF
bind 10.0.0.106
port 18106
dir /oldboyedu/data/sentinel
sentinel monitor oldboyedu_master 10.0.0.108 8108 1
sentinel down-after-milliseconds oldboyedu_master 5000
sentinel auth-pass oldboyedu_master oldboyedu
EOF

(2)前台启动sentinel
redis-sentinel /oldboyedu/softwares/sentinel/sentinel.conf

作业:
    (1)完成课上的所有练习,包括但不限于字符串,列表,字典,集合,有序集合,主从复制,单哨兵模式;
    (2)实现单哨兵的VIP飘逸功能(扩展题);
谨此笔记,记录过往。凭君阅览,如能收益,莫大奢望。
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇