使用Sentinel机制实现Redis高可用主从复制(2)

192.168.1.11:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.1.12,port=6379,state=online,offset=1023,lag=1
slave1:ip=192.168.1.13,port=6379,state=online,offset=1023,lag=1
master_repl_offset:1023
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:1022
192.168.1.11:6379> set 1 1000
OK
192.168.1.11:6379> keys *
1) "1"
192.168.1.11:6379> get 1
"1000"

[root@station12 ~]# redis-cli -h 192.168.1.12 -p 6379

192.168.1.12:6379> keys *
1) "1"
192.168.1.12:6379> get 1
"1000"

[root@station13 ~]# redis-cli -h 192.168.1.13 -p 6379

192.168.1.13:6379> get 1
"1000"
192.168.1.13:6379> keys *
1) "1"

3.1、主库sentinel配置文件

[root@station11 ~]# cp /usr/local/redis/sentinel.conf /etc/redis/

[root@station11 ~]# vim /etc/redis/sentinel.conf

port 26379
sentinel monitor mymaster 192.168.1.11 6379 1
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 60000
logfile "/var/log/sentinel_master.log"
protected-mode no  打开保护模式,否则sentinel不会监听除了127.0.0.1和192.168.1.1之外IP,也就不会超过1张投票给station11已经死,需要重新选举主服
注意:192.168.1.12和192.168.1.13上的sentinel配置和192.168.1.11一样,只有端口和log不一样如下:
slave1 192.168.1.12

[root@station11 ~]# ansible all -m copy -a "src=/etc/redis/sentinel.conf dest=/etc/redis/"

3.2、从库slave1的sentinel配置文件

[root@station12 ~]# vim /etc/redis/sentinel.conf

port 26479
logfile "/var/log/sentinel_slave1.log"

3.3、从库slave1的sentinel配置文件

[root@station13 ~]# vim /etc/redis/sentinel.conf

port 26579
logfile "/var/log/sentinel_slave2.log"

3.4、主从库sentinel连接时日志

[root@station11 ~]# nohup redis-sentinel /etc/redis/sentinel.conf&

[1] 6640

[root@station11 ~]# tail -f /var/log/sentinel_master.log

6640:X 23 Jan 00:22:34.910 # Sentinel ID is 17c9ee07632d60c4c0aa75a853bbda93966caa22
6640:X 23 Jan 00:22:34.910 # +monitor master mymaster 192.168.1.11 6379 quorum 1
6640:X 23 Jan 00:22:34.910 * +slave slave 192.168.1.12:6379 192.168.1.12 6379 @ mymaster 192.168.1.11 6379
6640:X 23 Jan 00:22:34.912 * +slave slave 192.168.1.13:6379 192.168.1.13 6379 @ mymaster 192.168.1.11 6379

[root@station12 ~]# nohup redis-sentinel /etc/redis/sentinel.conf&

[2] 7782

[root@station12 ~]# tail -f /var/log/sentinel_slave1.log

7782:X 23 Jan 00:24:36.059 # Sentinel ID is 7c000aa564ed603eeefd031333ebc2c916597ec6
7782:X 23 Jan 00:24:36.059 # +monitor master mymaster 192.168.1.11 6379 quorum 1
7782:X 23 Jan 00:24:36.060 * +slave slave 192.168.1.12:6379 192.168.1.12 6379 @ mymaster 192.168.1.11 6379
7782:X 23 Jan 00:24:36.061 * +slave slave 192.168.1.13:6379 192.168.1.13 6379 @ mymaster 192.168.1.11 6379
7782:X 23 Jan 00:24:36.516 * +sentinel sentinel 17c9ee07632d60c4c0aa75a853bbda93966caa22 192.168.1.11 26379 @ mymaster 192.168.1.11 6379
7782:X 23 Jan 00:24:41.597 # +sdown sentinel 17c9ee07632d60c4c0aa75a853bbda93966caa22 192.168.1.11 26379 @ mymaster 192.168.1.11 6379

[root@station13 ~]# nohup redis-sentinel /etc/redis/sentinel.conf&

[2] 5763

[root@station13 ~]# tail -f /var/log/sentinel_slave2.log

5763:X 23 Jan 00:26:14.286 # Sentinel ID is a78518e4955d3602c61e212be0cbdc378daa3cdc
5763:X 23 Jan 00:26:14.286 # +monitor master mymaster 192.168.1.11 6379 quorum 1
5763:X 23 Jan 00:26:14.287 * +slave slave 192.168.1.12:6379 192.168.1.12 6379 @ mymaster 192.168.1.11 6379
5763:X 23 Jan 00:26:14.288 * +slave slave 192.168.1.13:6379 192.168.1.13 6379 @ mymaster 192.168.1.11 6379
5763:X 23 Jan 00:26:14.708 * +sentinel sentinel 7c000aa564ed603eeefd031333ebc2c916597ec6 192.168.1.12 26479 @ mymaster 192.168.1.11 6379
5763:X 23 Jan 00:26:15.779 * +sentinel sentinel 17c9ee07632d60c4c0aa75a853bbda93966caa22 192.168.1.11 26379 @ mymaster 192.168.1.11 6379
5763:X 23 Jan 00:26:19.716 # +sdown sentinel 7c000aa564ed603eeefd031333ebc2c916597ec6 192.168.1.12 26479 @ mymaster 192.168.1.11 6379
5763:X 23 Jan 00:26:20.797 # +sdown sentinel 17c9ee07632d60c4c0aa75a853bbda93966caa22 192.168.1.11 26379 @ mymaster 192.168.1.11 6379

3.5、客户端查看sentinel

[root@station11 ~]# redis-cli -p 26379

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/8bb540c36180276257f85fb9f486a5ab.html