zl程序教程

您现在的位置是:首页 >  数据库

当前栏目

Redis哨兵模式搭建及故障切换模拟

Redis模拟模式 搭建 切换 哨兵 故障
2023-09-14 09:02:05 时间

0. Redis 哨兵架构

在这里插入图片描述

序号HOSTNAMEIPADDRESS操作系统版本
1Master192.168.31.8CentOS Linux release 8.4.2105
2Slave-1192.168.31.18CentOS Linux release 8.4.2105
3Slave-2192.168.31.28CentOS Linux release 8.4.2105

1. Redis安装

主从节点都执行

yum install -y redis

2. Redis配置文件修改

主从节点都执行

sed -Ei -e 's/^(bind ).*/\10.0.0.0/' \
-e 's/^# (masterauth ).*/\1123456/' \
-e 's/^# (requirepass ).*/\1123456/' /etc/redis.conf
systemctl enable --now redis

3. 从节点配置

仅在从节点上执行

echo "replicaof 192.168.31.8 6379" >> /etc/redis.conf
systemctl restart redis

在这里插入图片描述

4. 主节点验证

systemctl restart redis
redis-cli -a 123456 info replication

在这里插入图片描述

5. 从节点验证

redis-cli -a 123456 info replication

在这里插入图片描述

6. sentinel配置

Sentinel实际上是一个特殊的redis服务器,有些redis指令支持,但很多指令并不支持.默认监听在26379/tcp端口.

\cp /etc/redis-sentinel.conf{,.bak}
cat > /etc/redis-sentinel.conf<<EOF
bind 0.0.0.0
port 26379
daemonize yes
pidfile "/var/run/redis-sentinel.pid"
logfile "/var/log/sentinel_26379.log"
dir "/tmp"
sentinel monitor mymaster 192.168.31.8 6379 2
sentinel auth-pass mymaster 123456
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes
logfile /var/log/redis/sentinel.log
EOF
touch /var/log/sentinel_26379.log
chown redis.redis /var/log/sentinel_26379.log
systemctl enable --now redis-sentinel.service

在这里插入图片描述

7. 哨兵日志

redis-cli -p 26379 info sentinel

在这里插入图片描述
在这里插入图片描述

8. 停止Redis Master测试故障转移

master上停止Redis

systemctl stop redis

在这里插入图片描述
随便哪个节点上查看sentinel状态

redis-cli -a 123456 -p 26379 info sentinel

在这里插入图片描述

9. 查看故障转移状态

tail -f /var/log/redis/sentinel.log

在这里插入图片描述
此时另外一台redis的配置文件会被修改

在这里插入图片描述

10. 故障恢复

当master恢复后,会被自动连接.但不会替代master的位置,会以slave的身份存在.

redis-cli -a 123456 info replication
redis-cli -a 123456 -p 26379 info sentinel

在这里插入图片描述