千家信息网

基于Sentinel的Redis高可用方案

发表于:2024-11-29 作者:千家信息网编辑
千家信息网最后更新 2024年11月29日,数据存储我们在应用设计过程中非常重要的一部分,无论是关系型数据库,还是Redis、MongoDB等非关系型数据库,都有很多的高可用方案,还有一些针对不同业务设计的中间件,使其性能更有特色,更能保证数据
千家信息网最后更新 2024年11月29日基于Sentinel的Redis高可用方案

数据存储我们在应用设计过程中非常重要的一部分,无论是关系型数据库,还是Redis、MongoDB等非关系型数据库,都有很多的高可用方案,还有一些针对不同业务设计的中间件,使其性能更有特色,更能保证数据存储的稳定和安全。

目前主流的Redis的数据存储架构有Redis单点,Redis主从,基于Sentinel的Redis主备、基于keepalive的redis主备,以及Redis集群Cluster,还有豌豆荚开源的Codis等是目前业内比较流行的解决方案,不同的存储架构,是若干个技术工程师,根据自己公司的业务,踩过N次坑,总结出的结果,没有最好的解决方案,只有最适合的解决方案。

在上家公司出,采用了redis的主从架构,版本是2.8,上线一年多,尚未出现过问题,但是潜在的隐患是大家都知道的,主从架构中,当主宕机或者意外情况时,主与从不能平滑切换,将会影响业务,所以这里就是一个×××,而Sentinel(哨兵)方案是主从架构的升级版,能够让主从之间自动切换。提高系统稳定性和高可用性。以下是最近自己测试哨兵架构的相关记录。

Sentinel官方文档:https://redis.io/topics/sentinel

操作系统:CentOS 6.7

redis版本:3.2.9

主机规划:

序号Host
Redis RoleRedis PortSentinel Port
110.1.4.82slave637926379 26380
210.1.4.99master637926379 26380

整体采用,主从结构,每台主机再部署2个哨兵,整体是2个Redis 4个Sentinel集群的架构。

步骤:

1、配置Redis主从

由于配置redis主从比较简单,所以在这里不会去详细的介绍,想了解如何配置的朋友,请参考以下文章。

http://dreamlinux.blog.51cto.com/9079323/1832029

http://dreamlinux.blog.51cto.com/9079323/1832069

2、检验redis主从是否正常

登录redis的Master主节点检查redis主从

127.0.0.1:6379> info 127.0.0.1:6379> info # Serverredis_version:3.2.9redis_git_sha1:00000000redis_git_dirty:0redis_build_id:66cf707c3c51d9fredis_mode:standaloneos:Linux 2.6.32-696.6.3.el6.x86_64 x86_64arch_bits:64multiplexing_api:epollgcc_version:4.4.7process_id:26318run_id:7954f18bcb4084964efb58460027399d583fba57tcp_port:6379uptime_in_seconds:14402uptime_in_days:0hz:10lru_clock:9455690executable:/usr/local/bin/redis-serverconfig_file:/etc/redis/6379.conf# Clientsconnected_clients:7client_longest_output_list:0client_biggest_input_buf:0blocked_clients:0# Memoryused_memory:2017176used_memory_human:1.92Mused_memory_rss:9969664used_memory_rss_human:9.51Mused_memory_peak:2098064used_memory_peak_human:2.00Mtotal_system_memory:2103533568total_system_memory_human:1.96Gused_memory_lua:37888used_memory_lua_human:37.00Kmaxmemory:0maxmemory_human:0Bmaxmemory_policy:noevictionmem_fragmentation_ratio:4.94mem_allocator:jemalloc-4.0.3# Persistenceloading:0rdb_changes_since_last_save:0rdb_bgsave_in_progress:0rdb_last_save_time:1502627793rdb_last_bgsave_status:okrdb_last_bgsave_time_sec:0rdb_current_bgsave_time_sec:-1aof_enabled:0aof_rewrite_in_progress:0aof_rewrite_scheduled:0aof_last_rewrite_time_sec:-1aof_current_rewrite_time_sec:-1aof_last_bgrewrite_status:okaof_last_write_status:ok# Statstotal_connections_received:20total_commands_processed:89212instantaneous_ops_per_sec:6total_net_input_bytes:6172607total_net_output_bytes:33781397instantaneous_input_kbps:0.26instantaneous_output_kbps:0.67rejected_connections:0sync_full:1sync_partial_ok:0sync_partial_err:0expired_keys:0evicted_keys:0keyspace_hits:0keyspace_misses:0pubsub_channels:1pubsub_patterns:0latest_fork_usec:643migrate_cached_sockets:0# Replicationrole:master  #当前host的角色connected_slaves:1slave0:ip=10.1.4.82,port=6379,state=online,offset=23160,lag=1  #看到从节点的一些信息master_repl_offset:23160repl_backlog_active:1repl_backlog_size:1048576repl_backlog_first_byte_offset:2repl_backlog_histlen:23159# CPUused_cpu_sys:10.40used_cpu_user:6.24used_cpu_sys_children:0.00used_cpu_user_children:0.00# Clustercluster_enabled:0# Keyspacedb0:keys=5,expires=0,avg_ttl=0

设置相关的key检查

master

127.0.0.1:6379> dbsize (integer) 5127.0.0.1:6379> keys * 1) "winnerlook"2) "redis"3) "bund"4) "baidu"5) "bund33"127.0.0.1:6379>

slave

127.0.0.1:6379> dbsize (integer) 5127.0.0.1:6379> keys * 1) "winnerlook"2) "redis"3) "bund"4) "baidu"5) "bund33"127.0.0.1:6379>

由此可见配置的redis主从是可以正常工作的,接下来就是准备配置Sentinel,并启动相关的服务。

10.1.4.99 sentinel 1配置文件

pdaemonize yes  #后台运行protected-mode no #保护模式关闭# Generated by CONFIG REWRITEdir "/etc/redis"port 26379  #哨兵端口sentinel monitor master1 10.1.4.99 6379 2 # 监听IP  端口  默认最少投票数量sentinel down-after-milliseconds master1 5000 sentinel failover-timeout master1 5000  #选举超时时间logfile "/data/redis/sentinel/sentinel_26379.log" #日志文件sentinel auth-pass master1 Redis2017  #redis密码

10.1.4.99 sentinel 2配置文件

pdaemonize yes  #后台运行protected-mode no #保护模式关闭# Generated by CONFIG REWRITEdir "/etc/redis"port 26380 #哨兵端口sentinel monitor master1 10.1.4.99 6379 2 # 监听IP  端口  默认最少投票数量sentinel down-after-milliseconds master1 5000 sentinel failover-timeout master1 5000  #选举超时时间logfile "/data/redis/sentinel/sentinel_26379.log" #日志文件sentinel auth-pass master1 Redis2017  #redis密码

10.1.4.82 sentinel 1配置

pdaemonize yes  #后台运行protected-mode no #保护模式关闭# Generated by CONFIG REWRITEdir "/etc/redis"port 26379  #哨兵端口sentinel monitor master1 10.1.4.99 6379 2 # 监听IP  端口  默认最少投票数量sentinel down-after-milliseconds master1 5000 sentinel failover-timeout master1 5000  #选举超时时间logfile "/data/redis/sentinel/sentinel_26379.log" #日志文件sentinel auth-pass master1 Redis2017  #redis密码

10.1.4.82 sentinel 2配置

pdaemonize yes  #后台运行protected-mode no #保护模式关闭# Generated by CONFIG REWRITEdir "/etc/redis"port 26380 #哨兵端口sentinel monitor master1 10.1.4.99 6379 2 # 监听IP  端口  默认最少投票数量sentinel down-after-milliseconds master1 5000 sentinel failover-timeout master1 5000  #选举超时时间logfile "/data/redis/sentinel/sentinel_26379.log" #日志文件sentinel auth-pass master1 Redis2017  #redis密码

启动哨兵服务的两种方式

1、redis-sentinel /path/to/sentinel.conf
2、redis-server /path/to/sentinel.conf --sentinel

启动10.1.4.99的哨兵服务

redis-sentinel   /etc/redis/sentinel_26380.conf redis-sentinel   /etc/redis/sentinel_26379.conf

检查26379端口相关日志

[root@bundcredit sentinel]# tail  -f  sentinel_26379.log   `-._    `-._`-.__.-'_.-'    _.-'                                         `-._    `-.__.-'    _.-'                                                 `-._        _.-'                                                         `-.__.-'                                               27098:X 13 Aug 21:51:25.242 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.27098:X 13 Aug 21:51:25.242 # Sentinel ID is 361c7d3014a041a1a74163355882d88f5fab758127098:X 13 Aug 21:51:25.242 # +monitor master master1 10.1.4.99 6379 quorum 227098:X 13 Aug 21:51:30.271 # +sdown sentinel 76d5a26748410e72ca703e255f44a5ed2b3f83e4 10.1.4.99 26380 @ master1 10.1.4.99 637927098:X 13 Aug 21:51:30.410 # -sdown sentinel 76d5a26748410e72ca703e255f44a5ed2b3f83e4 10.1.4.99 26380 @ master1 10.1.4.99 637927098:X 13 Aug 21:52:30.617 # +sdown sentinel 5c3033c0ea6d1e0d861e9d9e99e94edb49c5daac 10.1.4.82 26379 @ master1 10.1.4.99 637927098:X 13 Aug 21:53:23.855 # -sdown sentinel 5c3033c0ea6d1e0d861e9d9e99e94edb49c5daac 10.1.4.82 26379 @ master1 10.1.4.99 637927098:X 13 Aug 21:53:34.463 # +sdown master master1 10.1.4.99 637927098:X 13 Aug 21:53:34.532 # +odown master master1 10.1.4.99 6379 #quorum 2/227098:X 13 Aug 21:53:34.532 # +new-epoch 127098:X 13 Aug 21:53:34.532 # +try-failover master master1 10.1.4.99 637927098:X 13 Aug 21:53:34.541 # +vote-for-leader 361c7d3014a041a1a74163355882d88f5fab7581 127098:X 13 Aug 21:53:34.542 # b73258f1019f65c5fee3b88b9c2eae7f85d985f3 voted for b73258f1019f65c5fee3b88b9c2eae7f85d985f3 127098:X 13 Aug 21:53:34.559 # 5c3033c0ea6d1e0d861e9d9e99e94edb49c5daac voted for b73258f1019f65c5fee3b88b9c2eae7f85d985f3 127098:X 13 Aug 21:53:34.560 # 76d5a26748410e72ca703e255f44a5ed2b3f83e4 voted for b73258f1019f65c5fee3b88b9c2eae7f85d985f3 127098:X 13 Aug 21:53:35.156 # +config-update-from sentinel b73258f1019f65c5fee3b88b9c2eae7f85d985f3 10.1.4.82 26380 @ master1 10.1.4.99 637927098:X 13 Aug 21:53:35.156 # +switch-master master1 10.1.4.99 6379 10.1.4.82 637927098:X 13 Aug 21:53:35.156 * +slave slave 10.1.4.99:6379 10.1.4.99 6379 @ master1 10.1.4.82 637927098:X 13 Aug 21:53:40.173 # +sdown slave 10.1.4.99:6379 10.1.4.99 6379 @ master1 10.1.4.82 637927098:X 13 Aug 21:54:07.583 # -sdown slave 10.1.4.99:6379 10.1.4.99 6379 @ master1 10.1.4.82 6379

检查26380端口相关日志

[root@bundcredit sentinel]# tail  -f  sentinel_26379.log   `-._    `-._`-.__.-'_.-'    _.-'                                         `-._    `-.__.-'    _.-'                                                 `-._        _.-'                                                         `-.__.-'                                               27098:X 13 Aug 21:51:25.242 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.27098:X 13 Aug 21:51:25.242 # Sentinel ID is 361c7d3014a041a1a74163355882d88f5fab758127098:X 13 Aug 21:51:25.242 # +monitor master master1 10.1.4.99 6379 quorum 227098:X 13 Aug 21:51:30.271 # +sdown sentinel 76d5a26748410e72ca703e255f44a5ed2b3f83e4 10.1.4.99 26380 @ master1 10.1.4.99 637927098:X 13 Aug 21:51:30.410 # -sdown sentinel 76d5a26748410e72ca703e255f44a5ed2b3f83e4 10.1.4.99 26380 @ master1 10.1.4.99 637927098:X 13 Aug 21:52:30.617 # +sdown sentinel 5c3033c0ea6d1e0d861e9d9e99e94edb49c5daac 10.1.4.82 26379 @ master1 10.1.4.99 637927098:X 13 Aug 21:53:23.855 # -sdown sentinel 5c3033c0ea6d1e0d861e9d9e99e94edb49c5daac 10.1.4.82 26379 @ master1 10.1.4.99 637927098:X 13 Aug 21:53:34.463 # +sdown master master1 10.1.4.99 637927098:X 13 Aug 21:53:34.532 # +odown master master1 10.1.4.99 6379 #quorum 2/227098:X 13 Aug 21:53:34.532 # +new-epoch 127098:X 13 Aug 21:53:34.532 # +try-failover master master1 10.1.4.99 637927098:X 13 Aug 21:53:34.541 # +vote-for-leader 361c7d3014a041a1a74163355882d88f5fab7581 127098:X 13 Aug 21:53:34.542 # b73258f1019f65c5fee3b88b9c2eae7f85d985f3 voted for b73258f1019f65c5fee3b88b9c2eae7f85d985f3 127098:X 13 Aug 21:53:34.559 # 5c3033c0ea6d1e0d861e9d9e99e94edb49c5daac voted for b73258f1019f65c5fee3b88b9c2eae7f85d985f3 127098:X 13 Aug 21:53:34.560 # 76d5a26748410e72ca703e255f44a5ed2b3f83e4 voted for b73258f1019f65c5fee3b88b9c2eae7f85d985f3 127098:X 13 Aug 21:53:35.156 # +config-update-from sentinel b73258f1019f65c5fee3b88b9c2eae7f85d985f3 10.1.4.82 26380 @ master1 10.1.4.99 637927098:X 13 Aug 21:53:35.156 # +switch-master master1 10.1.4.99 6379 10.1.4.82 637927098:X 13 Aug 21:53:35.156 * +slave slave 10.1.4.99:6379 10.1.4.99 6379 @ master1 10.1.4.82 637927098:X 13 Aug 21:53:40.173 # +sdown slave 10.1.4.99:6379 10.1.4.99 6379 @ master1 10.1.4.82 637927098:X 13 Aug 21:54:07.583 # -sdown slave 10.1.4.99:6379 10.1.4.99 6379 @ master1 10.1.4.82 6379^C[root@bundcredit sentinel]# tail  -f  sentinel_26369.log tail: cannot open `sentinel_26369.log' for reading: No such file or directorytail: no files remaining[root@bundcredit sentinel]# tail  -f  sentinel_26380.log27102:X 13 Aug 21:53:34.496 # +sdown master master1 10.1.4.99 637927102:X 13 Aug 21:53:34.558 # +new-epoch 127102:X 13 Aug 21:53:34.560 # +vote-for-leader b73258f1019f65c5fee3b88b9c2eae7f85d985f3 127102:X 13 Aug 21:53:34.580 # +odown master master1 10.1.4.99 6379 #quorum 4/227102:X 13 Aug 21:53:34.580 # Next failover delay: I will not start a failover before Sun Aug 13 21:53:45 201727102:X 13 Aug 21:53:35.156 # +config-update-from sentinel b73258f1019f65c5fee3b88b9c2eae7f85d985f3 10.1.4.82 26380 @ master1 10.1.4.99 637927102:X 13 Aug 21:53:35.156 # +switch-master master1 10.1.4.99 6379 10.1.4.82 637927102:X 13 Aug 21:53:35.156 * +slave slave 10.1.4.99:6379 10.1.4.99 6379 @ master1 10.1.4.82 637927102:X 13 Aug 21:53:40.201 # +sdown slave 10.1.4.99:6379 10.1.4.99 6379 @ master1 10.1.4.82 637927102:X 13 Aug 21:54:07.583 # -sdown slave 10.1.4.99:6379 10.1.4.99 6379 @ master1 10.1.4.82 6379

启动10.1.4.82服务器相关服务

redis-sentinel   /etc/redis/sentinel_26380.conf redis-sentinel   /etc/redis/sentinel_26379.conf

检查26379端口相关日志

15947:X 13 Aug 21:45:33.882 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.15947:X 13 Aug 21:45:33.893 # Sentinel ID is 5c3033c0ea6d1e0d861e9d9e99e94edb49c5daac15947:X 13 Aug 21:45:33.893 # +monitor master master1 10.1.4.99 6379 quorum 215947:X 13 Aug 21:45:33.894 * +slave slave 10.1.4.82:6379 10.1.4.82 6379 @ master1 10.1.4.99 637915951:X 13 Aug 21:45:39.170 * Increased maximum number of open files to 10032 (it was originally set to 1024).                _._                                                             _.-``__ ''-._                                                   _.-``    `.  `_.  ''-._           Redis 3.2.9 (00000000/0) 64 bit  .-`` .-```.  ```\/    _.,_ ''-._                                    (    '      ,       .-`  | `,    )     Running in sentinel mode |`-._`-...-` __...-.``-._|'` _.-'|     Port: 26380 |    `-._   `._    /     _.-'    |     PID: 15951  `-._    `-._  `-./  _.-'    _.-'                                    |`-._`-._    `-.__.-'    _.-'_.-'|                                   |    `-._`-._        _.-'_.-'    |           http://redis.io          `-._    `-._`-.__.-'_.-'    _.-'                                    |`-._`-._    `-.__.-'    _.-'_.-'|                                   |    `-._`-._        _.-'_.-'    |                                    `-._    `-._`-.__.-'_.-'    _.-'                                         `-._    `-.__.-'    _.-'                                                 `-._        _.-'                                                         `-.__.-'                                               15951:X 13 Aug 21:45:39.171 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.15951:X 13 Aug 21:45:39.174 # Sentinel ID is b73258f1019f65c5fee3b88b9c2eae7f85d985f315951:X 13 Aug 21:45:39.174 # +monitor master master1 10.1.4.99 6379 quorum 215951:X 13 Aug 21:45:39.175 * +slave slave 10.1.4.82:6379 10.1.4.82 6379 @ master1 10.1.4.99 637915951:X 13 Aug 21:45:40.025 * +sentinel sentinel 5c3033c0ea6d1e0d861e9d9e99e94edb49c5daac 10.1.4.82 26379 @ master1 10.1.4.99 637915947:X 13 Aug 21:45:41.196 * +sentinel sentinel b73258f1019f65c5fee3b88b9c2eae7f85d985f3 10.1.4.82 26380 @ master1 10.1.4.99 637915951:X 13 Aug 21:45:57.729 * +sentinel sentinel 361c7d3014a041a1a74163355882d88f5fab7581 10.1.4.99 26379 @ master1 10.1.4.99 637915947:X 13 Aug 21:45:57.729 * +sentinel sentinel 361c7d3014a041a1a74163355882d88f5fab7581 10.1.4.99 26379 @ master1 10.1.4.99 637915947:X 13 Aug 21:46:05.114 * +sentinel sentinel 76d5a26748410e72ca703e255f44a5ed2b3f83e4 10.1.4.99 26380 @ master1 10.1.4.99 637915951:X 13 Aug 21:46:05.115 * +sentinel sentinel 76d5a26748410e72ca703e255f44a5ed2b3f83e4 10.1.4.99 26380 @ master1 10.1.4.99 637915951:X 13 Aug 21:47:16.648 # +sdown master master1 10.1.4.99 637915951:X 13 Aug 21:47:20.907 # +sdown sentinel 361c7d3014a041a1a74163355882d88f5fab7581 10.1.4.99 26379 @ master1 10.1.4.99 637915951:X 13 Aug 21:47:20.908 # +sdown sentinel 76d5a26748410e72ca703e255f44a5ed2b3f83e4 10.1.4.99 26380 @ master1 10.1.4.99 637915947:X 13 Aug 21:47:20.908 # +sdown sentinel 361c7d3014a041a1a74163355882d88f5fab7581 10.1.4.99 26379 @ master1 10.1.4.99 637915951:X 13 Aug 21:47:20.979 # +tilt #tilt mode entered15947:X 13 Aug 21:47:20.979 # +tilt #tilt mode entered15947:X 13 Aug 21:47:50.982 # -tilt #tilt mode exited15947:X 13 Aug 21:47:50.983 # -sdown sentinel 361c7d3014a041a1a74163355882d88f5fab7581 10.1.4.99 26379 @ master1 10.1.4.99 637915951:X 13 Aug 21:47:51.030 # -tilt #tilt mode exited15951:X 13 Aug 21:47:51.030 # -sdown master master1 10.1.4.99 637915951:X 13 Aug 21:47:51.030 # -sdown sentinel 361c7d3014a041a1a74163355882d88f5fab7581 10.1.4.99 26379 @ master1 10.1.4.99 637915951:X 13 Aug 21:47:51.030 # -sdown sentinel 76d5a26748410e72ca703e255f44a5ed2b3f83e4 10.1.4.99 26380 @ master1 10.1.4.99 637915947:X 13 Aug 21:48:42.378 # +sdown sentinel b73258f1019f65c5fee3b88b9c2eae7f85d985f3 10.1.4.82 26380 @ master1 10.1.4.99 637915947:X 13 Aug 21:49:06.458 # -sdown sentinel b73258f1019f65c5fee3b88b9c2eae7f85d985f3 10.1.4.82 26380 @ master1 10.1.4.99 637915947:X 13 Aug 21:49:42.140 # +sdown sentinel 361c7d3014a041a1a74163355882d88f5fab7581 10.1.4.99 26379 @ master1 10.1.4.99 637915947:X 13 Aug 21:50:59.960 # +sdown sentinel 76d5a26748410e72ca703e255f44a5ed2b3f83e4 10.1.4.99 26380 @ master1 10.1.4.99 637915947:X 13 Aug 21:51:20.923 # -sdown sentinel 361c7d3014a041a1a74163355882d88f5fab7581 10.1.4.99 26379 @ master1 10.1.4.99 637915947:X 13 Aug 21:51:26.062 # -sdown sentinel 76d5a26748410e72ca703e255f44a5ed2b3f83e4 10.1.4.99 26380 @ master1 10.1.4.99 637915947:X 13 Aug 21:52:20.592 # User requested shutdown...15947:X 13 Aug 21:52:20.592 * Removing the pid file.15947:X 13 Aug 21:52:20.592 # Sentinel is now ready to exit, bye bye...17056:X 13 Aug 21:53:18.720 * Increased maximum number of open files to 10032 (it was originally set to 1024).                _._                                                             _.-``__ ''-._                                                   _.-``    `.  `_.  ''-._           Redis 3.2.9 (00000000/0) 64 bit  .-`` .-```.  ```\/    _.,_ ''-._                                    (    '      ,       .-`  | `,    )     Running in sentinel mode |`-._`-...-` __...-.``-._|'` _.-'|     Port: 26379 |    `-._   `._    /     _.-'    |     PID: 17056  `-._    `-._  `-./  _.-'    _.-'                                    |`-._`-._    `-.__.-'    _.-'_.-'|                                   |    `-._`-._        _.-'_.-'    |           http://redis.io          `-._    `-._`-.__.-'_.-'    _.-'                                    |`-._`-._    `-.__.-'    _.-'_.-'|                                   |    `-._`-._        _.-'_.-'    |                                    `-._    `-._`-.__.-'_.-'    _.-'                                         `-._    `-.__.-'    _.-'                                                 `-._        _.-'                                                         `-.__.-'                                               17056:X 13 Aug 21:53:18.721 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.17056:X 13 Aug 21:53:18.721 # Sentinel ID is 5c3033c0ea6d1e0d861e9d9e99e94edb49c5daac17056:X 13 Aug 21:53:18.721 # +monitor master master1 10.1.4.99 6379 quorum 217056:X 13 Aug 21:53:29.549 # +sdown master master1 10.1.4.99 637917056:X 13 Aug 21:53:29.643 # +new-epoch 117056:X 13 Aug 21:53:29.645 # +vote-for-leader b73258f1019f65c5fee3b88b9c2eae7f85d985f3 117056:X 13 Aug 21:53:29.645 # +odown master master1 10.1.4.99 6379 #quorum 2/217056:X 13 Aug 21:53:29.645 # Next failover delay: I will not start a failover before Sun Aug 13 21:53:39 201717056:X 13 Aug 21:53:30.241 # +config-update-from sentinel b73258f1019f65c5fee3b88b9c2eae7f85d985f3 10.1.4.82 26380 @ master1 10.1.4.99 637917056:X 13 Aug 21:53:30.241 # +switch-master master1 10.1.4.99 6379 10.1.4.82 637917056:X 13 Aug 21:53:30.241 * +slave slave 10.1.4.99:6379 10.1.4.99 6379 @ master1 10.1.4.82 637917056:X 13 Aug 21:53:35.285 # +sdown slave 10.1.4.99:6379 10.1.4.99 6379 @ master1 10.1.4.82 637917056:X 13 Aug 21:54:02.488 # -sdown slave 10.1.4.99:6379 10.1.4.99 6379 @ master1 10.1.4.82 637917056:X 13 Aug 21:54:12.441 * +convert-to-slave slave 10.1.4.99:6379 10.1.4.99 6379 @ master1 10.1.4.82 6379

检查26380端口相关日志

[root@localhost redis]# tail  -200f   /data/redis/sentinel/sentinel_26380.log 16390:X 13 Aug 21:49:05.965 * Increased maximum number of open files to 10032 (it was originally set to 1024).                _._                                                             _.-``__ ''-._                                                   _.-``    `.  `_.  ''-._           Redis 3.2.9 (00000000/0) 64 bit  .-`` .-```.  ```\/    _.,_ ''-._                                    (    '      ,       .-`  | `,    )     Running in sentinel mode |`-._`-...-` __...-.``-._|'` _.-'|     Port: 26380 |    `-._   `._    /     _.-'    |     PID: 16390  `-._    `-._  `-./  _.-'    _.-'                                    |`-._`-._    `-.__.-'    _.-'_.-'|                                   |    `-._`-._        _.-'_.-'    |           http://redis.io          `-._    `-._`-.__.-'_.-'    _.-'                                    |`-._`-._    `-.__.-'    _.-'_.-'|                                   |    `-._`-._        _.-'_.-'    |                                    `-._    `-._`-.__.-'_.-'    _.-'                                         `-._    `-.__.-'    _.-'                                                 `-._        _.-'                                                         `-.__.-'                                               16390:X 13 Aug 21:49:05.966 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.16390:X 13 Aug 21:49:05.966 # Sentinel ID is b73258f1019f65c5fee3b88b9c2eae7f85d985f316390:X 13 Aug 21:49:05.966 # +monitor master master1 10.1.4.99 6379 quorum 216390:X 13 Aug 21:49:42.173 # +sdown sentinel 361c7d3014a041a1a74163355882d88f5fab7581 10.1.4.99 26379 @ master1 10.1.4.99 637916390:X 13 Aug 21:50:59.918 # +sdown sentinel 76d5a26748410e72ca703e255f44a5ed2b3f83e4 10.1.4.99 26380 @ master1 10.1.4.99 637916390:X 13 Aug 21:51:21.049 # -sdown sentinel 361c7d3014a041a1a74163355882d88f5fab7581 10.1.4.99 26379 @ master1 10.1.4.99 637916390:X 13 Aug 21:51:25.188 # -sdown sentinel 76d5a26748410e72ca703e255f44a5ed2b3f83e4 10.1.4.99 26380 @ master1 10.1.4.99 637916390:X 13 Aug 21:52:25.659 # +sdown sentinel 5c3033c0ea6d1e0d861e9d9e99e94edb49c5daac 10.1.4.82 26379 @ master1 10.1.4.99 637916390:X 13 Aug 21:53:19.651 # -sdown sentinel 5c3033c0ea6d1e0d861e9d9e99e94edb49c5daac 10.1.4.82 26379 @ master1 10.1.4.99 637916390:X 13 Aug 21:53:29.563 # +sdown master master1 10.1.4.99 637916390:X 13 Aug 21:53:29.615 # +odown master master1 10.1.4.99 6379 #quorum 3/216390:X 13 Aug 21:53:29.615 # +new-epoch 116390:X 13 Aug 21:53:29.615 # +try-failover master master1 10.1.4.99 637916390:X 13 Aug 21:53:29.626 # +vote-for-leader b73258f1019f65c5fee3b88b9c2eae7f85d985f3 116390:X 13 Aug 21:53:29.627 # 361c7d3014a041a1a74163355882d88f5fab7581 voted for 361c7d3014a041a1a74163355882d88f5fab7581 116390:X 13 Aug 21:53:29.645 # 5c3033c0ea6d1e0d861e9d9e99e94edb49c5daac voted for b73258f1019f65c5fee3b88b9c2eae7f85d985f3 116390:X 13 Aug 21:53:29.646 # 76d5a26748410e72ca703e255f44a5ed2b3f83e4 voted for b73258f1019f65c5fee3b88b9c2eae7f85d985f3 116390:X 13 Aug 21:53:29.698 # +elected-leader master master1 10.1.4.99 637916390:X 13 Aug 21:53:29.698 # +failover-state-select-slave master master1 10.1.4.99 637916390:X 13 Aug 21:53:29.764 # +selected-slave slave 10.1.4.82:6379 10.1.4.82 6379 @ master1 10.1.4.99 637916390:X 13 Aug 21:53:29.764 * +failover-state-send-slaveof-noone slave 10.1.4.82:6379 10.1.4.82 6379 @ master1 10.1.4.99 637916390:X 13 Aug 21:53:29.826 * +failover-state-wait-promotion slave 10.1.4.82:6379 10.1.4.82 6379 @ master1 10.1.4.99 637916390:X 13 Aug 21:53:30.179 # +promoted-slave slave 10.1.4.82:6379 10.1.4.82 6379 @ master1 10.1.4.99 637916390:X 13 Aug 21:53:30.179 # +failover-state-reconf-slaves master master1 10.1.4.99 637916390:X 13 Aug 21:53:30.237 # +failover-end master master1 10.1.4.99 637916390:X 13 Aug 21:53:30.237 # +switch-master master1 10.1.4.99 6379 10.1.4.82 637916390:X 13 Aug 21:53:30.238 * +slave slave 10.1.4.99:6379 10.1.4.99 6379 @ master1 10.1.4.82 637916390:X 13 Aug 21:53:35.295 # +sdown slave 10.1.4.99:6379 10.1.4.99 6379 @ master1 10.1.4.82 637916390:X 13 Aug 21:54:02.671 # -sdown slave 10.1.4.99:6379 10.1.4.99 6379 @ master1 10.1.4.82 6379

检查redis主从和哨兵的状态

10.1.4.99 主从状态

127.0.0.1:6379> info [section]# Replicationrole:masterconnected_slaves:1slave0:ip=10.1.4.82,port=6379,state=online,offset=2678,lag=0master_repl_offset:2678repl_backlog_active:1repl_backlog_size:1048576repl_backlog_first_byte_offset:2repl_backlog_histlen:2677

哨兵状态

[root@bundcredit redis]# redis-cli  -p  26379  -a "Redis2017"127.0.0.1:26379> info# Sentinelsentinel_masters:1sentinel_tilt:0sentinel_running_scripts:0sentinel_scripts_queue_length:0sentinel_simulate_failure_flags:0master0:name=master1,status=ok,address=10.1.4.99:6379,slaves=1,sentinels=4

10.1.4.82 主从状态

127.0.0.1:6379> info [section]# Replication# Replicationrole:slavemaster_host:10.1.4.99master_port:6379master_link_status:upmaster_last_io_seconds_ago:1master_sync_in_progress:0slave_repl_offset:49526slave_priority:100slave_read_only:0connected_slaves:0master_repl_offset:0repl_backlog_active:0repl_backlog_size:1048576repl_backlog_first_byte_offset:0repl_backlog_histlen:0

哨兵状态

[root@bundcredit redis]# redis-cli  -p  26379  -a "Redis2017"127.0.0.1:26379> info# Sentinelsentinel_masters:1sentinel_tilt:0sentinel_running_scripts:0sentinel_scripts_queue_length:0sentinel_simulate_failure_flags:0master0:name=master1,status=ok,address=10.1.4.99:6379,slaves=1,sentinels=4127.0.0.1:26379>

异常测试

1、停止10.1.4.99的redis服务

[root@bundcredit redis]# redis-cli   -h  10.1.4.99  -p  6379  -a "Redis2017"10.1.4.99:6379> SHUTDOWNnot connected>

2、哨兵日志

27102:X 13 Aug 21:53:40.201 # +sdown slave 10.1.4.99:6379 10.1.4.99 6379 @ master1 10.1.4.82 637927102:X 13 Aug 21:54:07.583 # -sdown slave 10.1.4.99:6379 10.1.4.99 6379 @ master1 10.1.4.82 637927102:X 13 Aug 22:17:23.410 # +sdown master master1 10.1.4.82 637927102:X 13 Aug 22:17:23.445 # +new-epoch 227102:X 13 Aug 22:17:23.447 # +vote-for-leader 5c3033c0ea6d1e0d861e9d9e99e94edb49c5daac 227102:X 13 Aug 22:17:23.462 # +odown master master1 10.1.4.82 6379 #quorum 3/227102:X 13 Aug 22:17:23.462 # Next failover delay: I will not start a failover before Sun Aug 13 22:17:34 201727102:X 13 Aug 22:17:24.499 # -odown master master1 10.1.4.82 637927102:X 13 Aug 22:17:24.533 # +config-update-from sentinel 5c3033c0ea6d1e0d861e9d9e99e94edb49c5daac 10.1.4.82 26379 @ master1 10.1.4.82 637927102:X 13 Aug 22:17:24.533 # +switch-master master1 10.1.4.82 6379 10.1.4.99 637927102:X 13 Aug 22:17:24.534 * +slave slave 10.1.4.82:6379 10.1.4.82 6379 @ master1 10.1.4.99 637927102:X 13 Aug 22:17:29.580 # +sdown slave 10.1.4.82:6379 10.1.4.82 6379 @ master1 10.1.4.99 637927102:X 13 Aug 22:17:33.951 # -sdown slave 10.1.4.82:6379 10.1.4.82 6379 @ master1 10.1.4.99 637927102:X 13 Aug 22:17:43.855 * +convert-to-slave slave 10.1.4.82:6379 10.1.4.82 6379 @ master1 10.1.4.99 637927102:X 13 Aug 22:24:14.405 # +sdown master master1 10.1.4.99 637927102:X 13 Aug 22:24:14.445 # +new-epoch 327102:X 13 Aug 22:24:14.446 # +vote-for-leader b73258f1019f65c5fee3b88b9c2eae7f85d985f3 327102:X 13 Aug 22:24:14.477 # +odown master master1 10.1.4.99 6379 #quorum 4/227102:X 13 Aug 22:24:14.477 # Next failover delay: I will not start a failover before Sun Aug 13 22:24:24 201727102:X 13 Aug 22:24:15.600 # +config-update-from sentinel b73258f1019f65c5fee3b88b9c2eae7f85d985f3 10.1.4.82 26380 @ master1 10.1.4.99 637927102:X 13 Aug 22:24:15.601 # +switch-master master1 10.1.4.99 6379 10.1.4.82 637927102:X 13 Aug 22:24:15.601 * +slave slave 10.1.4.99:6379 10.1.4.99 6379 @ master1 10.1.4.82 637927102:X 13 Aug 22:24:20.627 # +sdown slave 10.1.4.99:6379 10.1.4.99 6379 @ master1 10.1.4.82 637927102:X 13 Aug 22:25:49.007 # -sdown slave 10.1.4.99:6379 10.1.4.99 6379 @ master1 10.1.4.82 637927102:X 13 Aug 22:25:58.984 * +convert-to-slave slave 10.1.4.99:6379 10.1.4.99 6379 @ master1 10.1.4.82 6379

3、检查服务

10.1.4.82# Replicationrole:masterconnected_slaves:1slave0:ip=10.1.4.99,port=6379,state=online,offset=17942,lag=0master_repl_offset:17942repl_backlog_active:1repl_backlog_size:1048576repl_backlog_first_byte_offset:2repl_backlog_histlen:17941

启动10.1.4.99

检查服务

# Replicationrole:slavemaster_host:10.1.4.82master_port:6379master_link_status:upmaster_last_io_seconds_ago:0master_sync_in_progress:0slave_repl_offset:2546slave_priority:100slave_read_only:0connected_slaves:0master_repl_offset:0repl_backlog_active:0repl_backlog_size:1048576repl_backlog_first_byte_offset:0repl_backlog_histlen:0

再次停止10.1.4.82 redis服务服务正常


注意事项:

在配置redis.conf时需要注意以下事项

1、protected-mode no #保护模式关闭 在3.2版本后添加的安全模式 最好关闭2、slave-read-only  no  #默认的从库不支持写入,这里需要开启,当经过哨兵写入时才支持写入数据

在配置sentinel.conf时需要注意

1、pdaemonize yes  #后台运行2、protected-mode no #保护模式关闭3、sentinel monitor master1 10.1.4.99 6379 2 # 监听IP  端口  默认最少投票数量4、sentinel down-after-milliseconds master1 5000 5、sentinel failover-timeout master1 5000  #选举超时时间6、logfile "/data/redis/sentinel/sentinel_26379.log" #日志文件7、sentinel auth-pass master1 Redis2017  #redis密码  如果主从设置密码后记得要设置密码,否则不能登录


0