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台主机操作系统完全相同,只需在主节点编译1次Redis,复制出1个哨兵,修改后同步到其他2个节点上
- 需提前确保三台服务器间的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 save 900 1 save 300 10 save 60 10000 stop-writes-on-bgsave-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-save-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 +slaveof +ping +exec +subscribe +config|rewrite +role +publish +info +client|setname +client|kill +script|kill ACL setuser replica-user on >replicapassword ~* &* +psync +replconf +ping ACL SAVE quit
以上命令为default用户设置了更复杂的密码,创建了客户端用户app-user、哨兵用户sentinel-user、备份用户replica-user,同时配置了这些用户的权限,这里的备份用户和哨兵用户权限参考官方文档,app-user用户权限仅供参考。
注意:ACL设置密码时#开头配置预哈希密码,>开头配置明文密码,最终到acl中都是哈希密文。
关闭redis主节点,删去相关日志,此时users.acl文件已经生成了,acl文件只需存在主从节点即可。
ps -ef|grep redis kill -9 <PID> rm -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_slaves为2,state状态均为online即可 # Replication role:master connected_slaves:2 slave0:ip=192.168.56.146,port=6379,state=online,offset=44565,lag=0,io-thread=0 slave1: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,能看出adress为主节点地址,slave为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,slaves=2,sentinels=3 quit
官方文档中哨兵可以直接查看集群中的从节点和哨兵节点的状态,这是在redis-cli鉴权后的命令
SENTINEL replicas mymaster SENTINEL sentinels mymaster
到此这篇关于Redis 哨兵搭建+ACL权限控制的实现的文章就介绍到这了,更多相关Redis 哨兵搭建+ACL权限控制内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
Redis Desktop Manager(Redis可视化工具)安装及使用图文教程
这篇文章主要介绍了Redis Desktop Manager(Redis可视化工具)安装及使用图文教程,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2023-04-04


最新评论