Redis 哨兵模式与 ACL 权限控制概念解析
Redis 哨兵模式详解
Redis 在生产环境中常见的集群部署模式主要有三种:主从复制、Cluster 集群以及哨兵模式。其中,主从复制是另外两种架构的基础组成部分。

- 主从复制,简单来说就是让从节点作为主节点的数据备份副本。一旦主节点发生故障,它本身不会主动切换角色,需要人工介入处理。
- Cluster 模式则更为灵活——多个节点均担任主节点角色,每个主节点只负责存储部分数据分片,同时每个主节点至少配备一个从节点作为备份。当某个主节点宕机时,对应的从节点会自动升级为新的主节点,整个过程无需人工干预。
- 哨兵模式,顾名思义,就像古代城墙上的哨兵一样,持续监控节点的运行状态。一旦发现某个节点出现故障,哨兵便会自动将从节点提升为主节点。该架构中只有一个主节点,但至少包含一个从节点和一个哨兵节点。业界推荐的典型部署方案是“一主二从三哨兵”。下面我们将详细介绍这种部署方式的实现过程。
ACL 权限控制机制
ACL,全称为 Access Control List(访问控制列表)。在 Redis 未启用 ACL 时,所有客户端连接均使用默认用户(default),权限整齐划一。ACL 正是为解决这一问题而设计——它可以为不同用户分配账号、密码和权限,实现对每个用户操作权限的精细化管控。这一机制从 Redis 6.0 版本开始引入,其工作原理是 Redis 服务端在启动时加载一个记录 ACL 规则的文件,文件中的密码采用 SHA256 加密存储。
部署规划方案
| IP 地址 | 端口号 | 用途说明 | 部署目录 |
|---|---|---|---|
| 192.168.56.145 | 6379 | Redis 主节点 | /opt/redis |
| 192.168.56.146 | 6379 | Redis 从节点 | /opt/redis |
| 192.168.56.147 | 6379 | Redis 从节点 | /opt/redis |
| 192.168.56.145 | 26379 | Redis 哨兵节点 | /opt/redis-sentinel |
| 192.168.56.146 | 26379 | Redis 哨兵节点 | /opt/redis-sentinel |
| 192.168.56.147 | 26379 | Redis 哨兵节点 | /opt/redis-sentinel |
- 本次部署采用最少 3 台服务器,构成一主二从三哨兵的 Redis 高可用架构。
- 默认 3 台主机操作系统相同,只需在主节点上编译一次 Redis 源码,然后复制出一份哨兵配置,修改后同步到其他两个节点即可。
- 需要提前确保三台服务器之间的 6379 和 26379 端口可以互相访问。
编译 Redis 源码
#安装gcc c/c++编译器 yum -y install gcc gcc-c++ #创建安装目录 mkdir -p /opt/redis #解压redis源码并编译redis tar xf redis-8.6.3.tar.gz cd redis-8.6.3 make PREFIX=/opt/redis install MALLOC=jemalloc #复制出哨兵节点目录 cp -r /opt/redis /opt/redis-sentinel #删除哨兵节点目录中redis的配置 rm -rf /opt/redis-sentinel/redis.conf
配置 Redis 主节点
/opt/redis/redis.conf
bind 0.0.0.0 protected-mode no port 6379 tcp-backlog 511 timeout 300 tcp-keepalive 300 daemonize yes supervised no pidfile /var/run/redis.pid loglevel notice databases 16 always-show-logo yes sa ve 900 1 sa ve 300 10 sa ve 60 10000 stop-writes-on-bgsa ve-error yes rdbcompression yes rdbchecksum yes dbfilename dump.rdb dir ./ replica-serve-stale-data yes replica-read-only yes repl-diskless-sync no repl-diskless-sync-delay 5 repl-disable-tcp-nodelay no replica-priority 100 maxmemory-policy allkeys-lru lazyfree-lazy-eviction yes lazyfree-lazy-expire yes lazyfree-lazy-server-del yes replica-lazy-flush yes appendonly no appendfilename "appendonly.aof" 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 lua-time-limit 5000 slowlog-log-slower-than 1000 slowlog-max-len 1000 latency-monitor-threshold 0 notify-keyspace-events "" hash-max-ziplist-entries 512 hash-max-ziplist-value 64 list-max-ziplist-size -2 list-compress-depth 0 set-max-intset-entries 512 zset-max-ziplist-entries 128 zset-max-ziplist-value 64 hll-sparse-max-bytes 3000 stream-node-max-bytes 4096 stream-node-max-entries 100 activerehashing yes client-output-buffer-limit normal 0 0 0 client-output-buffer-limit replica 256mb 64mb 60 client-output-buffer-limit pubsub 32mb 8mb 60 hz 10 dynamic-hz yes aof-rewrite-incremental-fsync yes rdb-sa ve-incremental-fsync yes logfile ./redis.log #主要是下边这三行 masterauth replicapassword masteruser replica-user aclfile ./users.acl
aclfile 配置项用于指定 ACL 规则文件的路径。
masteruser 指定连接主节点时使用的用户,replica-user 是我们后续打算创建的从节点备份用户,可提前配置。
masterauth 是连接主节点时所需的密码,这里暂时使用从节点备份用户的密码。
临时启动主节点,配置 ACL 用户
#生成default用户hash密码,防止用户通过redis-cli查到命令历史 echo -n "default密码" |sha256sum #启动主节点 cd /opt/redis ./bin/redis-server ./redis.conf #连接主节点,执行ACL指令配置用户 ./bin/redis-cli ACL SETUSER default on #admin密码hash sanitize-payload ~* &* +@all ACL SETUSER app-user on >appuserpassword ~* &* +@keyspace +@read +@write +wait +ping +info +eval +keys +@pubsub ACL SETUSER sentinel-user on >sentinelpassword ~* &* allchannels +multi +sla veof +ping +exec +subscribe +config|rewrite +role +publish +info +client|setname +client|kill +script|kill ACL setuser replica-user on >replicapassword ~* &* +psync +replconf +ping ACL SA VE quit
以上命令为 default 用户设置了更复杂的密码,同时创建了客户端用户 app-user、哨兵用户 sentinel-user 以及备份用户 replica-user,并为这些用户配置了相应的权限。其中备份用户和哨兵用户的权限参考官方文档,应用用户 app-user 的权限仅供参考。
注意:在 ACL 设置密码时,以 # 开头配置预哈希密码,以 > 开头配置明文密码,最终写入 ACL 文件时都会转换为哈希密文。
关闭 Redis 主节点,删除相关日志文件。此时 users.acl 文件已经生成,该 ACL 文件只需存在于主节点和从节点上即可。
ps -ef|grep redis kill -9rm -f redis.log
配置哨兵节点
在 145 节点上配置第一个哨兵节点
cd /opt/redis-sentinel vim sentinel.conf cat sentinel.conf #参考以下配置修改哨兵配置 port 26379 protected-mode no daemonize yes pidfile /var/run/redis-sentinel.pid logfile ./sentinel.log dir /tmp sentinel monitor mymaster 192.168.56.145 6379 2 sentinel auth-user mymaster sentinel-user sentinel auth-pass mymaster sentinelpassword 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
复制哨兵节点配置到其他服务器
在 145 节点上将 /opt/redis-sentinel 目录复制到 146、147 节点
scp -r /opt/redis-sentinel root@192.168.56.146:/opt/redis-sentinel scp -r /opt/redis-sentinel root@192.168.56.147:/opt/redis-sentinel
复制并创建从节点
在 145 节点上将 /opt/redis 目录复制到 146 节点
scp -r /opt/redis root@192.168.56.146:/opt/redis scp -r /opt/redis root@192.168.56.147:/opt/redis
登录 146、147 节点,修改 /opt/redis/redis.conf 配置文件
vim /opt/redis/redis.conf #追加一行同步配置,指向主节点 replicaof 192.168.56.145 6379
登录 147 节点,同样修改 /opt/redis/redis.conf 配置文件
vim /opt/redis/redis.conf #追加一行同步配置,指向主节点 replicaof 192.168.56.145 6379
启动主从节点
依次登录 145、146、147 节点,执行启动 Redis 命令
cd /opt/redis ./bin/redis-server ./redis.conf
启动哨兵节点
依次登录 145、146、147 节点,执行启动哨兵命令
cd /opt/redis-sentinel ./bin/redis-sentinel ./sentinel.conf
查看集群运行状态
在 145 主节点上查看主从信息
cd /opt/redis ./bin/redis-cli -p 6379 AUTH default <密码> info replication #输出示例的前5行,检查connected_sla ves为2,state状态均为online即可 # Replication role:master connected_sla ves:2 sla ve0:ip=192.168.56.146,port=6379,state=online,offset=44565,lag=0,io-thread=0 sla ve1:ip=192.168.56.147,port=6379,state=online,offset=44422,lag=1,io-thread=0 quit
在任意一个哨兵节点上查看哨兵信息
cd /opt/redis-sentinel ./bin/redis-cli -p 26379 info sentinel #输出示例,检查master0的status为ok,能看出address为主节点地址,sla ve为2,sentinels为3即正常 # Sentinel sentinel_masters:1 sentinel_tilt:0 sentinel_tilt_since_seconds:-1 sentinel_total_tilt:0 sentinel_running_scripts:0 sentinel_scripts_queue_length:0 sentinel_simulate_failure_flags:0 master0:name=mymaster,status=ok,address=192.168.56.145:6379,sla ves=2,sentinels=3 quit
根据官方文档,哨兵可以直接通过命令查看集群中的从节点和哨兵节点状态,这些命令需要在 redis-cli 鉴权后执行。
SENTINEL replicas mymaster SENTINEL sentinels mymaster
