阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Redis集群redis主从自动切换Sentinel(哨兵模式)

165次阅读
没有评论

共计 9850 个字符,预计需要花费 25 分钟才能阅读完成。

Redis Sentinel
Sentinel(哨兵)是用于监控 redis 集群中 Master 状态的工具,其已经被集成在 redis2.4+ 的版本中

一、Sentinel 作用:
1):Master 状态检测
2):如果 Master 异常,则会进行 Master-Slave 切换,将其中一个 Slave 作为 Master,将之前的 Master 作为 Slave
3):Master-Slave 切换后,master_redis.conf、slave_redis.conf 和 sentinel.conf 的内容都会发生改变,即 master_redis.conf 中会多一行 slaveof 的配置,sentinel.conf 的监控目标会随之调换
二、Sentinel 工作方式:
1):每个 Sentinel 以每秒钟一次的频率向它所知的 Master,Slave 以及其他 Sentinel 实例发送一个 PING 命令
2):如果一个实例(instance)距离最后一次有效回复 PING 命令的时间超过 down-after-milliseconds 选项所指定的值,则这个实例会被 Sentinel 标记为主观下线。
3):如果一个 Master 被标记为主观下线,则正在监视这个 Master 的所有 Sentinel 要以每秒一次的频率确认 Master 的确进入了主观下线状态。
4):当有足够数量的 Sentinel(大于等于配置文件指定的值)在指定的时间范围内确认 Master 的确进入了主观下线状态,则 Master 会被标记为客观下线
5):在一般情况下,每个 Sentinel 会以每 10 秒一次的频率向它已知的所有 Master,Slave 发送 INFO 命令
6):当 Master 被 Sentinel 标记为客观下线时,Sentinel 向下线的 Master 的所有 Slave 发送 INFO 命令的频率会从 10 秒一次改为每秒一次
7):若没有足够数量的 Sentinel 同意 Master 已经下线,Master 的客观下线状态就会被移除。
若 Master 重新向 Sentinel 的 PING 命令返回有效回复,Master 的主观下线状态就会被移除。

主观下线和客观下线
主观下线:Subjectively Down,简称 SDOWN,指的是当前 Sentinel 实例对某个 redis 服务器做出的下线判断。
客观下线:Objectively Down,简称 ODOWN,指的是多个 Sentinel 实例在对 Master Server 做出 SDOWN 判断,并且通过 SENTINEL is-master-down-by-addr 命令互相交流之后,得出的 Master Server 下线判断,然后开启 failover.

SDOWN 适合于 Master 和 Slave,只要一个 Sentinel 发现 Master 进入了 ODOWN,这个 Sentinel 就可能会被其他 Sentinel 推选出,并对下线的主服务器执行自动故障迁移操作。
ODOWN 只适用于 Master,对于 Slave 的 Redis 实例,Sentinel 在将它们判断为下线前不需要进行协商,所以 Slave 的 Sentinel 永远不会达到 ODOWN。

三、配置:
1:指定监听 Master(三个节点)
# vi /main/redis/sentinel_26379.conf
port 26379
daemonize yes
dir /tmp
logfile “/var/log/redis_26382.log”
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 900000

 

 

 
# vi /main/redis/sentinel_26381.conf
port 26381
daemonize yes
dir /tmp
logfile “/var/log/redis_26382.log”
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 900000

 
# vi /main/redis/sentinel_26382.conf
port 26382
daemonize yes
dir /tmp
logfile “/var/log/redis_26382.log”
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 900000

 

 

# 上面配置文件说明如下:
####
port 5000#sentinel
# 监听的端口
sentinel monitor mymaster192.168.241.150 9400 2 
#192.168.241.150 是 master redis 的 IP 地址和端口,2 是代表 2 个 sentinel 检测到异常,才判断是 real fail. Mymaster 主机组的名称可以随便定义
sentinel down-after-millisecondsmymaster 30000      #指定 sentinel 监控到 redis 实例持续异常多长时间后,会判决其状态为 down。若实际业务需要 sentinel 尽快判决出 redis 实例异常,则可适当配小, 单位是毫秒
sentinel can-failover mymaster yes                  #在 sentinel 检测到 O_DOWN 后,是否对这台 redis 启动 failover 机制
sentinel parallel-syncs mymaster 1                  #执行故障转移时,最多可以有多少个从服务器同时对新的主服务器进行同步,这个数字越小,完成故障转移所需的时间就越长,但越大就意味着越多的从服务器因为复制而不可用。可以通过将这个值设为 1 来保证每次只有一个从服务器处于不能处理命令请求的状态。
sentinel failover-timeout mymaster900000            #若 sentinel 在该配置值内未能完成 failover 操作(即故障时 master/slave 自动切换),则认为本次 failover 失败
sentinel auth-pass master1rot@minunix              #当 redis 访问都需要密码的时候,即在 redis.conf 有配置 requirepass 项的时候,需要定义此项
sentinel notification-script master1/data/scripts/send_mail.sh
# 当 sentinel 触发时,切换主从状态时,需要执行的脚本。当主 down 的时候可以通知当事人,

2:启动 sentinel(三个节点):
# redis-sentinel /etc/redis/sentinel_26379.conf
# redis-sentinel /etc/redis/sentinel_26381.conf
# redis-sentinel /etc/redis/sentinel_26382.conf
注意,必须使用 redis-sentinel 命令启动 sentinel_26379.conf,虽然 redis-sentinel 是 redis-server 的软连接。
启动 sentinel 后,查看 sentinel_26379.conf、sentinel_26380.conf、sentinel_26381.conf
可发现配置文件会记录已经加入的集群节点, 并且配置文件内容改变了:
[root@localhost redis]# cat sentinel-26379.conf
port 26379
daemonize yes
pidfile “/var/run/redis-26379.pid”
logfile “/var/log/redis_26379.log”
dir “/tmp”
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel config-epoch mymaster 0
sentinel leader-epoch mymaster 0
sentinel known-slave mymaster 127.0.0.1 6382
# Generated by CONFIG REWRITE
sentinel known-slave mymaster 127.0.0.1 6381
sentinel known-sentinel mymaster 127.0.0.1 263818fa3f71262abb6adfb838a598f449f855a9d4f3f
sentinel known-sentinel mymaster 127.0.0.1 2638226f3c5cf596971dbf8c8ed112d015fa252f36a08
sentinel current-epoch 0
[root@localhost redis]# cat sentinel-26381.conf     
#master1
port 26381
daemonize yes
pidfile “/var/run/redis-26381.pid”
logfile “/var/log/redis_26381.log”
dir “/tmp”
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel config-epoch mymaster 0
sentinel leader-epoch mymaster 0
sentinel known-slave mymaster 127.0.0.1 6382
# Generated by CONFIG REWRITE
sentinel known-slave mymaster 127.0.0.1 6381
sentinel known-sentinel mymaster 127.0.0.1 263798eb55903cd604571d4a816a2abfc6e7d60f9e356
sentinel known-sentinel mymaster 127.0.0.1 2638226f3c5cf596971dbf8c8ed112d015fa252f36a08
sentinel current-epoch 0
[root@localhost redis]# cat sentinel-26382.conf 
#master1
port 26382
daemonize yes
pidfile “/var/run/redis-26382.pid”
logfile “/var/log/redis_26382.log”
dir “/tmp”
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel config-epoch mymaster 0
sentinel leader-epoch mymaster 0
sentinel known-slave mymaster 127.0.0.1 6381
# Generated by CONFIG REWRITE
sentinel known-slave mymaster 127.0.0.1 6382
sentinel known-sentinel mymaster 127.0.0.1 263818fa3f71262abb6adfb838a598f449f855a9d4f3f
sentinel known-sentinel mymaster 127.0.0.1 263798eb55903cd604571d4a816a2abfc6e7d60f9e356
sentinel current-epoch 0
 
 
 
查看启动状态:
[root@localhost redis]# ps -ef |grep redis               
root      2630    1  016:41 ?        00:00:03 redis-server*:6379             
root      2645    1  016:46 ?        00:00:02 redis-server*:6381             
root      2650    1  016:46 ?        00:00:02 redis-server*:6382             
root      2705    1  017:13 ?        00:00:02 redis-sentinel*:26379 [sentinel]
root      2724  2276  017:22 pts/1    00:00:00 tail -f/var/log/redis_26379.log
root      2736    1  0 17:26 ?        00:00:00 redis-sentinel *:26382[sentinel]
root      2745    1  117:27 ?        00:00:00 redis-sentinel*:26381 [sentinel]
root      2749  1640  017:27 pts/0    00:00:00 grep redis
 
查看集群状态:
[root@localhost redis]# redis-cli info replication
# Replication
role:master
connected_slaves:2
slave0:ip=127.0.0.1,port=6381,state=online,offset=262006,lag=0
slave1:ip=127.0.0.1,port=6382,state=online,offset=262006,lag=0
master_repl_offset:262139
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:262138

查看 sentinel 状态:
[root@localhost redis]# redis-cli -p 26379 info sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
master0:name=mymaster,status=ok,address=127.0.0.1:6379,slaves=2,sentinels=4
[root@localhost redis]# redis-cli -p 26381 infosentinel 
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
master0:name=mymaster,status=ok,address=127.0.0.1:6379,slaves=2,sentinels=3
[root@localhost redis]# redis-cli -p 26382 info sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
master0:name=mymaster,status=ok,address=127.0.0.1:6379,slaves=2,sentinels=4
 
查看 sentinel 监控的所有 master:
[root@localhost redis]# redis-cli -p 26379 sentinelmasters
查看 mymaster 下有哪些 slaves
[root@localhost redis]# redis-cli -p 26379 sentinelslaves mymaster
强制进行主从切换:
[root@localhost redis]# redis-cli -p 26379 sentinelfailover mymaster
OK
 
[root@localhost redis]# redis-cli -p 26379 sentinelget-master-addr-by-name mymaster
1) “127.0.0.1”
2) “6382”
[root@localhost redis]# redis-cli -p 26379 sentinelfailover mymaster
OK
[root@localhost redis]# redis-cli -p 26379 sentinelget-master-addr-by-name mymaster             
1) “127.0.0.1”
2) “6381
 
查看 mymaster 集群中的 master 地址和端口
[root@localhost redis]# redis-cli -p 26379 sentinelget-master-addr-by-name mymaster
1) “127.0.0.1”
2) “6379”
 
模拟故障转移:
Down 掉 6379 服务,看是否自动切换:
[root@localhost redis]# redis-cli -p 26379 sentinelget-master-addr-by-name mymaster
1) “127.0.0.1”
2) “6382”
从上可看到会自动切换到了 6382 服务了。
 
模拟 master 恢复服务后的集群状态:
[root@localhost redis]# redis-cli -p 26379 sentinelget-master-addr-by-name mymaster
1) “127.0.0.1”
2) “6382”
 
[root@localhost redis]# redis-cli -p 26379 sentinelmasters               
1)  1)”name”
    2)”mymaster”
    3)”ip”
    4)”127.0.0.1″
    5)”port”
6) “6382”
。。。。。。。
 
[root@localhost redis]# redis-cli -p 26379 sentinelslaves mymaster
1)  1)”name”
    2)”127.0.0.1:6381″
    3)”ip”
    4)”127.0.0.1″
    5)”port”
    6)”6381″
    7)”runid”
    8)”a05d4350123b04c8bc98b00a3c1b0179b3b756e5″
    9)”flags”
  10) “slave”
  11)”pending-commands”
  12)”0″
  13)”last-ping-sent”
  14)”0″
  15)”last-ok-ping-reply”
  16)”498″
  17)”last-ping-reply”
  18)”498″
  19)”down-after-milliseconds”
  20)”30000″
  21)”info-refresh”
  22)”8512″
  23)”role-reported”
  24)”slave”
  25)”role-reported-time”
  26)”219661″
  27)”master-link-down-time”
  28)”0″
  29)”master-link-status”
  30)”ok”
  31)”master-host”
  32)”127.0.0.1″
  33)”master-port”
  34)”6382″
  35)”slave-priority”
  36)”100″
  37)”slave-repl-offset”
  38)”41424″
2)  1)”name”
    2)”127.0.0.1:6379″
    3)”ip”
    4)”127.0.0.1″
    5)”port”
    6)”6379″
    7)”runid”
    8)”a2b0b1b19758c3cdfeb534a3c1274bcc6c19e55b”
    9)”flags”
  10)”slave”
  11) “pending-commands”
  12)”0″
  13)”last-ping-sent”
  14)”0″
  15)”last-ok-ping-reply”
  16)”209″
  17)”last-ping-reply”
  18)”209″
  19)”down-after-milliseconds”
  20)”30000″
  21)”info-refresh”
  22)”9908″
  23)”role-reported”
  24)”slave”
  25)”role-reported-time”
  26)”29937″
  27)”master-link-down-time”
  28)”0″
  29)”master-link-status”
  30)”ok”
  31)”master-host”
  32)”127.0.0.1″
  33)”master-port”
  34)”6382″
  35)”slave-priority”
  36)”100″
  37)”slave-repl-offset”
  38)”41158″
 
结果:可以从上看出,就算 master 恢复了,集群也不会让 master 重新接管,而是作为 slave 出现了。
3:设置开机启动(三个节点)
# echo “redis-sentinel /etc/redis/sentinel_26379.conf ” >>/etc/rc.local

四、注意点:
1):首次启动时,必须先启动 Master
2):Sentinel 只在 server 端做主从切换,app 端要自己开发 (例如 Jedis 库的 SentinelJedis,能够监控 Sentinel 的状态)
3):若 Master 已经被判定为下线,Sentinel 已经选择了新的 Master,也已经将 old Master 改成 Slave,但是还没有将其改成 new Master。若此时重启 old Master,则 Redis 集群将处于无 Master 状态,此时只能手动修改配置文件,然后重新启动集群
到此 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/146375.htm

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-22发表,共计9850字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中