Redis常见有主从备份、Cluster、哨兵这三种集群部署模式。其中主从备份是后两者的实现基础。

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 |
#安装gcc c/c++编译器yum -y install gcc gcc-c++#创建安装目录mkdir -p /opt/redis#解压redis源码并编译redistar xf redis-8.6.3.tar.gzcd redis-8.6.3make PREFIX=/opt/redis install MALLOC=jemalloc#复制出哨兵节点目录cp -r /opt/redis /opt/redis-sentinel#删除哨兵节点目录中redis的配置rm -rf /opt/redis-sentinel/redis.conf
/opt/redis/redis.conf
bind 0.0.0.0protected-mode noport 6379tcp-backlog 511timeout 300tcp-keepalive 300daemonize yessupervised nopidfile /var/run/redis.pidloglevel noticedatabases 16always-show-logo yessave 900 1save 300 10save 60 10000stop-writes-on-bgsave-error yesrdbcompression yesrdbchecksum yesdbfilename dump.rdbdir ./replica-serve-stale-data yesreplica-read-only yesrepl-diskless-sync norepl-diskless-sync-delay 5repl-disable-tcp-nodelay noreplica-priority 100maxmemory-policy allkeys-lrulazyfree-lazy-eviction yeslazyfree-lazy-expire yeslazyfree-lazy-server-del yesreplica-lazy-flush yesappendonly noappendfilename "appendonly.aof"appendfsync everysecno-appendfsync-on-rewrite noauto-aof-rewrite-percentage 100auto-aof-rewrite-min-size 64mbaof-load-truncated yesaof-use-rdb-preamble yeslua-time-limit 5000slowlog-log-slower-than 1000slowlog-max-len 1000latency-monitor-threshold 0notify-keyspace-events ""hash-max-ziplist-entries 512hash-max-ziplist-value 64list-max-ziplist-size -2list-compress-depth 0set-max-intset-entries 512zset-max-ziplist-entries 128zset-max-ziplist-value 64hll-sparse-max-bytes 3000stream-node-max-bytes 4096stream-node-max-entries 100activerehashing yesclient-output-buffer-limit normal 0 0 0client-output-buffer-limit replica 256mb 64mb 60client-output-buffer-limit pubsub 32mb 8mb 60hz 10dynamic-hz yesaof-rewrite-incremental-fsync yesrdb-save-incremental-fsync yeslogfile ./redis.log#主要是下边这三行masterauth replicapasswordmasteruser replica-useraclfile ./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-cliACL SETUSER default on #admin密码hash sanitize-payload ~* &* +@allACL SETUSER app-user on >appuserpassword ~* &* +@keyspace +@read +@write +wait +ping +info +eval +keys +@pubsubACL SETUSER sentinel-user on >sentinelpassword ~* &* allchannels +multi +slaveof +ping +exec +subscribe +config|rewrite +role +publish +info +client|setname +client|kill +script|killACL setuser replica-user on >replicapassword ~* &* +psync +replconf +pingACL SAVEquit
以上命令为default用户设置了更复杂的密码,创建了客户端用户app-user、哨兵用户sentinel-user、备份用户replica-user,同时配置了这些用户的权限,这里的备份用户和哨兵用户权限参考官方文档,app-user用户权限仅供参考。
注意:ACL设置密码时#开头配置预哈希密码,>开头配置明文密码,最终到acl中都是哈希密文。
关闭redis主节点,删去相关日志,此时users.acl文件已经生成了,acl文件只需存在主从节点即可。
ps -ef|grep rediskill -9 <PID>rm -f redis.log
在145节点上配置第一个哨兵节点
cd /opt/redis-sentinelvim sentinel.confcat sentinel.conf#参考以下配置修改哨兵配置port 26379protected-mode nodaemonize yespidfile /var/run/redis-sentinel.pidlogfile ./sentinel.logdir /tmpsentinel monitor mymaster 192.168.56.145 6379 2sentinel auth-user mymaster sentinel-usersentinel auth-pass mymaster sentinelpasswordsentinel down-after-milliseconds mymaster 30000acllog-max-len 128sentinel parallel-syncs mymaster 1sentinel failover-timeout mymaster 180000sentinel deny-scripts-reconfig yesSENTINEL resolve-hostnames noSENTINEL announce-hostnames no
在145节点复制/opt/redis-sentinel到146、147节点
scp -r /opt/redis-sentinel [email protected]:/opt/redis-sentinelscp -r /opt/redis-sentinel [email protected]:/opt/redis-sentinel
在145节点复制/opt/redis到146节点
scp -r /opt/redis [email protected]:/opt/redisscp -r /opt/redis [email protected]:/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 6379AUTH default <密码>info replication#输出示例的前5行,检查connected_slaves为2,state状态均为online即可# Replicationrole:masterconnected_slaves:2slave0:ip=192.168.56.146,port=6379,state=online,offset=44565,lag=0,io-thread=0slave1:ip=192.168.56.147,port=6379,state=online,offset=44422,lag=1,io-thread=0quit
任一哨兵节点上查看哨兵信息
cd /opt/redis-sentinel./bin/redis-cli -p 26379info sentinel#输出示例,检查master0的status为ok,能看出adress为主节点地址,slave为2,sentinels为3即正常# Sentinelsentinel_masters:1sentinel_tilt:0sentinel_tilt_since_seconds:-1sentinel_total_tilt:0sentinel_running_scripts:0sentinel_scripts_queue_length:0sentinel_simulate_failure_flags:0master0:name=mymaster,status=ok,address=192.168.56.145:6379,slaves=2,sentinels=3quit
官方文档中哨兵可以直接查看集群中的从节点和哨兵节点的状态,这是在redis-cli鉴权后的命令
SENTINEL replicas mymasterSENTINEL sentinels mymaster
樱花动漫APP下载-樱花动漫官方正版APP最新下载入口
premiere画中画大小位置调节方法 - premiere如何调节画中画大小和位置
丁香修剪技巧有哪些-如何用丁香修剪技巧打造优美树形
在Win2008 R2中如何删除DHCP管理工具-Win2008 R2怎样删除DHCP管理工具
如何通过支付宝阿宝自动匹配生活服务入口-怎样借助支付宝阿宝自动匹配生活服务入口
edius素材添加单门推效果的操作方法-edius素材如何添加单门推效果