redis哨兵(sentinel)搭建以及springboot实战

哨兵是建立在redis主从集群的基础上的,所以本文是承接上一篇redis主从复制的。一主二从三哨兵,哨兵也是部在主从节点上的,端口为26379

主从节点

master
node01 192.168.71.101 6379
slave1
node02 192.168.71.102 6379
slave2
node03 192.168.71.103 6379

哨兵节点

sentinel1
192.168.71.101 26379
sentinel2
192.168.71.102 26379
sentinel3
192.168.71.103 26379

redis哨兵(sentinel)搭建以及springboot实战_第1张图片

哨兵配置

开启守护进程

daemonize yes

关闭保护模式

protected-mode no

指定监听主服务器名称、ip、port和几个sentinel检测异常执行故障转移

#mymaster是自定义的,因为我是三哨兵,所以指定当有2个哨兵检测到异常才执行故障转移
sentinel monitor mymaster 192.168.71.102 6379 2

指定监听服务器密码

#注意这个步骤必须在指定监听服务器名称后面,不然会报不知道mymaster
sentinel auth-pass mymaster 123456

其他配置使用默认配置

端口

port 26379

指定sentinel判定master断线时长

sentinel down-after-milliseconds mymaster 30000

指定故障切换超时时长

sentinel failover-timeout mymaster 900000

指定sentinel日志文件

logfile "/opt/redis/log/sentinel.log"

三台哨兵的配置都是一样的,配置完成后,分别启动三台机子的redis服务进程和sentinel服务进程

#启动redis服务进程
./redis-server ../redis.conf
#启动sentinel服务进程
./redis-server ../sentinel.conf --sentinel
#或者
./redis-sentinel ../sentinel.conf

打开192.168.71.101节点的sentinel日志文件查看有以下内容

[root@node03 log]# tail -20 sentinel.log 
10656:X 17 Jun 14:00:43.355 * Running mode=sentinel, port=26379.
10656:X 17 Jun 14:00:43.356 # Sentinel ID is 69eba2364497f8ea68131787de656207cec5ee0e
10656:X 17 Jun 14:00:43.356 # +monitor master mymaster 192.168.71.101 6379 quorum 2
10656:X 17 Jun 14:00:43.356 * +slave slave 192.168.71.102:6379 192.168.71.102 6379 @ mymaster 192.168.71.101 6379
10656:X 17 Jun 14:00:43.372 * +slave slave 192.168.71.103:6379 192.168.71.103 6379 @ mymaster 192.168.71.101 6379
10656:X 17 Jun 14:01:10.429 * +sentinel sentinel f0fc111b6d3ad60914566d2df3b5ee592415f3e4 192.168.71.102 26379 @ mymaster 192.168.71.101 6379
10656:X 17 Jun 14:01:26.347 * +sentinel sentinel d445b01ec9d79f90fe9f3d0377906be2290399f3 192.168.71.103 26379 @ mymaster 192.168.71.101 6379

打开192.168.71.102节点的sentinel日志文件查看有以下内容

[root@node03 log]# tail -20 sentinel.log 
6254:X 17 Jun 14:01:34.097 * Running mode=sentinel, port=26379.
6254:X 17 Jun 14:01:34.098 # Sentinel ID is f0fc111b6d3ad60914566d2df3b5ee592415f3e4
6254:X 17 Jun 14:01:34.098 # +monitor master mymaster 192.168.71.101 6379 quorum 2
6254:X 17 Jun 14:01:34.099 * +slave slave 192.168.71.102:6379 192.168.71.102 6379 @ mymaster 192.168.71.101 6379
6254:X 17 Jun 14:01:34.118 * +slave slave 192.168.71.103:6379 192.168.71.103 6379 @ mymaster 192.168.71.101 6379
6254:X 17 Jun 14:01:35.644 * +sentinel sentinel 69eba2364497f8ea68131787de656207cec5ee0e 192.168.71.101 26379 @ mymaster 192.168.71.101 6379
6254:X 17 Jun 14:01:52.041 * +sentinel sentinel d445b01ec9d79f90fe9f3d0377906be2290399f3 192.168.71.103 26379 @ mymaster 192.168.71.101 6379

打开192.168.71.103节点的sentinel日志文件查看有以下内容

[root@node03 log]# tail -20 sentinel.log 
3798:X 18 Jun 03:43:30.475 * Running mode=sentinel, port=26379.
3798:X 18 Jun 03:43:30.475 # Sentinel ID is d445b01ec9d79f90fe9f3d0377906be2290399f3
3798:X 18 Jun 03:43:30.475 # +monitor master mymaster 192.168.71.101 6379 quorum 2
3798:X 18 Jun 03:43:30.476 * +slave slave 192.168.71.102:6379 192.168.71.102 6379 @ mymaster 192.168.71.101 6379
3798:X 18 Jun 03:43:30.486 * +slave slave 192.168.71.103:6379 192.168.71.103 6379 @ mymaster 192.168.71.101 6379
3798:X 18 Jun 03:43:30.769 * +sentinel sentinel f0fc111b6d3ad60914566d2df3b5ee592415f3e4 192.168.71.102 26379 @ mymaster 192.168.71.101 6379
3798:X 18 Jun 03:43:32.396 * +sentinel sentinel 69eba2364497f8ea68131787de656207cec5ee0e 192.168.71.101 26379 @ mymaster 192.168.71.101 6379

验证高可用

手动kill掉master 192.168.71.101节点的redis服务进程

[root@node01 log]# ps -ef|grep redis
root      10644      1  0 13:56 ?        00:00:00 src/redis-server 0.0.0.0:6379
root      10656  10545  0 14:00 pts/1    00:00:00 ./redis-server *:26379 [sentinel]       
root      10695  10676  0 14:05 pts/0    00:00:00 grep redis
[root@node01 log]# kill -9 10644

30s后观察192.168.71.102和192.168.71.103两台sentinel日志文件变化,这个30s就是上面指定sentinel判定master断线的时长

sentinel down-after-milliseconds mymaster 30000

可以看到sentinel在投票选举新的master,并会自动修改redis.conf和sentinel.conf文件保证集群的高可用。

192.168.71.101节点的sentinel日志

[root@node01 log]# tail -f sentinel.log 
10656:X 17 Jun 14:01:10.429 * +sentinel sentinel f0fc111b6d3ad60914566d2df3b5ee592415f3e4 192.168.71.102 26379 @ mymaster 192.168.71.101 6379
10656:X 17 Jun 14:01:26.347 * +sentinel sentinel d445b01ec9d79f90fe9f3d0377906be2290399f3 192.168.71.103 26379 @ mymaster 192.168.71.101 6379
10656:X 17 Jun 14:06:36.683 # +sdown master mymaster 192.168.71.101 6379
10656:X 17 Jun 14:06:36.827 # +new-epoch 1
10656:X 17 Jun 14:06:36.830 # +vote-for-leader d445b01ec9d79f90fe9f3d0377906be2290399f3 1
10656:X 17 Jun 14:06:37.309 # +config-update-from sentinel d445b01ec9d79f90fe9f3d0377906be2290399f3 192.168.71.103 26379 @ mymaster 192.168.71.101 6379
10656:X 17 Jun 14:06:37.310 # +switch-master mymaster 192.168.71.101 6379 192.168.71.102 6379
10656:X 17 Jun 14:06:37.310 * +slave slave 192.168.71.103:6379 192.168.71.103 6379 @ mymaster 192.168.71.102 6379
10656:X 17 Jun 14:06:37.310 * +slave slave 192.168.71.101:6379 192.168.71.101 6379 @ mymaster 192.168.71.102 6379
10656:X 17 Jun 14:07:07.316 # +sdown slave 192.168.71.101:6379 192.168.71.101 6379 @ mymaster 192.168.71.102 6379

192.168.71.102节点的sentinel日志

6254:X 17 Jun 14:07:02.426 # +sdown master mymaster 192.168.71.101 6379
6254:X 17 Jun 14:07:02.519 # +new-epoch 1
6254:X 17 Jun 14:07:02.524 # +vote-for-leader d445b01ec9d79f90fe9f3d0377906be2290399f3 1
6254:X 17 Jun 14:07:02.526 # +odown master mymaster 192.168.71.101 6379 #quorum 2/2
6254:X 17 Jun 14:07:02.526 # Next failover delay: I will not start a failover before Mon Jun 17 14:37:03 2019
6254:X 17 Jun 14:07:03.004 # +config-update-from sentinel d445b01ec9d79f90fe9f3d0377906be2290399f3 192.168.71.103 26379 @ mymaster 192.168.71.101 6379
6254:X 17 Jun 14:07:03.004 # +switch-master mymaster 192.168.71.101 6379 192.168.71.102 6379
6254:X 17 Jun 14:07:03.004 * +slave slave 192.168.71.103:6379 192.168.71.103 6379 @ mymaster 192.168.71.102 6379
6254:X 17 Jun 14:07:03.004 * +slave slave 192.168.71.101:6379 192.168.71.101 6379 @ mymaster 192.168.71.102 6379
6254:X 17 Jun 14:07:33.052 # +sdown slave 192.168.71.101:6379 192.168.71.101 6379 @ mymaster 192.168.71.102 6379

192.168.71.103节点的sentinel日志

3798:X 18 Jun 03:48:42.871 # +sdown master mymaster 192.168.71.101 6379
3798:X 18 Jun 03:48:42.943 # +odown master mymaster 192.168.71.101 6379 #quorum 3/2
3798:X 18 Jun 03:48:42.943 # +new-epoch 1
3798:X 18 Jun 03:48:42.943 # +try-failover master mymaster 192.168.71.101 6379
3798:X 18 Jun 03:48:42.945 # +vote-for-leader d445b01ec9d79f90fe9f3d0377906be2290399f3 1
3798:X 18 Jun 03:48:42.952 # f0fc111b6d3ad60914566d2df3b5ee592415f3e4 voted for d445b01ec9d79f90fe9f3d0377906be2290399f3 1
3798:X 18 Jun 03:48:42.952 # 69eba2364497f8ea68131787de656207cec5ee0e voted for d445b01ec9d79f90fe9f3d0377906be2290399f3 1
3798:X 18 Jun 03:48:43.001 # +elected-leader master mymaster 192.168.71.101 6379
3798:X 18 Jun 03:48:43.001 # +failover-state-select-slave master mymaster 192.168.71.101 6379
3798:X 18 Jun 03:48:43.067 # +selected-slave slave 192.168.71.102:6379 192.168.71.102 6379 @ mymaster 192.168.71.101 6379
3798:X 18 Jun 03:48:43.067 * +failover-state-send-slaveof-noone slave 192.168.71.102:6379 192.168.71.102 6379 @ mymaster 192.168.71.101 6379
3798:X 18 Jun 03:48:43.168 * +failover-state-wait-promotion slave 192.168.71.102:6379 192.168.71.102 6379 @ mymaster 192.168.71.101 6379
3798:X 18 Jun 03:48:43.324 # +promoted-slave slave 192.168.71.102:6379 192.168.71.102 6379 @ mymaster 192.168.71.101 6379
3798:X 18 Jun 03:48:43.324 # +failover-state-reconf-slaves master mymaster 192.168.71.101 6379
3798:X 18 Jun 03:48:43.423 * +slave-reconf-sent slave 192.168.71.103:6379 192.168.71.103 6379 @ mymaster 192.168.71.101 6379
3798:X 18 Jun 03:48:44.064 # -odown master mymaster 192.168.71.101 6379
3798:X 18 Jun 03:48:44.354 * +slave-reconf-inprog slave 192.168.71.103:6379 192.168.71.103 6379 @ mymaster 192.168.71.101 6379
3798:X 18 Jun 03:48:44.354 * +slave-reconf-done slave 192.168.71.103:6379 192.168.71.103 6379 @ mymaster 192.168.71.101 6379
3798:X 18 Jun 03:48:44.444 # +failover-end master mymaster 192.168.71.101 6379
3798:X 18 Jun 03:48:44.444 # +switch-master mymaster 192.168.71.101 6379 192.168.71.102 6379
3798:X 18 Jun 03:48:44.444 * +slave slave 192.168.71.103:6379 192.168.71.103 6379 @ mymaster 192.168.71.102 6379
3798:X 18 Jun 03:48:44.444 * +slave slave 192.168.71.101:6379 192.168.71.101 6379 @ mymaster 192.168.71.102 6379
3798:X 18 Jun 03:49:14.487 # +sdown slave 192.168.71.101:6379 192.168.71.101 6379 @ mymaster 192.168.71.102 6379

从日志可以看出最后新投票选的master是192.168.71.102节点,这时再去启动老master 192.168.71.101的redis服务,哨兵会将192.168.71.101转成192.168.71.102的slave,从192.168.71.102 sentinel日志可以看到信息

[root@node02 log]# tail -f sentinel.log 
6254:X 17 Jun 14:07:02.426 # +sdown master mymaster 192.168.71.101 6379
6254:X 17 Jun 14:07:02.519 # +new-epoch 1
6254:X 17 Jun 14:07:02.524 # +vote-for-leader d445b01ec9d79f90fe9f3d0377906be2290399f3 1
6254:X 17 Jun 14:07:02.526 # +odown master mymaster 192.168.71.101 6379 #quorum 2/2
6254:X 17 Jun 14:07:02.526 # Next failover delay: I will not start a failover before Mon Jun 17 14:37:03 2019
6254:X 17 Jun 14:07:03.004 # +config-update-from sentinel d445b01ec9d79f90fe9f3d0377906be2290399f3 192.168.71.103 26379 @ mymaster 192.168.71.101 6379
6254:X 17 Jun 14:07:03.004 # +switch-master mymaster 192.168.71.101 6379 192.168.71.102 6379
6254:X 17 Jun 14:07:03.004 * +slave slave 192.168.71.103:6379 192.168.71.103 6379 @ mymaster 192.168.71.102 6379
6254:X 17 Jun 14:07:03.004 * +slave slave 192.168.71.101:6379 192.168.71.101 6379 @ mymaster 192.168.71.102 6379
6254:X 17 Jun 14:07:33.052 # +sdown slave 192.168.71.101:6379 192.168.71.101 6379 @ mymaster 192.168.71.102 6379
6254:X 17 Jun 18:14:50.840 # -sdown slave 192.168.71.101:6379 192.168.71.101 6379 @ mymaster 192.168.71.102 6379
6254:X 17 Jun 18:15:00.777 * +convert-to-slave slave 192.168.71.101:6379 192.168.71.101 6379 @ mymaster 192.168.71.102 6379
6254:X 17 Jun 18:15:06.193 # +sdown sentinel 69eba2364497f8ea68131787de656207cec5ee0e 192.168.71.101 26379 @ mymaster 192.168.71.102 6379
6254:X 17 Jun 18:16:17.556 # -sdown sentinel 69eba2364497f8ea68131787de656207cec5ee0e 192.168.71.101 26379 @ mymaster 192.168.71.102 6379

与springboot实战

github地址

你可能感兴趣的:(redis)