Redis哨兵Sentinel的具體使用
主從架構(gòu)和MySQL的主從復(fù)制一樣,無法實現(xiàn)master和slave角色的自動切換,即當(dāng)master出現(xiàn)故障時, 不能實現(xiàn)自動的將一個slave節(jié)點(diǎn)提升為新的master節(jié)點(diǎn),即主從復(fù)制無法實現(xiàn)自動的故障轉(zhuǎn)移功能,如果想實現(xiàn)轉(zhuǎn)移,則需要手動修改配置,才能將 slave 服務(wù)器提升新的master節(jié)點(diǎn).此外只有一個主節(jié)點(diǎn)支持寫操作,所以業(yè)務(wù)量很大時會導(dǎo)致Redis服務(wù)性能達(dá)到瓶頸
需要解決的主從復(fù)制以下存在的問題:
- master和slave角色的自動切換,且不能影響業(yè)務(wù)
- 提升Redis服務(wù)整體性能,支持更高并發(fā)訪問
1. 哨兵 Sentinel 工作原理
生產(chǎn)環(huán)境如果要使用此功能建議使用Redis的2.8版本以上版本
Sentinel 故障轉(zhuǎn)移
- 多個sentinel發(fā)現(xiàn)并確認(rèn)master有問題。
- 選舉出一個sentinel作為領(lǐng)導(dǎo)。
- 選出一個slave作為master。
- 通知其余slave成為新的master的slave。
- 通知客戶端主從變化
- 等待老的master復(fù)活成為新master的slave。
專門的Sentinel 服務(wù)進(jìn)程是用于監(jiān)控redis集群中Master工作的狀態(tài),當(dāng)Master主服務(wù)器發(fā)生故障的時候,可以實現(xiàn)Master和Slave的角色的自動切換,從而實現(xiàn)系統(tǒng)的高可用性
Sentinel是一個分布式系統(tǒng),即需要在多個節(jié)點(diǎn)上各自同時運(yùn)行一個sentinel進(jìn)程,Sentienl 進(jìn)程通過流言協(xié)議(gossip protocols)來接收關(guān)于Master是否下線狀態(tài),并使用投票協(xié)議(Agreement Protocols)來決定是否執(zhí)行自動故障轉(zhuǎn)移,并選擇合適的Slave作為新的Master
每個Sentinel進(jìn)程會向其它Sentinel、Master、Slave定時發(fā)送消息,來確認(rèn)對方是否存活,如果發(fā)現(xiàn)某個節(jié)點(diǎn)在指定配置時間內(nèi)未得到響應(yīng),則會認(rèn)為此節(jié)點(diǎn)已離線,即為主觀宕機(jī)Subjective Down,簡稱為 SDOWN
如果哨兵集群中的多數(shù)Sentinel進(jìn)程認(rèn)為Master存在SDOWN,共同利用 is-master-down-by-addr 命令,互相通知后,則認(rèn)為客觀宕機(jī)Objectively Down, 簡稱 ODOWN
接下來利用投票算法,從所有slave節(jié)點(diǎn)中,選一臺合適的slave將之提升為新Master節(jié)點(diǎn),然后自動修改其它slave相關(guān)配置,指向新的master節(jié)點(diǎn),最終實現(xiàn)故障轉(zhuǎn)移failover
Redis Sentinel中的Sentinel節(jié)點(diǎn)個數(shù)應(yīng)該為大于等于3且最好為奇數(shù)
客戶端初始化時連接的是Sentinel節(jié)點(diǎn)集合,不再是具體的Redis節(jié)點(diǎn),即 Sentinel只是配置中心不是代理。
Redis Sentinel 節(jié)點(diǎn)與普通 Redis 沒有區(qū)別,要實現(xiàn)讀寫分離依賴于客戶端程序
Sentinel 機(jī)制類似于MySQL中的MHA功能,只解決master和slave角色的自動故障轉(zhuǎn)移問題,但單個 Master 的性能瓶頸問題并沒有解決
Redis 3.0 之前版本中,生產(chǎn)環(huán)境一般使用哨兵模式較多,Redis 3.0后推出Redis cluster功能,可以支持更大規(guī)模的高并發(fā)環(huán)境
Sentinel中的三個定時任務(wù)
每10 秒每個sentinel 對master和slave執(zhí)行info
發(fā)現(xiàn)slave節(jié)點(diǎn)
確認(rèn)主從關(guān)系
每2秒每個sentinel通過master節(jié)點(diǎn)的channel交換信息(pub/sub)
通過sentinel__:hello頻道交互
交互對節(jié)點(diǎn)的“看法”和自身信息
每1秒每個sentinel對其他sentinel和redis執(zhí)行ping
2. 實現(xiàn)哨兵架構(gòu)與具體搭建步驟
哨兵需要先實現(xiàn)主從復(fù)制
哨兵的前提是已經(jīng)實現(xiàn)了Redis的主從復(fù)制
注意: master 的配置文件中masterauth 和 slave 都必須相同
2.1 準(zhǔn)備主從復(fù)制環(huán)境配置
先通過一鍵編譯腳本在三臺主機(jī)上安裝好redis,搭建一主兩從
| 主機(jī)IP | 節(jié)點(diǎn) |
|---|---|
| 10.0.0.100 | 主 |
| 10.0.0.101 | 從 |
| 10.0.0.102 | 從 |
# 修改所有主從節(jié)點(diǎn)配置文件 [root@ubuntu2204 ~]#vim /apps/redis/etc/redis.conf bind 0.0.0.0 masterauth 123456 requirepass 123456 # 修改所有從節(jié)點(diǎn)配置文件 [root@ubuntu2204 ~]#vim /apps/redis/etc/redis.conf replicaof 10.0.0.100 6379 # 所有主從節(jié)點(diǎn)執(zhí)行 重啟redis服務(wù) [root@ubuntu2204 ~]#systemctl restart redis # 查看主從節(jié)點(diǎn)狀態(tài) [root@ubuntu2204 ~]#redis-cli -a 123456 info replication
2.2 編輯哨兵配置
sentinel 配置
Sentinel實際上是一個特殊的redis服務(wù)器,有些redis指令支持,但很多指令并不支持.默認(rèn)監(jiān)聽在 26379/tcp端口.
哨兵服務(wù)可以和Redis服務(wù)器分開部署在不同主機(jī),但為了節(jié)約成本一般會部署在一起
所有redis節(jié)點(diǎn)使用相同的以下示例的配置文件
# 在源碼目錄有sentinel.conf,復(fù)制到安裝目錄即可 [root@ubuntu2204 ~]#cp /usr/local/src/redis-7.2.6/sentinel.conf /apps/redis/etc/ [root@ubuntu2204 ~]#ll /apps/redis/etc/ [root@ubuntu2204 ~]#chown redis.redis /apps/redis/etc/sentinel.conf [root@ubuntu2204 ~]#ll /apps/redis/etc/ # 修改配置文件 [root@ubuntu2204 ~]#vim /apps/redis/etc/sentinel.conf protected-mode no port 26379 daemonize no pidfile /apps/redis/run/redis-sentinel.pid logfile "/apps/redis/log/redis-sentinel.log" dir /apps/redis/data/sentinel #mymaster是集群的名稱,此行指定當(dāng)前mymaster集群中master服務(wù)器的地址和端口 #2為法定人數(shù)限制(quorum),即有幾個sentinel認(rèn)為master down了就進(jìn)行故障轉(zhuǎn)移,一般此值是所有sentinel節(jié)點(diǎn)(一般總數(shù)是>=3的 奇數(shù),如:3,5,7等)的一半以上的整數(shù)值,比如,總數(shù)是3,即3/2=1.5,取整為2,是master的ODOWN客觀下線的依據(jù) sentinel monitor mymaster 10.0.0.100 6379 2 # mymaster集群中master的密碼,注意此行要在上面行的下面 sentinel auth-pass mymaster 123456 # 判斷mymaster集群中所有節(jié)點(diǎn)的主觀下線(SDOWN)的時間,單位:毫秒,建議3000 sentinel down-after-milliseconds mymaster 3000 # #發(fā)生故障轉(zhuǎn)移后,可以同時向新master同步數(shù)據(jù)的slave的數(shù)量,數(shù)字越小總同步時間越長,但可以減輕新master的負(fù)載壓力 sentinel parallel-syncs mymaster 1 # 所有slaves指向新的master所需的超時時間,單位:毫秒 sentinel failover-timeout mymaster 180000 acllog-max-len 128 # 禁止修改腳本 sentinel deny-scripts-reconfig yes SENTINEL resolve-hostnames no SENTINEL announce-hostnames no
# 修改一個節(jié)點(diǎn),其他節(jié)點(diǎn)直接scp [root@ubuntu2204 ~]#scp /apps/redis/etc/sentinel.conf 10.0.0.101:/apps/redis/etc/ [root@ubuntu2204 ~]#scp /apps/redis/etc/sentinel.conf 10.0.0.102:/apps/redis/etc/ # 修改scp后的兩個sentinel.conf文件的權(quán)限 [root@ubuntu2204 ~]#chown redis.redis /apps/redis/etc/sentinel.conf # 驗證三個哨兵服務(wù)器的配置 [root@ubuntu2204 ~]#grep -Ev '^#|^$' /apps/redis/etc/sentinel.conf protected-mode no port 26379 daemonize no pidfile /apps/redis/run/redis-sentinel.pid loglevel notice logfile "/apps/redis/log/redis-sentinel.log" dir /apps/redis/data/sentinel sentinel monitor mymaster 10.0.0.100 6379 2 sentinel auth-pass mymaster 123456 sentinel down-after-milliseconds mymaster 30000 acllog-max-len 128 sentinel parallel-syncs mymaster 1 sentinel failover-timeout mymaster 180000 sentinel deny-scripts-reconfig yes SENTINEL resolve-hostnames no SENTINEL announce-hostnames no SENTINEL master-reboot-down-after-period mymaster 0
2.3 啟動哨兵服務(wù)
# 生成service文件 [root@ubuntu2204 ~]#vim /lib/systemd/system/redis-sentinel.service [Unit] Description=Redis Sentinel After=network.target [Service] ExecStart=/apps/redis/bin/redis-sentinel /apps/redis/etc/sentinel.conf --supervised systemd ExecStop=/bin/kill -s QUIT $MAINPID Type=notify User=redis Group=redis RuntimeDirectory=redis RuntimeDirectoryMode=0755 [Install] WantedBy=multi-user.target [root@ubuntu2204 ~]#systemctl daemon-reload [root@ubuntu2204 ~]#systemctl enable --now redis-sentinel.service [root@ubuntu2204 ~]#systemctl status redis-sentinel.service 注意: 1. 確保權(quán)限是redis 2. Redis Sentinel 配置 daemonize yes 與 --supervised systemd 沖突,導(dǎo)致 systemd 無法管理服務(wù)進(jìn)程,應(yīng)將 daemonize 改為 no 3. --supervised systemd:表示 Redis Sentinel 由 systemd 管理進(jìn)程生命周期,Redis 不能自己后臺運(yùn)行
2.4 驗證哨兵服務(wù)
# 查看哨兵服務(wù)端口狀態(tài) [root@ubuntu2204 run]#ss -nlt # 查看哨兵日志 [root@ubuntu2204 ~]#tail -f /apps/redis/log/redis-sentinel.log # 當(dāng)前sentinel狀態(tài) [root@ubuntu2204 ~]#redis-cli -p 26379 info sentinel # 在sentinel狀態(tài)中尤其是最后一行,涉及到masterIP是多少,有幾個slave,有幾個sentinels,必須是符合全部服務(wù)器數(shù)量 # 兩個slave,三個sentinel服務(wù)器,如果sentinels值不符合,檢查myid可能沖突 master0:name=mymaster,status=ok,address=10.0.0.100:6379,slaves=2,sentinels=3 # 查看 Redis 各節(jié)點(diǎn)狀態(tài) [root@ubuntu2204 ~]#redis-cli -a 123456 info replication
2.5 停止 Master 節(jié)點(diǎn)實現(xiàn)故障轉(zhuǎn)移
# 停止 Master 節(jié)點(diǎn) [root@ubuntu2204 ~]#systemctl stop redis # 變成101 [root@ubuntu2204 ~]#redis-cli -p 26379 info sentinel master0:name=mymaster,status=ok,address=10.0.0.101:6379,slaves=2,sentinels=3 # 故障轉(zhuǎn)移后從節(jié)點(diǎn)redis.conf中的replicaof行的master IP會被修改 [root@ubuntu2204 ~]#grep replicaof /apps/redis/etc/redis.conf # Master-Replica replication. Use replicaof to make a Redis instance a copy of # replicaof <masterip> <masterport> replicaof 10.0.0.101 6379 # 哨兵配置文件的sentinel monitor IP 同樣也會被修改 [root@ubuntu2204 ~]#grep 'sentinel monitor' /apps/redis/etc/sentinel.conf # sentinel monitor <master-name> <ip> <redis-port> <quorum> sentinel monitor mymaster 10.0.0.101 6379 2 # 驗證 Redis 各節(jié)點(diǎn)狀態(tài) [root@ubuntu2204 ~]#redis-cli -a 123456 info replication [root@ubuntu2204 ~]#redis-cli -p 26379 info sentinel
2.6 原 Master 重新加入 Redis 集群
# 啟動redis [root@ubuntu2204 ~]#systemctl start redis # 觀察狀態(tài) [root@ubuntu2204 ~]#grep replicaof /apps/redis/etc/redis.conf [root@ubuntu2204 ~]#redis-cli -a 123456 info replication [root@ubuntu2204 ~]#redis-cli -p 26379 info sentinel
3. Sentinel 運(yùn)維
在Sentinel主機(jī)手動觸發(fā)故障切換
[root@ubuntu2204 ~]#vim /apps/redis/etc/redis.conf replica-priority 80 # 或者動態(tài)修改 [root@ubuntu2204 ~]#redis-cli -a 123456 127.0.0.1:6379> CONFIG GET replica-priority 127.0.0.1:6379> CONFIG SET replica-priority 60 127.0.0.1:6379> CONFIG GET replica-priority # 原主節(jié)點(diǎn)自動變成從節(jié)點(diǎn) [root@ubuntu2204 ~]#redis-cli -p 26379 127.0.0.1:26379> sentinel failover mymaster OK 127.0.0.1:26379>
4. 應(yīng)用程序連接 Sentinel
Redis 官方支持多種開發(fā)語言的客戶端: https://redis.io/clients
4. 1 客戶端連接 Sentinel 工作原理
- 客戶端獲取 Sentinel 節(jié)點(diǎn)集合,選舉出一個 Sentinel
- 由這個sentinel 通過masterName 獲取master節(jié)點(diǎn)信息,客戶端通過sentinel get-master-addr-by-name master-name這個api來獲取對應(yīng)主節(jié)點(diǎn)信息
- 客戶端發(fā)送role指令確認(rèn)master的信息,驗證當(dāng)前獲取的“主節(jié)點(diǎn)”是真正的主節(jié)點(diǎn),這樣的目的是為 了防止故障轉(zhuǎn)移期間主節(jié)點(diǎn)的變化
- 客戶端保持和Sentinel節(jié)點(diǎn)集合的聯(lián)系,即訂閱Sentinel節(jié)點(diǎn)相關(guān)頻道,時刻獲取關(guān)于主節(jié)點(diǎn)的相關(guān)信息,獲取新的master 信息變化,并自動連接新的master
到此這篇關(guān)于Redis哨兵Sentinel的具體使用的文章就介紹到這了,更多相關(guān)Redis哨兵Sentinel內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
如何使用Redis實現(xiàn)電商系統(tǒng)的庫存扣減
在日常開發(fā)中有很多地方都有類似扣減庫存的操作,本文主要介紹了如何使用Redis實現(xiàn)電商系統(tǒng)的庫存扣減,具有一定的參考價值,感興趣的可以了解一下2022-01-01
AOP?Redis自定義注解實現(xiàn)細(xì)粒度接口IP訪問限制
這篇文章主要為大家介紹了AOP?Redis自定義注解實現(xiàn)細(xì)粒度接口IP訪問限制,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10

