it编程 > 数据库 > Redis

redis-sentinel基础概念及部署流程

27人参与 2025-08-20 Redis

一. 引言

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

二. 核心功能

三. 核心组件

四. 故障转移流程

五. 服务部署

#安装依赖
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 。

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。

(0)

您想发表意见!!点此发布评论

推荐阅读

Redis实现高效内存管理的示例代码

08-20

Redis的哈希Hash类型常用命令的使用小结

08-20

Redis延迟双删的具体使用

08-21

Redis的哨兵模式工作流程及原理详解

08-21

基于Redis自动过期的流处理暂停机制

08-19

Redis服务端主动回收配置的使用小结

08-19

猜你喜欢

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论