本文主要是介绍redis-sentinel基础概念及部署流程,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
《redis-sentinel基础概念及部署流程》RedisSentinel是Redis的高可用解决方案,通过监控主从节点、自动故障转移、通知机制及配置提供,实现集群故障恢复与服务持续可用,核心组件包...
一. 引言
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" ################################# REPLICAhttp://www.chinasem.cnTION ################################# 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-www.chinasem.cnserver redis.conf
可通过以下指令,查看主从信息:
redis-cli -h ${masterip} -p 6379 -a ${password} info replication
六. sentinphpel部署China编程配置
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" djsir "/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 。
总结
这篇关于redis-sentinel基础概念及部署流程的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!