荣耀彩票代理

IT技术互动交流平台

架构设计:系统存储(17)Redis集群方案:高可用

来源:IT165收集  发布日期:2016-12-22 20:36:19

1、概述

CONGBENPIANWENZHANGKAISHI,WOMENJIANGXIANGDUZHEJIESHAOJIZHONGRedisDEGAOKEYONGGAOFUZAIJIQUNFANGAN。CHULEJIESHAORedis 3.XBANBENZHONGTUIJIANDEYUANSHENGJIQUNFANGANWAI,HAIHUIJIESHAOSHIYONGDISANFANGZUJIANDAJIANRedisJIQUNDEFANGFA。BENWENWOMENHUISHOUXIANJIESHAORedisDEGAOKEYONGJIQUNFANGAN。

2、Redis高可用方案

Redis提供的高可用方案和我们介绍过的很多软件的高可用方案类似,都是使用主从节点的思路。即是有一个Master节点在平时提供服务,另外一个或多个Slave节点在平时不提供服务(或只提供数据读取服务)。当Master节点由于某些原因停止服务后,再人工/自动完成Slave节点到Master节点的切换工作,以便整个Redis集群继续向外提供服务。既然是要进行角色切换,且要求这些节点针对外部调用者来说没有任何不同,最重要的就是Master节点和Slave节点的数据同步过程。数据同步最关键的设计思路是如何在数据一致性和同步性能上找到一个完美的平衡点

同步复制的工作思路可以概括为:Master节点的任何数据变化都会立即同步到一个或多个Slave节点上。只要一个Slave节点同步失败(例如超时),都会认为整个数据写操作过程失败。这样的设计考虑侧重于保证各节点上的数据绝对一致,完全没有考虑对Master节点的响应性能,甚至会出现Master节点为了保证数据一致性而停止对后续写操作请求的响应

异步复制的工作思路可以概括为:Master节点首先保证对外部请求的响应性能,它和Slave节点的数据同步一般由一个新的进程/线程独立完成。数据复制过程由Slave节点周期性发起或者由它一直驻留在Master节点的连接进行实时监控又或者由Master节点主动推送数据,再或者是同时使用多个异步复制过程。由于在Slave节点进行数据同步时,Master节点一直在处理新的数据写请求,所以Slave节点已完成同步的数据和Master上的实时数据一般会存在一些差异荣耀彩票代理。例如MySQL原生支持的数据复制过程,就是一个异步过程。

很显然异步复制思路在对调用者的响应性能上,表现要比同步复制好得多。但如果由于异步复制而导致的节点间数据差异达到某种程度,就失去了数据同步的意义了。所以如何减少节点间的数据差异就成为异步复制过程中需要关注的要点。而后者的处理办法就有很多了,例如MySQL由第三方插件支持的半同步方式,又例如讲解ActiveMQ消息队列时提到的AutoAck和DUPS_OK_ACK,再例如我们下文介绍的Diskless Replication和Master写保护。

2-1、主从复制工作过程

RedisDEZHUCONGFUZHIGONGNENGCHULEZHICHIYIGEMasterJIEDIANDUIYINGDUOGESlaveJIEDIANDETONGSHIJINXINGFUZHIWAI,HAIZHICHISlaveJIEDIANXIANGQITADUOGESlaveJIEDIANJINXINGFUZHI。ZHEIYANGJIUSHIDEJIAGOUSHINENGGOULINGHUOZUZHIYEWUHUANCUNSHUJUDECHUANBO,LIRUSHIYONGDUOGESlaveZUOWEISHUJUDUQUFUWUDETONGSHI,ZHUANMENSHIYONGYIGESlaveJIEDIANWEILIUSHIFENXIGONGJUFUWU。RedisDEZHUCONGFUZHIGONGNENGFENWEILIANGZHONGSHUJUTONGBUMOSHI:QUANLIANGSHUJUTONGBUHEZENGLIANGSHUJUTONGBU。

这里写图片描述

上图简要说明了Redis众喎?http://chsp8.com/pro/pkqt/" target="_blank" class="keylink">QTWFzdGVyvdq147W9U2xhdmW92rXjtcTIq8G/yv2+3c2ssr25/bPMoaO1sVNsYXZlvdq147j4tqi1xHJ1bl9pZLrNTWFzdGVytcRydW5faWSyu9K71sLKsaOsu/LV31NsYXZluPi2qLXEyc/Su7TO1PbBv82ssr21xG9mZnNldLXEzrvWw9TaTWFzdGVytcS7t9DOxNq05tbQzt63qLaozrvKsaOouvPOxLvhzOG1vaOpo6xNYXN0ZXK+zbvhttRTbGF2ZbeixvDIq8G/zayyvbLZ1/eho9XiyrE8c3Ryb25nPs7ewtvE+srHt/HU2k1hc3RlcrTyv6rBy1JEQr/s1dW5psTco6zL/LrNU2xhdmW92rXjtcTDv9K7tM7Iq8G/zayyvbLZ1/e5/bPMtry74bj80MIvtLS9qE1hc3RlcsnPtcRSRELOxLz+PC9zdHJvbmc+oaPU2lNsYXZlway907W9TWFzdGVyo6yyos3qs8m12tK7tM7Iq8G/yv2+3c2ssr2686OsvdPPwsC0TWFzdGVytb1TbGF2ZbXEyv2+3c2ssr25/bPM0ruw477NysfU9sG/zayyvdDOyr3By6Oo0rKzxs6qsr+31s2ssr2jqaGj1PbBv82ssr25/bPMsrvU2db30qrSwMC1UkRCzsS8/qOsTWFzdGVyu+G9q9DCsvrJ+rXEyv2+3bHku6+y2df3tOa3xdTa0ru49sTatObH+NPyo6zV4rj2xNq05sf40/KyydPDu7fQzrm51Oyho7n9s8zI58/Co7o8L3A+CjxwPjxpbWcgYWx0PQ=="这里写图片描述" src="http://chsp8.com/uploadfile/files/2016/1222/20161222193742342.png" title="" />

WEISHENMEZAIMasterSHANGXINZENGDESHUJUCHULEGENJUMasterJIEDIANSHANGRDBHUOZHEAOFDESHEZHIJINXINGRIZHIWENJIANGENGXINWAI,HAIHUITONGSHIJIANGSHUJUBIANHUAXIERUYIGEHUANXINGNEICUNJIEGOU,BINGYIHOUZHEWEIYIJUJINXINGSlaveJIEDIANDEZENGLIANGGENGXINNI?ZHUYAOYUANYINYOUYIXIAJIGE:

YOUYUWANGLUOHUANJINGDEBUWENDING,WANGLUODOUDONG/YANCHIDOUKENENGZAOCHENGSlaveHEMasterZANSHIDUANKAILIANJIE,ZHEIZHONGQINGKUANGYAOYUANYUANDUOYUXINDESlaveLIANJIEDAOMasterDEQINGKUANG。RUGUOYISHANGSUOYOUQINGKUANGDOUSHIYONGQUANLIANGGENGXIN,JIUHUIDADAZENGJIAMasterDEFUZAIYALI——XIERDBWENJIANSHIYOUDALIANGI/OGUOCHENGDE,SUIRANLinux Page CaheTEXINGHUIJIANSHAOXINGNENGXIAOHAO。

LINGWAIZAISHUJULIANGDADAOYIDINGGUIMODEQINGKUANGXIA,SHIYONGQUANLIANGGENGXINJINXINGHESlaveDEDIYICITONGBUSHIYIGEBUDEYIDEXUANZE——YINWEIYAOJINKUAIJIANSHAOSlaveJIEDIANHEMasterJIEDIANDESHUJUCHAYI。SUOYIZHINENGZHANYONGMasterJIEDIANDEZIYUANHEWANGLUODAIKUANZIYUAN。

SHIYONGNEICUNJILUSHUJUZENGLIANGCAOZUO,KEYIYOUXIAOJIANSHAOMasterJIEDIANZAIZHEIFANGMIANFUCHUDEI/ODAIJIA。ERZUOCHENGHUANXINGNEICUNDEYUANYIN,SHIWEILEBAOZHENGZAIMANZUSHUJUJILUXUQIUDEQINGKUANGXIAJINKENENGJIANSHAONEICUNDEZHANYONGLIANG。ZHEIGEHUANXINGNEICUNDEDAXIAO,KEYITONGGUOrepl-backlog-sizeCANSHUJINXINGSHEZHI。

SlaveZHONGLIANHOUHUIXIANGMasterFASONGZHIQIANJIESHOUDAODEMaster run_idXINXIHESHANGYICIWANCHENGBUFENTONGBUDEoffsetDEWEIZHIXINXI。RUGUOMasterNENGGOUQUEDINGZHEIGErun_idHEZIJIDErun_idYIZHIQIENENGGOUZAIHUANXINGNEICUNZHONGZHAODAOZHEIGEoffsetDEWEIZHI,MasterJIUHUIFASONGCONGoffsetDEWEIZHIKAISHIXIANGSlaveFASONGZENGLIANGSHUJU。NEIMELIANJIEZHENGCHANGDEGEGESlaveJIEDIANRUHEJIESHOUXINSHUJUNI?LIANJIEZHENGCHANGDESlaveJIEDIANJIANGHUIZAIMasterJIEDIANJIANGSHUJUXIERUHUANXINGNEICUNHOU,ZHUDONGJIESHOUDAOLAIZIMasterDESHUJUFUZHIXINXI。

2-2、基本Master/Slave配置

RedisTIGONGDEZHUCONGFUZHIGONGNENGDEPEIZHIXINXI,ZAIRedisZHUPEIZHIWENJIANDE“REPLICATION”BUFEN。YIXIASHIZHEIGEBUFENDEZHUYAOCANSHUXIANGSHUOMING:

slaveof <masterip> <masterport>:RUGUONINXUYAOJIANGMOUGEJIEDIANSHEZHIWEIMOUGEMasterJIEDIANDESlaveJIEDIAN,NINXUYAOZAIZHEILIZHIDINGMasterJIEDIANDEIPXINXIHEDUANKOUXINXI。ZHEIGESHEZHIXIANGMORENSHIGUANBIDE,YEJISHISHUOMasterJIEDIANBUXUYAOSHEZHIZHEIGECANSHU。LINGWAI,CHULETONGGUOPEIZHIWENJIANSHEZHIWAI,NINHAIKEYITONGGUORedisDEKEHUDUANMINGLINGJINXINGslaveofSHEDING。

荣耀彩票代理slave-serve-stale-data:DANGmasterJIEDIANDUANKAIHEDANGQIANsalveJIEDIANDELIANJIEHUOZHEDANGQIANslaveJIEDIANZHENGZAIJINXINGHEmasterJIEDIANDESHUJUTONGBUSHI,RUGUOSHOUDAOLEKEHUDUANDESHUJUDUQUQINGQIU,slaveFUWUQISHIFOUSHIYONGCHENJIUSHUJUXIANGKEHUDUANTIGONGFUWU。GAICANSHUDEMORENZHIWEIyes。

荣耀彩票代理slave-read-only SHIFOUJIANGsalveJIEDIANSHEZHIWEI“ZHIDU”。YIDANSHEZHIWEI“ZHIDU”,BIAOSHIZHEIGESalveJIEDIANZHIHUIJINXINGSHUJUDUQUFUWU,RUGUOKEHUDUANZHIJIEXIANGZHEIGESalveJIEDIANFASONGXIESHUJUDEQINGQIU,ZEHUISHOUDAOCUOWUTISHI。JIANYICAIYONGMORENDE“yes”ZHIJINXINGSHEDING。

repl-diskless-sync:上文已经介绍过Redis的主从复制功能基于RDB,后者的过程是将数据刷入RDB文件(实际上是Linux的Page Cache区域),然后基于RDB文件内容的更新情况和Salve当前已同步的数据标记点来进行Salve上的数据更新。所以这个过程实际会增加一定的数据延迟,消耗一定的处理资源。基于这个情况,Redis中提供了一种不经过物理磁盘设备就进行主从数据同步的技术,称为diskless。但是直到Redis version 3.2这个技术也一直处于试验状态,所以并不推荐在生产环境下使用:“
WARNING: DISKLESS REPLICATION IS EXPERIMENTAL CURRENTLY”。

repl-diskless-sync-delay:ZHEIGECANSHUZHIYOUZAISHANGYIGECANSHUSHEZHIWEI“yes”SHICAIQIZUOYONG,ZHUYAOSHISHEZHIZAIJINXINGLIANGCIdisklessMOSHIDESHUJUTONGBUCAOZUODESHIJIANJIANGE。MORENWEI5MIAO。

荣耀彩票代理repl-ping-slave-period:SlaveJIEDIANXIANGMasterJIEDIANFASONGpingZHILINGDESHIJIANJIANGE,MORENWEI10MIAO。

repl-timeout:ZHEISHIYIGECHAOSHIJIAN,DANGMOUXIECAOZUODADAOZHEIGESHIJIANSHI,MasterHESlaveSHUANGFANGDOUHUIRENWEIDUIFANGYIJINGDUANKAILIANJIE。SHIJISHANGNINKEYIJIANGZHEIGESHIJIANKANCHENGSHIYIGEZUYUEDAOQIDESHIJIAN。NEIMEZHEIGECAOZUOSHIJIANHUIYINGXIANGNAXIECAOZUONI?A、XIANGSlaveJINXINGDESHUJUTONGBUCAOZUOBENSHENBUNENGCHAOGUOZHEIGESHIJIAN;B、SlaveXIANGMasterFASONGYIGEPINGZHILINGBINGDENGDAIXIANGYINGDESHIJIAN;C、MasterXIANGSlaveFASONGPONGHUIFUBINGDENGDAIACKDESHIJIAN。

repl-disable-tcp-nodelay:ZHEIGEXUANXIANGDEMORENZHIWEIno,TADUIYOUHUAZHUCONGFUZHISHISHIYONGDEWANGLUOZIYUANFEICHANGYOUYONG。YAOMINGBAIZHEIGECANSHUDEHANYI,JIUSHOUXIANYAOJIESHIYIXIAtcp-nodelaySHIGESHENMEWANYIER?TCPSHUJUBAODEBAOWENTOUBAOHANHENDUOSHUXING,ZHEIXIESHUXINGJIBENSHANGQIDAOJILUHEBAOZHENGCHUANSHUMUDE、CHUANSHUZHUANGTAIDEZUOYONG,DANMEIYOUSHUJUBAODESUOXIEDAIDEYEWUSHUJU(CHENGZHIWEIYOUXIAOZAIHE)。NEIMEHENMINGXIAN,20GEZIJIENEIRONGDEXINXIFENCHENG20GESHUJUBAOJINXINGCHUANSHUHEZHIYONGYIGESHUJUBAOJINXINGCHUANSHU,XUYAOZHANYONGDEWANGLUOZIYUANJIUWANQUANBUYIYANG。JohnNagleZAI1984NIANFAMINGLEYIZHONGJIANQINGWANGLUOCHUANSHUYALIDESUANFA,JIUSHIWEILEJIEJUEZHEIGEWENTI(SUANFADEMINGZIJIUJIAOZUO“Nagle”,HOUXUDEJISHURENYUANYOUZUOLEHENDUOGAIJINHESHENGJI)。QIJIBENSILUJIUSHIJIANGYAOFASONGDENEIRONGCOUGOUYIDINGDESHULIANGHOU,ZAIYONGYIGESHUJUBAOFASONGCHUQU。RUGUOGAISHUXINGSHEZHIWEIyes,RedisJIANGSHIYONG“Nagle”SUANFA(HUOLEISISUANFA),RANGSHUJUBAOZHONGDEYOUXIAOZAIHECOUGOUYIDINGSHULIANGHOU,ZAIFASONGCHUQU;SHEZHICHENGno,RedisJIUBUHUIZHEIMEZUO。

荣耀彩票代理repl-backlog-size:SHANGWENYIJINGJIESHAOGUOLERedisZHONGWEILEJINXINGZENGLIANGTONGBUSUOZHUNBEIDEHUANXINGNEICUNQUYU,YIJIRedisZHEIYANGZUODEYUANYINE,SUOYIZHEILIJIUBUZAIZHUISHULE。ZHEIGEXUANXIANGJIUSHIYONGLAISHEZHIHUANXINGNEICUNDEDAXIAODE,ZHEIGEXUANXIANGDEMORENZHIWEI1MB;ZHENGSHIDESHENGCHANHUANJINGXIAKEYISHAOWEIJIADAYIXIE,LIRU5MB。

slave-priority:DANGQIANSlaveJIEDIANDEYOUXIANJIQUANZHONG。WOMENHOUWENHUIJIESHAOYIKUANRedisZIDAIDEJIANKONGHEGUZHANGZHUANYIGONGJU:Redis Sentinel,ZHEIGEGONGJUYUNXUYIGEMasterJIEDIANXIAYOUDUOGESlaveJIEDIAN,BINGQIEKEYIZIDONGQIEHUANSlaveJIEDIANWEIMasterJIEDIAN。RUGUOSlaveJIEDIANDEYOUXIANJIQUANZHONGZHIYUEDI,JIUHUIZAIQIEHUANSHIYOUXIANCHENGWEIXINDEMasterJIEDIAN。

荣耀彩票代理min-slaves-to-writeHEmin-slaves-max-lag:WEILEJINKENENGBIMIANMasterJIEDIANDUIYINGDEDUOGESlaveJIEDIANZAISHUJUFUZHIGUOCHENGZHONGSHUJUCHAYIBEIYUELAYUEDA。RedisFUWUTIGONGLEYIZUJUJUESHUJUXIECAOZUODECELVE,ZHEIGECELVEKEYIJIESHIWEI:DANGMasterSHANGZAImin-slaves-max-lagSHIJIAN(DANWEIMIAO)JIANGEHOU,RENRANYOUmin-slaves-to-writeGESlaveHETAZHENGCHANGLIANJIE,NEIMEMasterCAIYUNXUJINXINGSHUJUXIECAOZUO。

2-3、Master和Slave设置实例

TAOLUNLERedisZHONGZHUCONGFUZHIDEJIBENYUANLIHERedisZHUPEIZHIWENJIANZHONGZHENDUIZHUCONGFUZHIDESHEDINGXUANXIANGYIYIHOU,WOMENLAIKANYIGESHIJISHEZHIGUOCHENG。ZHUYI,YOUYUZHEIGEGUOCHENGFEICHANGJIANDANSUOYIWOMENHUI“FEICHANGKUAI”。SHOUXIANMasterFUWUQIBUXUYAOZHENDUIZHUCONGFUZHIZUORENHEDESHEZHI(ZHEIBUBAOKUODUIZHUCONGFUZHIGUOCHENGDEPEIZHIYOUHUA)。SUOYIWOMENJIUZHIJIELAIKANSlaveJIEDIANDEPEIZHI:

Slave节点上我们只需要做一件事情,就是打开slaveof选项:
......
# slaveof选项的设置,给定master节点的ip和port就可以了
# 192.168.61.140就是master节点
slaveof 192.168.61.140 6379
......
接着,我们马上就可以看看同步效果了。首先确保您的master节点使工作正常的,然后就可以启动Slave节点了:
......
5349:S 17 Dec 04:20:00.773 * Connecting to MASTER 192.168.61.140:6379
5349:S 17 Dec 04:20:00.773 * MASTER <-> SLAVE sync started
5349:S 17 Dec 04:20:00.774 * Non blocking connect for SYNC fired the event.
5349:S 17 Dec 04:20:00.775 * Master replied to PING, replication can continue...
5349:S 17 Dec 04:20:00.776 * Partial resynchronization not possible (no cached master)
5349:S 17 Dec 04:20:00.782 * Full resync from master: 976f0b31cbf6acd4fcc888301ea4639a7c591136:1
5349:S 17 Dec 04:20:00.864 * MASTER <-> SLAVE sync: receiving 119 bytes from master
5349:S 17 Dec 04:20:00.865 * MASTER <-> SLAVE sync: Flushing old data
5349:S 17 Dec 04:20:00.865 * MASTER <-> SLAVE sync: Loading DB in memory
5349:S 17 Dec 04:20:00.865 * MASTER <-> SLAVE sync: Finished with success
5349:S 17 Dec 04:20:01.068 * Background append only file rewriting started by pid 5352
5349:S 17 Dec 04:20:01.082 * AOF rewrite child asks to stop sending diffs.
5352:C 17 Dec 04:20:01.082 * Parent agreed to stop sending diffs. Finalizing AOF...
5352:C 17 Dec 04:20:01.082 * Concatenating 0.00 MB of AOF diff received from parent.
5352:C 17 Dec 04:20:01.082 * SYNC append only file rewrite performed
5352:C 17 Dec 04:20:01.082 * AOF rewrite: 6 MB of memory used by copy-on-write
5349:S 17 Dec 04:20:01.168 * Background AOF rewrite terminated with success
5349:S 17 Dec 04:20:01.168 * Residual parent diff successfully flushed to the rewritten AOF (0.00 MB)
5349:S 17 Dec 04:20:01.168 * Background AOF rewrite finished successfully
......

荣耀彩票代理BIZHEZAISlaveJIEDIANSHANGKAIQILEDINGQIDERDBKUAIZHAOHEAOFRIZHIGONGNENG,SUOYIGEWEIDUZHEKEYIHULVENEIXIERIZHIXINXI,ZHIJIEGUANZHU“Connecting to MASTER ….”HE“MASTER <-> SLAVE …….”ZHEIXIERIZHIXINXIJIUHAO。

以下是Master节点上给出的日志信息
......
5614:M 17 Dec 04:20:00.789 * Slave 192.168.61.145:6379 asks for synchronization
5614:M 17 Dec 04:20:00.789 * Full resync requested by slave 192.168.61.145:6379
5614:M 17 Dec 04:20:00.789 * Starting BGSAVE for SYNC with target: disk
5614:M 17 Dec 04:20:00.791 * Background saving started by pid 5620
5620:C 17 Dec 04:20:00.814 * DB saved on disk
5620:C 17 Dec 04:20:00.815 * RDB: 6 MB of memory used by copy-on-write
5614:M 17 Dec 04:20:00.875 * Background saving terminated with success
5614:M 17 Dec 04:20:00.877 * Synchronization with slave 192.168.61.145:6379 succeeded
......

KANLAIMasterJIEDIANSHOUDAOLESlaveJIEDIANDELIANJIEXINXI,BINGWANCHENGLEQUANLIANGSHUJUTONGBUCAOZUO。

2-4、关闭RDB功能的说明

荣耀彩票代理YISHANGJIESHAODEMasterJIEDIANHESlaveJIEDIANDESHEZHISHIFOUTEBIEJIANDAN?SHIDE,SHIJISHANGZHIXUYAODAKAILESlaveJIEDIANSHANG“REPLICATION”QUYUDEslaveofXUANXIANGJIUKEYIRANGRedisDEZHUCONGFUZHIGONGNENGYUNZUOQILAI。XIANZAIWOMENWANGHUIDAO,HUIDAOSHANGYIPIANWENZHANGDEJIESHAO。ZAISHANGYIPIANWENZHANGJIESHAORDBKUAIZHAOGONGNENGDEPEIZHIXIANGSHI,WENZHANGTIDAOLEKEYIYONGYIXIAFANGSHIGUANBIRDBKUAIZHAOGONGNENG:

# 以下为默认的设置为,注释掉即可
# save 900 1
# save 300 10
# save 60 10000
# 在设置以下选项,就可以关闭RDB功能
save ''

但是根据本文对Redis主从复制的介绍,我们可以发现Redis的RDB快照功能实际上是无法真正关闭的!以上所谓关闭RDB功能的设置,只是关闭了Redis服务在正常工作时定期快照的条件设定,但只要有Slave节点请求全量数据同步,Master节点就会强制做一次RDB快照。并且如果客户端主动发送BGSAVE命令,要求Redis服务进行RDB快照时,Redis也会被动执行RDB快照操作。

但是本文还是建议在组建Redis高可用集群时,关闭Master节点上的RDB功能荣耀彩票代理。读者一定要清楚这样做的原因:这不是为了像个别网络资料说的那样真正关闭Redis的RDB快照功能,而是尽可能减少Master上主动进行RDB操作的次数,并将RDB快照工作转移到各个Slave节点完成。

3、Redis Sentinel

RedisFUWUTIGONGLEXINGNENGJIAOGAODEZHUCONGFUZHIGONGNENG,DANSHIMEIYOUTIGONGYUANSHENGDEMaster——SlaveDEQIEHUANGONGNENG。YEJIUSHISHUORUGUONINZHISHIPEIZHILERedisDEZHUCONGFUZHIGONGNENG,NEIMEZAIMasterJIEDIANCHUXIANGUZHANGSHI,NINBIXUSHOUDONGJIANGYITAISlaveZHUANGTAIDEJIEDIANQIEHUANWEIMasterZHUANGTAI。DANGRANZHEIGEWENTIZAIRedis Version 2.8 BANBENQIANSHIYOUBIAOZHUNJIEJUEFANGANDE,NEIJIUSHI:Keepalived + RedisFUWUZUCHENGDEGAOKEYONGJIQUN。

这里写图片描述

由Keepalived监控Redis高可用集群众喎?http://chsp8.com/pro/pkqt/" target="_blank" class="keylink">QTWFzdGVyvdq147XEuaTX99e0zKyjrLKi1NrS7LOjx+m/9s/Cx9C7u8Ht0ru49r3ateO908zmuaTX96GjtavKx6Os1eK49re9sLjKx9PQ0rvQqc7KzOLBy6OsxuTW0Nau0ru+zcrHy/nT0LXEU2xhdmW92rXj1NpTdGFuZGJ517TMrMqxzt63qLfWtaNNYXN0ZXK92rXjtcTIzrrO0NTE3NG5waYmbWRhc2g7Jm1kYXNoO7y0yrnE+sno1sPBy3JlYWQtb25sebXIss7K/dKysrvQ0KOs0vLOqlZJULj5sb6yu7vhsNHH68fzx9C5/ciloaOyosfS1eLW1re9yr27ubK7zKu3vbHjvOC/2FJlZGlzuN+/ydPDvK/IutbQuPe49rf+zvG92rXjtcTKtcqx17TMrKGjPC9wPgo8cD6001ZlcnNpb24gMi44sOaxvr+qyryjrFJlZGlzzOG5qcHL0ru49tStyfq1xNb3tNPXtMysvOC/2LrNx9C7u7XE1+m8/iZtZGFzaDsmbWRhc2g7UmVkaXMgU2VudGluZWyho82ouf3L/Ly8yvXIy9Sxsru1q7/J0tTN6rPJUmVkaXO437/J08O8r8i6tcTKysqxvOC/2KOsu7m/ydLUzai5/bHgs8zK1rbOvPXH4byvyLrW0E1hc3Rlcr3atePW0LbBstnX97XE0bnBpqGjsb692sTayN2jrM7Sw8fP8rbB1d+96cnc1eK49lJlZGlzIFNlbnRpbmVstcS88rWlyrnTw6GjPC9wPgo8aDQgaWQ9"3-1基本配置">3-1、基本配置

YOUYURedis SentinelSHIRedisYUANSHENGZHICHIDE,YIRedis Version 3.2WEILI,ZAIXIAZAIANZHUANGHOUJIUKEYIZHIJIESHIYONGMINGLING“redis-sentinel”QIDONGSentinelLE。SentinelDEZHUPEIZHIWENJIANMOBANCUNFANGZAIRedisANZHUANGMULUDEXIA,MORENMINGWEI“sentinel.conf”。YIXIAMINGLINGKEYIQIDONGSentinel(QIDONGSentinelSUOYIJUDEPEIZHIWENJIANSHIYIDINGYAOXIEDAIDECANSHU):

# redis-sentinel ./sentinel.conf

Redis SentinelBENSHENYEZHICHIJIQUNBUSHU,ERQIEWEILEZAISHENGCHANHUANJINGXIABIMIANSentinelDANDIANGUZHANG,SUOYIYEJIANYITONGSHIBUSHUDUOGESentinelJIEDIAN。BUSHUDUOGESentinelHAIYOUYIGEYUANYIN,JIUSHITIGAOMaster——SlaveQIEHUANDEZHUNQUEXING。YIXIADEPEIZHIWENJIANJIESHAOHUISHUOMINGZHEIYIDIAN。

荣耀彩票代理XIAMIANWOMENJIESHAOYIXIESentinelZHUPEIZHIWENJIANZHONGDEGUANJIANPEIZHI,ZHUYISentinelZHUPEIZHIWENJIANYEYOULEISIRedisZHUPEIZHIWENJIANTIGONGDEFANGWENBAOHUMOSHI(protected-mode)、FANGWENZHEQUANXIANKONGZHI(auth-pass)DENG,DANSHITAMENDEYIYIJIBENSHANGLEISIQIANWENJIESHAOGUODE,ZAIRedisZHUPEIZHIWENJIANZHONGDEXIANGSINEIRONG,SUOYIZHEILIJIUBUZAIZHUISHULE。

sentinel monitor <master-name> <ip> <redis-port> <quorum>:

ZHEIGESHUXINGSHIRedis SentinelZHONGDEZUIZHUYAOSHEZHIYUANSU,HUANJUHUASHUORUGUOYAOKAIQISentinelSHENZHIKEYIZHISHEZHIZHEIGESHUXING。TABAOKUOLESIGECANSHU:master-name,ZHEIGECANSHUSHIYIGEYINGWENMINGSHUOMINGLESentinelFUWUJIANTINGDEMasterJIEDIANDEBIEMING,RUGUOYIGESentinelFUWUXUYAOTONGSHIJIANKONGDUOGEMaster,ZHEIXUYAOSHEZHIDUOGEBUTONGDEmaster-name;ipHEredis-port,ZHIXIANGsentinelXUYAOJIANKONGDERedisJIQUNZUICHUDENEIGEMasterJIEDIAN(WEISHENMEHUISHIZUICHUNI?HOUWENHUISHUOMING)DEipHEDUANKOU;quorum,TOUPIAOSHULIANGZHEIGECANSHUHENZHONGYAO,RUGUOSHISentinelJIQUNFANGSHIXIA,TASHEDING“DANGquorumGESentinelRENWEIMasterYICHANGLE,JIUPANDINGGAIMasterZHENDEYICHANGLE”。DANGESentinelJIEDIANRENWEIMasterXIAXIANLEBEICHENGWEIZHUGUANXIAXIAN,ERquorumGESentinelJIEDIANDOURENWEIMasterXIAXIANDEQINGKUANGBEICHENGWEIKEGUANXIAXIAN。

sentinel parallel-syncs <master-name> <numslaves>:

YIDANYUANLAIDEMasterJIEDIANBEIRENWEIKEGUANXIAXIANLE,SentinelJIUHUIQIDONGQIEHUANGUOCHENG。DAZHILAIJIANGJIUSHICONGDANGQIANSUOYOUSlaveJIEDIANXUANZEYIGEJIEDIANCHENGWEIXINDEMasterJIEDIAN(ZHEISHIZAIRedisZHONGSHEDINGDEslave-priorityCANSHUJIUHUIQIZUOYONGLE)。ERQITADESlaveQIslaveofDEMasterXINXIJIANGBEIsentinelQIEHUANDAOXINDEMasterSHANG。ERYICITONGSHIBINGXINGQIEHUANDUOSHAOGESlaveDAOXINDEMasterSHANGJIUSHIZHEIGECANSHUJUEDINGDE。RUGUOZHENGGERedisGAOKEYONGJIQUNDEJIEDIANSHULIANGBUDUO(MEIYOUCHAOGUO6GE),JIANYISHIYONGMORENZHIJIUKEYILE。

ZHUPEIZHIWENJIANZHONGBEIrewriteDECANSHUNEIRONG:sentinel.confWENJIANZHONGDEPEIZHINEIRONGHUISUIZHESentinelDEJIANKONGQINGKUANGFASHENGBIANHUA——YOUSentinelCHENGXUDONGTAIXIERUDAOWENJIANZHONG。LIRUsentinel known-slaveCANSHU、sentinel current-epochCANSHUHEsentinel leader-epochCANSHU。

ZHUYI,ZAISentinelZHONGNINZHIXUYAOPEIZHIZUICHUDEMasterDEJIANKONGWEIZHI,WUXUPEIZHIMasterXIARENHESlaveDEWEIZHI,SentinelHUIZIJISHIBIEDAOZHEIXIEMasterZHIJIEDEHUOZHEJIANJIEDESlave。

3-2、切换效果

荣耀彩票代理JIESHAOWANPEIZHIHOU,WOMENLAIJIANDANKANYIGESentinelGONGZUOHEQIEHUANDELIZI。ZHEIGELIZIZHONGDEYOUYIGEMasterJIEDIANHEYIGESlaveJIEDIAN,DANGMasterJIEDIANCHUXIANGUZHANGSHI,TONGGUOSentinelJIANKONGDAOYICHANGQINGKUANGBINGZIDONGWANCHENGSlaveZHUANGTAIDEQIEHUAN。

SHOUXIANQINGBAOZHENGNINDEMasterJIEDIANHESlaveJIEDIANDOUSHIZHENGCHANGGONGZUODE,ZHEIGEGUOCHENGKEYICANJIANBIZHEZHIQIANWENZHANGDEJIESHAO:

节点地址 节点作用
192.168.61.140 Redis Master
192.168.61.145 Redis Slave
192.168.61.140 Redis Sentinel

ZHEILIJIUBUZAIZHUISHURedis MasterHERedis SlaveDENEIRONGLE,YINWEIZAIBENWENDI2JIEZHONGYIJINGXIANGXIJIESHAOGUO。SHIJISHANGNINZHIXUYAODAKAISlaveJIEDIANDEZHUPEIZHIWENJIAN,BINGZENGJIAslaveofDEPEIZHIXINXI,JIANGQIZHIXIANGMasterDEIPHEDUANKOUJIUKEYILE。YIXIASHISentinelJIEDIANZHUYAOGENGGAIDEPEIZHIXINXI:

......
sentinel monitor mymaster 192.168.61.140 6379 1
......

YOUYUZAICESHIHUANJINGZHONGWOMENZHISHIYONGLEYIGESentinelJIEDIAN,SUOYISHEZHIsentinel monitorPEIZHIXIANGZHONGDEquorumWEI1JIUKEYILE,DAIBIAOYOUYIGESentinelJIEDIANRENWEIMasterBUKEYONGLE,JIUKAIQIGUZHANGZHUANYIGUOCHENG。DANGRANSHENGCHANHUANJINGXIABUJIANYIZHEIYANGSHIYONG。

ZHIHOUWOMENSHIYONGYIXIASentinelDEZHUYAOPEIZHIXINXIQIDONGSentinel:

# redis-sentinel ./sentinel.conf

......
8576:X 19 Dec 00:49:01.085 # Sentinel ID is 5a5eb7b97de060e7ad5f6aa20475a40b3d9fd3e1
8576:X 19 Dec 00:49:01.085 # +monitor master mymaster 192.168.61.140 6379 quorum 1
......

荣耀彩票代理ZHIHOUZHUDONGZHONGZHIYUANLAIMasterDEYUNXINGGUOCHENG(NINKEYIZHIJIESHIYONGkillMINGLING,HUOZHEBADIAOWANGXIAN,YOUSUOXINGZHIJIEGUANJI),LAIGUANCHASlaveJIEDIANHESentinelJIEDIANDERIZHIQINGKUANG:

DANGDUANKAIYUANLAIDEMasterJIEDIANHOU,SlaveJIEDIANJIANGTISHILIANJIESHIXIAOBINGKAISHIZHONGSHI。DANGSentinelKAISHIJINRUGUZHANGZHUANYIBINGWANCHENGHOU,SalveYOUHUIDAYINXIANGYINGDEGUOCHENGXINXI:

......
8177:S 19 Dec 00:53:17.467 * Connecting to MASTER 192.168.61.140:6379
8177:S 19 Dec 00:53:17.468 * MASTER <-> SLAVE sync started
8177:S 19 Dec 00:53:17.468 # Error condition on socket for SYNC: Connection refused
......
8177:M 19 Dec 00:53:18.134 * Discarding previously cached master state.
8177:M 19 Dec 00:53:18.134 * MASTER MODE enabled (user request from 'id=3 addr=192.168.61.140:51827 fd=5 name=sentinel-5a5eb7b9-cmd age=258 idle=0 flags=x db=0 sub=0 psub=0 multi=3 qbuf=0 qbuf-free=32768 obl=36 oll=0 omem=0 events=r cmd=exec')
8177:M 19 Dec 00:53:18.138 # CONFIG REWRITE executed with success.
......

荣耀彩票代理CONGYISHANGSlaveJIEDIANDENEIRONGKEYIKANDAO,SlaveBEIQIEHUANCHENGLEMasterZHUANGTAI。NEIMESentinelBENSHENYOUNAXIEZHONGYAODERIZHIXINXINI?RUXIASUOSHI:

......
// 当前Sentinel节点确定原Master主观下线
8576:X 19 Dec 00:53:18.074 # +sdown master mymaster 192.168.61.140 6379
// 由于设置的quorum为1,所以一个Sentinel节点的主管下线就认为Master客观下线了
8576:X 19 Dec 00:53:18.074 # +odown master mymaster 192.168.61.140 6379 #quorum 1/1
// 第三代,每转移一次故障epoch的值+1,
// 不好意思,在书写测试实例前,本人已经自行测试了两次故障转移,所以这里看到的epoch为3
// 这个信息会自动写入到Sentinel节点的主配置文件中
8576:X 19 Dec 00:53:18.074 # +new-epoch 3
// 开始进行故障转移
8576:X 19 Dec 00:53:18.074 # +try-failover master mymaster 192.168.61.140 6379

// 选举出主导故障转移的Sentinel节点,因为不是所有Sentinel节点都会主导这个过程
8576:X 19 Dec 00:53:18.084 # +vote-for-leader 5a5eb7b97de060e7ad5f6aa20475a40b3d9fd3e1 3
8576:X 19 Dec 00:53:18.084 # +elected-leader master mymaster 192.168.61.140 6379
8576:X 19 Dec 00:53:18.084 # +failover-state-select-slave master mymaster 192.168.61.140 6379

// 选择提升哪一个slave作为新的master
8576:X 19 Dec 00:53:18.156 # +selected-slave slave 192.168.61.145:6379 192.168.61.145 6379 @ mymaster 192.168.61.140 6379
8576:X 19 Dec 00:53:18.156 * +failover-state-send-slaveof-noone slave 192.168.61.145:6379 192.168.61.145 6379 @ mymaster 192.168.61.140 6379
8576:X 19 Dec 00:53:18.211 * +failover-state-wait-promotion slave 192.168.61.145:6379 192.168.61.145 6379 @ mymaster 192.168.61.140 6379

// 提升原来的slave
8576:X 19 Dec 00:53:19.201 # +promoted-slave slave 192.168.61.145:6379 192.168.61.145 6379 @ mymaster 192.168.61.140 6379
// 试图重写所有salves节点的配置信息,并让它们指向新的master
8576:X 19 Dec 00:53:19.201 # +failover-state-reconf-slaves master mymaster 192.168.61.140 6379
// 故障转移结束
8576:X 19 Dec 00:53:19.250 # +failover-end master mymaster 192.168.61.140 6379
// 最终完成master节点的切换
8576:X 19 Dec 00:53:19.250 # +switch-master mymaster 192.168.61.140 6379 192.168.61.145 6379

// 注意原有的master节点会再显示一条作为主观下线,但是这次下线信息是以salve身份通知的
// 这是因为这次故障切换后,原来的master就算再上线,也只会作为Slave节点了
8576:X 19 Dec 00:53:19.251 * +slave slave 192.168.61.140:6379 192.168.61.140 6379 @ mymaster 192.168.61.145 6379
8576:X 19 Dec 00:53:49.305 # +sdown slave 192.168.61.140:6379 192.168.61.140 6379 @ mymaster 192.168.61.145 6379
......

荣耀彩票代理TONGGUOSlaveJIEDIANHESentinelJIEDIANDERIZHIKEYIKANDAO,ZAIJINGGUOLEDUANZANDESHIJIANHOUSentinelCHENGGONGJIANGWEIYIYIGESlaveJIEDIANZHUANHUANCHENGLEMasterJIEDIAN,BINGJIXUXIANGWAIBUTIGONGFUWU。

荣耀彩票代理ZHIHOUWOMENZHONGXINQIDONGYUANYOUMasterJIEDIAN,KANKANHUIFASHENGSHENME:

# 以下是原有Master启动后,在Sentinel显示的信息

......
8576:X 19 Dec 01:31:12.743 * +reboot slave 192.168.61.140:6379 192.168.61.140 6379 @ mymaster 192.168.61.145 6379
8576:X 19 Dec 01:31:12.805 # -sdown slave 192.168.61.140:6379 192.168.61.140 6379 @ mymaster 192.168.61.145 6379
......

一个非常重要的现象是,当原来的Master节点再次启动时,即使配置文件中没有设定slaveof信息,它也会在Sentinel的协调下称为Slave节点。这是因为任何一次Master到Slave的切换都是要付出代价的,其中除了状态本身的判断外,还有Sentinel自身协调和选举过程(选举哪一个Sentinel进行实质的切换动作),还有新的Master的选定问题,甚至包括Slave的slaveof目标变化过程中需要处理的数据一致性问题等等工作。所以最好的办法就是:只要能够保证Redis高可用集群持续工作,就不进行Master状态的切换

3-3、Java客户端配合Sentinel的使用

通过Sentinel组建的高可用集群对比通过第三方软件组建的高可用集群而言,有其明显的优点。例如可以实时返回集群中每个Redis节点的状态,且各节点间更能保持最佳的数据一致性,另外还可以在必要的时候通过转移客户端读操作,减轻Master节点的工作压力。但是它也有一个很明显的缺点,就是由于整个集群可以向调用者开放多个Redis节点的地址,且Sentinel本身并不能充当路由器荣耀彩票代理的作用,所以当Redis高可用集群进行状态切换时,客户端可能并不清楚原有的Master节点已经失效了。如下图所示:

这里写图片描述

HAIHAODESHI,JavaZUICHANGYONGDERedisKEHUDUANjedisTIGONGLEYIZUZHENDUISentinelDEJIQUNGONGJU,RANGKEHUDUANKEYIZAIHUOQUDANGQIANRedisGAOKEYONGJIQUNZHONGDEMasterJIEDIANHOU,ZAIZAIZHEIGEMasterJIEDIANSHANGWANCHENGSHUJUDUXIECAOZUO。DANLINGWAIYIGEDUCAOZUODEFUZAIWENTIHAISHIMEIYOUBEIJIEJUE,SUOYOUDEDUCAOZUOYEZHIHUIZAIMasterJIEDIANWANCHENG。

这里写图片描述

荣耀彩票代理WOMENLAIKANKANYIXIEGUANJIANDAIMA:

......
// 这是基本的连接配置
// 当让这些属性都可以根据您的实际情况进行更改
JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxTotal(100); 
poolConfig.setMaxIdle(50); 
poolConfig.setMinIdle(20); 
poolConfig.setMaxWaitMillis(6 * 1000); 
poolConfig.setTestOnBorrow(true);

// 以下是可用的多个Sentinel节点的ip和端口
Set<String> jedisClusterNodes = new HashSet<String>();
jedisClusterNodes.add('192.168.61.140:26379');
//如果有多个Sentinel一个一个添加进去
//jedisClusterNodes.add('192.168.61.139:26379');
JedisSentinelPool jedisSentinelPool = new JedisSentinelPool('mymaster', jedisClusterNodes , poolConfig);

// 开始插入信息
for(Integer index = 0 ; index < 10000 ; index++) {
    // 获取最新的master信息
    Jedis master = null;
    try {
        master = jedisSentinelPool.getResource();
    } catch(JedisConnectionException e) {
        // 如果出现异常,说明当前的maser断开了连接,那么等待一段时间后重试
        LOGGER.info('master is loss , waiting for try again......');
        synchronized (MasterSlaveApp.class) {
            MasterSlaveApp.class.wait(5000);
        }
        index--;
        continue;
    }

    // 开始正式插入
    master.set(('key' + index).getBytes(), index.toString().getBytes());
    LOGGER.info('write : ' + 'key' + index);
    synchronized (MasterSlaveApp.class) {
        // 停止0.5秒,以便观察现象
        MasterSlaveApp.class.wait(500);
    }
}
jedisSentinelPool.close();
......

ZAISHILIDAIMAZHONGSentinelJIEDIANZHIYOUYIGE,CUNZAIYU192.168.61.140:26379SHANG。RUGUOSHISHENGCHANHUANJINGJIANYIBUYAODUISentinelJINXINGDANDIANBUSHU,FOUZEYIDANSentinelDANDIANBENGKUIHUIZAOCHENGZHENGGERedisGAOKEYONGJIQUNZAIKEHUDUANWUFAJINXINGMasterJIEDIANDEQIEHUAN。ZAICHUSHIJIEDUAN192.168.61.140:6379SHImasterJIEDIAN,RANHOUWOMENZAICHENGXUZHIXINGGUOCHENGZHONGJIANGYUANYOUDEmasterJIEDIANGUANBI,ZHEISHISHANGMIANDEKEHUDUANDAIMAPIANDUANKENENGDESHUCHUYIXIARIZHIXINXI(BUFEN):

......
14639 [main] INFO redis_test.test.MasterSlaveApp  - write : key29
16144 [main] INFO redis_test.test.MasterSlaveApp  - master is loss , waiting for try again......
22148 [main] INFO redis_test.test.MasterSlaveApp  - master is loss , waiting for try again......
28151 [main] INFO redis_test.test.MasterSlaveApp  - master is loss , waiting for try again......
34155 [main] INFO redis_test.test.MasterSlaveApp  - master is loss , waiting for try again......
40159 [main] INFO redis_test.test.MasterSlaveApp  - master is loss , waiting for try again......
十二月 20, 2016 4:12:22 下午 redis.clients.jedis.JedisSentinelPool initPool
信息: Created JedisPool to master at 192.168.61.145:6379
46163 [main] INFO redis_test.test.MasterSlaveApp  - master is loss , waiting for try again......
51166 [main] INFO redis_test.test.MasterSlaveApp  - write : key30
51670 [main] INFO redis_test.test.MasterSlaveApp  - write : key31
......

==================================
后文内容预告:
4、Redis负载均衡方案
4-1、Twitter Twemproxy
4-2、Twemproxy存在的问题
5、Redis 3.X Cluster
5-1、Redis Cluster 简介
5-2、Redis Cluster 搭建实例
5-3、Client连接到Redis Cluster

荣耀彩票代理==========GEWEIDUZHEDUIWENZHANGLUOJIRUGUOYOUZHIYI,HUANYINGZAIPINGLUNQULIUYAN。GANXIEBANGZHUWOZHICHUCUOWUDEDUZHE。

Tag标签:         
  • 专题推荐

About IT165 - 广告服务 - 隐私声明 - 版权申明 - 免责条款 - 网站地图 - 网友投稿 - 联系方式
本站内容来自于互联网,仅供用于网络技术学习,学习中请遵循相关法律法规