本篇内容介绍了“redis sentinel哨兵模式安装部署和切换的方法是什么”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让自由互联小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
1.安装redis
[root@mdb01redis-4.0.14]#makecdsrcCCMakefile.depmake[1]:Leavingdirectory`/u01/redis/redis-4.0.14/srcmake[1]:Enteringdirectory`/u01/redis/redis-4.0.14/srcCCadlist.oInfileincludedfromadlist.c:34:0:zmalloc.h:50:31:fatalerror:jemalloc/jemalloc.h:Nosuchfileordirectory#include^compilationterminated.make[1]:***[adlist.o]Error1make[1]:Leavingdirectory`/u01/redis/redis-4.0.14/srcmake:***[all]Error2报错,用下面的方式编译
[root@mdb01redis-4.0.14]#makeMALLOC=libc[root@mdb01src]#cdsrc/[root@mdb01src]#makeinstallCCMakefile.depHint:Itsagoodideatorunmaketest;)INSTALLinstallINSTALLinstallINSTALLinstallINSTALLinstallINSTALLinstall[root@mdb01redis-4.0.14]#mkdir6379/{run,log,conf,rdb}-p[root@mdb01redis-4.0.14]#cpredis.conf/u01/redis/6379/conf[root@mdb01redis-4.0.14]#cpsentinel.conf/u01/redis/6379/confconf ##配置文件目录
log ##日志文件目录
run ##运行的pid文件目录
rdb ##快照和AOF文件目录
修改redis.conf如下参数
[root@mdb01conf]#moreredis.conf|grep-v^#|grep-v^$bind127.0.0.1192.168.61.16protected-modenodaemonizeyespidfile/u01/redis/6379/run/redis_6379.pidlogfile"/u01/redis/6379/log//redis_6379.log"masterauthredisrequirepassredis启动
redis-server /u01/redis/6379/conf/redis.conf
设置密码
config set requirepass redis
2.搭建从库
搭建从库的过程跟主库是一样的。只是参数文件作为slave的主库信息:
slaveof 192.168.61.16 6379
slave-read-only yes
3.主从切换
杀掉主库
从库执行:
slaveof no one
主库恢复后:
slaveof 192.168.61.16 6379
主库宕机到恢复的时间内,从库写入的数据,会在重新建立主从关系后全部丢失。因为在重新与主库建立同步关系时,
会尝试去主库重新同步数据。
4081:S04Apr16:44:14.374*ConnectingtoMASTER192.168.61.16:63794081:S04Apr16:44:14.375*MASTERSLAVEsyncstarted4081:S04Apr16:44:14.375*NonblockingconnectforSYNCfiredtheevent.4081:S04Apr16:44:14.375*MasterrepliedtoPING,replicationcancontinue...4081:S04Apr16:44:14.376*Tryingapartialresynchronization(request11331acb42a97da2b6b4300ed50608b7ccf61d7b:5562).4081:S04Apr16:44:14.377*Fullresyncfrommaster:a99fee4793c1b63ccd7f1b13ac04fa72ede2de2c:04081:S04Apr16:44:14.377*Discardingpreviouslycachedmasterstate.4081:S04Apr16:44:14.434*MASTERSLAVEsync:receiving199bytesfrommaster4081:S04Apr16:44:14.434*MASTERSLAVEsync:Flushingolddata4081:S04Apr16:44:14.434*MASTERSLAVEsync:LoadingDBinmemory4081:S04Apr16:44:14.434*MASTERSLAVEsync:Finishedwithsuccess4.搭建哨兵模式
192.168.61.16 master+sentinel
192.168.61.17 slave+sentinel
192.168.61.18 slave+sentinel
[root@mdb01 conf]# more sentinel.conf |grep -v ^#|grep -v ^$
port26379dir/tmpsentinelmonitormymaster192.168.61.1663792#mymaster自己起的名字,主库IP和端口,2是指两个哨兵连接不到主库的时候才会切换,一般设置为N/2+1(N为Sentinel总数)。sentineldown-after-millisecondsmymaster15000#哨兵连接主库超时的时间是15秒。15秒内连接不上该哨兵认为主库出现问题,满足两个就会发生切换sentinelparallel-syncsmymaster1#切换后,从库每次要跟新主保持数据同步,这个时候不建议所有从库开始跟主库重新拉数据,虽然恢复时间最短,但是会造成没有可用节点sentinelfailover-timeoutmymaster180000sentineldeny-scripts-reconfigyes注意这里down-aafter-milliseconds单位是毫秒,开始我配成了15,认为是15秒,结果出现了主从一致在频繁切换的问题。
每一台主从上都开启:
redis-sentinel /u01/redis/6379/conf/sentinel.conf
也可以用下面的方式
redis-server /u01/redis/6379/conf/sentinel.conf --sentinel
模拟切换:
将16主库杀掉
01
13529:X05Apr11:07:29.846#+sdownmastermymaster192.168.61.166379#一个sentinel认为16已经down掉,此时为主观下线(sdown)13529:X05Apr11:07:29.922#+odownmastermymaster192.168.61.166379#quorum2/2#quorum已经达到了两个sentinel认为16已经down,此时为客观下线(odown)13529:X05Apr11:07:50.872#+tilt#tiltmodeentered13529:X05Apr11:08:20.942#-tilt#tiltmodeexited13529:X05Apr11:10:54.650#+new-epoch1361#开启一个新epoch,下面就开始根据偏移量选举,然后进行切换13529:X05Apr11:10:54.651#+vote-for-leader36c1c343cc73638eae2177e5a71d4fff92cf0b97136113529:X05Apr11:10:54.688#Nextfailoverdelay:IwillnotstartafailoverbeforeSunApr511:16:55202013529:X05Apr11:10:55.834#+config-update-fromsentinel36c1c343cc73638eae2177e5a71d4fff92cf0b97192.168.61.1726379@mymaster192.168.61.16637913529:X05Apr11:10:55.834#+switch-mastermymaster192.168.61.166379192.168.61.17637913529:X05Apr11:10:55.835*+slaveslave192.168.61.18:6379192.168.61.186379@mymaster192.168.61.17637913529:X05Apr11:10:55.835*+slaveslave192.168.61.16:6379192.168.61.166379@mymaster192.168.61.17637913529:X05Apr11:11:10.878#+sdownslave192.168.61.16:6379192.168.61.166379@mymaster192.168.61.17637902:
7953:X05Apr11:07:29.852#+sdownmastermymaster192.168.61.1663797953:X05Apr11:07:29.921#+odownmastermymaster192.168.61.166379#quorum3/27953:X05Apr11:10:54.646#+new-epoch13617953:X05Apr11:10:54.646#+try-failovermastermymaster192.168.61.1663797953:X05Apr11:10:54.648#+vote-for-leader36c1c343cc73638eae2177e5a71d4fff92cf0b9713617953:X05Apr11:10:54.651#84bc46cf5364f80a28d073fc012803ce230cdcdcvotedfor36c1c343cc73638eae2177e5a71d4fff92cf0b9713617953:X05Apr11:10:54.651#84bc46cf5364f80a28d073fc012803ce230cdcdevotedfor36c1c343cc73638eae2177e5a71d4fff92cf0b9713617953:X05Apr11:10:54.732#+elected-leadermastermymaster192.168.61.1663797953:X05Apr11:10:54.732#+failover-state-select-slavemastermymaster192.168.61.1663797953:X05Apr11:10:54.808#+selected-slaveslave192.168.61.17:6379192.168.61.176379@mymaster192.168.61.1663797953:X05Apr11:10:54.808*+failover-state-send-slaveof-nooneslave192.168.61.17:6379192.168.61.176379@mymaster192.168.61.1663797953:X05Apr11:10:54.880*+failover-state-wait-promotionslave192.168.61.17:6379192.168.61.176379@mymaster192.168.61.1663797953:X05Apr11:10:55.767#+promoted-slaveslave192.168.61.17:6379192.168.61.176379@mymaster192.168.61.1663797953:X05Apr11:10:55.767#+failover-state-reconf-slavesmastermymaster192.168.61.1663797953:X05Apr11:10:55.832*+slave-reconf-sentslave192.168.61.18:6379192.168.61.186379@mymaster192.168.61.1663797953:X05Apr11:10:56.818#-odownmastermymaster192.168.61.1663797953:X05Apr11:10:56.820*+slave-reconf-inprogslave192.168.61.18:6379192.168.61.186379@mymaster192.168.61.1663797953:X05Apr11:10:56.820*+slave-reconf-doneslave192.168.61.18:6379192.168.61.186379@mymaster192.168.61.1663797953:X05Apr11:10:56.891#+failover-endmastermymaster192.168.61.1663797953:X05Apr11:10:56.891#+switch-mastermymaster192.168.61.166379192.168.61.1763797953:X05Apr11:10:56.892*+slaveslave192.168.61.18:6379192.168.61.186379@mymaster192.168.61.1763797953:X05Apr11:10:56.892*+slaveslave192.168.61.16:6379192.168.61.166379@mymaster192.168.61.1763797953:X05Apr11:11:11.932#+sdownslave192.168.61.16:6379192.168.61.166379@mymaster192.168.61.17637917被选举为新的主库,那么这里会有一个提升为主库的动作
03:
7732:X05Apr11:07:29.846#+sdownmastermymaster192.168.61.1663797732:X05Apr11:07:30.921#+odownmastermymaster192.168.61.166379#quorum3/27732:X05Apr11:10:54.650#+new-epoch13617732:X05Apr11:10:54.651#+vote-for-leader36c1c343cc73638eae2177e5a71d4fff92cf0b9713617732:X05Apr11:10:54.716#Nextfailoverdelay:IwillnotstartafailoverbeforeSunApr511:16:5520207732:X05Apr11:10:55.841#+config-update-fromsentinel36c1c343cc73638eae2177e5a71d4fff92cf0b97192.168.61.1726379@mymaster192.168.61.1663797732:X05Apr11:10:55.841#+switch-mastermymaster192.168.61.166379192.168.61.1763797732:X05Apr11:10:55.844*+slaveslave192.168.61.18:6379192.168.61.186379@mymaster192.168.61.1763797732:X05Apr11:10:55.844*+slaveslave192.168.61.16:6379192.168.61.166379@mymaster192.168.61.1763797732:X05Apr11:11:10.869#+sdownslave192.168.61.16:6379192.168.61.166379@mymaster192.168.61.176379该节点也参与了投票,重构。
将老的主库拉起,老的主库会作为从库加入到集群中
01:
13529:X05Apr11:14:32.115#-sdownslave192.168.61.16:6379192.168.61.166379@mymaster192.168.61.17637913529:X05Apr11:17:32.623*+fix-slave-configslave192.168.61.16:6379192.168.61.166379@mymaster192.168.61.17637902:7953:X05Apr11:14:32.573#-sdownslave192.168.61.16:6379192.168.61.166379@mymaster192.168.61.17637903:7732:X05Apr11:14:32.684#-sdownslave192.168.61.16:6379192.168.61.166379@mymaster192.168.61.176379主动切换:
[root@mdb02 ~]# redis-cli -p 26379 sentinel failover mymaster
OK
01:
13529:X05Apr11:23:13.409#ExecutinguserrequestedFAILOVERofmymaster13529:X05Apr11:23:13.409#+new-epoch136213529:X05Apr11:23:13.409#+try-failovermastermymaster192.168.61.17637913529:X05Apr11:23:13.458#+vote-for-leader84bc46cf5364f80a28d073fc012803ce230cdcde136213529:X05Apr11:23:13.458#+elected-leadermastermymaster192.168.61.17637913529:X05Apr11:23:13.459#+failover-state-select-slavemastermymaster192.168.61.17637913529:X05Apr11:23:13.512#+selected-slaveslave192.168.61.16:6379192.168.61.166379@mymaster192.168.61.17637913529:X05Apr11:23:13.512*+failover-state-send-slaveof-nooneslave192.168.61.16:6379192.168.61.166379@mymaster192.168.61.17637913529:X05Apr11:23:13.589*+failover-state-wait-promotionslave192.168.61.16:6379192.168.61.166379@mymaster192.168.61.17637913529:X05Apr11:23:14.490#+promoted-slaveslave192.168.61.16:6379192.168.61.166379@mymaster192.168.61.17637913529:X05Apr11:23:14.490#+failover-state-reconf-slavesmastermymaster192.168.61.17637913529:X05Apr11:23:14.516*+slave-reconf-sentslave192.168.61.18:6379192.168.61.186379@mymaster192.168.61.17637913529:X05Apr11:23:15.579*+slave-reconf-inprogslave192.168.61.18:6379192.168.61.186379@mymaster192.168.61.17637913529:X05Apr11:23:15.579*+slave-reconf-doneslave192.168.61.18:6379192.168.61.186379@mymaster192.168.61.17637913529:X05Apr11:23:15.679#+failover-endmastermymaster192.168.61.17637913529:X05Apr11:23:15.680#+switch-mastermymaster192.168.61.176379192.168.61.16637913529:X05Apr11:23:15.680*+slaveslave192.168.61.18:6379192.168.61.186379@mymaster192.168.61.16637913529:X05Apr11:23:15.680*+slaveslave192.168.61.17:6379192.168.61.176379@mymaster192.168.61.16637902:
7953:X05Apr11:23:14.186#+new-epoch13627953:X05Apr11:23:14.517#+config-update-fromsentinel84bc46cf5364f80a28d073fc012803ce230cdcde192.168.61.1626379@mymaster192.168.61.1763797953:X05Apr11:23:14.517#+switch-mastermymaster192.168.61.176379192.168.61.1663797953:X05Apr11:23:14.517*+slaveslave192.168.61.18:6379192.168.61.186379@mymaster192.168.61.1663797953:X05Apr11:23:14.517*+slaveslave192.168.61.17:6379192.168.61.176379@mymaster192.168.61.16637903:
7732:X05Apr11:23:14.186#+new-epoch13627732:X05Apr11:23:14.519#+config-update-fromsentinel84bc46cf5364f80a28d073fc012803ce230cdcde192.168.61.1626379@mymaster192.168.61.1763797732:X05Apr11:23:14.519#+switch-mastermymaster192.168.61.176379192.168.61.1663797732:X05Apr11:23:14.519*+slaveslave192.168.61.18:6379192.168.61.186379@mymaster192.168.61.1663797732:X05Apr11:23:14.519*+slaveslave192.168.61.17:6379192.168.61.176379@mymaster192.168.61.1663797732:X05Apr11:23:24.585*+convert-to-slaveslave192.168.61.17:6379192.168.61.176379@mymaster192.168.61.166379“redis sentinel哨兵模式安装部署和切换的方法是什么”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注编程笔记网站,自由互联小编将为大家输出更多高质量的实用文章!
【文章转自中东服务器 http://www.558idc.com/dibai.html欢迎留下您的宝贵建议】