redis-sentinel基础概念及部署

一. 引言:

Redis Sentinel 是 redis 官方提供的高可用解决方案,主要用于监控 Redis 主从集群,在主节点故障时自动完成故障转移,确保服务持续可用。

二. 核心功能

    1. 监控(monitoring):持续检查主节点(master)和从节点(slave)是否正常运行。
    1. 自动故障转移(automatic failover):当主节点故障时,自动将一个从节点晋升为新主节点,并让其他从节点指向新主节点。
    1. 通知(notification):通过 API 向管理员或其他应用程序发送故障通知。
    1. 配置提供者(configuration provider):客户端可通过 Sentinel 获取当前主节点地址(无需硬编码)。

三. 核心组件

  1. sentinel 节点:特殊的 Redis 进程(非数据存储节点),通常部署 3 个及以上以保证自身高可用。

  2. 主节点(master):负责处理写操作的 redis 节点。

  3. 从节点(slave):同步主节点数据,提供读服务,主节点故障时可被晋升。

四. 故障转移流程

    1. 多个 sentinel 检测到主节点故障。
    1. 选举一个 sentinel 作为领导者,负责执行故障转移。
    1. 从合格的从节点中选择一个晋升为新主节点。
    1. 其他从节点切换到新主节点同步数据。
    1. 原主节点恢复后,作为从节点加入新主节点。

五. 服务部署:

复制代码
#安装依赖
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



#如果是从节点,新增replicaof ${masterip} 6379

启动服务:

复制代码
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



#其他节点,参考以上配置。

启动服务:

复制代码
redis-sentinel sentinel.conf

查看sentinel的状态:

复制代码
redis-cli -h 172.88.19.102 -p 26379 info Sentinel

七. 验证:

验证自动切换主从:

kill 掉master: 6379

发现master发生变化,即成功。 延迟生效时间,取决于: sentinel down-after-milliseconds mymaster 30000 。


深耕运维行业多年,擅长运维体系建设,方案落地。欢迎交流!

V**: ywjw996**

《 运维经纬 》

相关推荐
云间月13142 小时前
飞算JavaAI:从智能调度到出行服务的全链路技术升级
java·redis·飞算javaai炫技赛
AI 嗯啦2 小时前
SQL详细语法教程(三)mysql的函数知识
android·开发语言·数据库·python·sql·mysql
柏油3 小时前
Spring @Cacheable 解读
redis·后端·spring
时序数据说3 小时前
国内时序数据库概览
大数据·数据库·物联网·时序数据库·iotdb
杰克尼4 小时前
mysql-条件查询案例
数据库·mysql
运维行者_6 小时前
使用Applications Manager进行 Apache Solr 监控
运维·网络·数据库·网络安全·云计算·apache·solr
千层冷面9 小时前
Flask ORM 查询详解:Model.query vs db.session.query vs db.session.execute
数据库·python·django·flask
Navicat中国9 小时前
Navicat 询问 AI | 如何转换 SQL 为另一种数据库类型
数据库·人工智能·sql·数据库开发·navicat