91欧美超碰AV自拍|国产成年人性爱视频免费看|亚洲 日韩 欧美一厂二区入|人人看人人爽人人操aV|丝袜美腿视频一区二区在线看|人人操人人爽人人爱|婷婷五月天超碰|97色色欧美亚州A√|另类A√无码精品一级av|欧美特级日韩特级

0
  • 聊天消息
  • 系統(tǒng)消息
  • 評(píng)論與回復(fù)
登錄后你可以
  • 下載海量資料
  • 學(xué)習(xí)在線課程
  • 觀看技術(shù)視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會(huì)員中心
創(chuàng)作中心

完善資料讓更多小伙伴認(rèn)識(shí)你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示

Redis哨兵模式的自動(dòng)故障檢測(cè)與主從切換實(shí)戰(zhàn)

馬哥Linux運(yùn)維 ? 來(lái)源:馬哥Linux運(yùn)維 ? 2026-02-27 11:05 ? 次閱讀
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群

一、概述

1.1 背景介紹

Redis 主從復(fù)制解決了讀擴(kuò)展和數(shù)據(jù)冗余問(wèn)題,但主節(jié)點(diǎn)故障時(shí)需要人工介入切換,這在生產(chǎn)環(huán)境中是不可接受的。Sentinel(哨兵)模式在主從架構(gòu)之上增加了自動(dòng)故障檢測(cè)和故障轉(zhuǎn)移能力,是 Redis 高可用的標(biāo)準(zhǔn)方案之一。

Sentinel 本質(zhì)上是一組獨(dú)立進(jìn)程,持續(xù)監(jiān)控 Redis 主從節(jié)點(diǎn)的健康狀態(tài),在主節(jié)點(diǎn)不可用時(shí)自動(dòng)完成新主節(jié)點(diǎn)選舉和客戶端重定向。Redis 8.x 對(duì) Sentinel 的穩(wěn)定性和性能做了進(jìn)一步優(yōu)化,但核心機(jī)制與 7.x 保持一致。

1.2 技術(shù)特點(diǎn)

自動(dòng)故障轉(zhuǎn)移:主節(jié)點(diǎn)宕機(jī)后無(wú)需人工干預(yù),Sentinel 集群自動(dòng)完成切換,通常在 30 秒內(nèi)完成

服務(wù)發(fā)現(xiàn):客戶端通過(guò) Sentinel 獲取當(dāng)前主節(jié)點(diǎn)地址,屏蔽了主從切換的地址變化

配置傳播:故障轉(zhuǎn)移完成后,Sentinel 自動(dòng)更新所有從節(jié)點(diǎn)的復(fù)制配置

多 Sentinel 協(xié)作:通過(guò) quorum 機(jī)制避免單點(diǎn)誤判,防止網(wǎng)絡(luò)抖動(dòng)觸發(fā)不必要的切換

1.3 適用場(chǎng)景

數(shù)據(jù)量在單機(jī)可承載范圍內(nèi)(通常 < 100GB),不需要數(shù)據(jù)分片

對(duì)寫入可用性要求高,能接受切換期間約 30 秒的寫入中斷

讀寫比高,通過(guò)從節(jié)點(diǎn)分擔(dān)讀流量

不需要 Cluster 的水平擴(kuò)展能力,但需要比單主更高的可用性

1.4 環(huán)境要求

組件 版本要求 說(shuō)明
Redis 8.x 主從節(jié)點(diǎn)和 Sentinel 版本保持一致
操作系統(tǒng) Linux(內(nèi)核 4.x+) 需要穩(wěn)定的網(wǎng)絡(luò)棧
節(jié)點(diǎn)數(shù)量 最少 3 個(gè) Sentinel 保證 quorum 投票的奇數(shù)原則
網(wǎng)絡(luò)延遲 < 10ms(Sentinel 節(jié)點(diǎn)間) 延遲過(guò)高影響故障檢測(cè)準(zhǔn)確性

二、詳細(xì)步驟

2.1 Sentinel 工作原理

2.1.1 主觀下線與客觀下線

理解這兩個(gè)概念是正確配置 Sentinel 的基礎(chǔ)。

主觀下線(SDOWN,Subjectively Down):?jiǎn)蝹€(gè) Sentinel 實(shí)例認(rèn)為某節(jié)點(diǎn)不可用。判斷依據(jù)是在down-after-milliseconds時(shí)間內(nèi)沒(méi)有收到節(jié)點(diǎn)的有效響應(yīng)(PING 返回 PONG 或錯(cuò)誤響應(yīng))。SDOWN 只是單個(gè) Sentinel 的主觀判斷,不會(huì)觸發(fā)故障轉(zhuǎn)移。

客觀下線(ODOWN,Objectively Down):多個(gè) Sentinel 實(shí)例都認(rèn)為主節(jié)點(diǎn)不可用,且達(dá)到 quorum 數(shù)量。只有主節(jié)點(diǎn)才有 ODOWN 狀態(tài),從節(jié)點(diǎn)和 Sentinel 節(jié)點(diǎn)只有 SDOWN。ODOWN 是觸發(fā)故障轉(zhuǎn)移的前提條件。

Sentinel A: PING master → 無(wú)響應(yīng) → 標(biāo)記 master SDOWN
Sentinel A: 詢問(wèn) Sentinel B、C →"你們也認(rèn)為 master 掛了嗎?"
Sentinel B:"是的,我也收不到響應(yīng)"
Sentinel C:"我也是"
達(dá)到 quorum=2 → Sentinel A 標(biāo)記 master ODOWN → 開始 Leader 選舉

2.1.2 故障轉(zhuǎn)移流程

Leader 選舉:Sentinel 集群通過(guò) Raft 協(xié)議選出一個(gè) Leader,由 Leader 負(fù)責(zé)執(zhí)行故障轉(zhuǎn)移

新主節(jié)點(diǎn)選擇:Leader 從可用從節(jié)點(diǎn)中按以下優(yōu)先級(jí)選擇新主節(jié)點(diǎn):

replica-priority(slave-priority)值最小的優(yōu)先(0 表示永不成為主節(jié)點(diǎn))

復(fù)制偏移量最大的優(yōu)先(數(shù)據(jù)最新)

Run ID 字典序最小的優(yōu)先(決勝條件)

執(zhí)行切換:向選中的從節(jié)點(diǎn)發(fā)送REPLICAOF NO ONE,使其成為新主節(jié)點(diǎn)

重新配置:通知其他從節(jié)點(diǎn)復(fù)制新主節(jié)點(diǎn),更新 Sentinel 配置文件

通知客戶端:通過(guò) Pub/Sub 頻道+switch-master發(fā)布切換事件

2.2 3節(jié)點(diǎn) Sentinel 集群配置

2.2.1 節(jié)點(diǎn)規(guī)劃

節(jié)點(diǎn)角色    IP       端口
Redis 主節(jié)點(diǎn)  10.0.1.10    6379
Redis 從節(jié)點(diǎn)1  10.0.1.11    6379
Redis 從節(jié)點(diǎn)2  10.0.1.12    6379
Sentinel 1   10.0.1.10    26379
Sentinel 2   10.0.1.11    26379
Sentinel 3   10.0.1.12    26379

Sentinel 和 Redis 節(jié)點(diǎn)共用機(jī)器是常見的部署方式,節(jié)省資源。但要確保 Sentinel 進(jìn)程和 Redis 進(jìn)程的故障是獨(dú)立的——Redis 掛了不代表 Sentinel 也掛。

2.2.2 Redis 主節(jié)點(diǎn)配置

# 文件路徑:/etc/redis/redis.conf(主節(jié)點(diǎn) 10.0.1.10)
bind 10.0.1.10 127.0.0.1
port 6379
daemonize yes
pidfile /var/run/redis/redis-server.pid
logfile /var/log/redis/redis-server.log
dir /var/lib/redis

# 持久化配置
save 900 1
save 300 10
save 60 10000
appendonly yes
appendfsync everysec

# 認(rèn)證密碼(主從和 Sentinel 必須一致)
requirepass "Redis@Secure2024!"
masterauth "Redis@Secure2024!"

# 內(nèi)存配置
maxmemory 8gb
maxmemory-policy allkeys-lru

# 腦裂防護(hù):至少 1 個(gè)從節(jié)點(diǎn)同步才接受寫入
min-replicas-to-write 1
min-replicas-max-lag 10

2.2.3 Redis 從節(jié)點(diǎn)配置

# 文件路徑:/etc/redis/redis.conf(從節(jié)點(diǎn) 10.0.1.11 和 10.0.1.12)
bind 10.0.1.11 127.0.0.1  # 10.0.1.12 節(jié)點(diǎn)改為對(duì)應(yīng) IP
port 6379
daemonize yes
logfile /var/log/redis/redis-server.log
dir /var/lib/redis

# 指定主節(jié)點(diǎn)
replicaof 10.0.1.10 6379

# 認(rèn)證
requirepass "Redis@Secure2024!"
masterauth "Redis@Secure2024!"

# 從節(jié)點(diǎn)只讀(默認(rèn)已是 yes,顯式聲明更清晰)
replica-read-only yes

# 從節(jié)點(diǎn)優(yōu)先級(jí)(數(shù)值越小越優(yōu)先成為新主節(jié)點(diǎn))
# 10.0.1.11 設(shè)為 100,10.0.1.12 設(shè)為 200,優(yōu)先選 .11 作為新主
replica-priority 100

# 持久化
appendonly yes
appendfsync everysec

2.2.4 Sentinel 配置

三個(gè) Sentinel 節(jié)點(diǎn)使用相同的配置文件,只有bind地址不同:

# 文件路徑:/etc/redis/sentinel.conf
# 三個(gè)節(jié)點(diǎn)分別修改 bind 為各自 IP

bind 10.0.1.10 127.0.0.1  # 節(jié)點(diǎn)1;節(jié)點(diǎn)2改為10.0.1.11;節(jié)點(diǎn)3改為10.0.1.12
port 26379
daemonize yes
pidfile /var/run/redis/redis-sentinel.pid
logfile /var/log/redis/sentinel.log
dir /var/lib/redis

# 監(jiān)控主節(jié)點(diǎn),quorum=2 表示需要 2 個(gè) Sentinel 同意才能觸發(fā)故障轉(zhuǎn)移
sentinel monitor mymaster 10.0.1.10 6379 2

# 主節(jié)點(diǎn)認(rèn)證密碼
sentinel auth-pass mymaster Redis@Secure2024!

# 主節(jié)點(diǎn)超過(guò) 5000ms 無(wú)響應(yīng),標(biāo)記為 SDOWN
sentinel down-after-milliseconds mymaster 5000

# 故障轉(zhuǎn)移超時(shí)時(shí)間:3 分鐘內(nèi)未完成則認(rèn)為失敗
sentinel failover-timeout mymaster 180000

# 故障轉(zhuǎn)移后,同時(shí)向新主節(jié)點(diǎn)同步的從節(jié)點(diǎn)數(shù)量
# 設(shè)為 1 表示逐個(gè)同步,避免所有從節(jié)點(diǎn)同時(shí)不可讀
sentinel parallel-syncs mymaster 1

# Sentinel 自身的訪問(wèn)密碼(Redis 6.2+ 支持)
requirepass "Sentinel@Secure2024!"
sentinel sentinel-pass Sentinel@Secure2024!

2.2.5 啟動(dòng)順序

# 1. 先啟動(dòng)主節(jié)點(diǎn)
systemctl start redis-server # 10.0.1.10

# 2. 啟動(dòng)從節(jié)點(diǎn)
systemctl start redis-server # 10.0.1.11 和 10.0.1.12

# 3. 驗(yàn)證主從復(fù)制狀態(tài)
redis-cli -h 10.0.1.10 -p 6379 -a'Redis@Secure2024!'info replication

# 預(yù)期輸出關(guān)鍵字段:
# role:master
# connected_slaves:2
# slave0:ip=10.0.1.11,port=6379,state=online,offset=xxx,lag=0
# slave1:ip=10.0.1.12,port=6379,state=online,offset=xxx,lag=0

# 4. 啟動(dòng) Sentinel(三個(gè)節(jié)點(diǎn)都要啟動(dòng))
redis-sentinel /etc/redis/sentinel.conf
# 或
systemctl start redis-sentinel

# 5. 驗(yàn)證 Sentinel 狀態(tài)
redis-cli -h 10.0.1.10 -p 26379 -a'Sentinel@Secure2024!'sentinel masters

2.3 驗(yàn)證故障轉(zhuǎn)移

# 模擬主節(jié)點(diǎn)故障
redis-cli -h 10.0.1.10 -p 6379 -a'Redis@Secure2024!'DEBUG sleep 30

# 在另一個(gè)終端觀察 Sentinel 日志
tail -f /var/log/redis/sentinel.log

# 預(yù)期日志序列:
# +sdown master mymaster 10.0.1.10 6379
# +odown master mymaster 10.0.1.10 6379#quorum2/2
# +try-failover master mymaster 10.0.1.10 6379
# +elected-leader master mymaster 10.0.1.10 6379
# +selected-slave slave 10.0.1.11:6379 mymaster 10.0.1.10 6379
# +promoted-slave slave 10.0.1.11:6379 mymaster 10.0.1.10 6379
# +switch-master mymaster 10.0.1.10 6379 10.0.1.11 6379

# 故障轉(zhuǎn)移完成后,查詢新主節(jié)點(diǎn)
redis-cli -h 10.0.1.10 -p 26379 -a'Sentinel@Secure2024!'sentinel get-master-addr-by-name mymaster

三、示例代碼和配置

3.1 客戶端接入 Sentinel

客戶端不能直連 Redis 主節(jié)點(diǎn) IP,必須通過(guò) Sentinel 獲取當(dāng)前主節(jié)點(diǎn)地址,否則主從切換后客戶端無(wú)法自動(dòng)重連。

3.1.1 Python(redis-py)

importredis
fromredis.sentinelimportSentinel

# 連接 Sentinel 集群
sentinel = Sentinel(
  [
    ('10.0.1.10',26379),
    ('10.0.1.11',26379),
    ('10.0.1.12',26379),
  ],
  socket_timeout=0.5,
  sentinel_kwargs={'password':'Sentinel@Secure2024!'}
)

# 獲取主節(jié)點(diǎn)連接(自動(dòng)處理故障轉(zhuǎn)移后的地址變更)
master = sentinel.master_for(
 'mymaster',
  socket_timeout=0.5,
  password='Redis@Secure2024!',
  db=0,
  retry_on_timeout=True
)

# 獲取從節(jié)點(diǎn)連接(讀操作)
slave = sentinel.slave_for(
 'mymaster',
  socket_timeout=0.5,
  password='Redis@Secure2024!',
  db=0
)

# 寫操作走主節(jié)點(diǎn)
master.set('key','value', ex=3600)

# 讀操作走從節(jié)點(diǎn)
value = slave.get('key')

3.1.2 Java(Jedis)

importredis.clients.jedis.JedisSentinelPool;
importredis.clients.jedis.Jedis;
importredis.clients.jedis.JedisPoolConfig;
importjava.util.HashSet;
importjava.util.Set;

// 配置連接池
JedisPoolConfig poolConfig =newJedisPoolConfig();
poolConfig.setMaxTotal(50);
poolConfig.setMaxIdle(10);
poolConfig.setMinIdle(5);
poolConfig.setTestOnBorrow(true);

// Sentinel 節(jié)點(diǎn)集合
Set sentinels =newHashSet<>();
sentinels.add("10.0.1.10:26379");
sentinels.add("10.0.1.11:26379");
sentinels.add("10.0.1.12:26379");

// 創(chuàng)建 Sentinel 連接池
JedisSentinelPool sentinelPool =newJedisSentinelPool(
 "mymaster",
  sentinels,
  poolConfig,
 2000,     // connectionTimeout
 2000,     // soTimeout
 "Redis@Secure2024!", // Redis 密碼
 0,       // database
 null,     // clientName
 0,       // sentinelConnectionTimeout
 0,       // sentinelSoTimeout
 "Sentinel@Secure2024!", // Sentinel 密碼
 null     // sentinelClientName
);

// 使用連接
try(Jedis jedis = sentinelPool.getResource()) {
  jedis.set("key","value");
  String value = jedis.get("key");
}

3.2 Sentinel 監(jiān)控腳本

#!/bin/bash
# 文件名:/opt/redis/scripts/sentinel_check.sh
# 功能:檢查 Sentinel 集群健康狀態(tài),輸出關(guān)鍵指標(biāo)

SENTINEL_HOSTS=("10.0.1.10""10.0.1.11""10.0.1.12")
SENTINEL_PORT=26379
SENTINEL_PASS="Sentinel@Secure2024!"
MASTER_NAME="mymaster"

echo"===== Sentinel 集群狀態(tài)檢查 ====="
echo"時(shí)間:$(date)"
echo""

# 檢查每個(gè) Sentinel 節(jié)點(diǎn)
forhostin"${SENTINEL_HOSTS[@]}";do
 echo"--- Sentinel:${host}:${SENTINEL_PORT}---"

 # 獲取當(dāng)前主節(jié)點(diǎn)地址
  master_info=$(redis-cli -h"${host}"-p"${SENTINEL_PORT}"
    -a"${SENTINEL_PASS}"--no-auth-warning 
    sentinel get-master-addr-by-name"${MASTER_NAME}"2>/dev/null)

 if[ $? -eq 0 ];then
    master_ip=$(echo"${master_info}"| head -1)
    master_port=$(echo"${master_info}"| tail -1)
   echo" 當(dāng)前主節(jié)點(diǎn):${master_ip}:${master_port}"
 else
   echo" [ERROR] 無(wú)法連接 Sentinel"
   continue
 fi

 # 獲取 Sentinel 詳細(xì)信息
  sentinel_info=$(redis-cli -h"${host}"-p"${SENTINEL_PORT}"
    -a"${SENTINEL_PASS}"--no-auth-warning 
    sentinel masters 2>/dev/null)

 # 提取關(guān)鍵字段
  num_slaves=$(echo"${sentinel_info}"| grep -A1"num-slaves"| tail -1)
  num_sentinels=$(echo"${sentinel_info}"| grep -A1"num-other-sentinels"| tail -1)
  quorum=$(echo"${sentinel_info}"| grep -A1"quorum"| tail -1)
  flags=$(echo"${sentinel_info}"| grep -A1"^flags$"| tail -1)

 echo" 從節(jié)點(diǎn)數(shù)量:${num_slaves}"
 echo" 其他 Sentinel 數(shù)量:${num_sentinels}"
 echo" Quorum:${quorum}"
 echo" 主節(jié)點(diǎn)狀態(tài):${flags}"
 echo""
done

3.3 腦裂防護(hù)配置詳解

腦裂(Split-Brain)是 Sentinel 模式最危險(xiǎn)的場(chǎng)景:主節(jié)點(diǎn)因網(wǎng)絡(luò)分區(qū)與 Sentinel 失聯(lián),Sentinel 誤判主節(jié)點(diǎn)宕機(jī)并選出新主節(jié)點(diǎn),此時(shí)集群中存在兩個(gè)主節(jié)點(diǎn),客戶端寫入數(shù)據(jù)后,網(wǎng)絡(luò)恢復(fù)時(shí)舊主節(jié)點(diǎn)的數(shù)據(jù)會(huì)被覆蓋丟失。

# 在主節(jié)點(diǎn) redis.conf 中配置
# 含義:至少有 1 個(gè)從節(jié)點(diǎn)在線且復(fù)制延遲不超過(guò) 10 秒,才接受寫入
# 當(dāng)主節(jié)點(diǎn)與所有從節(jié)點(diǎn)斷開(網(wǎng)絡(luò)分區(qū)場(chǎng)景),主節(jié)點(diǎn)自動(dòng)拒絕寫入
min-replicas-to-write 1
min-replicas-max-lag 10

這個(gè)配置的代價(jià)是:當(dāng)從節(jié)點(diǎn)全部宕機(jī)或網(wǎng)絡(luò)延遲超過(guò) 10 秒時(shí),主節(jié)點(diǎn)會(huì)拒絕寫入,業(yè)務(wù)會(huì)報(bào)錯(cuò)。這是一個(gè)可用性和一致性之間的權(quán)衡,對(duì)數(shù)據(jù)一致性要求高的場(chǎng)景(金融、訂單)應(yīng)該開啟,對(duì)可用性優(yōu)先的場(chǎng)景(緩存、會(huì)話)可以不開啟。

四、最佳實(shí)踐和注意事項(xiàng)

4.1 最佳實(shí)踐

4.1.1 Sentinel 節(jié)點(diǎn)部署

奇數(shù)原則:Sentinel 節(jié)點(diǎn)數(shù)量必須是奇數(shù)(3、5、7),quorum 設(shè)為(n/2)+1。3 節(jié)點(diǎn)集群 quorum=2,允許 1 個(gè) Sentinel 故障;5 節(jié)點(diǎn)集群 quorum=3,允許 2 個(gè)故障。不要部署 2 個(gè) Sentinel,quorum=2 時(shí)任意一個(gè)故障都會(huì)導(dǎo)致無(wú)法完成故障轉(zhuǎn)移。

跨機(jī)架部署:3 個(gè) Sentinel 節(jié)點(diǎn)分布在 3 個(gè)不同的物理機(jī)架或可用區(qū),避免單機(jī)架故障導(dǎo)致 Sentinel 集群失去 quorum。

Sentinel 與 Redis 分離:資源充足時(shí),Sentinel 進(jìn)程部署在獨(dú)立機(jī)器上,避免 Redis 進(jìn)程消耗大量?jī)?nèi)存時(shí)影響 Sentinel 的心跳響應(yīng)。

4.1.2 故障轉(zhuǎn)移參數(shù)調(diào)優(yōu)

down-after-milliseconds直接決定故障檢測(cè)速度和誤判率之間的平衡:

設(shè)置過(guò)?。? 3000ms):網(wǎng)絡(luò)抖動(dòng)、GC 停頓、系統(tǒng)負(fù)載高峰都可能觸發(fā)誤判

設(shè)置過(guò)大(> 30000ms):真實(shí)故障的檢測(cè)時(shí)間過(guò)長(zhǎng),業(yè)務(wù)中斷時(shí)間延長(zhǎng)

推薦值:5000-10000ms,結(jié)合業(yè)務(wù)對(duì)中斷時(shí)間的容忍度決定

4.1.3 持久化配置

Sentinel 模式下主從節(jié)點(diǎn)都應(yīng)開啟 AOF 持久化,appendfsync設(shè)為everysec。如果主節(jié)點(diǎn)關(guān)閉持久化,主節(jié)點(diǎn)重啟后會(huì)以空數(shù)據(jù)集啟動(dòng),從節(jié)點(diǎn)同步后數(shù)據(jù)全部丟失——這是一個(gè)經(jīng)典的數(shù)據(jù)丟失場(chǎng)景。

4.2 注意事項(xiàng)

4.2.1 配置注意事項(xiàng)

警告:Sentinel 在完成故障轉(zhuǎn)移后會(huì)自動(dòng)修改sentinel.conf文件。如果用配置管理工具(Ansible、Chef)管理該文件,必須排除 Sentinel 自動(dòng)寫入的字段,否則下次配置推送會(huì)覆蓋 Sentinel 的狀態(tài)記錄,導(dǎo)致集群狀態(tài)混亂。

不要在 Sentinel 運(yùn)行時(shí)手動(dòng)編輯sentinel.conf

主從節(jié)點(diǎn)的requirepass和masterauth必須設(shè)置為相同的密碼

故障轉(zhuǎn)移完成后,原主節(jié)點(diǎn)恢復(fù)時(shí)會(huì)自動(dòng)降級(jí)為從節(jié)點(diǎn),不需要手動(dòng)干預(yù)

4.2.2 常見錯(cuò)誤

錯(cuò)誤現(xiàn)象 原因分析 解決方案
Sentinel 持續(xù)出現(xiàn)+sdown但不觸發(fā)故障轉(zhuǎn)移 quorum 未達(dá)到,部分 Sentinel 節(jié)點(diǎn)無(wú)法通信 檢查 Sentinel 節(jié)點(diǎn)間的網(wǎng)絡(luò)連通性和防火墻規(guī)則
故障轉(zhuǎn)移后客戶端仍連接舊主節(jié)點(diǎn) 客戶端硬編碼了主節(jié)點(diǎn) IP 改造客戶端使用 Sentinel 連接池
從節(jié)點(diǎn)復(fù)制延遲持續(xù)增大 主節(jié)點(diǎn)寫入量超過(guò)從節(jié)點(diǎn)同步能力 增大repl_backlog_size,檢查網(wǎng)絡(luò)帶寬
Sentinel 選舉超時(shí),故障轉(zhuǎn)移失敗 failover-timeout 設(shè)置過(guò)小 增大failover-timeout,檢查 Sentinel 節(jié)點(diǎn)網(wǎng)絡(luò)

4.3 Sentinel vs Cluster 選型

選 Sentinel 的場(chǎng)景:數(shù)據(jù)量在單機(jī)內(nèi)存范圍內(nèi)、寫入 QPS < 10 萬(wàn)、需要強(qiáng)一致的 Lua 腳本或事務(wù)操作。

選 Cluster 的場(chǎng)景:數(shù)據(jù)量超過(guò)單機(jī)內(nèi)存、寫入 QPS 超過(guò)單主節(jié)點(diǎn)瓶頸、需要線性擴(kuò)展寫入能力。

五、故障排查和監(jiān)控

5.1 故障排查

5.1.1 日志查看

# 實(shí)時(shí)查看 Sentinel 日志
tail -f /var/log/redis/sentinel.log

# 查看最近的故障轉(zhuǎn)移記錄
grep"switch-master|failover|odown"/var/log/redis/sentinel.log | tail -50

# 查看主節(jié)點(diǎn)復(fù)制狀態(tài)
redis-cli -h 10.0.1.10 -p 6379 -a'Redis@Secure2024!'info replication

# 查看復(fù)制延遲(lag 字段,單位秒)
redis-cli -h 10.0.1.10 -p 6379 -a'Redis@Secure2024!'
 info replication | grep"slave[0-9]"

5.1.2 常見問(wèn)題排查

問(wèn)題一:Sentinel 無(wú)法發(fā)現(xiàn)從節(jié)點(diǎn)

# 檢查 Sentinel 已知的從節(jié)點(diǎn)列表
redis-cli -h 10.0.1.10 -p 26379 -a'Sentinel@Secure2024!'
 sentinel slaves mymaster

# Sentinel 通過(guò)主節(jié)點(diǎn)的 INFO 命令發(fā)現(xiàn)從節(jié)點(diǎn)
# 如果從節(jié)點(diǎn)未出現(xiàn),檢查從節(jié)點(diǎn)的 replicaof 配置是否正確
redis-cli -h 10.0.1.11 -p 6379 -a'Redis@Secure2024!'info replication

問(wèn)題二:故障轉(zhuǎn)移后舊主節(jié)點(diǎn)無(wú)法加入集群

# 舊主節(jié)點(diǎn)恢復(fù)后,檢查其角色
redis-cli -h 10.0.1.10 -p 6379 -a'Redis@Secure2024!'info replication

# 如果仍顯示 role:master,手動(dòng)設(shè)置為從節(jié)點(diǎn)
redis-cli -h 10.0.1.10 -p 6379 -a'Redis@Secure2024!'
 replicaof 10.0.1.11 6379

5.2 性能監(jiān)控

5.2.1 監(jiān)控指標(biāo)說(shuō)明

指標(biāo)名稱 正常范圍 告警閾值 說(shuō)明
復(fù)制延遲(lag) 0-1 秒 > 10 秒 從節(jié)點(diǎn)數(shù)據(jù)落后主節(jié)點(diǎn)的時(shí)間
connected_slaves 等于從節(jié)點(diǎn)數(shù) < 預(yù)期數(shù)量 在線從節(jié)點(diǎn)數(shù)量
Sentinel 響應(yīng)時(shí)間 < 5ms > 100ms Sentinel 進(jìn)程健康狀態(tài)
master_last_io_seconds_ago < 5 > 30 主從最后通信時(shí)間間隔
repl_backlog_active 1 0 復(fù)制積壓緩沖區(qū)是否活躍

5.3 備份與恢復(fù)

#!/bin/bash
# 從從節(jié)點(diǎn)備份,避免影響主節(jié)點(diǎn)性能
SLAVE_HOST="10.0.1.11"
REDIS_PASS="Redis@Secure2024!"
BACKUP_DIR="/opt/redis/backups"
DATE=$(date +%Y%m%d_%H%M%S)

mkdir -p"${BACKUP_DIR}"

# 觸發(fā)從節(jié)點(diǎn) BGSAVE
redis-cli -h"${SLAVE_HOST}"-p 6379 -a"${REDIS_PASS}"--no-auth-warning BGSAVE

# 等待完成
sleep 5

# 復(fù)制 RDB 文件
cp /var/lib/redis/dump.rdb"${BACKUP_DIR}/dump_${DATE}.rdb"
echo"備份完成:${BACKUP_DIR}/dump_${DATE}.rdb"

# 保留最近 7 天
find"${BACKUP_DIR}"-name"dump_*.rdb"-mtime +7 -delete

六、總結(jié)

6.1 技術(shù)要點(diǎn)回顧

SDOWN vs ODOWN:?jiǎn)蝹€(gè) Sentinel 的主觀判斷不觸發(fā)切換,需要達(dá)到 quorum 數(shù)量的 Sentinel 共同確認(rèn)才進(jìn)入客觀下線狀態(tài)

quorum 配置:3 節(jié)點(diǎn)集群設(shè)為 2,是可用性和防誤判的最佳平衡點(diǎn)

腦裂防護(hù):min-replicas-to-write讓主節(jié)點(diǎn)在網(wǎng)絡(luò)分區(qū)時(shí)主動(dòng)拒絕寫入,避免數(shù)據(jù)丟失

客戶端接入:必須通過(guò) Sentinel 發(fā)現(xiàn)主節(jié)點(diǎn)地址,不能硬編碼 Redis 主節(jié)點(diǎn) IP

replica-priority:通過(guò)優(yōu)先級(jí)控制故障轉(zhuǎn)移時(shí)的新主節(jié)點(diǎn)選擇

6.2 進(jìn)階學(xué)習(xí)方向

Redis Cluster:當(dāng)單主節(jié)點(diǎn)無(wú)法承載數(shù)據(jù)量或?qū)懭雺毫r(shí),Cluster 是下一步演進(jìn)方向

Sentinel 與 Proxy 結(jié)合:在 Sentinel 前部署 Twemproxy 或 Codis,對(duì)客戶端屏蔽 Sentinel 發(fā)現(xiàn)邏輯

Redis 持久化深度優(yōu)化:AOF rewrite 策略、RDB 與 AOF 混合持久化對(duì)故障恢復(fù)時(shí)間的影響

6.3 參考資料

Redis Sentinel 官方文檔

Redis 8.x Release Notes

Redis 高可用架構(gòu)設(shè)計(jì)

附錄

A. 命令速查表

# 查詢當(dāng)前主節(jié)點(diǎn)地址
redis-cli -p 26379 -a'pass'sentinel get-master-addr-by-name mymaster

# 查看所有主節(jié)點(diǎn)信息
redis-cli -p 26379 -a'pass'sentinel masters

# 查看從節(jié)點(diǎn)列表
redis-cli -p 26379 -a'pass'sentinel slaves mymaster

# 查看其他 Sentinel 節(jié)點(diǎn)
redis-cli -p 26379 -a'pass'sentinel sentinels mymaster

# 手動(dòng)觸發(fā)故障轉(zhuǎn)移(測(cè)試用)
redis-cli -p 26379 -a'pass'sentinel failover mymaster

# 重置 Sentinel 狀態(tài)
redis-cli -p 26379 -a'pass'sentinel reset mymaster

B. 配置參數(shù)詳解

參數(shù) 默認(rèn)值 說(shuō)明
down-after-milliseconds 30000 節(jié)點(diǎn)無(wú)響應(yīng)多少毫秒后標(biāo)記為 SDOWN
failover-timeout 180000 故障轉(zhuǎn)移超時(shí)時(shí)間(毫秒)
parallel-syncs 1 故障轉(zhuǎn)移后同時(shí)同步的從節(jié)點(diǎn)數(shù)量
quorum 需手動(dòng)設(shè)置 觸發(fā) ODOWN 所需的最少 Sentinel 數(shù)量
min-replicas-to-write 0 主節(jié)點(diǎn)接受寫入所需的最少在線從節(jié)點(diǎn)數(shù)
min-replicas-max-lag 10 從節(jié)點(diǎn)復(fù)制延遲超過(guò)此值(秒)視為不可用

C. 術(shù)語(yǔ)表

術(shù)語(yǔ) 英文 解釋
主觀下線 SDOWN (Subjectively Down) 單個(gè) Sentinel 認(rèn)為節(jié)點(diǎn)不可用
客觀下線 ODOWN (Objectively Down) 多個(gè) Sentinel 共同確認(rèn)節(jié)點(diǎn)不可用
仲裁數(shù) Quorum 觸發(fā)故障轉(zhuǎn)移所需的最少 Sentinel 同意數(shù)
腦裂 Split-Brain 網(wǎng)絡(luò)分區(qū)導(dǎo)致集群中出現(xiàn)兩個(gè)主節(jié)點(diǎn)的場(chǎng)景
復(fù)制積壓 Replication Backlog 主節(jié)點(diǎn)保存的最近寫命令緩沖區(qū),用于斷線重連后的增量同步

聲明:本文內(nèi)容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點(diǎn)僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場(chǎng)。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問(wèn)題,請(qǐng)聯(lián)系本站處理。 舉報(bào)投訴
  • 集群
    +關(guān)注

    關(guān)注

    0

    文章

    143

    瀏覽量

    17662
  • 組件
    +關(guān)注

    關(guān)注

    1

    文章

    573

    瀏覽量

    19022
  • Redis
    +關(guān)注

    關(guān)注

    0

    文章

    392

    瀏覽量

    12185

原文標(biāo)題:Redis哨兵模式詳解:自動(dòng)故障檢測(cè)與主從切換實(shí)戰(zhàn)

文章出處:【微信號(hào):magedu-Linux,微信公眾號(hào):馬哥Linux運(yùn)維】歡迎添加關(guān)注!文章轉(zhuǎn)載請(qǐng)注明出處。

收藏 人收藏
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群

    評(píng)論

    相關(guān)推薦
    熱點(diǎn)推薦

    redis集群環(huán)境安裝及配置

    redis集群主從配置
    發(fā)表于 03-08 09:59

    Redis主從復(fù)制的作用和步驟

    Redis青銅修煉手冊(cè)(五) --- Redis主從復(fù)制
    發(fā)表于 06-27 07:20

    機(jī)器在自動(dòng)循環(huán)和手動(dòng)模式下的切換

    如果系統(tǒng)出現(xiàn)故障,機(jī)器操作員將無(wú)法切換模式,或者如果機(jī)器在自動(dòng)循環(huán)模式下,機(jī)器操作員則不能進(jìn)入手動(dòng)模式
    發(fā)表于 05-08 11:02 ?8990次閱讀

    詳解Redis主從復(fù)制和哨兵機(jī)制

    Redis主從復(fù)制主要有兩個(gè)角色,主機(jī)(master)對(duì)外提供讀寫功能,從機(jī)(slave)對(duì)外只提供讀功能,主機(jī)定期把數(shù)據(jù)同步到從機(jī)上保證數(shù)據(jù)一致性。
    的頭像 發(fā)表于 05-03 18:14 ?2322次閱讀
    詳解<b class='flag-5'>Redis</b><b class='flag-5'>主從</b>復(fù)制和<b class='flag-5'>哨兵</b>機(jī)制

    Redis的四種模式復(fù)制、哨兵、Cluster以及集群模式

    概述 Redis作為緩存的高效中間件,在我們?nèi)粘5拈_發(fā)中被頻繁的使用,今天就來(lái)說(shuō)一說(shuō)Redis的四種模式,分別是「單機(jī)版、主從復(fù)制、哨兵、以
    的頭像 發(fā)表于 09-30 17:51 ?3245次閱讀
    <b class='flag-5'>Redis</b>的四種<b class='flag-5'>模式</b>復(fù)制、<b class='flag-5'>哨兵</b>、Cluster以及集群<b class='flag-5'>模式</b>

    談?wù)?b class='flag-5'>Redis怎樣配置實(shí)現(xiàn)主從復(fù)制?

    之前總結(jié)過(guò)redis的持久化機(jī)制:深度剖析Redis持久化機(jī)制,持久化機(jī)制主要解決redis數(shù)據(jù)單機(jī)備份問(wèn)題;redis的高可用需要考慮數(shù)據(jù)的多機(jī)備份,多機(jī)備份通過(guò)
    發(fā)表于 01-31 11:31 ?999次閱讀

    Redis主從、哨兵、Redis Cluster集群

    ? 前言 今天跟小伙伴們一起學(xué)習(xí)Redis主從、哨兵、Redis Cluster集群。 Redis主從
    的頭像 發(fā)表于 06-12 14:58 ?1651次閱讀
    <b class='flag-5'>Redis</b>的<b class='flag-5'>主從</b>、<b class='flag-5'>哨兵</b>、<b class='flag-5'>Redis</b> Cluster集群

    什么是Redis主從復(fù)制

    Master節(jié)點(diǎn)的能力,主掛了服務(wù)就不可以寫數(shù)據(jù)了。僅僅就是增強(qiáng)了應(yīng)用讀數(shù)據(jù)的并發(fā)量同時(shí)做數(shù)據(jù)備份。 一般生產(chǎn)環(huán)境會(huì)采用 哨兵 或者 Redis Cluster 這種具備Master自動(dòng)選舉的方案,我們學(xué)習(xí)時(shí)還是要掌握
    的頭像 發(fā)表于 10-09 15:09 ?1040次閱讀
    什么是<b class='flag-5'>Redis</b><b class='flag-5'>主從</b>復(fù)制

    mysql主從復(fù)制三種模式

    MySQL主從復(fù)制是一種常見的數(shù)據(jù)同步方式,它可以實(shí)現(xiàn)將一個(gè)數(shù)據(jù)庫(kù)的更改同步到其他多個(gè)數(shù)據(jù)庫(kù)的功能。主從復(fù)制可以提高數(shù)據(jù)庫(kù)的可用性和性能,以及提供故障恢復(fù)和數(shù)據(jù)備份的支持。在MySQL中,有三種
    的頭像 發(fā)表于 11-16 14:04 ?2389次閱讀

    redis查看主從節(jié)點(diǎn)命令

    Redis是一種開源的內(nèi)存數(shù)據(jù)結(jié)構(gòu)存儲(chǔ)系統(tǒng),常被用作數(shù)據(jù)庫(kù)、緩存和消息中間件。在Redis中,可以通過(guò)一些命令來(lái)查看主從節(jié)點(diǎn)的信息,以便進(jìn)行監(jiān)控和管理。 Redis
    的頭像 發(fā)表于 12-04 11:44 ?2579次閱讀

    redis哨兵和集群有什么區(qū)別

    重要的區(qū)別。 哨兵模式: 哨兵模式是一種用于實(shí)現(xiàn)Redis高可用性的方案。在哨兵
    的頭像 發(fā)表于 12-04 14:53 ?3887次閱讀

    Redis使用重要的兩個(gè)機(jī)制:Reids持久化和主從復(fù)制

    今天這篇文章,我們一起了解 Redis 使用中非常重要的兩個(gè)機(jī)制:Reids 持久化和主從復(fù)制。 我們都知道Redis是一個(gè)內(nèi)存數(shù)據(jù)庫(kù),在學(xué)習(xí)主從同步之前,我們首先要想到
    的頭像 發(fā)表于 12-18 10:33 ?746次閱讀
    <b class='flag-5'>Redis</b>使用重要的兩個(gè)機(jī)制:Reids持久化和<b class='flag-5'>主從</b>復(fù)制

    Redis Cluster之故障轉(zhuǎn)移

    1. Redis Cluster 簡(jiǎn)介 Redis Cluster 是 Redis 官方提供的 Redis 集群功能。 為什么要實(shí)現(xiàn) Redis
    的頭像 發(fā)表于 01-20 09:21 ?1417次閱讀
    <b class='flag-5'>Redis</b> Cluster之<b class='flag-5'>故障</b>轉(zhuǎn)移

    Redis實(shí)戰(zhàn)筆記

    《 2024最新Redis 實(shí)戰(zhàn)筆記》,這份筆記對(duì) Redis 的相關(guān)知識(shí)做了系統(tǒng)全面的介紹,還是PDF版本,可自由復(fù)制,特別適合 Redis 初學(xué)者快速入門和提高。 ? 本筆記適合人
    的頭像 發(fā)表于 02-09 09:12 ?774次閱讀
    <b class='flag-5'>Redis</b><b class='flag-5'>實(shí)戰(zhàn)</b>筆記

    Redis集群部署與性能優(yōu)化實(shí)戰(zhàn)

    Redis作為高性能的內(nèi)存數(shù)據(jù)庫(kù),在現(xiàn)代互聯(lián)網(wǎng)架構(gòu)中扮演著關(guān)鍵角色。作為運(yùn)維工程師,掌握Redis的部署、配置和優(yōu)化技能至關(guān)重要。本文將從實(shí)戰(zhàn)角度出發(fā),詳細(xì)介紹Redis集群的搭建、性
    的頭像 發(fā)表于 07-08 17:56 ?853次閱讀