你好,游客 登录 注册 搜索
背景:
阅读新闻

基于Sentinel的Redis高可用方案

[日期:2017-08-15] 来源:Linux社区  作者:dreamlinux [字体: ]

数据存储我们在应用设计过程中非常重要的一部分,无论是关系型数据库,还是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 Role Redis  Port Sentinel Port
1 10.1.4.82 slave 6379 26379  26380
2 10.1.4.99 master 6379 26379    26380

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

http://www.linuxidc.com/Linux/2017-08/146252.htm

2、检验redis主从是否正常

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

127.0.0.1:6379> info 
 
127.0.0.1:6379> info 
# Server
redis_version:3.2.9
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:66cf707c3c51d9f
redis_mode:standalone
os:Linux 2.6.32-696.6.3.el6.x86_64 x86_64
arch_bits:64
multiplexing_api:epoll
gcc_version:4.4.7
process_id:26318
run_id:7954f18bcb4084964efb58460027399d583fba57
tcp_port:6379
uptime_in_seconds:14402
uptime_in_days:0
hz:10
lru_clock:9455690
executable:/usr/local/bin/redis-server
config_file:/etc/redis/6379.conf
 
# Clients
connected_clients:7
client_longest_output_list:0
client_biggest_input_buf:0
blocked_clients:0
 
# Memory
used_memory:2017176
used_memory_human:1.92M
used_memory_rss:9969664
used_memory_rss_human:9.51M
used_memory_peak:2098064
used_memory_peak_human:2.00M
total_system_memory:2103533568
total_system_memory_human:1.96G
used_memory_lua:37888
used_memory_lua_human:37.00K
maxmemory:0
maxmemory_human:0B
maxmemory_policy:noeviction
mem_fragmentation_ratio:4.94
mem_allocator:jemalloc-4.0.3
 
# Persistence
loading:0
rdb_changes_since_last_save:0
rdb_bgsave_in_progress:0
rdb_last_save_time:1502627793
rdb_last_bgsave_status:ok
rdb_last_bgsave_time_sec:0
rdb_current_bgsave_time_sec:-1
aof_enabled:0
aof_rewrite_in_progress:0
aof_rewrite_scheduled:0
aof_last_rewrite_time_sec:-1
aof_current_rewrite_time_sec:-1
aof_last_bgrewrite_status:ok
aof_last_write_status:ok
 
# Stats
total_connections_received:20
total_commands_processed:89212
instantaneous_ops_per_sec:6
total_net_input_bytes:6172607
total_net_output_bytes:33781397
instantaneous_input_kbps:0.26
instantaneous_output_kbps:0.67
rejected_connections:0
sync_full:1
sync_partial_ok:0
sync_partial_err:0
expired_keys:0
evicted_keys:0
keyspace_hits:0
keyspace_misses:0
pubsub_channels:1
pubsub_patterns:0
latest_fork_usec:643
migrate_cached_sockets:0
 
# Replication
role:master  #当前host的角色
connected_slaves:1
slave0:ip=10.1.4.82,port=6379,state=online,offset=23160,lag=1  #看到从节点的一些信息
master_repl_offset:23160
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:23159
 
# CPU
used_cpu_sys:10.40
used_cpu_user:6.24
used_cpu_sys_children:0.00
used_cpu_user_children:0.00
 
# Cluster
cluster_enabled:0
 
# Keyspace
db0:keys=5,expires=0,avg_ttl=0

设置相关的key检查
master
127.0.0.1:6379> dbsize 
(integer) 5
127.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) 5
127.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 REWRITE
dir "/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 REWRITE
dir "/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 REWRITE
dir "/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 REWRITE
dir "/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 361c7d3014a041a1a74163355882d88f5fab7581
27098:X 13 Aug 21:51:25.242 # +monitor master master1 10.1.4.99 6379 quorum 2
27098:X 13 Aug 21:51:30.271 # +sdown sentinel 76d5a26748410e72ca703e255f44a5ed2b3f83e4 10.1.4.99 26380 @ master1 10.1.4.99 6379
27098:X 13 Aug 21:51:30.410 # -sdown sentinel 76d5a26748410e72ca703e255f44a5ed2b3f83e4 10.1.4.99 26380 @ master1 10.1.4.99 6379
27098:X 13 Aug 21:52:30.617 # +sdown sentinel 5c3033c0ea6d1e0d861e9d9e99e94edb49c5daac 10.1.4.82 26379 @ master1 10.1.4.99 6379
27098:X 13 Aug 21:53:23.855 # -sdown sentinel 5c3033c0ea6d1e0d861e9d9e99e94edb49c5daac 10.1.4.82 26379 @ master1 10.1.4.99 6379
27098:X 13 Aug 21:53:34.463 # +sdown master master1 10.1.4.99 6379
27098:X 13 Aug 21:53:34.532 # +odown master master1 10.1.4.99 6379 #quorum 2/2
27098:X 13 Aug 21:53:34.532 # +new-epoch 1
27098:X 13 Aug 21:53:34.532 # +try-failover master master1 10.1.4.99 6379
27098:X 13 Aug 21:53:34.541 # +vote-for-leader 361c7d3014a041a1a74163355882d88f5fab7581 1
27098:X 13 Aug 21:53:34.542 # b73258f1019f65c5fee3b88b9c2eae7f85d985f3 voted for b73258f1019f65c5fee3b88b9c2eae7f85d985f3 1
27098:X 13 Aug 21:53:34.559 # 5c3033c0ea6d1e0d861e9d9e99e94edb49c5daac voted for b73258f1019f65c5fee3b88b9c2eae7f85d985f3 1
27098:X 13 Aug 21:53:34.560 # 76d5a26748410e72ca703e255f44a5ed2b3f83e4 voted for b73258f1019f65c5fee3b88b9c2eae7f85d985f3 1
27098:X 13 Aug 21:53:35.156 # +config-update-from sentinel b73258f1019f65c5fee3b88b9c2eae7f85d985f3 10.1.4.82 26380 @ master1 10.1.4.99 6379
27098:X 13 Aug 21:53:35.156 # +switch-master master1 10.1.4.99 6379 10.1.4.82 6379
27098:X 13 Aug 21:53:35.156 * +slave slave 10.1.4.99:6379 10.1.4.99 6379 @ master1 10.1.4.82 6379
27098:X 13 Aug 21:53:40.173 # +sdown slave 10.1.4.99:6379 10.1.4.99 6379 @ master1 10.1.4.82 6379
27098: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 361c7d3014a041a1a74163355882d88f5fab7581
27098:X 13 Aug 21:51:25.242 # +monitor master master1 10.1.4.99 6379 quorum 2
27098:X 13 Aug 21:51:30.271 # +sdown sentinel 76d5a26748410e72ca703e255f44a5ed2b3f83e4 10.1.4.99 26380 @ master1 10.1.4.99 6379
27098:X 13 Aug 21:51:30.410 # -sdown sentinel 76d5a26748410e72ca703e255f44a5ed2b3f83e4 10.1.4.99 26380 @ master1 10.1.4.99 6379
27098:X 13 Aug 21:52:30.617 # +sdown sentinel 5c3033c0ea6d1e0d861e9d9e99e94edb49c5daac 10.1.4.82 26379 @ master1 10.1.4.99 6379
27098:X 13 Aug 21:53:23.855 # -sdown sentinel 5c3033c0ea6d1e0d861e9d9e99e94edb49c5daac 10.1.4.82 26379 @ master1 10.1.4.99 6379
27098:X 13 Aug 21:53:34.463 # +sdown master master1 10.1.4.99 6379
27098:X 13 Aug 21:53:34.532 # +odown master master1 10.1.4.99 6379 #quorum 2/2
27098:X 13 Aug 21:53:34.532 # +new-epoch 1
27098:X 13 Aug 21:53:34.532 # +try-failover master master1 10.1.4.99 6379
27098:X 13 Aug 21:53:34.541 # +vote-for-leader 361c7d3014a041a1a74163355882d88f5fab7581 1
27098:X 13 Aug 21:53:34.542 # b73258f1019f65c5fee3b88b9c2eae7f85d985f3 voted for b73258f1019f65c5fee3b88b9c2eae7f85d985f3 1
27098:X 13 Aug 21:53:34.559 # 5c3033c0ea6d1e0d861e9d9e99e94edb49c5daac voted for b73258f1019f65c5fee3b88b9c2eae7f85d985f3 1
27098:X 13 Aug 21:53:34.560 # 76d5a26748410e72ca703e255f44a5ed2b3f83e4 voted for b73258f1019f65c5fee3b88b9c2eae7f85d985f3 1
27098:X 13 Aug 21:53:35.156 # +config-update-from sentinel b73258f1019f65c5fee3b88b9c2eae7f85d985f3 10.1.4.82 26380 @ master1 10.1.4.99 6379
27098:X 13 Aug 21:53:35.156 # +switch-master master1 10.1.4.99 6379 10.1.4.82 6379
27098:X 13 Aug 21:53:35.156 * +slave slave 10.1.4.99:6379 10.1.4.99 6379 @ master1 10.1.4.82 6379
27098:X 13 Aug 21:53:40.173 # +sdown slave 10.1.4.99:6379 10.1.4.99 6379 @ master1 10.1.4.82 6379
27098: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 directory
tail: no files remaining
[root@bundcredit sentinel]# tail  -f  sentinel_26380.log
27102:X 13 Aug 21:53:34.496 # +sdown master master1 10.1.4.99 6379
27102:X 13 Aug 21:53:34.558 # +new-epoch 1
27102:X 13 Aug 21:53:34.560 # +vote-for-leader b73258f1019f65c5fee3b88b9c2eae7f85d985f3 1
27102:X 13 Aug 21:53:34.580 # +odown master master1 10.1.4.99 6379 #quorum 4/2
27102:X 13 Aug 21:53:34.580 # Next failover delay: I will not start a failover before Sun Aug 13 21:53:45 2017
27102:X 13 Aug 21:53:35.156 # +config-update-from sentinel b73258f1019f65c5fee3b88b9c2eae7f85d985f3 10.1.4.82 26380 @ master1 10.1.4.99 6379
27102:X 13 Aug 21:53:35.156 # +switch-master master1 10.1.4.99 6379 10.1.4.82 6379
27102:X 13 Aug 21:53:35.156 * +slave slave 10.1.4.99:6379 10.1.4.99 6379 @ master1 10.1.4.82 6379
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 6379
27102: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 5c3033c0ea6d1e0d861e9d9e99e94edb49c5daac
15947:X 13 Aug 21:45:33.893 # +monitor master master1 10.1.4.99 6379 quorum 2
15947:X 13 Aug 21:45:33.894 * +slave slave 10.1.4.82:6379 10.1.4.82 6379 @ master1 10.1.4.99 6379
15951: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 b73258f1019f65c5fee3b88b9c2eae7f85d985f3
15951:X 13 Aug 21:45:39.174 # +monitor master master1 10.1.4.99 6379 quorum 2
15951:X 13 Aug 21:45:39.175 * +slave slave 10.1.4.82:6379 10.1.4.82 6379 @ master1 10.1.4.99 6379
15951:X 13 Aug 21:45:40.025 * +sentinel sentinel 5c3033c0ea6d1e0d861e9d9e99e94edb49c5daac 10.1.4.82 26379 @ master1 10.1.4.99 6379
15947:X 13 Aug 21:45:41.196 * +sentinel sentinel b73258f1019f65c5fee3b88b9c2eae7f85d985f3 10.1.4.82 26380 @ master1 10.1.4.99 6379
15951:X 13 Aug 21:45:57.729 * +sentinel sentinel 361c7d3014a041a1a74163355882d88f5fab7581 10.1.4.99 26379 @ master1 10.1.4.99 6379
15947:X 13 Aug 21:45:57.729 * +sentinel sentinel 361c7d3014a041a1a74163355882d88f5fab7581 10.1.4.99 26379 @ master1 10.1.4.99 6379
15947:X 13 Aug 21:46:05.114 * +sentinel sentinel 76d5a26748410e72ca703e255f44a5ed2b3f83e4 10.1.4.99 26380 @ master1 10.1.4.99 6379
15951:X 13 Aug 21:46:05.115 * +sentinel sentinel 76d5a26748410e72ca703e255f44a5ed2b3f83e4 10.1.4.99 26380 @ master1 10.1.4.99 6379
15951:X 13 Aug 21:47:16.648 # +sdown master master1 10.1.4.99 6379
15951:X 13 Aug 21:47:20.907 # +sdown sentinel 361c7d3014a041a1a74163355882d88f5fab7581 10.1.4.99 26379 @ master1 10.1.4.99 6379
15951:X 13 Aug 21:47:20.908 # +sdown sentinel 76d5a26748410e72ca703e255f44a5ed2b3f83e4 10.1.4.99 26380 @ master1 10.1.4.99 6379
15947:X 13 Aug 21:47:20.908 # +sdown sentinel 361c7d3014a041a1a74163355882d88f5fab7581 10.1.4.99 26379 @ master1 10.1.4.99 6379
15951:X 13 Aug 21:47:20.979 # +tilt #tilt mode entered
15947:X 13 Aug 21:47:20.979 # +tilt #tilt mode entered
15947:X 13 Aug 21:47:50.982 # -tilt #tilt mode exited
15947:X 13 Aug 21:47:50.983 # -sdown sentinel 361c7d3014a041a1a74163355882d88f5fab7581 10.1.4.99 26379 @ master1 10.1.4.99 6379
15951:X 13 Aug 21:47:51.030 # -tilt #tilt mode exited
15951:X 13 Aug 21:47:51.030 # -sdown master master1 10.1.4.99 6379
15951:X 13 Aug 21:47:51.030 # -sdown sentinel 361c7d3014a041a1a74163355882d88f5fab7581 10.1.4.99 26379 @ master1 10.1.4.99 6379
15951:X 13 Aug 21:47:51.030 # -sdown sentinel 76d5a26748410e72ca703e255f44a5ed2b3f83e4 10.1.4.99 26380 @ master1 10.1.4.99 6379
15947:X 13 Aug 21:48:42.378 # +sdown sentinel b73258f1019f65c5fee3b88b9c2eae7f85d985f3 10.1.4.82 26380 @ master1 10.1.4.99 6379
15947:X 13 Aug 21:49:06.458 # -sdown sentinel b73258f1019f65c5fee3b88b9c2eae7f85d985f3 10.1.4.82 26380 @ master1 10.1.4.99 6379
15947:X 13 Aug 21:49:42.140 # +sdown sentinel 361c7d3014a041a1a74163355882d88f5fab7581 10.1.4.99 26379 @ master1 10.1.4.99 6379
15947:X 13 Aug 21:50:59.960 # +sdown sentinel 76d5a26748410e72ca703e255f44a5ed2b3f83e4 10.1.4.99 26380 @ master1 10.1.4.99 6379
15947:X 13 Aug 21:51:20.923 # -sdown sentinel 361c7d3014a041a1a74163355882d88f5fab7581 10.1.4.99 26379 @ master1 10.1.4.99 6379
15947:X 13 Aug 21:51:26.062 # -sdown sentinel 76d5a26748410e72ca703e255f44a5ed2b3f83e4 10.1.4.99 26380 @ master1 10.1.4.99 6379
15947: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 5c3033c0ea6d1e0d861e9d9e99e94edb49c5daac
17056:X 13 Aug 21:53:18.721 # +monitor master master1 10.1.4.99 6379 quorum 2
17056:X 13 Aug 21:53:29.549 # +sdown master master1 10.1.4.99 6379
17056:X 13 Aug 21:53:29.643 # +new-epoch 1
17056:X 13 Aug 21:53:29.645 # +vote-for-leader b73258f1019f65c5fee3b88b9c2eae7f85d985f3 1
17056:X 13 Aug 21:53:29.645 # +odown master master1 10.1.4.99 6379 #quorum 2/2
17056:X 13 Aug 21:53:29.645 # Next failover delay: I will not start a failover before Sun Aug 13 21:53:39 2017
17056:X 13 Aug 21:53:30.241 # +config-update-from sentinel b73258f1019f65c5fee3b88b9c2eae7f85d985f3 10.1.4.82 26380 @ master1 10.1.4.99 6379
17056:X 13 Aug 21:53:30.241 # +switch-master master1 10.1.4.99 6379 10.1.4.82 6379
17056:X 13 Aug 21:53:30.241 * +slave slave 10.1.4.99:6379 10.1.4.99 6379 @ master1 10.1.4.82 6379
17056:X 13 Aug 21:53:35.285 # +sdown slave 10.1.4.99:6379 10.1.4.99 6379 @ master1 10.1.4.82 6379
17056:X 13 Aug 21:54:02.488 # -sdown slave 10.1.4.99:6379 10.1.4.99 6379 @ master1 10.1.4.82 6379
17056: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 b73258f1019f65c5fee3b88b9c2eae7f85d985f3
16390:X 13 Aug 21:49:05.966 # +monitor master master1 10.1.4.99 6379 quorum 2
16390:X 13 Aug 21:49:42.173 # +sdown sentinel 361c7d3014a041a1a74163355882d88f5fab7581 10.1.4.99 26379 @ master1 10.1.4.99 6379
16390:X 13 Aug 21:50:59.918 # +sdown sentinel 76d5a26748410e72ca703e255f44a5ed2b3f83e4 10.1.4.99 26380 @ master1 10.1.4.99 6379
16390:X 13 Aug 21:51:21.049 # -sdown sentinel 361c7d3014a041a1a74163355882d88f5fab7581 10.1.4.99 26379 @ master1 10.1.4.99 6379
16390:X 13 Aug 21:51:25.188 # -sdown sentinel 76d5a26748410e72ca703e255f44a5ed2b3f83e4 10.1.4.99 26380 @ master1 10.1.4.99 6379
16390:X 13 Aug 21:52:25.659 # +sdown sentinel 5c3033c0ea6d1e0d861e9d9e99e94edb49c5daac 10.1.4.82 26379 @ master1 10.1.4.99 6379
16390:X 13 Aug 21:53:19.651 # -sdown sentinel 5c3033c0ea6d1e0d861e9d9e99e94edb49c5daac 10.1.4.82 26379 @ master1 10.1.4.99 6379
16390:X 13 Aug 21:53:29.563 # +sdown master master1 10.1.4.99 6379
16390:X 13 Aug 21:53:29.615 # +odown master master1 10.1.4.99 6379 #quorum 3/2
16390:X 13 Aug 21:53:29.615 # +new-epoch 1
16390:X 13 Aug 21:53:29.615 # +try-failover master master1 10.1.4.99 6379
16390:X 13 Aug 21:53:29.626 # +vote-for-leader b73258f1019f65c5fee3b88b9c2eae7f85d985f3 1
16390:X 13 Aug 21:53:29.627 # 361c7d3014a041a1a74163355882d88f5fab7581 voted for 361c7d3014a041a1a74163355882d88f5fab7581 1
16390:X 13 Aug 21:53:29.645 # 5c3033c0ea6d1e0d861e9d9e99e94edb49c5daac voted for b73258f1019f65c5fee3b88b9c2eae7f85d985f3 1
16390:X 13 Aug 21:53:29.646 # 76d5a26748410e72ca703e255f44a5ed2b3f83e4 voted for b73258f1019f65c5fee3b88b9c2eae7f85d985f3 1
16390:X 13 Aug 21:53:29.698 # +elected-leader master master1 10.1.4.99 6379
16390:X 13 Aug 21:53:29.698 # +failover-state-select-slave master master1 10.1.4.99 6379
16390: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 6379
16390: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 6379
16390: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 6379
16390: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 6379
16390:X 13 Aug 21:53:30.179 # +failover-state-reconf-slaves master master1 10.1.4.99 6379
16390:X 13 Aug 21:53:30.237 # +failover-end master master1 10.1.4.99 6379
16390:X 13 Aug 21:53:30.237 # +switch-master master1 10.1.4.99 6379 10.1.4.82 6379
16390:X 13 Aug 21:53:30.238 * +slave slave 10.1.4.99:6379 10.1.4.99 6379 @ master1 10.1.4.82 6379
16390:X 13 Aug 21:53:35.295 # +sdown slave 10.1.4.99:6379 10.1.4.99 6379 @ master1 10.1.4.82 6379
16390: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]
# Replication
role:master
connected_slaves:1
slave0:ip=10.1.4.82,port=6379,state=online,offset=2678,lag=0
master_repl_offset:2678
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:2677

哨兵状态
[root@bundcredit redis]# redis-cli  -p  26379  -a "Redis2017"
127.0.0.1:26379> info
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0: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
# Replication
role:slave
master_host:10.1.4.99
master_port:6379
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:49526
slave_priority:100
slave_read_only:0
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

哨兵状态
[root@bundcredit redis]# redis-cli  -p  26379  -a "Redis2017"
127.0.0.1:26379> info
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=master1,status=ok,address=10.1.4.99:6379,slaves=1,sentinels=4
127.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> SHUTDOWN
not 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 6379
27102: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
27102:X 13 Aug 22:17:23.410 # +sdown master master1 10.1.4.82 6379
27102:X 13 Aug 22:17:23.445 # +new-epoch 2
27102:X 13 Aug 22:17:23.447 # +vote-for-leader 5c3033c0ea6d1e0d861e9d9e99e94edb49c5daac 2
27102:X 13 Aug 22:17:23.462 # +odown master master1 10.1.4.82 6379 #quorum 3/2
27102:X 13 Aug 22:17:23.462 # Next failover delay: I will not start a failover before Sun Aug 13 22:17:34 2017
27102:X 13 Aug 22:17:24.499 # -odown master master1 10.1.4.82 6379
27102:X 13 Aug 22:17:24.533 # +config-update-from sentinel 5c3033c0ea6d1e0d861e9d9e99e94edb49c5daac 10.1.4.82 26379 @ master1 10.1.4.82 6379
27102:X 13 Aug 22:17:24.533 # +switch-master master1 10.1.4.82 6379 10.1.4.99 6379
27102:X 13 Aug 22:17:24.534 * +slave slave 10.1.4.82:6379 10.1.4.82 6379 @ master1 10.1.4.99 6379
27102:X 13 Aug 22:17:29.580 # +sdown slave 10.1.4.82:6379 10.1.4.82 6379 @ master1 10.1.4.99 6379
27102:X 13 Aug 22:17:33.951 # -sdown slave 10.1.4.82:6379 10.1.4.82 6379 @ master1 10.1.4.99 6379
27102: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 6379
27102:X 13 Aug 22:24:14.405 # +sdown master master1 10.1.4.99 6379
27102:X 13 Aug 22:24:14.445 # +new-epoch 3
27102:X 13 Aug 22:24:14.446 # +vote-for-leader b73258f1019f65c5fee3b88b9c2eae7f85d985f3 3
27102:X 13 Aug 22:24:14.477 # +odown master master1 10.1.4.99 6379 #quorum 4/2
27102:X 13 Aug 22:24:14.477 # Next failover delay: I will not start a failover before Sun Aug 13 22:24:24 2017
27102:X 13 Aug 22:24:15.600 # +config-update-from sentinel b73258f1019f65c5fee3b88b9c2eae7f85d985f3 10.1.4.82 26380 @ master1 10.1.4.99 6379
27102:X 13 Aug 22:24:15.601 # +switch-master master1 10.1.4.99 6379 10.1.4.82 6379
27102:X 13 Aug 22:24:15.601 * +slave slave 10.1.4.99:6379 10.1.4.99 6379 @ master1 10.1.4.82 6379
27102:X 13 Aug 22:24:20.627 # +sdown slave 10.1.4.99:6379 10.1.4.99 6379 @ master1 10.1.4.82 6379
27102:X 13 Aug 22:25:49.007 # -sdown slave 10.1.4.99:6379 10.1.4.99 6379 @ master1 10.1.4.82 6379
27102: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
# Replication
role:master
connected_slaves:1
slave0:ip=10.1.4.99,port=6379,state=online,offset=17942,lag=0
master_repl_offset:17942
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:17941

启动10.1.4.99
检查服务
# Replication
role:slave
master_host:10.1.4.82
master_port:6379
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_repl_offset:2546
slave_priority:100
slave_read_only:0
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_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密码  如果主从设置密码后记得要设置密码,否则不能登录

下面关于Redis的文章您也可能喜欢,不妨参考下:

Ubuntu 14.04下Redis安装及简单测试 http://www.linuxidc.com/Linux/2014-05/101544.htm

Redis主从复制基本配置 http://www.linuxidc.com/Linux/2015-03/115610.htm

CentOS 7下Redis的安装与配置 http://www.linuxidc.com/Linux/2017-02/140363.htm

Ubuntu 14.04安装Redis与简单配置 http://www.linuxidc.com/Linux/2017-01/139075.htm

Ubuntu 16.04环境中安装PHP7.0 Redis扩展 http://www.linuxidc.com/Linux/2016-09/135631.htm

Redis 单机&集群离线安装部署 http://www.linuxidc.com/Linux/2017-03/141403.htm

CentOS 7.0 安装Redis 3.2.1详细过程和使用常见问题 http://www.linuxidc.com/Linux/2016-09/135071.htm

Ubuntu 16.04环境中安装PHP7.0 Redis扩展 http://www.linuxidc.com/Linux/2016-09/135631.htm

Ubuntu 15.10下Redis集群部署文档 http://www.linuxidc.com/Linux/2016-06/132340.htm

Redis实战 中文PDF http://www.linuxidc.com/Linux/2016-04/129932.htm

本文永久更新链接地址http://www.linuxidc.com/Linux/2017-08/146251.htm

linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款