Redis進階:哨兵集羣
1.哨兵模式
測試主從結構的高可用失敗,單獨使用主從複製,只能做到數據的備份,無法使任何一個從節點在主節點宕機後啓動爲主節點繼續提供服務,redis中提供主從高可用的技術爲哨兵模式。
也就是說,哨兵模式是針對主從模式進行高可用,哨兵模式是基於主從複製模式的。
1.1.哨兵進程的工作原理
在redis中可以啓動哨兵的進程,將某一個主從結構配置到哨兵,讓哨兵來管理當前的主從結構。
同一個主從結構可以由多個哨兵進程管理(便於選舉),在監控主從結構時,所有的哨兵進程會調用info命令查看當前的主從狀態,一旦發現返回的結果中master宕機,所有的哨兵進程會進行選舉的操作(過半選舉),選出替代主節點執行服務的從節點,執行命令將從節點變換成主節點,繼續提供服務。
結構如下圖:
哨兵進程啓動之後,會自動加載主節點下掛接的從節點,調用info命令查看到的從節點信息,哨兵監聽過程使用的是底層RPC通信協議。
最終的哨兵高可用集羣模式如下圖:
可以調用測試代碼,執行代碼邏輯,操作sentinel。
1.2.選舉機制
哨兵集羣中,監控管理主從結構的哨兵個數最好是奇數個。
集羣選舉容忍度:
2個哨兵存在的時候,爲了達到過半原則,可以允許幾個宕機?
- 2個哨兵的選舉容忍度0
- 3個哨兵的選舉容忍度1。
- 4個哨兵的選舉容忍度1。
- 5個哨兵的選舉容忍度2。
- 6個哨兵的選舉容忍度2。
由上述得出結論:2n和2n-1個集羣的選舉容忍度相同,爲了節省資源,最好配置奇數個哨兵集羣。
2.安裝哨兵集羣
哨兵集羣是基於Redis的主從集羣而來的,所以要進行哨兵集羣的安裝,需要先安裝Redis的主從集羣。
2.1.安裝主從複製
參看前一章節。
2.2.哨兵配置操作步驟
哨兵的配置文件所在的目錄爲Redis的HOME的根下。
/redis-6.2.6/sentinel.conf
在這個配置文件中,配置主從的關係,主節點信息。
規劃兩個配置文件,一個使用默認的26379端口,另外一個使用26380端口。
1.1.1.移動文件
將此配置文件從HOME目錄移動到和命令所在的同一目錄。
[root@lk7 bin]# cp /home/software/redis-6.2.6/sentinel.conf /usr/local/bin/
1.1.2.修改配置文件
修改啓動哨兵的配置文件sentinel.conf,內容如下:
- P15:bind需要註釋掉ip信息不要綁定,當前配置文件默認是註釋的。
- P17:安全配置protected-mode no註釋去掉,配置爲no。
- P21:端口,默認端口26379。
- p26:是否開啓後臺運行,這個需要和日誌路徑配合使用,如果指定了日誌文件路徑則開啓後臺運行,如果沒有指定,則不開啓,yes爲開啓後臺運行,no爲不開啓後臺運行。
- p31:pid存放位置,這個一般使用默認即可。
- p36:日誌文件名,默認日誌不保存。此項如果配置了,哨兵的監控信息就不會在屏幕上展示了。
- p65:日誌文件存放路徑,不存在需要手動創建,不然啓動會報錯。
- P84:主節點信息,sentinel monitor mymaster 127.0.0.1 6379 2修改監聽主從的掛接配置
- sentinel monitor mymaster 127.0.0.1 6379 2
- sentinel monitor :開始監聽主從結構中的主節點。
- mymaster:監聽當前主從結構的代號,可以自定義。
- ip:主節點所在的ip(使用內網地址),如果哨兵和主從節點在同一個機器,不要使用127.0.0.1,會造成代碼訪問失效。
- port:主節點端口號。
- 2 : 哨兵的工作單位數量最小值,需要修改成1。
- P225:失敗重新選舉,選舉新主節點失敗時的時間延遲(第二輪選舉和第一輪選舉的時間間隔)
- sentinel failover-timeout mymaster 10000
- 當前哨兵集羣對某一個事件的選舉如果不成立,將會根據這裏配置的時間毫秒數進行多輪選舉,直到最終結果出現。
例如:
15 # bind 127.0.0.1 192.168.1.1
17 protected-mode no
21 port 26379
26 daemonize yes
31 pidfile /var/run/redis-sentinel.pid
36 logfile "sentinel01.log"
65 dir "/tmp/redis"
84 sentinel monitor mymaster 127.0.0.1 6381 2
224 # Default is 3 minutes.
225 sentinel failover-timeout mymaster 10000
1.1.3.複製配置文件
將配置好的sentinel.conf文件複製2個。
[root@lk7 bin]# cp sentinel.conf sentinel01.conf
[root@lk7 bin]# cp sentinel.conf sentinel02.conf
[root@lk7 bin]# vim sentinel02.conf
將sentinel02.conf的端口號改爲26380。
2.3.啓動哨兵進程
啓動哨兵進程,開啓監聽主從結構,命令如下:
redis-sentinel 啓動文件
操作如下:
[root@lk7 bin]# redis-sentinel sentinel01.conf
[root@lk7 bin]# tail -f /tmp/redis/sentinel01.log
4063:X 12 Feb 2022 20:53:10.020 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
4063:X 12 Feb 2022 20:53:10.021 # Redis version=6.2.6, bits=64, commit=00000000, modified=0, pid=4063, just started
4063:X 12 Feb 2022 20:53:10.021 # Configuration loaded
4063:X 12 Feb 2022 20:53:10.021 * Increased maximum number of open files to 10032 (it was originally set to 1024).
4063:X 12 Feb 2022 20:53:10.021 * monotonic clock: POSIX clock_gettime
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 6.2.6 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in sentinel mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 26379
| `-._ `._ / _.-' | PID: 4063
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | https://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
4063:X 12 Feb 2022 20:53:10.021 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
4063:X 12 Feb 2022 20:53:10.022 # Sentinel ID is 110f88ebe35298d2a07ace06d18a29468fb36ad3
4063:X 12 Feb 2022 20:53:10.022 # +monitor master mymaster 127.0.0.1 6381 quorum 2
4063:X 12 Feb 2022 20:53:10.023 * +slave slave 192.168.106.171:6383 192.168.106.171 6383 @ mymaster 127.0.0.1 6381
4063:X 12 Feb 2022 20:53:10.024 * +slave slave 192.168.106.171:6382 192.168.106.171 6382 @ mymaster 127.0.0.1 6381
4063:X 12 Feb 2022 20:53:18.130 * +sentinel sentinel 89ac1bb631eb8ccfc9c854478dfa9114073ec760 192.168.106.171 26380 @ mymaster 127.0.0.1 6381
4063:X 12 Feb 2022 20:53:18.134 * +sentinel-address-switch master mymaster 127.0.0.1 6381 ip 127.0.0.1 port 26380 for 89ac1bb631eb8ccfc9c854478dfa9114073ec760
4063:X 12 Feb 2022 20:53:18.135 * +sentinel-address-switch master mymaster 127.0.0.1 6381 ip 192.168.106.171 port 26380 for 89ac1bb631eb8ccfc9c854478dfa9114073ec760
4063:X 12 Feb 2022 20:53:18.136 * +sentinel-address-switch master mymaster 127.0.0.1 6381 ip 127.0.0.1 port 26380 for 89ac1bb631eb8ccfc9c854478dfa9114073ec760
4063:X 12 Feb 2022 20:53:20.173 * +fix-slave-config slave 192.168.106.171:6383 192.168.106.171 6383 @ mymaster 127.0.0.1 6381
4063:X 12 Feb 2022 20:53:20.173 * +fix-slave-config slave 192.168.106.171:6382 192.168.106.171 6382 @ mymaster 127.0.0.1 6381
2.4.測試
kill掉主節點進程,查看哨兵能否啓動高可用。
[root@lk7 ~]# ps -ef | grep redis
root 2150 1 0 20:16 ? 00:00:01 redis-server *:6382
root 2159 1 0 20:16 ? 00:00:01 redis-server *:6383
root 3648 2380 0 20:45 pts/1 00:00:00 redis-sentinel *:26379 [sentinel]
root 3843 3302 0 20:49 pts/2 00:00:00 redis-sentinel *:26380 [sentinel]
root 3887 1 0 20:49 ? 00:00:00 redis-server *:6381
root 3918 3460 0 20:50 pts/3 00:00:00 grep --color=auto redis
[root@lk7 ~]# kill 3648
[root@lk7 ~]# ps -ef | grep redis
root 2150 1 0 20:16 ? 00:00:01 redis-server *:6382
root 2159 1 0 20:16 ? 00:00:01 redis-server *:6383
root 3887 1 0 20:49 ? 00:00:00 redis-server *:6381
root 4073 3302 0 20:53 pts/2 00:00:00 redis-sentinel *:26380 [sentinel]
root 4171 2380 0 20:55 pts/1 00:00:00 redis-sentinel *:26379 [sentinel]
root 4190 3460 0 20:55 pts/3 00:00:00 grep --color=auto redis
觀察哨兵監視信息,看到如下信息:
4171:X 12 Feb 2022 20:58:27.242 * +switch-master mymaster 192.168.106.171 6382 127.0.0.1 6382
4171:X 12 Feb 2022 20:58:27.242 * +slave slave 192.168.106.171:6383 192.168.106.171 6383 @ mymaster 127.0.0.1 6382
4171:X 12 Feb 2022 20:58:27.242 * +slave slave 127.0.0.1:6381 127.0.0.1 6381 @ mymaster 127.0.0.1 6382
4171:X 12 Feb 2022 20:58:27.243 * +slave slave 127.0.0.1:6383 127.0.0.1 6383 @ mymaster 127.0.0.1 6382
4171:X 12 Feb 2022 20:58:27.243 * +slave slave 192.168.106.171:6382 192.168.106.171 6382 @ mymaster 127.0.0.1 6382
上述記錄將6382端口的節點選爲了主節點。
將宕機的主節點重啓,啓動後發現哨兵將重啓的主節點轉化成從節點提供主從服務。
[root@lk7 ~]# redis-server /usr/local/bin/master01.conf
[root@lk7 ~]# ps -ef | grep redis
root 2150 1 0 20:16 ? 00:00:02 redis-server *:6382
root 2159 1 0 20:16 ? 00:00:02 redis-server *:6383
root 4073 3302 0 20:53 pts/2 00:00:02 redis-sentinel *:26380 [sentinel]
root 4171 2380 0 20:55 pts/1 00:00:02 redis-sentinel *:26379 [sentinel]
root 4428 1 0 21:00 ? 00:00:00 redis-server *:6381
root 4437 3460 0 21:00 pts/3 00:00:00 grep --color=auto redis
[root@lk7 ~]# redis-cli -p 6381
127.0.0.1:6381> info replication
# Replication
role:master
connected_slaves:0
master_failover_state:no-failover
master_replid:e36b191a8ef04ed4b0a2d0d86ec48e4997e9bfc3
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
127.0.0.1:6381> info replication
# Replication
role:master
connected_slaves:0
master_failover_state:no-failover
master_replid:e36b191a8ef04ed4b0a2d0d86ec48e4997e9bfc3
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
127.0.0.1:6381> info replication
Error: Server closed the connection
127.0.0.1:6381> info replication
# Replication
role:slave
master_host:127.0.0.1
master_port:6382
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_read_repl_offset:26334
slave_repl_offset:26334
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:d70d2186f2a5559546c77ec55e16abeaebb80532
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:26334
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:25210
repl_backlog_histlen:1125
127.0.0.1:6381>
宕機掉一個哨兵,當兩個哨兵管理主從時,一個宕機,導致另一個的選舉沒有過半無法生效,quorum由此也可得出:最好啓動奇數個哨兵,保證每次至少有過半的哨兵選舉成功。
2.5.關閉哨兵進程
關閉服務的命令如下:
redis-cli -h hostname -p port shutdown
例如:
[root@lk7 bin]# redis-cli -p 26379 shutdown
[root@lk7 bin]# redis-cli -p 26380 shutdown
3.重啓哨兵
哨兵在停機後如何重新開啓?
3.1.啓動主從
先啓動3個主從節點
redis-server master01.conf
redis-server slave01.conf
redis-server slave02.conf
3.2.檢查主從關係
然後檢查主從關係,分別登錄每一個節點調用info replication查看。
127.0.0.1:6383> info replication
# Replication
role:slave
master_host:127.0.0.1
master_port:6382
master_link_status:down
master_last_io_seconds_ago:-1
master_sync_in_progress:0
slave_read_repl_offset:55841
slave_repl_offset:55841
master_link_down_since_seconds:22
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:31a6a3ecb08ff59eef2ccfd54dbf852563f73af6
master_replid2:3cee5b80db5a2a3954302ad119be2b74673f7a10
master_repl_offset:55841
second_repl_offset:52892
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:47736
repl_backlog_histlen:8106
此時主節點已經是6382端口了,將沒有掛接的重新掛接主從
slaveof 192.168.106.171 6382
3.3.檢查哨兵配置文件
然後查看sentinel配置文件中配置的地址和端口是否和上面看到的Info信息一致。
如果哨兵配置文件和主從結構一致則不需要修改直接啓動。
不一致,需要修改爲相同的信息,如下:
sentinel monitor mymaster 127.0.0.1 6382 2
如果端口和啓動的主從不一致,將端口修改後,把最後的配置內容刪除,如下:
# Generated by CONFIG REWRITE
user default on nopass sanitize-payload ~* &* +@all
sentinel myid 110f88ebe35298d2a07ace06d18a29468fb36ad3
sentinel config-epoch mymaster 15
sentinel leader-epoch mymaster 15
sentinel current-epoch 15
sentinel known-replica mymaster 127.0.0.1 6381
sentinel known-replica mymaster 127.0.0.1 6383
sentinel known-sentinel mymaster 192.168.106.171 26380 89ac1bb631eb8ccfc9c854478dfa9114073ec760
sentinel known-replica mymaster 192.168.106.171 6381
sentinel known-replica mymaster 192.168.106.171 6383
sentinel known-replica mymaster 192.168.106.171 6382
然後保存。
3.4.啓動哨兵
啓動哨兵:
redis-sentinel sentinel01.conf
redis-sentinel sentinel02.conf
3.5.狀態檢查
完成啓動之後,主從master一直處於down狀態。
[root@lk7 ~]# redis-cli -p 6383
127.0.0.1:6383> info replication
# Replication
role:slave
master_host:127.0.0.1
master_port:6383
master_link_status:down
master_last_io_seconds_ago:-1
master_sync_in_progress:0
slave_read_repl_offset:300720
slave_repl_offset:300720
master_link_down_since_seconds:-1
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:98a5592593e5ce29a73982989eb57c1c03a9075d
master_replid2:8da7c2e0cc7626594adf7ef3a60c0aef75f46f18
master_repl_offset:300720
second_repl_offset:297918
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:292880
repl_backlog_histlen:7841
127.0.0.1:6383>
哨兵一直對主節點進行切換,節點都是正常啓動的,但是在主從的狀態中,主節點一直是down狀態。
哨兵一直對主節點進行選舉,但是恢復不了master結點的up狀態。
讀數據都沒有問題,但是不能進行寫操作。
請遇到過這種問題的大神給個解答,謝謝!