Redis Sentinel的使用方法
1、sentinel monitor
用法:
sentinel monitor master-name ip port quorum
其中,master-name是主節(jié)點(diǎn)的名稱,ip,port不用解釋,是主節(jié)點(diǎn)的地址信息。
最后的quorum是判斷主節(jié)點(diǎn)最終不可達(dá)所需要的票數(shù)。這個(gè)值越大,判斷越可信,這個(gè)值越小,判斷越不可信,一般這個(gè)數(shù)字取的是sentinel節(jié)點(diǎn)數(shù)目的一半+1.同時(shí),該值還與sentinel節(jié)點(diǎn)的領(lǐng)導(dǎo)者選舉有關(guān),至少要有max(quorum,num (sentinel)/2+1)個(gè)節(jié)點(diǎn)參與選舉,才能選出領(lǐng)導(dǎo)者sentinel,從而完成故障轉(zhuǎn)換。例如有5個(gè)sentinel節(jié)點(diǎn),quorum設(shè)置為4,那么num(sentinel)/2+1=3,max函數(shù)返回4,最少要有4個(gè)節(jié)點(diǎn)才可以進(jìn)行領(lǐng)導(dǎo)者選舉。
2、sentinel down-after-milliseconds
用法如下:
sentinel down-after-milliseconds master-name times
該參數(shù)表示超過了配置的時(shí)間,沒有收到有效的回復(fù),則判斷sentinel節(jié)點(diǎn)不可達(dá),其中time的單位是毫秒。其中master-name為sentinel節(jié)點(diǎn)的名稱。
3、sentinel parallel-syncs
用法:
sentinel parallel-syncs master-name nums
4、sentinel failover-timeout
用法:
sentinel failover-timeout master-name times
failover-timeout通常翻譯成故障轉(zhuǎn)移超時(shí)時(shí)間(以下簡稱FT),但實(shí)際上它作用于故障轉(zhuǎn)移的各個(gè)階段。它的作用:
a、如果redis sentinel對(duì)一個(gè)主節(jié)點(diǎn)的額故障轉(zhuǎn)移失敗,那么下次在對(duì)該主節(jié)點(diǎn)做故障轉(zhuǎn)移的起始時(shí)間是FT的2倍
b、對(duì)于新選出來的主節(jié)點(diǎn),執(zhí)行slaveof no one的時(shí)候(因?yàn)樵瓉硭莻€(gè)從庫),如果出現(xiàn)失敗,當(dāng)超過FT時(shí),則故障轉(zhuǎn)移失敗。
c、選出新的主節(jié)點(diǎn)后,redis會(huì)在新主節(jié)點(diǎn)上執(zhí)行info命令,如果執(zhí)行時(shí)間超過FT時(shí),則故障轉(zhuǎn)移失敗
d、如果其余從節(jié)點(diǎn)和新的主節(jié)點(diǎn),在重新建立復(fù)制關(guān)系的時(shí)候,如果超過了FT的時(shí)間,那么故障轉(zhuǎn)移失敗。
5、sentinel auth-pass
用法:
sentinel auto-pass master-name password
從節(jié)點(diǎn)sentinel上配置主節(jié)點(diǎn)的密碼,從而保證可以和主節(jié)點(diǎn)進(jìn)行通信
6、sentinel notification-scripts
用法:
sentinel notification-scripts master-name scripts-path
7、sentinel client-reconfig-scripts
用法:
sentinel client-reconfig-scripts master-name scripts-path
在故障轉(zhuǎn)移結(jié)束后,會(huì)觸發(fā)對(duì)應(yīng)路徑的腳本,并向腳本發(fā)送故障轉(zhuǎn)移結(jié)果的相關(guān)參數(shù)。例如配置郵件或者報(bào)警。
參數(shù)6和參數(shù)7中需要注意,配置的腳本必須有執(zhí)行權(quán)限,同時(shí)必須包含shell腳本頭,腳本的最大執(zhí)行時(shí)間不能超過60s,不過在一般的線上環(huán)境,通常不會(huì)用這個(gè)參數(shù)來配置相關(guān)腳本。
Redis Sentinel同時(shí)監(jiān)控多個(gè)Redis節(jié)點(diǎn)
這個(gè)還是比較簡單的,只需要在sentinel的配置文件中寫上多個(gè)節(jié)點(diǎn)的名稱,其他參數(shù)也區(qū)分開來即可。例如:
sentinel monitor node1 IP1 port1 quorum1 sentinel client-reconfig-scripts node1 scripts-path1 -------- sentinel monitor node2 IP2 port2 quorum2 sentinel client-reconfig-scripts node2 scripts-path2
Redis Sentinel 動(dòng)態(tài)配置調(diào)整
我們可以使用sentinel set param value的方法來動(dòng)態(tài)的設(shè)置sentinel的參數(shù),這里有幾點(diǎn)需要注意:
1、sentinel set命令只對(duì)當(dāng)前sentinel節(jié)點(diǎn)有效
2、sentinel set命令如果執(zhí)行成功,會(huì)立即刷新配置文件,而不需要我們手動(dòng)config rewrite
3、建議所有sentinel節(jié)點(diǎn)的配置盡可能一致,可以提高故障轉(zhuǎn)移的有效性
4、sentinel對(duì)外不支持config命令
Redis Sentinel部署原則
a、sentinel節(jié)點(diǎn)不應(yīng)該部署在一臺(tái)物理機(jī)器上。這個(gè)比較容易理解
b、部署至少三個(gè)且奇數(shù)個(gè)sentinel節(jié)點(diǎn)。注意,這里的奇數(shù)個(gè)不是為了投票,而是因?yàn)榕紨?shù)個(gè)和奇數(shù)個(gè)的投票效果一致,奇數(shù)個(gè)可以節(jié)省一個(gè)節(jié)點(diǎn)。
c、有條件的情況下,盡量使用sentinel節(jié)點(diǎn)和redis節(jié)點(diǎn)一對(duì)一的方式部署,不要使用同一套sentinel來管理多個(gè)redis主節(jié)點(diǎn)。
相關(guān)的sentinel API:
1、sentinel masters
展示所有被監(jiān)控的主節(jié)點(diǎn)狀態(tài)以及相關(guān)的統(tǒng)計(jì)信息
[root@VM_48_10_centos ~]# redis-cli -p 26379 127.0.0.1:26379> sentinel masters 1) 1) "name" 2) "mymaster" 3) "ip" 4) "127.0.0.1" 5) "port" 6) "6379" 7) "runid" 8) "2ba04c1d8f837f2e419f6f5390c0d5938a6895f4" 9) "flags" 10) "master" 11) "pending-commands" 12) "0" 13) "last-ping-sent" 14) "0" 15) "last-ok-ping-reply" 16) "377" 17) "last-ping-reply" 18) "377" 19) "down-after-milliseconds" 20) "30000" 21) "info-refresh" 22) "8296" 23) "role-reported" 24) "master" 25) "role-reported-time" 26) "170821848" 27) "config-epoch" 28) "0" 29) "num-slaves" 30) "2" 31) "num-other-sentinels" 32) "2" 33) "quorum" 34) "2" 35) "failover-timeout" 36) "180000" 37) "parallel-syncs" 38) "1"
2、sentinel master master-name
展示指定master-name的主節(jié)點(diǎn)狀態(tài)以及相關(guān)的統(tǒng)計(jì)信息
3、sentinel slaves master-name
展示指定master-name的從節(jié)點(diǎn)狀態(tài)以及相關(guān)統(tǒng)計(jì)信息
127.0.0.1:26379> sentinel slaves mymaster 1) 1) "name" 2) "127.0.0.1:6380" 3) "ip" 4) "127.0.0.1" 5) "port" 6) "6380" 7) "runid" ----------------省略------------ 2) 1) "name" 2) "127.0.0.1:6381" 3) "ip" 4) "127.0.0.1" 5) "port" 6) "6381" 7) "runid"
4、sentinel sentinels master-name
展示指定master-name的sentinel節(jié)點(diǎn)集合,不包含當(dāng)前sentinel節(jié)點(diǎn)。
5、sentinel get-master-addr-by-name master-name
返回指定master-name的主節(jié)點(diǎn)的IP和端口
127.0.0.1:26379> sentinel get-master-addr-by-name mymaster 1) "127.0.0.1" 2) "6379"
6、sentinel reset pattern
sentinel對(duì)于符合pattern通配符風(fēng)格的主節(jié)點(diǎn)配置進(jìn)行重置,包含清除主節(jié)點(diǎn)的相關(guān)狀態(tài),重新發(fā)現(xiàn)從節(jié)點(diǎn)和sentinel節(jié)點(diǎn)等
7、sentinel failover master-name
對(duì)指定<master-name>的主節(jié)點(diǎn)進(jìn)行強(qiáng)制故障轉(zhuǎn)移,該命令在redis的日常運(yùn)維中非常有用。
8、sentinel ckquorum master-name
檢測(cè)當(dāng)前可達(dá)的sentinel節(jié)點(diǎn)總數(shù)是否達(dá)到了quorum的個(gè)數(shù),例如quorum的值為3,而當(dāng)前的可達(dá)的sentinel節(jié)點(diǎn)個(gè)數(shù)為2,則無法進(jìn)行故障轉(zhuǎn)移,redis sentinel的高可用特性也將失去
9、sentinel flushconfig
將sentinel節(jié)點(diǎn)的配置強(qiáng)制刷到磁盤上,這個(gè)命令sentinel節(jié)點(diǎn)自身用的比較多,當(dāng)配置文件丟失或者損壞時(shí),該命令比較有用。
10、sentinel remove master-name
取消當(dāng)前redis sentinel節(jié)點(diǎn)對(duì)于指定master-name主節(jié)點(diǎn)的監(jiān)控。
11、sentinel monitor master-name ip port quorum
這個(gè)命令和配置文件中的配置是一樣的,都是為了監(jiān)控主節(jié)點(diǎn)。只不過這個(gè)是通過命令的形式監(jiān)控的。
12、sentinel set master-name
動(dòng)態(tài)修改sentinel節(jié)點(diǎn)的配置選項(xiàng)
13、sentinel is-master-down-by-addr
sentinel節(jié)點(diǎn)之間用來交換對(duì)主節(jié)點(diǎn)是否下線的判斷,根據(jù)參數(shù)的不同,可以作為sentinel領(lǐng)導(dǎo)者選舉的通信方式。
以上就是Redis Sentinel的使用方法的詳細(xì)內(nèi)容,更多關(guān)于Redis Sentinel的使用的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Redis持久化方式之RDB和AOF的原理及優(yōu)缺點(diǎn)
在Redis中,數(shù)據(jù)可以分為兩類,即內(nèi)存數(shù)據(jù)和磁盤數(shù)據(jù),Redis?提供了兩種不同的持久化方式,其中?RDB?是快照備份機(jī)制,AOF?則是追加寫操作機(jī)制,本文將詳細(xì)給大家介紹Redis?持久化方式RDB和AOF的原理及優(yōu)缺點(diǎn),感興趣的同學(xué)可以跟著小編一起來學(xué)習(xí)2023-06-06Redis操作相關(guān)命令之查看、停止、啟動(dòng)命令
這篇文章主要介紹了Redis操作相關(guān)命令之查看、停止、啟動(dòng)命令,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09淺談RedisTemplate和StringRedisTemplate的區(qū)別
本文主要介紹了RedisTemplate和StringRedisTemplate的區(qū)別及個(gè)人見解,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06Redis實(shí)現(xiàn)分布式鎖的實(shí)例講解
在本篇文章里小編給大家整理了一篇關(guān)于Redis實(shí)現(xiàn)分布式鎖的實(shí)例講解內(nèi)容,有興趣的朋友們可以學(xué)習(xí)參考下。2021-12-12Java實(shí)現(xiàn)多級(jí)緩存的方法詳解
對(duì)于高并發(fā)系統(tǒng)來說,有三個(gè)重要的機(jī)制來保障其高效運(yùn)行,它們分別是:緩存、限流和熔斷,所以本文就來和大家探討一下多級(jí)緩存的實(shí)現(xiàn)方法,希望對(duì)大家有所幫助2024-02-02Redis內(nèi)存空間占用及避免數(shù)據(jù)丟失的方法
在現(xiàn)代的互聯(lián)網(wǎng)應(yīng)用中,Redis作為一種高性能的內(nèi)存數(shù)據(jù)庫,被廣泛應(yīng)用于緩存、會(huì)話管理和消息隊(duì)列等場(chǎng)景,然而,Redis的內(nèi)存資源是有限的,過多的內(nèi)存占用可能會(huì)導(dǎo)致數(shù)據(jù)丟失所以本文將給大家介紹一下Redis內(nèi)存空間占用及避免數(shù)據(jù)丟失的方法2023-08-08