Redis Sentinel為Redis提供高可用性。實際上办铡,這意味著使用Sentinel可以創(chuàng)建一個Redis部署辞做,在沒有人為干預(yù)的情況下抵抗某些類型的故障。
1. 故障轉(zhuǎn)移步驟
- 多個sentinel發(fā)現(xiàn)并缺人master有問題寡具。
- 選舉出一個sentinel作為領(lǐng)導(dǎo)秤茅。
- 選出一個slave作為master
- 通知其余slave成為新的master的slave
- 通知客戶端朱從變化
- 等待老的master復(fù)活成為信master的slave
2. 為什么要用哨兵
Redis Sentinel是一個分布式系統(tǒng):
Sentinel本身被設(shè)計成運行在多個Sentinel進程合作的配置中。具有多個Sentinel進程協(xié)作的優(yōu)勢如下:
- 當(dāng)多個Sentinels同意給定的主控器不再可用時童叠,執(zhí)行故障檢測框喳。這降低了誤報的可能性。
即使不是所有的Sentinel進程都在工作拯钻,Sentinel也能正常工作帖努,從而使系統(tǒng)對故障有效撰豺。畢竟粪般,擁有一個本身就是單點故障的故障切換系統(tǒng)是沒有意義的。 - Sentinel污桦,Redis實例(主服務(wù)器和從服務(wù)器)以及連接到Sentinel和Redis的客戶端的總和也是具有特定屬性的更大的分布式系統(tǒng)亩歹。在這篇文檔中,概念將從為了理解Sentinel的基本屬性所需的基本信息,到更復(fù)雜的信息(這些是可選的)小作,逐步引入亭姥,以便理解Sentinel的工作原理。
3. 快速開始
3.1 配置哨兵
分別配置sentinel.conf
和sentinel-26380.conf
port 26379
protected-mode no
sentinel monitor mymaster 47.xx.xxx.xxx 6379 2 # 127.0.0.1 一定要改成服務(wù)器 ip
sentinel auth-pass mymaster root++... #如果mamaster節(jié)點有密碼顾稀,則需要設(shè)置這項
sentinel down-after-milliseconds mymaster 60000
sentinel failover-timeout mymaster 180000
sentinel parallel-syncs mymaster 1
port 26380
sentinel monitor myslave 47.xx.xxx.xxx 6380 4
sentinel auth-pass myslave root++... #如果mamaster節(jié)點有密碼达罗,則需要設(shè)置這項
sentinel down-after-milliseconds myslave 10000
sentinel failover-timeout myslave 180000
sentinel parallel-syncs myslave 5
3.2 運行哨兵
兩種方法:
redis-sentinel /path/to/sentinel.conf
redis-server /path/to/sentinel.conf --sentinel
3.3 測試
mymaster啟動
[root@FantJ redis-4.0.9]# redis-sentinel sentinel.conf &
[1] 17859
[root@FantJ redis-4.0.9]# 17859:X 07 Sep 16:44:10.344 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
17859:X 07 Sep 16:44:10.344 # Redis version=4.0.9, bits=64, commit=00000000, modified=0, pid=17859, just started
17859:X 07 Sep 16:44:10.344 # Configuration loaded
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 4.0.9 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in sentinel mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 26379
| `-._ `._ / _.-' | PID: 17859
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
17859:X 07 Sep 16:44:10.346 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
17859:X 07 Sep 16:44:10.346 # Sentinel ID is e61d1d9c3a7441d2376cb98399fa9dd479076eef
17859:X 07 Sep 16:44:10.346 # +monitor master mymaster xxx.xx.xx.xx 6379 quorum 2
myslave啟動
[root@FantJ sentinel]# redis-sentinel sentinel-26380.conf &
[2] 17873
[root@FantJ sentinel]# 17873:X 07 Sep 16:46:11.401 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
17873:X 07 Sep 16:46:11.401 # Redis version=4.0.9, bits=64, commit=00000000, modified=0, pid=17873, just started
17873:X 07 Sep 16:46:11.401 # Configuration loaded
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 4.0.9 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in sentinel mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 26380
| `-._ `._ / _.-' | PID: 17873
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
17873:X 07 Sep 16:46:11.403 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
17873:X 07 Sep 16:46:11.403 # Sentinel ID is 0f375fb9f45af9c6a5402e88cb9296b24da6f067
17873:X 07 Sep 16:46:11.403 # +monitor master myslave xxxxx 6380 quorum 4
查看26379的sentinel信息:
[root@FantJ sentinel]# 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=sdown,address=47.xx.xx.xxx:6379,slaves=0,sentinels=1