Redis哨兵模式

redis哨兵

	Sentinel(哨兵)是用于监控Redis集群中Master状态的工具,是Redis高可用解决方案,哨兵可以监视一个或者多个redis master
服务,以及这些master服务的所有从服务。 某个master服务宕机后,会把这个master下的某个从服务升级为master来替代已宕机的
master继续工作。

如何启用哨兵?

	首先我们先重启之前搭建的三台redis实例,恢复6379为主,63806381为从节点的主从复制副本集。查看日志,确保正常。在编译
redis时,在/opt/liqiang/redis5/bin目录下,除了有 `redis-cli``redis-server`外,还有一个脚本`redis-sentinel`
	[root@localhost ~]# cd /opt/liqiang/redis5/bin
	[root@localhost bin]# ll
	总用量 32736
	-rwxr-xr-x. 1 root root 4366656 6  27 22:32 redis-benchmark
	-rwxr-xr-x. 1 root root 8111904 6  27 22:32 redis-check-aof
	-rwxr-xr-x. 1 root root 8111904 6  27 22:32 redis-check-rdb
	-rwxr-xr-x. 1 root root 4806888 6  27 22:32 redis-cli
	lrwxrwxrwx. 1 root root      12 6  27 22:32 redis-sentinel -> redis-server
	-rwxr-xr-x. 1 root root 8111904 6  27 22:32 redis-server
	[root@localhost bin]# ./redis-sentinel 
	2457:X 03 Jul 2022 15:19:53.803 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
	2457:X 03 Jul 2022 15:19:53.803 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=2457, just started
	2457:X 03 Jul 2022 15:19:53.803 # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-sentinel /path/to/sentinel.conf
	2457:X 03 Jul 2022 15:19:53.804 * Increased maximum number of open files to 10032 (it was originally set to 1024).
	                _._                                                  
	           _.-``__ ''-._                                             
	      _.-``    `.  `_.  ''-._           Redis 5.0.5 (00000000/0) 64 bit
	  .-`` .-```.  ```\/    _.,_ ''-._                                   
	 (    '      ,       .-`  | `,    )     Running in sentinel mode
	 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 26379
	 |    `-._   `._    /     _.-'    |     PID: 2457
	  `-._    `-._  `-./  _.-'    _.-'                                   
	 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
	 |    `-._`-._        _.-'_.-'    |           http://redis.io        
	  `-._    `-._`-.__.-'_.-'    _.-'                                   
	 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
	 |    `-._`-._        _.-'_.-'    |                                  
	  `-._    `-._`-.__.-'_.-'    _.-'                                   
	      `-._    `-.__.-'    _.-'                                       
	          `-._        _.-'                                           
	              `-.__.-'                                               
	
	2457:X 03 Jul 2022 15:19:53.806 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
	2457:X 03 Jul 2022 15:19:53.806 # Sentinel started without a config file. Exiting...

	通过日志可以看到,启动失败,失败原因时没有配置文件,发现默认的加载配置文件的路径是 /path/to/sentinel.conf。那么我们
就创建这个文件(这个文件的路径可以自定义), 每一个监控都有一个配置文件,我们就在redis的配置文件路径下创建哨兵的配置文件。

编辑配置文件,介绍几个配置选项,在下文会有更为详细的的解释

  • port < sentinel-port >:此sentinel的端口号,我们说过sentinel也是一个redis服务;
  • sentinel monitor < master-name > < ip > < redis-port > < quorum>:监视的主服务器的名称、ip、端口号和判定为客观下线所需要的投票数;
  • sentinel auth-pass < master-name > < password >:如果你的主服务器设置了密码,要在这里配置密码进行验证;
  • sentinel down-after-milliseconds < master-name > < milliseconds >:判定为主观下线所需的毫秒数;
  • sentinel failover-timeout < master-name > < milliseconds>:刷新故障迁移状态的最大时限;
  • sentinel parallel-syncs < master-name > < numslaves >:执行故障转移操作时,可同时对新的主服务器进行同步的从服务器数量
	[root@localhost bin]# cd /etc/redis/
	[root@localhost redis]# ll
	总用量 192
	-rw-r--r--. 1 root root 61873 6  27 22:56 6379.conf
	-rw-r--r--. 1 root root 61873 6  29 23:10 6380.conf
	-rw-r--r--. 1 root root 61873 6  27 22:50 6381.conf

	注意:
	1、先启动服务637963806381 服务器  
	263806381跟随6379   REPLICAOF 127.0.0.1 6379


	[root@localhost redis]# touch 26379-sentinel.conf
	[root@localhost redis]# vi 26379-sentinel.conf 
		文件中添加如下内容
		port 26379
		sentinel monitor mymaster 127.0.0.1 6379 2
	简单的配置文件,既然哨兵是一个特殊的redis-server,那么他也有端口号,因此要配置端口号,	

	sentinel monitor mymaster 127.0.0.1 6379 2 配置
	
	sentinel表示这是一个哨兵配置,
	monitor监控,表示他要监控的节点是什么,
	mymaster表示给这个节点起的一个名字,随便起,
	127.0.0.1 6379 表示监控的节点信息
	2表示权重,表示投票范围,也就是说,当主节点宕机时,有多少票说话才能管用。 
	配置完成后启动哨兵(启动哨兵之前先  主从节点启动,从节点跟随主节点)
	
	[root@localhost redis]# redis-sentinel ./26379-sentinel.conf 
	3225:X 03 Jul 2022 15:40:27.305 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
	3225:X 03 Jul 2022 15:40:27.305 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=3225, just started
	3225:X 03 Jul 2022 15:40:27.305 # Configuration loaded
	3225:X 03 Jul 2022 15:40:27.306 * Increased maximum number of open files to 10032 (it was originally set to 1024).
	                _._                                                  
	           _.-``__ ''-._                                             
	      _.-``    `.  `_.  ''-._           Redis 5.0.5 (00000000/0) 64 bit
	  .-`` .-```.  ```\/    _.,_ ''-._                                   
	 (    '      ,       .-`  | `,    )     Running in sentinel mode
	 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 26379
	 |    `-._   `._    /     _.-'    |     PID: 3225
	  `-._    `-._  `-./  _.-'    _.-'                                   
	 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
	 |    `-._`-._        _.-'_.-'    |           http://redis.io        
	  `-._    `-._`-.__.-'_.-'    _.-'                                   
	 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
	 |    `-._`-._        _.-'_.-'    |                                  
	  `-._    `-._`-.__.-'_.-'    _.-'                                   
	      `-._    `-.__.-'    _.-'                                       
	          `-._        _.-'                                           
	              `-.__.-'                                               
	
	3225:X 03 Jul 2022 15:40:27.307 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
	3225:X 03 Jul 2022 15:40:27.307 # Sentinel ID is d30e97c88cb2ab5b46bd05c9988b90e9326961a3
	3225:X 03 Jul 2022 15:40:27.307 # +monitor master mymaster 127.0.0.1 6379 quorum 2
	3225:X 03 Jul 2022 15:40:27.310 * +slave slave 127.0.0.1:6381 127.0.0.1 6381 @ mymaster 127.0.0.1 6379
	3225:X 03 Jul 2022 15:40:27.311 * +slave slave 127.0.0.1:6380 127.0.0.1 6380 @ mymaster 127.0.0.1 6379


	启动成功了,而且我们只监控了主节点,打印的日志中自动加入了从节点的信息,因为从节点连接主节点,主节点中包含了从节点的
信息。监控肯定也是集群,不能是一台,否则的话无法投票,使用 `ctrl+C`退出当前哨兵。因为现在是前台阻塞的。  删除原配置文件,
因为打开看一下可以发现启动后添加了很多其他配置,我们删掉重新配置。 搭建多个监控263792638026381 配置文件。

	先还原26379-sentinel.conf 配置文件
		port 26379
		sentinel monitor mymaster 127.0.0.1 6379 2
	
	[root@localhost redis]# cp 26379-sentinel.conf  26380-sentinel.conf
	[root@localhost redis]# cp 26379-sentinel.conf  26381-sentinel.conf
	[root@localhost redis]# ll
	总用量 204
	-rw-r--r--. 1 root root    54 7   3 15:49 26379-sentinel.conf
	-rw-r--r--. 1 root root    54 7   3 15:50 26380-sentinel.conf
	-rw-r--r--. 1 root root    54 7   3 15:50 26381-sentinel.conf
	-rw-r--r--. 1 root root 61873 6  27 22:56 6379.conf
	-rw-r--r--. 1 root root 61873 6  29 23:10 6380.conf
	-rw-r--r--. 1 root root 61873 6  27 22:50 6381.conf
	
		修改监控对应的端口
	[root@localhost redis]# vi 26380-sentinel.conf 
	[root@localhost redis]# vi 26381-sentinel.conf 

启动三个哨兵

	启动 哨兵
	[root@localhost redis]# redis-sentinel ./26379-sentinel.conf 
	[root@localhost redis]# redis-sentinel ./26380-sentinel.conf 
	[root@localhost redis]# redis-sentinel ./26381-sentinel.conf 

	查看26379控制台
	3426:X 03 Jul 2022 15:55:07.874 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
	3426:X 03 Jul 2022 15:55:07.875 # Sentinel ID is 0c1c3e0a6a14a7ff2aa0058d05f79dcd87ca40f5
	3426:X 03 Jul 2022 15:55:07.875 # +monitor master mymaster 127.0.0.1 6379 quorum 2
	3426:X 03 Jul 2022 15:55:07.876 * +slave slave 127.0.0.1:6381 127.0.0.1 6381 @ mymaster 127.0.0.1 6379
	3426:X 03 Jul 2022 15:55:07.876 * +slave slave 127.0.0.1:6380 127.0.0.1 6380 @ mymaster 127.0.0.1 6379
	3426:X 03 Jul 2022 15:55:40.646 * +sentinel sentinel 1556921f192f874d6171d304d5c02008744acbeb 127.0.0.1 26380 @ mymaster 127.0.0.1 6379
	3426:X 03 Jul 2022 15:56:02.403 * +sentinel sentinel 459ba4581b59423ea9c4077b2af47ecb4a304c67 127.0.0.1 26381 @ mymaster 127.0.0.1 6379

	查看26380控制台
	3433:X 03 Jul 2022 15:55:38.578 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
	3433:X 03 Jul 2022 15:55:38.579 # Sentinel ID is 1556921f192f874d6171d304d5c02008744acbeb
	3433:X 03 Jul 2022 15:55:38.579 # +monitor master mymaster 127.0.0.1 6379 quorum 2
	3433:X 03 Jul 2022 15:55:38.580 * +slave slave 127.0.0.1:6381 127.0.0.1 6381 @ mymaster 127.0.0.1 6379
	3433:X 03 Jul 2022 15:55:38.580 * +slave slave 127.0.0.1:6380 127.0.0.1 6380 @ mymaster 127.0.0.1 6379
	3433:X 03 Jul 2022 15:55:40.530 * +sentinel sentinel 0c1c3e0a6a14a7ff2aa0058d05f79dcd87ca40f5 127.0.0.1 26379 @ mymaster 127.0.0.1 6379
	3433:X 03 Jul 2022 15:56:02.404 * +sentinel sentinel 459ba4581b59423ea9c4077b2af47ecb4a304c67 127.0.0.1 26381 @ mymaster 127.0.0.1 6379


	查看26381控制台
	3448:X 03 Jul 2022 15:56:00.374 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
	3448:X 03 Jul 2022 15:56:00.375 # Sentinel ID is 459ba4581b59423ea9c4077b2af47ecb4a304c67
	3448:X 03 Jul 2022 15:56:00.375 # +monitor master mymaster 127.0.0.1 6379 quorum 2
	3448:X 03 Jul 2022 15:56:00.375 * +slave slave 127.0.0.1:6381 127.0.0.1 6381 @ mymaster 127.0.0.1 6379
	3448:X 03 Jul 2022 15:56:00.376 * +slave slave 127.0.0.1:6380 127.0.0.1 6380 @ mymaster 127.0.0.1 6379
	3448:X 03 Jul 2022 15:56:00.885 * +sentinel sentinel 0c1c3e0a6a14a7ff2aa0058d05f79dcd87ca40f5 127.0.0.1 26379 @ mymaster 127.0.0.1 6379
	3448:X 03 Jul 2022 15:56:01.085 * +sentinel sentinel 1556921f192f874d6171d304d5c02008744acbeb 127.0.0.1 26380 @ mymaster 127.0.0.1 6379

此时我们就启动了三个节点,监控6379这个主节点。那么此时如果6379节点挂机呢?  `ctrl+C`结束主节点


	[root@localhost redis]# ps -ef |grep redis
	root       2912   2806  0 15:36 pts/5    00:00:01 redis-server 127.0.0.1:6380
	root       2964   2767  0 15:37 pts/4    00:00:01 redis-server 127.0.0.1:6381
	root       3199   3156  0 15:39 pts/9    00:00:00 redis-cli -p 6379
	root       3200   3069  0 15:39 pts/8    00:00:00 redis-cli -p 6380
	root       3201   3115  0 15:39 pts/7    00:00:00 redis-cli -p 6381
	root       3426   1975  0 15:55 pts/0    00:00:00 redis-sentinel *:26379 [sentinel]
	root       3433   2119  0 15:55 pts/2    00:00:00 redis-sentinel *:26380 [sentinel]
	root       3448   2073  0 15:55 pts/1    00:00:00 redis-sentinel *:26381 [sentinel]
	root       3557   2982  0 16:03 pts/3    00:00:00 grep --color=auto redis

可以看到6379节点已经停了,先查看哨兵的投票信息

	3433:X 03 Jul 2022 16:03:12.779 # +sdown master mymaster 127.0.0.1 6379
	3433:X 03 Jul 2022 16:03:12.855 # +odown master mymaster 127.0.0.1 6379 #quorum 2/2
	3433:X 03 Jul 2022 16:03:12.855 # +new-epoch 1
	3433:X 03 Jul 2022 16:03:12.855 # +try-failover master mymaster 127.0.0.1 6379
	3433:X 03 Jul 2022 16:03:12.856 # +vote-for-leader 1556921f192f874d6171d304d5c02008744acbeb 1
	3433:X 03 Jul 2022 16:03:12.857 # 459ba4581b59423ea9c4077b2af47ecb4a304c67 voted for 1556921f192f874d6171d304d5c02008744acbeb 1
	3433:X 03 Jul 2022 16:03:12.858 # 0c1c3e0a6a14a7ff2aa0058d05f79dcd87ca40f5 voted for 1556921f192f874d6171d304d5c02008744acbeb 1
	3433:X 03 Jul 2022 16:03:12.933 # +elected-leader master mymaster 127.0.0.1 6379
	3433:X 03 Jul 2022 16:03:12.933 # +failover-state-select-slave master mymaster 127.0.0.1 6379
	3433:X 03 Jul 2022 16:03:12.994 # +selected-slave slave 127.0.0.1:6380 127.0.0.1 6380 @ mymaster 127.0.0.1 6379
	3433:X 03 Jul 2022 16:03:12.995 * +failover-state-send-slaveof-noone slave 127.0.0.1:6380 127.0.0.1 6380 @ mymaster 127.0.0.1 6379
	3433:X 03 Jul 2022 16:03:13.072 * +failover-state-wait-promotion slave 127.0.0.1:6380 127.0.0.1 6380 @ mymaster 127.0.0.1 6379
	3433:X 03 Jul 2022 16:03:13.135 # +promoted-slave slave 127.0.0.1:6380 127.0.0.1 6380 @ mymaster 127.0.0.1 6379
	3433:X 03 Jul 2022 16:03:13.135 # +failover-state-reconf-slaves master mymaster 127.0.0.1 6379
	3433:X 03 Jul 2022 16:03:13.196 * +slave-reconf-sent slave 127.0.0.1:6381 127.0.0.1 6381 @ mymaster 127.0.0.1 6379
	3433:X 03 Jul 2022 16:03:13.920 # -odown master mymaster 127.0.0.1 6379
	3433:X 03 Jul 2022 16:03:14.183 * +slave-reconf-inprog slave 127.0.0.1:6381 127.0.0.1 6381 @ mymaster 127.0.0.1 6379
	3433:X 03 Jul 2022 16:03:14.183 * +slave-reconf-done slave 127.0.0.1:6381 127.0.0.1 6381 @ mymaster 127.0.0.1 6379
	3433:X 03 Jul 2022 16:03:14.244 # +failover-end master mymaster 127.0.0.1 6379
	3433:X 03 Jul 2022 16:03:14.244 # +switch-master mymaster 127.0.0.1 6379 127.0.0.1 6380
	3433:X 03 Jul 2022 16:03:14.245 * +slave slave 127.0.0.1:6381 127.0.0.1 6381 @ mymaster 127.0.0.1 6380
	3433:X 03 Jul 2022 16:03:14.245 * +slave slave 127.0.0.1:6379 127.0.0.1 6379 @ mymaster 127.0.0.1 6380
	3433:X 03 Jul 2022 16:03:44.298 # +sdown slave 127.0.0.1:6379 127.0.0.1 6379 @ mymaster 127.0.0.1 6380
	3433:X 03 Jul 2022 16:06:46.097 # -sdown slave 127.0.0.1:6379 127.0.0.1 6379 @ mymaster 127.0.0.1 6380


	粗略的看一下,大概意思为:
	第1行表示,这个Sentinel判定主服务器主观下线;
	第2行表示,这个Sentinel判定主服务器客观下线(投票数2/2);
	第38行表示,选举领头Sentinel的过程,其他两个都给此sentinel投票了,此sentinel成为了领头sentinel;
	第913行表示,6380端口的从服务器被选中并提升为主服务器;
	第14行到18行表示,将6381原本的从服务器挂到6380下;
	第18行往后表示,将该sentinel监视的主服务器转为6380,重新获取从服务器信息
此时查看6380的日志:
	2912:S 03 Jul 2022 16:03:12.902 * Connecting to MASTER 127.0.0.1:6379
	2912:S 03 Jul 2022 16:03:12.902 * MASTER <-> REPLICA sync started
	2912:S 03 Jul 2022 16:03:12.902 # Error condition on socket for SYNC: Connection refused
	2912:M 03 Jul 2022 16:03:13.072 # Setting secondary replication ID to 8be5b830add214aa215693f2b9ffa81199fae02b, valid up to offset: 106039. New replication ID is 3d7e8be20e283e2f68c6263af46c643d55d2b40c
	2912:M 03 Jul 2022 16:03:13.072 * Discarding previously cached master state.
	2912:M 03 Jul 2022 16:03:13.072 * MASTER MODE enabled (user request from 'id=10 addr=127.0.0.1:54420 fd=11 name=sentinel-1556921f-cmd age=455 idle=0 flags=x db=0 sub=0 psub=0 multi=3 qbuf=140 qbuf-free=32628 obl=36 oll=0 omem=0 events=r cmd=exec')
	2912:M 03 Jul 2022 16:03:13.073 # CONFIG REWRITE executed with success.
	2912:M 03 Jul 2022 16:03:13.554 * Replica 127.0.0.1:6381 asks for synchronization
	2912:M 03 Jul 2022 16:03:13.554 * Partial resynchronization request from 127.0.0.1:6381 accepted. Sending 156 bytes of backlog starting from offset 106039.

	可以看到它会尝试连接6379,然后连几次后哨兵就讲6380给切换成了主节点,6381追随6380去了。

此时再重启6379节点:
	[root@localhost redis]# redis-server ./6379.conf 
	3583:C 03 Jul 2022 16:06:45.225 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
	3583:C 03 Jul 2022 16:06:45.225 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=3583, just started
	3583:C 03 Jul 2022 16:06:45.225 # Configuration loaded
	3583:M 03 Jul 2022 16:06:45.226 * Increased maximum number of open files to 10032 (it was originally set to 1024).
	                _._                                                  
	           _.-``__ ''-._                                             
	      _.-``    `.  `_.  ''-._           Redis 5.0.5 (00000000/0) 64 bit
	  .-`` .-```.  ```\/    _.,_ ''-._                                   
	 (    '      ,       .-`  | `,    )     Running in standalone mode
	 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
	 |    `-._   `._    /     _.-'    |     PID: 3583
	  `-._    `-._  `-./  _.-'    _.-'                                   
	 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
	 |    `-._`-._        _.-'_.-'    |           http://redis.io        
	  `-._    `-._`-.__.-'_.-'    _.-'                                   
	 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
	 |    `-._`-._        _.-'_.-'    |                                  
	  `-._    `-._`-.__.-'_.-'    _.-'                                   
	      `-._    `-.__.-'    _.-'                                       
	          `-._        _.-'                                           
	              `-.__.-'                                               
	
	3583:M 03 Jul 2022 16:06:45.227 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
	3583:M 03 Jul 2022 16:06:45.227 # Server initialized
	3583:M 03 Jul 2022 16:06:45.227 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
	3583:M 03 Jul 2022 16:06:45.227 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
	3583:M 03 Jul 2022 16:06:45.227 * DB loaded from disk: 0.000 seconds
	3583:M 03 Jul 2022 16:06:45.227 * Ready to accept connections
	3583:S 03 Jul 2022 16:06:55.510 * Before turning into a replica, using my master parameters to synthesize a cached master: I may be able to synchronize with the new master with just a partial transfer.
	3583:S 03 Jul 2022 16:06:55.510 * REPLICAOF 127.0.0.1:6380 enabled (user request from 'id=3 addr=127.0.0.1:40068 fd=7 name=sentinel-459ba458-cmd age=10 idle=0 flags=x db=0 sub=0 psub=0 multi=3 qbuf=148 qbuf-free=32620 obl=36 oll=0 omem=0 events=r cmd=exec')
	3583:S 03 Jul 2022 16:06:55.511 # CONFIG REWRITE executed with success.
	3583:S 03 Jul 2022 16:06:56.036 * Connecting to MASTER 127.0.0.1:6380
	3583:S 03 Jul 2022 16:06:56.036 * MASTER <-> REPLICA sync started
	3583:S 03 Jul 2022 16:06:56.036 * Non blocking connect for SYNC fired the event.
	3583:S 03 Jul 2022 16:06:56.036 * Master replied to PING, replication can continue...
	3583:S 03 Jul 2022 16:06:56.036 * Trying a partial resynchronization (request de2c5dd9190efa5442d3ec2552d9c67f315b3592:1).
	3583:S 03 Jul 2022 16:06:56.046 * Full resync from master: 3d7e8be20e283e2f68c6263af46c643d55d2b40c:150112
	3583:S 03 Jul 2022 16:06:56.046 * Discarding previously cached master state.
	3583:S 03 Jul 2022 16:06:56.145 * MASTER <-> REPLICA sync: receiving 248 bytes from master
	3583:S 03 Jul 2022 16:06:56.145 * MASTER <-> REPLICA sync: Flushing old data
	3583:S 03 Jul 2022 16:06:56.145 * MASTER <-> REPLICA sync: Loading DB in memory
	3583:S 03 Jul 2022 16:06:56.145 * MASTER <-> REPLICA sync: Finished with success


	可以看到重启后,6379也是追随63806380为主。
	至此使用哨兵机制完整的实现了。然后我们在回过头打开 26379-sentinel.conf  文件:

	[root@localhost redis]# vi 26379-sentinel.conf 

	port 26379
	sentinel myid 0c1c3e0a6a14a7ff2aa0058d05f79dcd87ca40f5
	# Generated by CONFIG REWRITE
	dir "/etc/redis"
	protected-mode no
	sentinel deny-scripts-reconfig yes
	sentinel monitor mymaster 127.0.0.1 6380 2
	sentinel config-epoch mymaster 1
	sentinel leader-epoch mymaster 1
	sentinel known-replica mymaster 127.0.0.1 6379
	sentinel known-replica mymaster 127.0.0.1 6381
	sentinel known-sentinel mymaster 127.0.0.1 26381 459ba4581b59423ea9c4077b2af47ecb4a304c67
	sentinel known-sentinel mymaster 127.0.0.1 26380 1556921f192f874d6171d304d5c02008744acbeb
	sentinel current-epoch 1

	注意,我们最开始配置,写的是监控主节点6379,但是经过一次重新选举后,6380成为了主节点master,因此哨兵自动修改了配置
文件中主节点的信息从6379变成了6380。至此完成了redis的哨兵配置。
redis的哨兵是如何发现其他哨兵的?
	在配置哨兵的时候,只配置了主节点的信息,但是当主节点宕机时需要其他的哨兵发起投票选出新的master,那么一个哨兵是如何
知道其他的哨兵的?redis自带的功能就是发布订阅,当一个主节点启动的时候,哨兵就会在主节点身上进行发布订阅。


使用 `redis-cli -p 6380`连接redis的master。
然后使用psubscribe查看哨兵之间通信的消息:
	127.0.0.1:6380> PSUBSCRIBE *
	Reading messages... (press Ctrl-C to quit)
	1) "psubscribe"
	2) "*"
	3) (integer) 1
	1) "pmessage"
	2) "*"
	3) "__sentinel__:hello"
	4) "127.0.0.1,26380,13d587768d58fa7d31a7be3bcec7634ef4e23cf8,1,mymaster,127.0.0.1,6380,1"
	1) "pmessage"
	2) "*"
	3) "__sentinel__:hello"
	4) "127.0.0.1,26381,342c6cdfe07f666fa860c59e56ba14f1fba07348,1,mymaster,127.0.0.1,6380,1"
	1) "pmessage"
	2) "*"
	3) "__sentinel__:hello"
	4) "127.0.0.1,26379,da43e59879c95b6a5c2912856d67e9dafb25f2cb,1,mymaster,127.0.0.1,6380,1"
	1) "pmessage"
	2) "*"
	3) "__sentinel__:hello"
	4) "127.0.0.1,26380,13d587768d58fa7d31a7be3bcec7634ef4e23cf8,1,mymaster,127.0.0.1,6380,1"

	可以看到有一个 `__sentinel__:hello`的通道,然后263792638026381都在这里面说话,所以只要有一个哨兵连接上主节点,
那么其他的节点就能发现。哨兵的配置文件在redis的源码中有sentinel.conf。当哨兵通过master节点发现其他节点后,会在本地配置文
件记录其他哨兵节点,然后哨兵之间除了通过master通信,也会有自己的发布订阅。

	127.0.0.1:26379> PSUBSCRIBE *
	Reading messages... (press Ctrl-C to quit)
	1) "psubscribe"
	2) "*"
	3) (integer) 1
	1) "pmessage"
	2) "*"
	3) "+sentinel-address-switch"
	4) "master mymaster 127.0.0.1 6380 ip 192.168.226.138 port 26381 for 342c6cdfe07f666fa860c59e56ba14f1fba07348"
	1) "pmessage"
	2) "*"


	哨兵之间通信的通道为 `+sentinel-address-switch`,查看此通道的信息:

	127.0.0.1:26379> SUBSCRIBE +sentinel-address-switch
	Reading messages... (press Ctrl-C to quit)
	1) "subscribe"
	2) "+sentinel-address-switch"
	3) (integer) 1
	1) "message"
	2) "+sentinel-address-switch"
	3) "master mymaster 127.0.0.1 6380 ip 192.168.226.138 port 26380 for 13d587768d58fa7d31a7be3bcec7634ef4e23cf8"
	1) "message"
	2) "+sentinel-address-switch"
	3) "master mymaster 127.0.0.1 6380 ip 127.0.0.1 port 26380 for 13d587768d58fa7d31a7be3bcec7634ef4e23cf8"
	1) "message"
	2) "+sentinel-address-switch"
	3) "master mymaster 127.0.0.1 6380 ip 192.168.226.138 port 26380 for 13d587768d58fa7d31a7be3bcec7634ef4e23cf8"
	1) "message"
	2) "+sentinel-address-switch"
	3) "master mymaster 127.0.0.1 6380 ip 127.0.0.1 port 26380 for 13d587768d58fa7d31a7be3bcec7634ef4e23cf8"
	1) "message"
	2) "+sentinel-address-switch"
	3) "master mymaster 127.0.0.1 6380 ip 192.168.226.138 port 26381 for 342c6cdfe07f666fa860c59e56ba14f1fba07348"
	1) "message"
	2) "+sentinel-address-switch"
	3) "master mymaster 127.0.0.1 6380 ip 127.0.0.1 port 26381 for 342c6cdfe07f666fa860c59e56ba14f1fba07348"
	1) "message"

可以看到这个channel中订阅另外连个节点发布的信息。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值