一. 引言
redis sentinel 是 redis 官方提供的高可用解决方案,主要用于监控 redis 主从集群,在主节点故障时自动完成故障转移,确保服务持续可用。
二. 核心功能
- 1. 监控(monitoring):持续检查主节点(master)和从节点(slave)是否正常运行。
- 2. 自动故障转移(automatic failover):当主节点故障时,自动将一个从节点晋升为新主节点,并让其他从节点指向新主节点。
- 3. 通知(notification):通过 api 向管理员或其他应用程序发送故障通知。
- 4. 配置提供者(configuration provider):客户端可通过 sentinel 获取当前主节点地址(无需硬编码)。
三. 核心组件
- 1. sentinel 节点:特殊的 redis 进程(非数据存储节点),通常部署 3 个及以上以保证自身高可用。
- 2. 主节点(master):负责处理写操作的 redis 节点。
- 3. 从节点(slave):同步主节点数据,提供读服务,主节点故障时可被晋升。
四. 故障转移流程
- 1. 多个 sentinel 检测到主节点故障。
- 2. 选举一个 sentinel 作为领导者,负责执行故障转移。
- 3. 从合格的从节点中选择一个晋升为新主节点。
- 4. 其他从节点切换到新主节点同步数据。
- 5. 原主节点恢复后,作为从节点加入新主节点。
五. 服务部署
#安装依赖 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 。
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
发表评论