您好,登錄后才能下訂單哦!
本篇內容介紹了“redis sentinel哨兵模式安裝部署和切換的方法是什么”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
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 <jemalloc/jemalloc.h> ^ 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哨兵模式安裝部署和切換的方法是什么”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。