重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
本篇内容介绍了“redis sentinel哨兵模式安装部署和切换的方法是什么”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
创新互联从2013年成立,先为鹰手营子等服务建站,鹰手营子等地企业,进行企业商务咨询服务。为鹰手营子企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。
1.安装redis
[root@mdb01 redis-4.0.14]# make cd src && make all make[1]: Entering directory `/u01/redis/redis-4.0.14/src' CC Makefile.dep make[1]: Leaving directory `/u01/redis/redis-4.0.14/src' make[1]: Entering directory `/u01/redis/redis-4.0.14/src' CC adlist.o In file included from adlist.c:34:0: zmalloc.h:50:31: fatal error: jemalloc/jemalloc.h: No such file or directory #include^ compilation terminated. make[1]: *** [adlist.o] Error 1 make[1]: Leaving directory `/u01/redis/redis-4.0.14/src' make: *** [all] Error 2
报错,用下面的方式编译
[root@mdb01 redis-4.0.14]# make MALLOC=libc [root@mdb01 src]# cd src/ [root@mdb01 src]# make install CC Makefile.dep Hint: It's a good idea to run 'make test' ;) INSTALL install INSTALL install INSTALL install INSTALL install INSTALL install [root@mdb01 redis-4.0.14]# mkdir 6379/{run,log,conf,rdb} -p [root@mdb01 redis-4.0.14]# cp redis.conf /u01/redis/6379/conf [root@mdb01 redis-4.0.14]# cp sentinel.conf /u01/redis/6379/conf
conf ##配置文件目录
log ##日志文件目录
run ##运行的pid文件目录
rdb ##快照和AOF文件目录
修改redis.conf如下参数
[root@mdb01 conf]# more redis.conf |grep -v '^#'|grep -v '^$' bind 127.0.0.1 192.168.61.16 protected-mode no daemonize yes pidfile /u01/redis/6379/run/redis_6379.pid logfile "/u01/redis/6379/log//redis_6379.log" masterauth redis requirepass redis
启动
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:S 04 Apr 16:44:14.374 * Connecting to MASTER 192.168.61.16:6379 4081:S 04 Apr 16:44:14.375 * MASTER <-> SLAVE sync started 4081:S 04 Apr 16:44:14.375 * Non blocking connect for SYNC fired the event. 4081:S 04 Apr 16:44:14.375 * Master replied to PING, replication can continue... 4081:S 04 Apr 16:44:14.376 * Trying a partial resynchronization (request 11331acb42a97da2b6b4300ed50608b7ccf61d7b:5562). 4081:S 04 Apr 16:44:14.377 * Full resync from master: a99fee4793c1b63ccd7f1b13ac04fa72ede2de2c:0 4081:S 04 Apr 16:44:14.377 * Discarding previously cached master state. 4081:S 04 Apr 16:44:14.434 * MASTER <-> SLAVE sync: receiving 199 bytes from master 4081:S 04 Apr 16:44:14.434 * MASTER <-> SLAVE sync: Flushing old data 4081:S 04 Apr 16:44:14.434 * MASTER <-> SLAVE sync: Loading DB in memory 4081:S 04 Apr 16:44:14.434 * MASTER <-> SLAVE sync: Finished with success
4.搭建哨兵模式
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 '^$'
port 26379 dir /tmp sentinel monitor mymaster 192.168.61.16 6379 2 #mymaster自己起的名字,主库IP和端口,2是指两个哨兵连接不到主库的时候才会切换,一般设置为N/2+1(N为Sentinel总数)。 sentinel down-after-milliseconds mymaster 15000 #哨兵连接主库超时的时间是15秒。15秒内连接不上该哨兵认为主库出现问题,满足两个就会发生切换 sentinel parallel-syncs mymaster 1 #切换后,从库每次要跟新主保持数据同步,这个时候不建议所有从库开始跟主库重新拉数据,虽然恢复时间最短,但是会造成没有可用节点 sentinel failover-timeout mymaster 180000 sentinel deny-scripts-reconfig yes
注意这里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:X 05 Apr 11:07:29.846 # +sdown master mymaster 192.168.61.16 6379 #一个sentinel认为16已经down掉,此时为主观下线(sdown) 13529:X 05 Apr 11:07:29.922 # +odown master mymaster 192.168.61.16 6379 #quorum 2/2 #quorum已经达到了两个sentinel认为16已经down,此时为客观下线(odown) 13529:X 05 Apr 11:07:50.872 # +tilt #tilt mode entered 13529:X 05 Apr 11:08:20.942 # -tilt #tilt mode exited 13529:X 05 Apr 11:10:54.650 # +new-epoch 1361 #开启一个新epoch,下面就开始根据偏移量选举,然后进行切换 13529:X 05 Apr 11:10:54.651 # +vote-for-leader 36c1c343cc73638eae2177e5a71d4fff92cf0b97 1361 13529:X 05 Apr 11:10:54.688 # Next failover delay: I will not start a failover before Sun Apr 5 11:16:55 2020 13529:X 05 Apr 11:10:55.834 # +config-update-from sentinel 36c1c343cc73638eae2177e5a71d4fff92cf0b97 192.168.61.17 26379 @ mymaster 192.168.61.16 6379 13529:X 05 Apr 11:10:55.834 # +switch-master mymaster 192.168.61.16 6379 192.168.61.17 6379 13529:X 05 Apr 11:10:55.835 * +slave slave 192.168.61.18:6379 192.168.61.18 6379 @ mymaster 192.168.61.17 6379 13529:X 05 Apr 11:10:55.835 * +slave slave 192.168.61.16:6379 192.168.61.16 6379 @ mymaster 192.168.61.17 6379 13529:X 05 Apr 11:11:10.878 # +sdown slave 192.168.61.16:6379 192.168.61.16 6379 @ mymaster 192.168.61.17 6379
02:
7953:X 05 Apr 11:07:29.852 # +sdown master mymaster 192.168.61.16 6379 7953:X 05 Apr 11:07:29.921 # +odown master mymaster 192.168.61.16 6379 #quorum 3/2 7953:X 05 Apr 11:10:54.646 # +new-epoch 1361 7953:X 05 Apr 11:10:54.646 # +try-failover master mymaster 192.168.61.16 6379 7953:X 05 Apr 11:10:54.648 # +vote-for-leader 36c1c343cc73638eae2177e5a71d4fff92cf0b97 1361 7953:X 05 Apr 11:10:54.651 # 84bc46cf5364f80a28d073fc012803ce230cdcdc voted for 36c1c343cc73638eae2177e5a71d4fff92cf0b97 1361 7953:X 05 Apr 11:10:54.651 # 84bc46cf5364f80a28d073fc012803ce230cdcde voted for 36c1c343cc73638eae2177e5a71d4fff92cf0b97 1361 7953:X 05 Apr 11:10:54.732 # +elected-leader master mymaster 192.168.61.16 6379 7953:X 05 Apr 11:10:54.732 # +failover-state-select-slave master mymaster 192.168.61.16 6379 7953:X 05 Apr 11:10:54.808 # +selected-slave slave 192.168.61.17:6379 192.168.61.17 6379 @ mymaster 192.168.61.16 6379 7953:X 05 Apr 11:10:54.808 * +failover-state-send-slaveof-noone slave 192.168.61.17:6379 192.168.61.17 6379 @ mymaster 192.168.61.16 6379 7953:X 05 Apr 11:10:54.880 * +failover-state-wait-promotion slave 192.168.61.17:6379 192.168.61.17 6379 @ mymaster 192.168.61.16 6379 7953:X 05 Apr 11:10:55.767 # +promoted-slave slave 192.168.61.17:6379 192.168.61.17 6379 @ mymaster 192.168.61.16 6379 7953:X 05 Apr 11:10:55.767 # +failover-state-reconf-slaves master mymaster 192.168.61.16 6379 7953:X 05 Apr 11:10:55.832 * +slave-reconf-sent slave 192.168.61.18:6379 192.168.61.18 6379 @ mymaster 192.168.61.16 6379 7953:X 05 Apr 11:10:56.818 # -odown master mymaster 192.168.61.16 6379 7953:X 05 Apr 11:10:56.820 * +slave-reconf-inprog slave 192.168.61.18:6379 192.168.61.18 6379 @ mymaster 192.168.61.16 6379 7953:X 05 Apr 11:10:56.820 * +slave-reconf-done slave 192.168.61.18:6379 192.168.61.18 6379 @ mymaster 192.168.61.16 6379 7953:X 05 Apr 11:10:56.891 # +failover-end master mymaster 192.168.61.16 6379 7953:X 05 Apr 11:10:56.891 # +switch-master mymaster 192.168.61.16 6379 192.168.61.17 6379 7953:X 05 Apr 11:10:56.892 * +slave slave 192.168.61.18:6379 192.168.61.18 6379 @ mymaster 192.168.61.17 6379 7953:X 05 Apr 11:10:56.892 * +slave slave 192.168.61.16:6379 192.168.61.16 6379 @ mymaster 192.168.61.17 6379 7953:X 05 Apr 11:11:11.932 # +sdown slave 192.168.61.16:6379 192.168.61.16 6379 @ mymaster 192.168.61.17 6379
17被选举为新的主库,那么这里会有一个提升为主库的动作
03:
7732:X 05 Apr 11:07:29.846 # +sdown master mymaster 192.168.61.16 6379 7732:X 05 Apr 11:07:30.921 # +odown master mymaster 192.168.61.16 6379 #quorum 3/2 7732:X 05 Apr 11:10:54.650 # +new-epoch 1361 7732:X 05 Apr 11:10:54.651 # +vote-for-leader 36c1c343cc73638eae2177e5a71d4fff92cf0b97 1361 7732:X 05 Apr 11:10:54.716 # Next failover delay: I will not start a failover before Sun Apr 5 11:16:55 2020 7732:X 05 Apr 11:10:55.841 # +config-update-from sentinel 36c1c343cc73638eae2177e5a71d4fff92cf0b97 192.168.61.17 26379 @ mymaster 192.168.61.16 6379 7732:X 05 Apr 11:10:55.841 # +switch-master mymaster 192.168.61.16 6379 192.168.61.17 6379 7732:X 05 Apr 11:10:55.844 * +slave slave 192.168.61.18:6379 192.168.61.18 6379 @ mymaster 192.168.61.17 6379 7732:X 05 Apr 11:10:55.844 * +slave slave 192.168.61.16:6379 192.168.61.16 6379 @ mymaster 192.168.61.17 6379 7732:X 05 Apr 11:11:10.869 # +sdown slave 192.168.61.16:6379 192.168.61.16 6379 @ mymaster 192.168.61.17 6379
该节点也参与了投票,重构。
将老的主库拉起,老的主库会作为从库加入到集群中
01:
13529:X 05 Apr 11:14:32.115 # -sdown slave 192.168.61.16:6379 192.168.61.16 6379 @ mymaster 192.168.61.17 6379 13529:X 05 Apr 11:17:32.623 * +fix-slave-config slave 192.168.61.16:6379 192.168.61.16 6379 @ mymaster 192.168.61.17 6379 02: 7953:X 05 Apr 11:14:32.573 # -sdown slave 192.168.61.16:6379 192.168.61.16 6379 @ mymaster 192.168.61.17 6379 03: 7732:X 05 Apr 11:14:32.684 # -sdown slave 192.168.61.16:6379 192.168.61.16 6379 @ mymaster 192.168.61.17 6379
主动切换:
[root@mdb02 ~]# redis-cli -p 26379 sentinel failover mymaster
OK
01:
13529:X 05 Apr 11:23:13.409 # Executing user requested FAILOVER of 'mymaster' 13529:X 05 Apr 11:23:13.409 # +new-epoch 1362 13529:X 05 Apr 11:23:13.409 # +try-failover master mymaster 192.168.61.17 6379 13529:X 05 Apr 11:23:13.458 # +vote-for-leader 84bc46cf5364f80a28d073fc012803ce230cdcde 1362 13529:X 05 Apr 11:23:13.458 # +elected-leader master mymaster 192.168.61.17 6379 13529:X 05 Apr 11:23:13.459 # +failover-state-select-slave master mymaster 192.168.61.17 6379 13529:X 05 Apr 11:23:13.512 # +selected-slave slave 192.168.61.16:6379 192.168.61.16 6379 @ mymaster 192.168.61.17 6379 13529:X 05 Apr 11:23:13.512 * +failover-state-send-slaveof-noone slave 192.168.61.16:6379 192.168.61.16 6379 @ mymaster 192.168.61.17 6379 13529:X 05 Apr 11:23:13.589 * +failover-state-wait-promotion slave 192.168.61.16:6379 192.168.61.16 6379 @ mymaster 192.168.61.17 6379 13529:X 05 Apr 11:23:14.490 # +promoted-slave slave 192.168.61.16:6379 192.168.61.16 6379 @ mymaster 192.168.61.17 6379 13529:X 05 Apr 11:23:14.490 # +failover-state-reconf-slaves master mymaster 192.168.61.17 6379 13529:X 05 Apr 11:23:14.516 * +slave-reconf-sent slave 192.168.61.18:6379 192.168.61.18 6379 @ mymaster 192.168.61.17 6379 13529:X 05 Apr 11:23:15.579 * +slave-reconf-inprog slave 192.168.61.18:6379 192.168.61.18 6379 @ mymaster 192.168.61.17 6379 13529:X 05 Apr 11:23:15.579 * +slave-reconf-done slave 192.168.61.18:6379 192.168.61.18 6379 @ mymaster 192.168.61.17 6379 13529:X 05 Apr 11:23:15.679 # +failover-end master mymaster 192.168.61.17 6379 13529:X 05 Apr 11:23:15.680 # +switch-master mymaster 192.168.61.17 6379 192.168.61.16 6379 13529:X 05 Apr 11:23:15.680 * +slave slave 192.168.61.18:6379 192.168.61.18 6379 @ mymaster 192.168.61.16 6379 13529:X 05 Apr 11:23:15.680 * +slave slave 192.168.61.17:6379 192.168.61.17 6379 @ mymaster 192.168.61.16 6379
02:
7953:X 05 Apr 11:23:14.186 # +new-epoch 1362 7953:X 05 Apr 11:23:14.517 # +config-update-from sentinel 84bc46cf5364f80a28d073fc012803ce230cdcde 192.168.61.16 26379 @ mymaster 192.168.61.17 6379 7953:X 05 Apr 11:23:14.517 # +switch-master mymaster 192.168.61.17 6379 192.168.61.16 6379 7953:X 05 Apr 11:23:14.517 * +slave slave 192.168.61.18:6379 192.168.61.18 6379 @ mymaster 192.168.61.16 6379 7953:X 05 Apr 11:23:14.517 * +slave slave 192.168.61.17:6379 192.168.61.17 6379 @ mymaster 192.168.61.16 6379
03:
7732:X 05 Apr 11:23:14.186 # +new-epoch 1362 7732:X 05 Apr 11:23:14.519 # +config-update-from sentinel 84bc46cf5364f80a28d073fc012803ce230cdcde 192.168.61.16 26379 @ mymaster 192.168.61.17 6379 7732:X 05 Apr 11:23:14.519 # +switch-master mymaster 192.168.61.17 6379 192.168.61.16 6379 7732:X 05 Apr 11:23:14.519 * +slave slave 192.168.61.18:6379 192.168.61.18 6379 @ mymaster 192.168.61.16 6379 7732:X 05 Apr 11:23:14.519 * +slave slave 192.168.61.17:6379 192.168.61.17 6379 @ mymaster 192.168.61.16 6379 7732:X 05 Apr 11:23:24.585 * +convert-to-slave slave 192.168.61.17:6379 192.168.61.17 6379 @ mymaster 192.168.61.16 6379
“redis sentinel哨兵模式安装部署和切换的方法是什么”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注创新互联网站,小编将为大家输出更多高质量的实用文章!