安装Redis
建议版本不要太低 >= 6.2,我这里是redis 7.2.5
bash
curl -fsSL https://packages.redis.io/gpg | sudo gpg --dearmor -o /usr/share/keyrings/redis-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/redis-archive-keyring.gpg] https://packages.redis.io/deb $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/redis.list
sudo apt-get update
sudo apt-get install redis
配置redis主从复制
配置Redis的主配置文件
$REDIS_PASSWORD 按自己的去配置,masterauth这个需要主从的配置文件里面都有,要么无法主从切换
bash
sed -i "s/bind 127.0.0.1/bind 0.0.0.0/g" /etc/redis/redis.conf
sed -i "792i requirepass $REDIS_PASSWORD" /etc/redis/redis.conf
sed -i "539i masterauth $REDIS_PASSWORD" /etc/redis/redis.conf
配置Redis的从配置文件。配置2个从节点
bash
sed -i "s/bind 127.0.0.1/bind 0.0.0.0/g" /etc/redis/redis.conf
sed -i "792i requirepass $REDIS_PASSWORD" /etc/redis/redis.conf
sed -i "539i masterauth $REDIS_PASSWORD" /etc/redis/redis.conf
# 配置文件找到这个配置下主节点redis的IP和端口
replicaof <masterip> <masterport>
确认主从关系
bash
# 登录主节点,这里密码设置123456,有主从关系说明主从已经配置正常
redis-cli -a 123456 info Replication
# Replication
role:master
connected_slaves:2
slave0:ip=10.204.121.216,port=6379,state=online,offset=12862889,lag=1
slave1:ip=10.204.121.217,port=6379,state=online,offset=12862889,lag=1
配置sentinel
bash
sentinel架构,第一种官网不建议,缺点:M1节点宕机不会切换主从:
+----+ +----+
| M1 |---------| R1 |
| S1 | | S2 |
+----+ +----+
Configuration: quorum = 1
第二种是最常用的,可以作为高可用,每个redis节点一个sentinel实例,当然这些实例可以放到任意可访问主节点的地方。
+----+
| M1 |
| S1 |
+----+
|
+----+ | +----+
| R2 |----+----| R3 |
| S2 | | S3 |
+----+ +----+
Configuration: quorum = 2
创建sentinel配置文件
bash
cat <<EOF > /etc/redis/sentinel.conf
port 5000
sentinel monitor mymaster 10.204.121.218 6379 2
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 60000
sentinel auth-pass mymaster 123456
sentinel parallel-syncs mymaster 1
logfile "/var/log/sentinel.log"
daemonize yes
EOF
解释:
port 默认是26379,这个设置5000
Redis 监视名为 mymaster 的主服务器,该主服务器位于地址 10.204.121.218 和端口 6379,仲裁数为 2,3个sentinel节点设置2
down-after-milliseconds 是指当 Sentinel 开始认为实例已关闭,发送ping命令没有响应
parallel-syncs 在故障转移后可以同时重新配置为使用新主节点的副本数量,将此选项设置为 1,您可以确保一次只有一个副本不可达
启动
在每个节点上配置好了sentinel需要的配置文件
bash
# 启动sentinel
redis-server /etc/redis/sentinel.conf --sentinel
# 所有的sentinel启动完成后,查看状态,当前主节点,slave sentinels为3,说明已经配置完成
redis-cli -p 5000 info Sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_tilt_since_seconds:-1
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=10.204.121.218:6379,slaves=2,sentinels=3
测试主从切换
把其中一台redis关机,tail -f /var/log/sentinel.log 会看到类似的日志
+sdown 说明节点上的sentinel主观认为redis down了,-sdown 说明是客观down了,后面就进行+convert-to-slave 主从切换
bash
568520:X 28 Jun 2024 01:33:37.365 * Sentinel new configuration saved on disk
568475:X 28 Jun 2024 01:33:37.368 * Sentinel new configuration saved on disk
568475:X 28 Jun 2024 01:33:42.360 # +sdown slave 10.204.121.218:6379 10.204.121.218 6379 @ mymaster 10.204.121.217 6379
568520:X 28 Jun 2024 01:33:42.372 # +sdown slave 10.204.121.218:6379 10.204.121.218 6379 @ mymaster 10.204.121.217 6379
568335:X 28 Jun 2024 01:33:42.378 # +sdown slave 10.204.121.218:6379 10.204.121.218 6379 @ mymaster 10.204.121.217 6379
568520:X 28 Jun 2024 02:55:33.783 # -sdown slave 10.204.121.218:6379 10.204.121.218 6379 @ mymaster 10.204.121.217 6379
568475:X 28 Jun 2024 02:55:34.050 # -sdown slave 10.204.121.218:6379 10.204.121.218 6379 @ mymaster 10.204.121.217 6379
568335:X 28 Jun 2024 02:55:34.596 # -sdown slave 10.204.121.218:6379 10.204.121.218 6379 @ mymaster 10.204.121.217 6379
568520:X 28 Jun 2024 02:55:43.744 * +convert-to-slave slave 10.204.121.218:6379 10.204.121.218 6379 @ mymaster 10.204.121.217 6379