三:redis哨兵模式實現主從故障切換2
本篇接著上一篇進行redis哨兵的配置練習實驗,一般經典的哨兵需要3個節點(為什麼是3個節點,不是兩個節點)後面專門寫篇文章來分析這個問題.
可以再用一臺虛擬機器安裝一個redis服務,這臺虛擬機器不需要啟動例項,啟動哨兵就行,我這裡還是用兩臺機器只是測試,生產上可以分開,在zys-zk01 上啟動一個哨兵埠5000,在zys-zk02上啟動兩個哨兵節點埠分別為5001,5002 ,這樣就可以模擬3個節點的效果
- 哨兵配置檔案
sentinel.conf
最小的配置
每一個哨兵都可以去監控多個maser-slaves的主從架構
因為可能你的公司裡,為不同的專案,部署了多個master-slaves的redis主從叢集
相同的一套哨兵叢集,就可以去監控不同的多個redis主從叢集
你自己給每個redis主從叢集分配一個邏輯的名稱,列如
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel down-after-milliseconds mymaster 60000
sentinel failover-timeout mymaster 180000
sentinel parallel-syncs mymaster 1
sentinel monitor resque 192.168.1.3 6380 4
sentinel down-after-milliseconds resque 10000
sentinel failover-timeout resque 180000
sentinel parallel-syncs resque 5
sentinel monitor mymaster 127.0.0.1 6379
類似這種配置,來指定對一個master的監控,給監控的master指定的一個名稱,因為後面分散式叢集架構裡會講解,可以配置多個master做資料拆分
sentinel down-after-milliseconds mymaster 60000
sentinel failover-timeout mymaster 180000
sentinel parallel-syncs mymaster 1
上面的三個配置,都是針對某個監控的master配置的,給其指定上面分配的名稱即可
上面這段配置,就監控了兩個master node
這是最小的哨兵配置,如果發生了master-slave故障轉移,或者新的哨兵程序加入哨兵叢集,那麼哨兵會自動更新自己的配置檔案
sentinel monitor master-group-name hostname port quorum
quorum的解釋如下:
(1)至少多少個哨兵要一致同意,master程序掛掉了,或者slave程序掛掉了,或者要啟動一個故障轉移操作
(2)quorum是用來識別故障的,真正執行故障轉移的時候,還是要在哨兵叢集執行選舉,選舉一個哨兵程序出來執行故障轉移操作
(3)假設有5個哨兵,quorum設定了2,那麼如果5個哨兵中的2個都認為master掛掉了; 2個哨兵中的一個就會做一個選舉,選舉一個哨兵出來,執行故障轉移; 如果5個哨兵中有3個哨兵都是執行的,那麼故障轉移就會被允許執行
down-after-milliseconds,超過多少毫秒跟一個redis例項斷了連線,哨兵就可能認為這個redis例項掛了
parallel-syncs,新的master別切換之後,同時有多少個slave被切換到去連線新master,重新做同步,數字越低,花費的時間越多
假設你的redis是1個master,4個slave
然後master宕機了,4個slave中有1個切換成了master,剩下3個slave就要掛到新的master上面去
這個時候,如果parallel-syncs是1,那麼3個slave,一個一個地掛接到新的master上面去,1個掛接完,而且從新的master sync完資料之後,再掛接下一個
如果parallel-syncs是3,那麼一次性就會把所有slave掛接到新的master上去
failover-timeout,執行故障轉移的timeout超時時長
- 哨兵的配置
哨兵預設用26379埠,預設不能跟其他機器在指定埠連通,只能在本地訪問
在zys-zk01上:
[root@zys-zk01 ~]# mkdir /etc/sentinal
[root@zys-zk01 ~]# mkdir -p /var/sentinal/5000
[root@zys-zk01 ~]# vim /etc/sentinal/5000.conf
port 5000
#bind 192.168.31.187
protected-mode no
dir /var/sentinal/5000
sentinel monitor mymaster 192.168.1.232 6379 2
sentinel down-after-milliseconds mymaster 30000
sentinel failover-timeout mymaster 60000
sentinel parallel-syncs mymaster 1
在zys-zk02上:
[root@zys-zk02 redis]# mkdir /etc/sentinal
[root@zys-zk02 redis]# mkdir -p /var/sentinal/5001
[root@zys-zk02 redis]# mkdir -p /var/sentinal/5002
[root@zys-zk02 redis]# vim /etc/sentinal/5001.conf
port 5001
protected-mode no
dir /var/sentinal/5001
sentinel monitor mymaster 192.168.1.232 6379 2
sentinel down-after-milliseconds mymaster 30000
sentinel failover-timeout mymaster 60000
sentinel parallel-syncs mymaster 1
[[email protected]-zk02 redis]# vim /etc/sentinal/5002.conf
port 5002
protected-mode no
dir /var/sentinal/5002
sentinel monitor mymaster 192.168.1.232 6379 2
sentinel down-after-milliseconds mymaster 30000
sentinel failover-timeout mymaster 60000
sentinel parallel-syncs mymaster 1
- 啟動哨兵
分別啟動三個哨兵程序,組成一個叢集,觀察一下日誌的輸出
redis-sentinel /etc/sentinal/5000.conf
redis-server /etc/sentinal/5000.conf –sentinel
我們這裡先不後臺啟動,觀看下日誌:
[[email protected] ~]# /usr/local/bin/redis-sentinel /etc/sentinal/5000.conf
2827:X 17 Jul 16:58:50.320 * Increased maximum number of open files to 10032 (it was originally set to 1024).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 3.2.8 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in sentinel mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 5000
| `-._ `._ / _.-' | PID: 2827
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
2827:X 17 Jul 16:58:50.322 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
2827:X 17 Jul 16:58:50.333 # Sentinel ID is fec51ceca1905577d0fa120fbf328e909a970950
2827:X 17 Jul 16:58:50.333 # +monitor master mymaster 192.168.1.232 6379 quorum 2
2827:X 17 Jul 16:58:50.334 * +slave slave 192.168.1.233:6380 192.168.1.233 6380 @ mymaster 192.168.1.232 6379
[[email protected] redis]# /usr/local/bin/redis-sentinel /etc/sentinal/5001.conf
2168:X 17 Jul 17:00:25.330 * Increased maximum number of open files to 10032 (it was originally set to 1024).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 3.2.8 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in sentinel mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 5001
| `-._ `._ / _.-' | PID: 2168
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
2168:X 17 Jul 17:00:25.332 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
2168:X 17 Jul 17:00:25.343 # Sentinel ID is 5a8b105ed5416ead28de3ef31a3f7231e8db1cdd
2168:X 17 Jul 17:00:25.343 # +monitor master mymaster 192.168.1.232 6379 quorum 2
2168:X 17 Jul 17:00:25.345 * +slave slave 192.168.1.233:6380 192.168.1.233 6380 @ mymaster 192.168.1.232 6379
2168:X 17 Jul 17:00:25.506 * +sentinel sentinel fec51ceca1905577d0fa120fbf328e909a970950 192.168.1.232 5000 @ mymaster 192.168.1.232 6379
[[email protected] ~]# /usr/local/bin/redis-sentinel /etc/sentinal/5002.conf
2204:X 17 Jul 17:01:16.838 * Increased maximum number of open files to 10032 (it was originally set to 1024).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 3.2.8 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in sentinel mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 5002
| `-._ `._ / _.-' | PID: 2204
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
2204:X 17 Jul 17:01:16.843 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
2204:X 17 Jul 17:01:16.846 # Sentinel ID is 3a886fa2b0eb8dd114b99c94b5828753bc16d4c3
2204:X 17 Jul 17:01:16.846 # +monitor master mymaster 192.168.1.232 6379 quorum 2
2204:X 17 Jul 17:01:16.847 * +slave slave 192.168.1.233:6380 192.168.1.233 6380 @ mymaster 192.168.1.232 6379
2204:X 17 Jul 17:01:17.173 * +sentinel sentinel fec51ceca1905577d0fa120fbf328e909a970950 192.168.1.232 5000 @ mymaster 192.168.1.232 6379
2204:X 17 Jul 17:01:18.558 * +sentinel sentinel 5a8b105ed5416ead28de3ef31a3f7231e8db1cdd 192.168.1.233 5001 @ mymaster 192.168.1.232 6379
看日誌哨兵都正常啟動,我們後臺啟動,然後看看檢查下哨兵的狀態
在5000.conf,5001.conf,5002.conf配置檔案裡都新增一行配置:
daemonize yes (後臺程序模式啟動)
[root@zys-zk01 ~]# /usr/local/bin/redis-sentinel /etc/sentinal/5000.conf
[root@zys-zk01 ~]# ps -ef|grep redis
root 1753 1 0 09:52 ? 00:00:26 /usr/local/bin/redis-server *:6379
root 2871 1 0 17:06 ? 00:00:00 /usr/local/bin/redis-sentinel *:5000 [sentinel]
root 2875 1864 0 17:06 pts/0 00:00:00 grep redis
[root@zys-zk02 redis]# /usr/local/bin/redis-sentinel /etc/sentinal/5001.conf
[root@zys-zk02 redis]# /usr/local/bin/redis-sentinel /etc/sentinal/5002.conf
[root@zys-zk02 redis]# ps -ef|grep redis
root 2086 1 0 16:26 ? 00:00:03 /usr/local/bin/redis-server *:6380
root 2222 1 0 17:08 ? 00:00:00 /usr/local/bin/redis-sentinel *:5001 [sentinel]
root 2226 1 0 17:08 ? 00:00:00 /usr/local/bin/redis-sentinel *:5002 [sentinel]
root 2231 2034 0 17:09 pts/0 00:00:00 grep redis
檢視哨兵狀態:
[root@zys-zk01 ~]# /usr/local/bin/redis-cli -h 192.168.1.232 -p 5000
192.168.1.232:5000> sentinel master mymaster
1) "name"
2) "mymaster"
3) "ip"
4) "192.168.1.232"
5) "port"
6) "6379"
7) "runid"
8) "59d3b326a792799ad2ca25bdba36337359b50f3a"
9) "flags"
10) "master"
11) "link-pending-commands"
12) "0"
13) "link-refcount"
14) "1"
15) "last-ping-sent"
16) "0"
17) "last-ok-ping-reply"
18) "96"
19) "last-ping-reply"
20) "96"
21) "down-after-milliseconds"
22) "30000"
23) "info-refresh"
24) "6834"
25) "role-reported"
26) "master"
27) "role-reported-time"
28) "187557"
29) "config-epoch"
30) "0"
31) "num-slaves"
32) "1"
33) "num-other-sentinels"
34) "2"
35) "quorum"
36) "2"
37) "failover-timeout"
38) "60000"
39) "parallel-syncs"
40) "1"
192.168.1.232:5000>
192.168.1.232:5000> SENTINEL slaves mymaster
1) 1) "name"
2) "192.168.1.233:6380"
3) "ip"
4) "192.168.1.233"
5) "port"
6) "6380"
7) "runid"
8) "8558cedb4a89829670696d44233f1e6cb603e2ab"
9) "flags"
10) "slave"
11) "link-pending-commands"
12) "0"
13) "link-refcount"
14) "1"
15) "last-ping-sent"
16) "0"
17) "last-ok-ping-reply"
18) "725"
19) "last-ping-reply"
20) "725"
21) "down-after-milliseconds"
22) "30000"
23) "info-refresh"
24) "5101"
25) "role-reported"
26) "slave"
27) "role-reported-time"
28) "236056"
29) "master-link-down-time"
30) "0"
31) "master-link-status"
32) "ok"
33) "master-host"
34) "192.168.1.232"
35) "master-port"
36) "6379"
37) "slave-priority"
38) "100"
39) "slave-repl-offset"
40) "69835"
192.168.1.232:5000>
192.168.1.232:5000> SENTINEL sentinels mymaster
1) 1) "name"
2) "5a8b105ed5416ead28de3ef31a3f7231e8db1cdd"
3) "ip"
4) "192.168.1.233"
5) "port"
6) "5001"
7) "runid"
8) "5a8b105ed5416ead28de3ef31a3f7231e8db1cdd"
9) "flags"
10) "sentinel"
11) "link-pending-commands"
12) "0"
13) "link-refcount"
14) "1"
15) "last-ping-sent"
16) "0"
17) "last-ok-ping-reply"
18) "142"
19) "last-ping-reply"
20) "142"
21) "down-after-milliseconds"
22) "30000"
23) "last-hello-message"
24) "391"
25) "voted-leader"
26) "?"
27) "voted-leader-epoch"
28) "0"
2) 1) "name"
2) "3a886fa2b0eb8dd114b99c94b5828753bc16d4c3"
3) "ip"
4) "192.168.1.233"
5) "port"
6) "5002"
7) "runid"
8) "3a886fa2b0eb8dd114b99c94b5828753bc16d4c3"
9) "flags"
10) "sentinel"
11) "link-pending-commands"
12) "0"
13) "link-refcount"
14) "1"
15) "last-ping-sent"
16) "0"
17) "last-ok-ping-reply"
18) "142"
19) "last-ping-reply"
20) "142"
21) "down-after-milliseconds"
22) "30000"
23) "last-hello-message"
24) "768"
ok,3個節點的哨兵配置及測試,到這裡就實驗測試完畢了,下一篇將主要介紹如何跟spring 進行整合進行開發