Redis進階:Redis的哨兵模式搭建
哨兵機制介紹
單機版的Redis存在性能瓶頸吱窝,Redis通過提高主從復(fù)制實現(xiàn)讀寫分離,提高了了Redis的可用性消恍,另一方便也能實現(xiàn)數(shù)據(jù)在多個Redis直接的備份惰匙。
上一篇文章我們通過配置Redis的主從復(fù)制機制來提高了Redis的可用性,但是一旦主節(jié)點出現(xiàn)問題署恍,就需要運維手工切換主從服務(wù)節(jié)點,即增加了人工成本蜻直,且容易出錯盯质,而且無法自動化切換袁串,Redis的哨兵機制就能實現(xiàn)自動的主從切換,以及實現(xiàn)對Redis服務(wù)的切換呼巷,那就讓我們來感受下哨兵機制的強大吧囱修。
準備條件
搭建主從服務(wù)
這一步在上篇文章以及詳細介紹了,這里就簡答提一下王悍,細節(jié)具體可以翻看上一篇破镰。
# 啟動Redis 主從復(fù)制集群機制
[root@localhost redis-5.0.7]# src/redis-server redis6379.conf
[root@localhost redis-5.0.7]# src/redis-server redis6380.conf
[root@localhost redis-5.0.7]# src/redis-server redis6381.conf
查看主從復(fù)制集群
127.0.0.1:6379> INFO replication
# Replication
role:master
connected_slaves:2
slave0:ip=127.0.0.1,port=6380,state=online,offset=14,lag=0
slave1:ip=127.0.0.1,port=6381,state=online,offset=14,lag=0
master_replid:7af61d3aee64d388592c3dabb16cd9b4a2158d7e
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:14
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:14
可以看到,我們的主從服務(wù)(一主二從)已經(jīng)搭建完畢压储。
準備哨兵配置文件
我們下載的Redis.tar.gz 解壓后里面是有一個senitel.conf配置文件的鲜漩,我們復(fù)制一個然后進行配置修改。
# 復(fù)制配置文件
[root@localhost redis-5.0.7]# cp sentinel.conf sentinel26379conf
# 修改sentinel26379conf配置文件
bind 0.0.0.0
port 26379
daemonize yes
pidfile /var/run/redis-sentinel26379.pid
logfile "/usr/local/redis/logs/sentinel-26379.log"
dir /tmp
# 哨兵 監(jiān)控 主節(jié)點名稱 IP地址 端口 判斷失效的哨兵個數(shù)
sentinel monitor mymaster 127.0.0.1 6379 2
# 配置主服務(wù)的密碼(如果主節(jié)點設(shè)置密碼這塊必須要進行配置)
sentinel auth-pass mymaster enjoyitlife
sentinel down-after-milliseconds mymaster 1000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 5000
將配置文件sentinel26379.conf 在復(fù)制兩份集惋,sentinel26380孕似、sentinel26381.conf。這兩個配置文件 值需要修改里的端口號就可以刮刑。
# sentinel26380 配置文件內(nèi)容 26381配置文件就不在贅述了喉祭。
bind 0.0.0.0
port 26380
daemonize yes
pidfile /var/run/redis-sentinel26380.pid
logfile "/usr/local/redis/logs/sentinel-26380.log"
dir /tmp
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel down-after-milliseconds mymaster 1000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 5000
sentinel auth-pass mymaster enjoyitlife
運行哨兵
運行哨兵
# 開啟三個哨兵監(jiān)控主節(jié)點
[root@localhost redis-5.0.7]# src/redis-sentinel sentinel26379.conf
[root@localhost redis-5.0.7]# src/redis-sentinel sentinel26380.conf
[root@localhost redis-5.0.7]# src/redis-sentinel sentinel26381.conf
也可以通過 redis-server /path/to/sentinel.conf --sentinel
運行哨兵。
查看哨兵狀態(tài)
# 通過 ps指令
[root@localhost redis-5.0.7]# ps -ef | grep redis
root 7929 1 0 07:49 ? 00:00:01 src/redis-server 0.0.0.0:6379
root 7933 7772 0 07:49 pts/5 00:00:00 src/redis-cli -p 6379 -a enjoyitlife
root 7935 1 0 07:50 ? 00:00:01 src/redis-server 127.0.0.1:6380
root 7941 1 0 07:50 ? 00:00:01 src/redis-server 127.0.0.1:6381
root 7949 1 0 08:00 ? 00:00:00 src/redis-sentinel 0.0.0.0:26379 [sentinel]
root 7954 1 0 08:00 ? 00:00:00 src/redis-sentinel 0.0.0.0:26380 [sentinel]
root 7959 1 0 08:00 ? 00:00:00 src/redis-sentinel 0.0.0.0:26381 [sentinel]
root 7978 7686 0 08:01 pts/4 00:00:00 grep --color=auto redis
# 通過客戶端 哨兵機制本質(zhì)也是Redis服務(wù)的一種模式 可以通過客戶端指令
[root@localhost ~]# cd /opt/software/redis/redis-5.0.7
[root@localhost redis-5.0.7]# src/redis-cli -p 26379
127.0.0.1:26379> INFO sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=127.0.0.1:6379,slaves=2,sentinels=3
可以看到至此雷绢,我們的哨兵機制已經(jīng)正確搭建完畢泛烙,當(dāng)前有三個哨兵,一主多從翘紊,3個服務(wù)節(jié)點蔽氨。
哨兵機制驗證
我們關(guān)閉主節(jié)點,然后等待1S后再進行查看霞溪。
# 關(guān)閉主節(jié)點
127.0.0.1:6379> SHUTDOWN
# 在從節(jié)點6380上進行查看
127.0.0.1:6380> INFO replication
# Replication
role:master
connected_slaves:0
master_replid:a83e3a8610f2a01691fd859143a4a7dc897b5916
master_replid2:7af61d3aee64d388592c3dabb16cd9b4a2158d7e
master_repl_offset:78278
second_repl_offset:69478
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:78278
可以看到現(xiàn)在的6380節(jié)點已經(jīng)有從節(jié)點升級為了主節(jié)點孵滞。我們再次啟動6379節(jié)點中捆。
# 啟動6379端口節(jié)點
[root@localhost redis-5.0.7]# src/redis-server redis6379.conf
[root@localhost redis-5.0.7]# src/redis-cli -p 6379 -a enjoyitlife
# 查看集群信息
127.0.0.1:6379> info replication
# Replication
role:slave
master_host:127.0.0.1
master_port:6380
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_repl_offset:107428
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:a83e3a8610f2a01691fd859143a4a7dc897b5916
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:107428
此時即使6379端口節(jié)點恢復(fù)正常了鸯匹,但是它已經(jīng)失去了主節(jié)點的身份,目前降級為了從節(jié)點了泄伪。下面就哨兵機制的原理在進行說明下殴蓬。
哨兵機制相關(guān)原理
主要功能
- 監(jiān)控 檢查主從服務(wù)節(jié)點是否服務(wù)正常
- 提醒 當(dāng)被監(jiān)控的節(jié)點出現(xiàn)問題時,哨兵機制可以發(fā)送通知
- 自動故障轉(zhuǎn)移 主節(jié)點不可以用時蟋滴,自動進行節(jié)點升級和故障轉(zhuǎn)移监氢。
- 提供配置 作為客戶端的配置提供者斥扛。
故障轉(zhuǎn)移流程
- 每個Sentinel每秒鐘向主節(jié)點、從節(jié)點、其他sentinel實例發(fā)送PING命令吧雹。
- 如果一個Sentinel發(fā)現(xiàn)主服務(wù)節(jié)點不可用,會將主節(jié)點標記為主觀下線流码,并通知其他Sentinel實例向主節(jié)點發(fā)送消息提陶。
- 如果有足夠數(shù)量的Sentinel實例行施,滿足配置文件中需要的個數(shù)
sentinel monitor mymaster 127.0.0.1 6379 2
這里配置的是2,那就將主服務(wù)標記為客觀下線魂那。 - Sentinel通過選舉機制選舉出一個Lead Sentinel蛾号,負責(zé)本次故障轉(zhuǎn)移。
- Sentinle選擇一個從服務(wù)節(jié)點涯雅,向其發(fā)送SALVE NO ONE 命令鲜结,使其升級為主節(jié)點。
- 通過發(fā)布與訂閱功能活逆, 將更新后的配置傳播給所有其他 Sentinel 精刷, 其他 Sentinel 對它們自己的配置進行更新。
- 向從服務(wù)器發(fā)送 [SLAVEOF] 命令蔗候,讓他們的主服務(wù)節(jié)點更新為上一步新主節(jié)點贬养。
- 當(dāng)所有從服務(wù)器都已經(jīng)開始復(fù)制新的主服務(wù)器時, 復(fù)制本次故障轉(zhuǎn)移的Sentinel 終止這次故障遷移操作琴庵。
注意要點
- Sentinel會在運行中自動的修改配置文件并進行持久化误算,主要涉及主從配置點。
- 每一次故障轉(zhuǎn)移Sentinel都會用過選舉機制選舉從本次故障轉(zhuǎn)移的領(lǐng)頭Sentinel迷殿,從而有領(lǐng)頭的Sentinel來負責(zé)故障轉(zhuǎn)移過程儿礼。
- Sentinel之間通過訂閱發(fā)布來進行消息傳輸。
- Sentinel機制很大程度依賴計算機時間庆寺,如果計算機出現(xiàn)故障蚊夫,或者進程被阻塞,Sentinel可能也會出現(xiàn)故障懦尝,從而進入TLTL保護模式知纷,進入TILT模式后,哨兵就不會在起作用陵霉。如果在30S內(nèi)恢復(fù)正常琅轧,就會退出TITL模式。
以上就是Redis哨兵模式操作的相關(guān)介紹了踊挠,更多其他指令可以參考官網(wǎng)乍桂,Redis官網(wǎng),謝謝閱讀效床,希望對你有所幫助睹酌。