三:redis哨兵模式实现主从故障切换2

本篇接着上一篇进行redis哨兵的配置练习实验,一般经典的哨兵需要3个节点(为什么是3个节点,不是两个节点)后面专门写篇文章来分析这个问题.
可以再用一台虚拟机安装一个redis服务,这台虚拟机不需要启动实例,启动哨兵就行,我这里还是用两台机器只是测试,生产上可以分开,在zys-zk01 上启动一个哨兵端口5000,在zys-zk02上启动两个哨兵节点端口分别为5001,5002 ,这样就可以模拟3个节点的效果

  • 哨兵配置文件

sentinel.conf

最小的配置
每一个哨兵都可以去监控多个maser-slaves的主从架构
因为可能你的公司里,为不同的项目,部署了多个master-slaves的redis主从集群
相同的一套哨兵集群,就可以去监控不同的多个redis主从集群
你自己给每个redis主从集群分配一个逻辑的名称,列如

sentinel monitor mymaster 127.0.0.1 6379 2
sentinel down-after-milliseconds mymaster 60000
sentinel failover-timeout mymaster 180000
sentinel parallel-syncs mymaster 1

sentinel monitor resque 192.168.1.3 6380 4
sentinel down-after-milliseconds resque 10000
sentinel failover-timeout resque 180000
sentinel parallel-syncs resque 5

sentinel monitor mymaster 127.0.0.1 6379

类似这种配置,来指定对一个master的监控,给监控的master指定的一个名称,因为后面分布式集群架构里会讲解,可以配置多个master做数据拆分

sentinel down-after-milliseconds mymaster 60000
sentinel failover-timeout mymaster 180000
sentinel parallel-syncs mymaster 1

上面的三个配置,都是针对某个监控的master配置的,给其指定上面分配的名称即可

上面这段配置,就监控了两个master node

这是最小的哨兵配置,如果发生了master-slave故障转移,或者新的哨兵进程加入哨兵集群,那么哨兵会自动更新自己的配置文件

sentinel monitor master-group-name hostname port quorum

quorum的解释如下:

(1)至少多少个哨兵要一致同意,master进程挂掉了,或者slave进程挂掉了,或者要启动一个故障转移操作
(2)quorum是用来识别故障的,真正执行故障转移的时候,还是要在哨兵集群执行选举,选举一个哨兵进程出来执行故障转移操作
(3)假设有5个哨兵,quorum设置了2,那么如果5个哨兵中的2个都认为master挂掉了; 2个哨兵中的一个就会做一个选举,选举一个哨兵出来,执行故障转移; 如果5个哨兵中有3个哨兵都是运行的,那么故障转移就会被允许执行

down-after-milliseconds,超过多少毫秒跟一个redis实例断了连接,哨兵就可能认为这个redis实例挂了

parallel-syncs,新的master别切换之后,同时有多少个slave被切换到去连接新master,重新做同步,数字越低,花费的时间越多
假设你的redis是1个master,4个slave
然后master宕机了,4个slave中有1个切换成了master,剩下3个slave就要挂到新的master上面去
这个时候,如果parallel-syncs是1,那么3个slave,一个一个地挂接到新的master上面去,1个挂接完,而且从新的master sync完数据之后,再挂接下一个
如果parallel-syncs是3,那么一次性就会把所有slave挂接到新的master上去
failover-timeout,执行故障转移的timeout超时时长

  • 哨兵的配置

哨兵默认用26379端口,默认不能跟其他机器在指定端口连通,只能在本地访问
在zys-zk01上:

[root@zys-zk01 ~]# mkdir /etc/sentinal
[root@zys-zk01 ~]# mkdir -p /var/sentinal/5000
[root@zys-zk01 ~]# vim /etc/sentinal/5000.conf

port 5000
#bind 192.168.31.187
protected-mode no
dir /var/sentinal/5000
sentinel monitor mymaster 192.168.1.232 6379 2
sentinel down-after-milliseconds mymaster 30000
sentinel failover-timeout mymaster 60000
sentinel parallel-syncs mymaster 1

在zys-zk02上:

[root@zys-zk02 redis]# mkdir /etc/sentinal
[root@zys-zk02 redis]# mkdir -p /var/sentinal/5001
[root@zys-zk02 redis]# mkdir -p /var/sentinal/5002
[root@zys-zk02 redis]# vim /etc/sentinal/5001.conf
port 5001
protected-mode no
dir /var/sentinal/5001
sentinel monitor mymaster 192.168.1.232 6379 2
sentinel down-after-milliseconds mymaster 30000
sentinel failover-timeout mymaster 60000
sentinel parallel-syncs mymaster 1


[root@zys-zk02 redis]# vim /etc/sentinal/5002.conf
port 5002
protected-mode no
dir /var/sentinal/5002
sentinel monitor mymaster 192.168.1.232 6379 2
sentinel down-after-milliseconds mymaster 30000
sentinel failover-timeout mymaster 60000
sentinel parallel-syncs mymaster 1
  • 启动哨兵
    分别启动三个哨兵进程,组成一个集群,观察一下日志的输出
    redis-sentinel /etc/sentinal/5000.conf
    redis-server /etc/sentinal/5000.conf –sentinel
    我们这里先不后台启动,观看下日志:
[root@zys-zk01 ~]# /usr/local/bin/redis-sentinel /etc/sentinal/5000.conf 
2827:X 17 Jul 16:58:50.320 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 3.2.8 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in sentinel mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 5000
 |    `-._   `._    /     _.-'    |     PID: 2827
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

2827:X 17 Jul 16:58:50.322 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
2827:X 17 Jul 16:58:50.333 # Sentinel ID is fec51ceca1905577d0fa120fbf328e909a970950
2827:X 17 Jul 16:58:50.333 # +monitor master mymaster 192.168.1.232 6379 quorum 2
2827:X 17 Jul 16:58:50.334 * +slave slave 192.168.1.233:6380 192.168.1.233 6380 @ mymaster 192.168.1.232 6379

[root@zys-zk02 redis]# /usr/local/bin/redis-sentinel /etc/sentinal/5001.conf 
2168:X 17 Jul 17:00:25.330 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 3.2.8 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in sentinel mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 5001
 |    `-._   `._    /     _.-'    |     PID: 2168
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

2168:X 17 Jul 17:00:25.332 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
2168:X 17 Jul 17:00:25.343 # Sentinel ID is 5a8b105ed5416ead28de3ef31a3f7231e8db1cdd
2168:X 17 Jul 17:00:25.343 # +monitor master mymaster 192.168.1.232 6379 quorum 2
2168:X 17 Jul 17:00:25.345 * +slave slave 192.168.1.233:6380 192.168.1.233 6380 @ mymaster 192.168.1.232 6379
2168:X 17 Jul 17:00:25.506 * +sentinel sentinel fec51ceca1905577d0fa120fbf328e909a970950 192.168.1.232 5000 @ mymaster 192.168.1.232 6379

[root@zys-zk02 ~]# /usr/local/bin/redis-sentinel  /etc/sentinal/5002.conf 
2204:X 17 Jul 17:01:16.838 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 3.2.8 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in sentinel mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 5002
 |    `-._   `._    /     _.-'    |     PID: 2204
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

2204:X 17 Jul 17:01:16.843 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
2204:X 17 Jul 17:01:16.846 # Sentinel ID is 3a886fa2b0eb8dd114b99c94b5828753bc16d4c3
2204:X 17 Jul 17:01:16.846 # +monitor master mymaster 192.168.1.232 6379 quorum 2
2204:X 17 Jul 17:01:16.847 * +slave slave 192.168.1.233:6380 192.168.1.233 6380 @ mymaster 192.168.1.232 6379
2204:X 17 Jul 17:01:17.173 * +sentinel sentinel fec51ceca1905577d0fa120fbf328e909a970950 192.168.1.232 5000 @ mymaster 192.168.1.232 6379
2204:X 17 Jul 17:01:18.558 * +sentinel sentinel 5a8b105ed5416ead28de3ef31a3f7231e8db1cdd 192.168.1.233 5001 @ mymaster 192.168.1.232 6379

看日志哨兵都正常启动,我们后台启动,然后看看检查下哨兵的状态

在5000.conf,5001.conf,5002.conf配置文件里都添加一行配置:
daemonize yes (后台进程模式启动)

[root@zys-zk01 ~]# /usr/local/bin/redis-sentinel /etc/sentinal/5000.conf 
[root@zys-zk01 ~]# ps -ef|grep redis
root       1753      1  0 09:52 ?        00:00:26 /usr/local/bin/redis-server *:6379              
root       2871      1  0 17:06 ?        00:00:00 /usr/local/bin/redis-sentinel *:5000 [sentinel]      
root       2875   1864  0 17:06 pts/0    00:00:00 grep redis
[root@zys-zk02 redis]# /usr/local/bin/redis-sentinel /etc/sentinal/5001.conf 
[root@zys-zk02 redis]# /usr/local/bin/redis-sentinel /etc/sentinal/5002.conf 
[root@zys-zk02 redis]# ps -ef|grep redis
root       2086      1  0 16:26 ?        00:00:03 /usr/local/bin/redis-server *:6380              
root       2222      1  0 17:08 ?        00:00:00 /usr/local/bin/redis-sentinel *:5001 [sentinel]      
root       2226      1  0 17:08 ?        00:00:00 /usr/local/bin/redis-sentinel *:5002 [sentinel]      
root       2231   2034  0 17:09 pts/0    00:00:00 grep redis

查看哨兵状态:

[root@zys-zk01 ~]# /usr/local/bin/redis-cli -h 192.168.1.232 -p 5000
192.168.1.232:5000> sentinel master mymaster
 1) "name"
 2) "mymaster"
 3) "ip"
 4) "192.168.1.232"
 5) "port"
 6) "6379"
 7) "runid"
 8) "59d3b326a792799ad2ca25bdba36337359b50f3a"
 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) "96"
19) "last-ping-reply"
20) "96"
21) "down-after-milliseconds"
22) "30000"
23) "info-refresh"
24) "6834"
25) "role-reported"
26) "master"
27) "role-reported-time"
28) "187557"
29) "config-epoch"
30) "0"
31) "num-slaves"
32) "1"
33) "num-other-sentinels"
34) "2"
35) "quorum"
36) "2"
37) "failover-timeout"
38) "60000"
39) "parallel-syncs"
40) "1"
192.168.1.232:5000>
192.168.1.232:5000> SENTINEL slaves mymaster
1)  1) "name"
    2) "192.168.1.233:6380"
    3) "ip"
    4) "192.168.1.233"
    5) "port"
    6) "6380"
    7) "runid"
    8) "8558cedb4a89829670696d44233f1e6cb603e2ab"
    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) "725"
   19) "last-ping-reply"
   20) "725"
   21) "down-after-milliseconds"
   22) "30000"
   23) "info-refresh"
   24) "5101"
   25) "role-reported"
   26) "slave"
   27) "role-reported-time"
   28) "236056"
   29) "master-link-down-time"
   30) "0"
   31) "master-link-status"
   32) "ok"
   33) "master-host"
   34) "192.168.1.232"
   35) "master-port"
   36) "6379"
   37) "slave-priority"
   38) "100"
   39) "slave-repl-offset"
   40) "69835"
192.168.1.232:5000>
192.168.1.232:5000> SENTINEL sentinels mymaster
1)  1) "name"
    2) "5a8b105ed5416ead28de3ef31a3f7231e8db1cdd"
    3) "ip"
    4) "192.168.1.233"
    5) "port"
    6) "5001"
    7) "runid"
    8) "5a8b105ed5416ead28de3ef31a3f7231e8db1cdd"
    9) "flags"
   10) "sentinel"
   11) "link-pending-commands"
   12) "0"
   13) "link-refcount"
   14) "1"
   15) "last-ping-sent"
   16) "0"
   17) "last-ok-ping-reply"
   18) "142"
   19) "last-ping-reply"
   20) "142"
   21) "down-after-milliseconds"
   22) "30000"
   23) "last-hello-message"
   24) "391"
   25) "voted-leader"
   26) "?"
   27) "voted-leader-epoch"
   28) "0"
2)  1) "name"
    2) "3a886fa2b0eb8dd114b99c94b5828753bc16d4c3"
    3) "ip"
    4) "192.168.1.233"
    5) "port"
    6) "5002"
    7) "runid"
    8) "3a886fa2b0eb8dd114b99c94b5828753bc16d4c3"
    9) "flags"
   10) "sentinel"
   11) "link-pending-commands"
   12) "0"
   13) "link-refcount"
   14) "1"
   15) "last-ping-sent"
   16) "0"
   17) "last-ok-ping-reply"
   18) "142"
   19) "last-ping-reply"
   20) "142"
   21) "down-after-milliseconds"
   22) "30000"
   23) "last-hello-message"
   24) "768"

ok,3个节点的哨兵配置及测试,到这里就实验测试完毕了,下一篇将主要介绍如何跟spring 进行整合进行开发

你可能感兴趣的:(redis)