小编给大家分享一下Redis Sentinel安装配置的示例分析,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!

Redis Sentinel概述

我们知道Redis类似MySQL数据库自带主从复制结构,产品环境中,如果一旦master发生crash,我们希望slave可以立即自动提升为主,接替业务提供服务,如何实现这个功能呢?redis sentinel集群可以帮助我们实现这个功能;

Redis Sentinel是Redis官方原生高可用解决方案,Redis Sentinel部署架构主要包括两部分:Redis Sentinel集群和Redis master-slave集群,其中Redis Sentinel集群是由若干Sentinel节点组成的分布式集群;

可以实现故障发现、故障转移、配置中心和客户端通知。Redis Sentinel的节点数量要满足2n+1(n>=1)的奇数个(官方建议至少3个)。

Redis Sentinel特点

(1)master与slave之间的failover是通过sentinel来监控,如果共有5个sentinel,配置参数中设置只要有2个sentinel认为master crash了,就会进行failover,但是进行failover的那个sentinel必须先获得至少3个sentinel的授权才能实行failover;

(2)sentinel集群不会同一时间多个sentinel并发执行failover,如果第一个进行failover的sentinel失败了,另外一个将会在一定时间内重新进行failover,以此类推;

(3)当failover后,sentinel会获得master的最新的一个配置版本号,然后在广播给其他sentinel,所以一个能够互相通信的sentinel集群最终会采用版本号最高且相同的配置;

(4)Redis Sentinel version1开始于Redis2.6,Redis Sentinel version 2 开始于Redis 2.8,建议使用Sentinel 2 ;

(5)Redis-Sentinel是Redis官方推荐的高可用性(HA) 解决方案,Redis-sentinel本身也是一个独立运行的进程,它能监控多个master-slave集群,发现master宕机后能进行自动切换。Sentinel可以监视任意多个主服务器(复用),以及主服务器属下的从服务器,并在被监视的主服务器下线时,自动执行故障转移操作。

SDOWN和ODOWN

SDOWN(主观宕机)是sentinel自己主观检测到master的状态是down;

ODOWN(客观宕机)需要大多数的sentinel都认为master宕机;

从SDOWN切换到ODOWN不需要任何一致性算法,只需要一个gossip协议,如果一个sentinel收到了足够多的sentinel发来消息告诉它某个master已经down掉了,SDOWN状态就会变成ODOWN状态。如果之后master可用了,这个状态就会相应地被清理掉。

Sentinel.conf相关参数

port 26379

#sentinel的端口号

sentinel monitor mymaster 127.0.0.1 6379 2

#sentinel监控的master名称默认是mymaster, 最后数字2表示如果有两个sentinel认为master挂了,则这个master即认为不可用;

注意:我们可以通过配置不同的master名称,让一套Sentinel Cluster监控多个Redis master-slave集群;

sentinel down-after-milliseconds mymaster 30000

# 默认30秒,sentinel会通过ping来判断master是否存活,如果在30秒内master返回pong给sentinel,则认为master是好的,否则sentinel认为master不可用;

sentinel parallel-syncs mymaster 1

#当Sentinel节点集合对主节点故障判定达成一致时,Sentinel领导者节点会做故障转移操作,选出新的主节点,原来的从节点会向新的主节点发起复制操作,限制每次向新的主节点发起复制操作的从节点个数为1

sentinel failover-timeout mymaster 180000

#故障转移超时时间为3min

Redis Sentinel中的身份验证

当一个master配置为需要密码才能连接时,客户端和slave在连接时都需要提供密码;

master通过requirepass设置自身的密码,不提供密码无法连接到这个master;

slave通过masterauth来设置访问master时的密码;

但是当使用了sentinel时,由于一个master可能会变成一个slave,一个slave也可能会变成master,所以需要同时设置上述两个配置项。

安装RedisSentinel

(1)Redis sentinel架构图和节点环境


RoelHostIPPortSentinel1sht-sgmhadoopnn-01172.16.101.5426379Sentinel2sht-sgmhadoopnn-01172.16.101.5526379Sentinel3sht-sgmhadoopnn-02172.16.101.5626379Mastersht-sgmhadoopdn-01172.16.101.586379Slave1sht-sgmhadoopdn-02172.16.101.596379Slave2sht-sgmhadoopdn-03172.16.101.606379

(2)配置Redis主从复制

[root@sht-sgmhadoopdn-01redis]#vimredis.confbind172.16.101.58[root@sht-sgmhadoopdn-01redis]#src/redis-serverredis.conf[root@sht-sgmhadoopdn-02redis]#vimredis.confbind172.16.101.59slaveof172.16.101.586379[root@sht-sgmhadoopdn-02redis]#src/redis-serverredis.conf[root@sht-sgmhadoopdn-03redis]#vimredis.confbind172.16.101.60slaveof172.16.101.586379[root@sht-sgmhadoopdn-03redis]#src/redis-serverredis.conf

检查主从复制的设置

[root@sht-sgmhadoopdn-01redis]#src/redis-cli-h172.16.101.58172.16.101.58:6379>clientlistid=3addr=172.16.101.59:35718fd=7name=age=26idle=0flags=Sdb=0sub=0psub=0multi=-1qbuf=0qbuf-free=0obl=0oll=0omem=0events=rcmd=replconfid=4addr=172.16.101.60:33986fd=8name=age=22idle=0flags=Sdb=0sub=0psub=0multi=-1qbuf=0qbuf-free=0obl=0oll=0omem=0events=rcmd=replconfid=5addr=172.16.101.58:38875fd=9name=age=4idle=0flags=Ndb=0sub=0psub=0multi=-1qbuf=0qbuf-free=32768obl=0oll=0omem=0events=rcmd=client

172.16.101.58:6379>inforeplication#Replicationrole:masterconnected_slaves:2slave0:ip=172.16.101.59,port=6379,state=online,offset=57,lag=0slave1:ip=172.16.101.60,port=6379,state=online,offset=57,lag=0master_repl_offset:57repl_backlog_active:1repl_backlog_size:1048576repl_backlog_first_byte_offset:2repl_backlog_histlen:56

(3)配置sentinel集群

三个sentinel节点的sentinel.conf文件配置一样,如果是在同一个主机上,则需要使用不同的端口号

[root@sht-sgmhadoopcm-01redis]#vimsentinel.confport26379daemonizeyesprotected-modenologfile"sentinel.log"dir/usr/local/redissentinelmonitormymaster172.16.101.5863792sentineldown-after-millisecondsmymaster30000sentinelparallel-syncsmymaster1

sentinel节点有两种启动方法:

src/redis-sentinelsentinel.confsrc/redis-serversentinel.conf--sentinel[root@sht-sgmhadoopcm-01redis]#src/redis-sentinelsentinel.conf[root@sht-sgmhadoopnn-01redis]#src/redis-sentinelsentinel.conf[root@sht-sgmhadoopnn-02redis]#src/redis-sentinelsentinel.conf[root@sht-sgmhadoopcm-01redis]#ps-ef|grepredis|grep-vgreproot75411022:33?00:00:00src/redis-sentinel*:26379[sentinel]

(4)检查整个集群的状态

[root@sht-sgmhadoopcm-01redis]#src/redis-cli-h172.16.101.54-p26379172.16.101.54:26379>clientlistid=3addr=172.16.101.55:43182fd=13name=sentinel-ab45fe6c-cmdage=138idle=0flags=Ndb=0sub=0psub=0multi=-1qbuf=0qbuf-free=0obl=0oll=0omem=0events=rcmd=pingid=4addr=172.16.101.56:60016fd=15name=sentinel-e32f20c0-cmdage=136idle=1flags=Ndb=0sub=0psub=0multi=-1qbuf=0qbuf-free=0obl=0oll=0omem=0events=rcmd=pingid=5addr=172.16.101.54:35342fd=17name=age=26idle=0flags=Ndb=0sub=0psub=0multi=-1qbuf=0qbuf-free=32768obl=0oll=0omem=0events=rcmd=client172.16.101.54:26379>infosentinel#Sentinelsentinel_masters:1sentinel_tilt:0sentinel_running_scripts:0sentinel_scripts_queue_length:0sentinel_simulate_failure_flags:0master0:name=mymaster,status=ok,address=172.16.101.58:6379,slaves=2,sentinels=3[root@sht-sgmhadoopdn-01redis]#src/redis-cli-h172.16.101.58-p6379172.16.101.58:6379>clientlistid=16addr=172.16.101.54:56510fd=10name=sentinel-30393e76-pubsubage=326idle=0flags=Ndb=0sub=1psub=0multi=-1qbuf=0qbuf-free=0obl=0oll=0omem=0events=rcmd=subscribeid=17addr=172.16.101.54:56508fd=11name=sentinel-30393e76-cmdage=326idle=0flags=Ndb=0sub=0psub=0multi=-1qbuf=0qbuf-free=0obl=0oll=0omem=0events=rcmd=pingid=18addr=172.16.101.55:57444fd=12name=sentinel-ab45fe6c-cmdage=177idle=0flags=Ndb=0sub=0psub=0multi=-1qbuf=0qbuf-free=0obl=0oll=0omem=0events=rcmd=publishid=19addr=172.16.101.55:57446fd=13name=sentinel-ab45fe6c-pubsubage=177idle=0flags=Ndb=0sub=1psub=0multi=-1qbuf=0qbuf-free=0obl=0oll=0omem=0events=rcmd=subscribeid=3addr=172.16.101.59:35718fd=7name=age=3936idle=0flags=Sdb=0sub=0psub=0multi=-1qbuf=0qbuf-free=0obl=0oll=0omem=0events=rcmd=replconfid=4addr=172.16.101.60:33986fd=8name=age=3932idle=0flags=Sdb=0sub=0psub=0multi=-1qbuf=0qbuf-free=32768obl=0oll=0omem=0events=rcmd=replconfid=20addr=172.16.101.56:55648fd=14name=sentinel-e32f20c0-cmdage=173idle=0flags=Ndb=0sub=0psub=0multi=-1qbuf=0qbuf-free=0obl=0oll=0omem=0events=rcmd=pingid=21addr=172.16.101.56:55650fd=15name=sentinel-e32f20c0-pubsubage=173idle=0flags=Ndb=0sub=1psub=0multi=-1qbuf=0qbuf-free=0obl=0oll=0omem=0events=rcmd=subscribeid=5addr=172.16.101.58:38875fd=9name=age=3914idle=0flags=Ndb=0sub=0psub=0multi=-1qbuf=0qbuf-free=32768obl=0oll=0omem=0events=rcmd=client

当我们启动主从节点和sentinel节点后,sentinel.conf配置文件会自动添加或修改参数

[root@sht-sgmhadoopcm-01redis]#catsentinel.confsentinelmyid30393e76e002cb64db92fb8bcb88d79f2d85a82bsentinelconfig-epochmymaster0sentinelleader-epochmymaster0#GeneratedbyCONFIGREWRITEsentinelknown-slavemymaster172.16.101.606379sentinelknown-slavemymaster172.16.101.596379sentinelknown-sentinelmymaster172.16.101.5526379ab45fe6c0f010473ce3b7b4d2120e1a83776b736sentinelknown-sentinelmymaster172.16.101.5626379e32f20c0f315e712c9921371f15729246f3816a0sentinelcurrent-epoch0[root@sht-sgmhadoopnn-01redis]#catsentinel.confsentinelmyidab45fe6c0f010473ce3b7b4d2120e1a83776b736sentinelconfig-epochmymaster0sentinelleader-epochmymaster0#GeneratedbyCONFIGREWRITEsentinelknown-slavemymaster172.16.101.606379sentinelknown-slavemymaster172.16.101.596379sentinelknown-sentinelmymaster172.16.101.5626379e32f20c0f315e712c9921371f15729246f3816a0sentinelknown-sentinelmymaster172.16.101.542637930393e76e002cb64db92fb8bcb88d79f2d85a82bsentinelcurrent-epoch0[root@sht-sgmhadoopnn-02redis]#catsentinel.confsentinelmyide32f20c0f315e712c9921371f15729246f3816a0sentinelconfig-epochmymaster0sentinelleader-epochmymaster0#GeneratedbyCONFIGREWRITEsentinelknown-slavemymaster172.16.101.606379sentinelknown-slavemymaster172.16.101.596379sentinelknown-sentinelmymaster172.16.101.542637930393e76e002cb64db92fb8bcb88d79f2d85a82bsentinelknown-sentinelmymaster172.16.101.5526379ab45fe6c0f010473ce3b7b4d2120e1a83776b736sentinelcurrent-epoch0

测试自动failover

[root@sht-sgmhadoopdn-01redis]#ps-ef|grepredisroot151281021:17?00:00:05src/redis-server172.16.101.58:6379[root@sht-sgmhadoopdn-01redis]#kill-915128[root@sht-sgmhadoopcm-01redis]#tail-fsentinel.log7541:X05Aug22:55:48.052#+sdownmastermymaster172.16.101.586379#sentinel主观认为mastercrash;7541:X05Aug22:55:48.143#+odownmastermymaster172.16.101.586379#quorum2/2#只要有两个sentinel节点认为mastercrash,则客观认为mastercrash7541:X05Aug22:55:48.143#+new-epoch17541:X05Aug22:55:48.143#+try-failovermastermymaster172.16.101.5863797541:X05Aug22:55:48.165#+vote-for-leader30393e76e002cb64db92fb8bcb88d79f2d85a82b17541:X05Aug22:55:48.166#ab45fe6c0f010473ce3b7b4d2120e1a83776b736votedforab45fe6c0f010473ce3b7b4d2120e1a83776b73617541:X05Aug22:55:48.173#e32f20c0f315e712c9921371f15729246f3816a0votedforab45fe6c0f010473ce3b7b4d2120e1a83776b73617541:X05Aug22:55:48.544#+config-update-fromsentinelab45fe6c0f010473ce3b7b4d2120e1a83776b736172.16.101.5526379@mymaster172.16.101.5863797541:X05Aug22:55:48.544#+switch-mastermymaster172.16.101.586379172.16.101.6063797541:X05Aug22:55:48.545*+slaveslave172.16.101.59:6379172.16.101.596379@mymaster172.16.101.6063797541:X05Aug22:55:48.545*+slaveslave172.16.101.58:6379172.16.101.586379@mymaster172.16.101.606379#从这一步到下一步执行failover成功之间需要等待30s,这是由于参数sentineldown-after-millisecondsmymaster控制,master30s之内没有响应sentinel才会真正的failover;7541:X05Aug22:56:18.562#+sdownslave172.16.101.58:6379172.16.101.586379@mymaster172.16.101.606379

master和slave发生了变化,IP60成为新的master,IP58成为slave

[root@sht-sgmhadoopcm-01redis]#src/redis-cli-h172.16.101.54-p26379172.16.101.54:26379>sentinelmasters1)1)"name"2)"mymaster"3)"ip"4)"172.16.101.60"......172.16.101.54:26379>sentinelslavesmymaster1)1)"name"2)"172.16.101.58:6379"3)"ip"4)"172.16.101.58"9)"flags"10)"s_down,slave,disconnected"2)1)"name"2)"172.16.101.59:6379"3)"ip"4)"172.16.101.59"9)"flags"10)"slave"

重启修复好的旧master之后,会自动成为新master的从库

[root@sht-sgmhadoopdn-01redis]#src/redis-serverredis.conf[root@sht-sgmhadoopcm-01redis]#tail-fsentinel.log7541:X05Aug23:11:10.556#-sdownslave172.16.101.58:6379172.16.101.586379@mymaster172.16.101.6063797541:X05Aug23:11:20.518*+convert-to-slaveslave172.16.101.58:6379172.16.101.586379@mymaster172.16.101.606379

Failover过程分析:

Each Sentinel detects the master is down with an +sdown event.

This event is later escalated to +odown, which means that multiple Sentinels agree about the fact the master is not reachable.

Sentinels vote a Sentinel that will start the first failover attempt.

The failover happens.

sentinel节点会定期通过ping检测redis的master是否存活,一旦master crash,

首先sentinel自己会主观认为master crash,然后三个sentinel之间彼此通信,只要有两个sentinel节点认为master crash,则客观认为master crash,

接着三个sentinel节点会投票,得到两票的一个sentinel会去执行failover,

最后master 30s之内没有响应sentinel才会真正的failover;

一旦挂掉的旧master修复,重新启动后,会作为新master的从库存在;

FAQ

Error1:

[root@sht-sgmhadoopcm-01redis]#src/redis-cli-h172.16.101.54-p26379172.16.101.54:26379>ping(error)DENIEDRedisisrunninginprotectedmodebecauseprotectedmodeisenabled,nobindaddresswasspecified,noauthenticationpasswordisrequestedtoclients.Inthismodeconnectionsareonlyacceptedfromtheloopbackinterface.IfyouwanttoconnectfromexternalcomputerstoRedisyoumayadoptoneofthefollowingsolutions:1)Justdisableprotectedmodesendingthecommand'CONFIGSETprotected-modeno'fromtheloopbackinterfacebyconnectingtoRedisfromthesamehosttheserverisrunning,howeverMAKESURERedisisnotpubliclyaccessiblefrominternetifyoudoso.UseCONFIGREWRITEtomakethischangepermanent.2)AlternativelyyoucanjustdisabletheprotectedmodebyeditingtheRedisconfigurationfile,andsettingtheprotectedmodeoptionto'no',andthenrestartingtheserver.3)Ifyoustartedtheservermanuallyjustfortesting,restartitwiththe'--protected-modeno'option.4)Setupabindaddressoranauthenticationpassword.NOTE:Youonlyneedtodooneoftheabovethingsinorderfortheservertostartacceptingconnectionsfromtheoutside.解决方法:[root@sht-sgmhadoopcm-01redis]#vimsentinel.confprotected-modeno

看完了这篇文章,相信你对“Redis Sentinel安装配置的示例分析”有了一定的了解,如果想了解更多相关知识,欢迎关注亿速云行业资讯频道,感谢各位的阅读!