redis-sentinel基礎(chǔ)概念及部署流程
一. 引言
Redis Sentinel 是 redis 官方提供的高可用解決方案,主要用于監(jiān)控 Redis 主從集群,在主節(jié)點故障時自動完成故障轉(zhuǎn)移,確保服務(wù)持續(xù)可用。
二. 核心功能
- 1. 監(jiān)控(monitoring):持續(xù)檢查主節(jié)點(master)和從節(jié)點(slave)是否正常運行。
- 2. 自動故障轉(zhuǎn)移(automatic failover):當(dāng)主節(jié)點故障時,自動將一個從節(jié)點晉升為新主節(jié)點,并讓其他從節(jié)點指向新主節(jié)點。
- 3. 通知(notification):通過 API 向管理員或其他應(yīng)用程序發(fā)送故障通知。
- 4. 配置提供者(configuration provider):客戶端可通過 Sentinel 獲取當(dāng)前主節(jié)點地址(無需硬編碼)。
三. 核心組件
- 1. sentinel 節(jié)點:特殊的 Redis 進程(非數(shù)據(jù)存儲節(jié)點),通常部署 3 個及以上以保證自身高可用。
- 2. 主節(jié)點(master):負(fù)責(zé)處理寫操作的 redis 節(jié)點。
- 3. 從節(jié)點(slave):同步主節(jié)點數(shù)據(jù),提供讀服務(wù),主節(jié)點故障時可被晉升。
四. 故障轉(zhuǎn)移流程
- 1. 多個 sentinel 檢測到主節(jié)點故障。
- 2. 選舉一個 sentinel 作為領(lǐng)導(dǎo)者,負(fù)責(zé)執(zhí)行故障轉(zhuǎn)移。
- 3. 從合格的從節(jié)點中選擇一個晉升為新主節(jié)點。
- 4. 其他從節(jié)點切換到新主節(jié)點同步數(shù)據(jù)。
- 5. 原主節(jié)點恢復(fù)后,作為從節(jié)點加入新主節(jié)點。
五. 服務(wù)部署
#安裝依賴 yum install make gcc #下載安裝 wget https://download.redis.io/releases/redis-7.4.3.tar.gz tar fxvz redis-7.4.3.tar.gz cd redis-7.4.3 make make install mkdir /etc/redis-server mkdir /etc/redis-sentinel mkdir -p /opt/redis-log cp redis-7.4.3/redis.conf /etc/redis-server/redis.conf
配置文件:
################################## NETWORK #####################################
bind 127.0.0.1 10.0.43.1
protected-mode yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
################################# GENERAL #####################################
daemonize yes
pidfile "/var/run/redis.pid"
loglevel notice
logfile "/data/redis-log/redis.log"
databases 16
always-show-logo no
set-proc-title yes
proc-title-template "{title} {listen-addr} {server-mode}"
locale-collate ""
################################ SNAPSHOTTING ################################
save 3600 1
save 300 100
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename "dump.rdb"
rdb-del-sync-files no
dir "/data/redis-data"
################################# REPLICATION #################################
masterauth "rCzxxxxsN5"
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync yes
repl-diskless-sync-delay 5
repl-diskless-sync-max-replicas 0
repl-diskless-load disabled
repl-disable-tcp-nodelay no
replica-priority 100
################################## SECURITY ###################################
acllog-max-len 128
requirepass "rCzxxxxsN5"
################################### CLIENTS ####################################
maxclients 10000
############################## MEMORY MANAGEMENT ################################
maxmemory 5gb
maxmemory-policy volatile-lru
maxmemory-samples 5
maxmemory-eviction-tenacity 10
replica-ignore-maxmemory yes
active-expire-effort 1
############################# LAZY FREEING ####################################
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
lazyfree-lazy-user-del no
lazyfree-lazy-user-flush no
################################ THREADED I/O #################################
io-threads 1
io-threads-do-reads no
############################ KERNEL OOM CONTROL ##############################
oom-score-adj no
oom-score-adj-values 0 200 800
#################### KERNEL transparent hugepage CONTROL ######################
disable-thp yes
############################## APPEND ONLY MODE ###############################
appendonly yes
appendfilename "appendonly.aof"
appenddirname "appendonlydir"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble yes
aof-timestamp-enabled no
################################ SHUTDOWN #####################################
shutdown-timeout 10
shutdown-on-sigint default
shutdown-on-sigterm default
################################## SLOW LOG ###################################
slowlog-log-slower-than 10000
slowlog-max-len 128
################################ LATENCY MONITOR ##############################
latency-monitor-threshold 0
################################ LATENCY TRACKING ##############################
latency-tracking-info-percentiles 50 99 99.9
############################# EVENT NOTIFICATION ##############################
notify-keyspace-events ""
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes
########################### ACTIVE DEFRAGMENTATION #######################
activedefrag no
active-defrag-ignore-bytes 100mb
active-defrag-threshold-lower 10
active-defrag-threshold-upper 100
active-defrag-cycle-min 1
active-defrag-cycle-max 25
active-defrag-max-scan-fields 1000
jemalloc-bg-thread yes
#如果是從節(jié)點,新增replicaof ${masterip} 6379啟動服務(wù):
redis-server redis.conf
可通過以下指令,查看主從信息:
redis-cli -h ${masterip} -p 6379 -a ${password} info replication六. sentinel部署配置
cp /opt/redis-7.4.3/sentinel.conf /etc/redis-sentinel/sentinel.conf cd /etc/redis-sentinel/ #vim sentinel-26379.conf protected-mode no port 26379 daemonize yes pidfile "/var/run/redis-sentinel.pid" loglevel notice logfile "/opt/redis-log/redis-sentinel.log" dir "/opt/redis-sentinel" sentinel monitor mymaster 10.0.43.1 6379 2 sentinel auth-pass mymaster rCzxxxxsN5 #master 密碼 sentinel down-after-milliseconds mymaster 30000 acllog-max-len 128 sentinel parallel-syncs mymaster 1 sentinel failover-timeout mymaster 180000 sentinel deny-scripts-reconfig yes sentinel resolve-hostnames no sentinel announce-hostnames no #其他節(jié)點,參考以上配置。
啟動服務(wù):
redis-sentinel sentinel.conf
查看sentinel的狀態(tài):
redis-cli -h 172.88.19.102 -p 26379 info Sentinel
七. 驗證
驗證自動切換主從:
kill 掉master: 6379

發(fā)現(xiàn)master發(fā)生變化,即成功。 延遲生效時間,取決于: sentinel down-after-milliseconds mymaster 30000 。
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
微服務(wù)Spring Boot 整合 Redis 實現(xiàn)好友關(guān)注功能
這篇文章主要介紹了微服務(wù)Spring Boot 整合 Redis 實現(xiàn) 好友關(guān)注,本文結(jié)合示例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-12-12
Redis中ZSet數(shù)據(jù)結(jié)構(gòu)與滑動窗口應(yīng)用實現(xiàn)
Redis?ZSET融合哈希表與跳躍表,支持O(1)成員查詢和O(logN)排序,適用于排行榜及滑動時間窗口限流,下面就來介紹一下Redis中ZSet數(shù)據(jù)結(jié)構(gòu)與滑動窗口應(yīng)用,感興趣的可以了解一下2025-07-07
使用redis-plus-plus庫連接redis的實現(xiàn)方法
本文主要介紹了使用redis-plus-plus庫連接redis的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-02-02
Redis數(shù)據(jù)類型string和Hash詳解
大家都知道Redis中有五大數(shù)據(jù)類型分別是String、List、Set、Hash和Zset,本文給大家分享Redis數(shù)據(jù)類型string和Hash的相關(guān)操作,感興趣的朋友跟隨小編一起看看吧2022-03-03

