架构概述
由于资源有限,只在一台ECS上演示测试
1个主节点:Redis 6380
2个从节点:Redis 6381、6382
3个哨兵节点:Sentinel 26380、26381、26382
一、搭建redis主从环境
1.创建目录结构
# 创建所有节点目录
mkdir -p /data/{6380,6381,6382}
2.配置文件
cat >> /data/6380/redis.conf << EOF
port 6380
daemonize yes
logfile "/data/6380/redis.log"
pidfile "/data/6380/redis.pid"
dbfilename "dump-6380.rdb"
dir "/data/6380"
requirepass 123456
masterauth 123456
EOF
cat >> /data/6381/redis.conf << EOF
port 6381
daemonize yes
logfile "/data/6381/redis.log"
pidfile "/data/6381/redis.pid"
dbfilename "dump-6381.rdb"
dir "/data/6381"
requirepass 123456
masterauth 123456
EOF
cat >> /data/6382/redis.conf << EOF
port 6382
daemonize yes
logfile "/data/6382/redis.log"
pidfile "/data/6382/redis.pid"
dbfilename "dump-6382.rdb"
dir "/data/6382"
requirepass 123456
masterauth 123456
EOF
3.启动 Redis 主从
#启动redis
redis-server /data/6380/redis.conf
redis-server /data/6381/redis.conf
redis-server /data/6382/redis.conf
#启动主从
redis-cli -p 6381 -a 123456 slaveof 127.0.0.1 6380
redis-cli -p 6382 -a 123456 slaveof 127.0.0.1 6380
#也可以在配置文件中写slaveof 127.0.0.1 6380,然后启动
#查询主从状态
info replication
第二步:搭建哨兵集群
-
哨兵配置文件 (26380)
cat > /data/26380/sentinel.conf << 'EOF'
port 26380
daemonize yes
logfile "/data/26380/sentinel.log"
pidfile "/data/26380/sentinel.pid"
dir "/data/26380"监控配置:sentinel monitor <主节点名称> <IP> <端口> <仲裁数>
sentinel monitor mymaster 127.0.0.1 6380 2
主节点密码
sentinel auth-pass mymaster 123456
主观下线时间(毫秒)
sentinel down-after-milliseconds mymaster 5000
故障转移超时时间
sentinel failover-timeout mymaster 60000
并行同步的从节点数
sentinel parallel-syncs mymaster 1
保护模式关闭(测试环境)
protected-mode no
哨兵自身连接密码(可选)
sentinel auth-pass mymaster 123456
EOF
-
复制配置到其他哨兵
创建目录
mkdir -p /data/{26381,26382}
复制并修改端口
for port in 26381 26382; do
cp /data/26380/sentinel.conf /data/port/sentinel.conf sed -i "s/26380/port/g" /data/port/sentinel.conf sed -i "s|/data/26380|/data/port|g" /data/$port/sentinel.conf
done -
启动哨兵集群
启动三个哨兵
redis-sentinel /data/26380/sentinel.conf
redis-sentinel /data/26381/sentinel.conf
redis-sentinel /data/26382/sentinel.conf验证哨兵启动
ps aux | grep redis-sentinel | grep -v grep
第三步:验证哨兵集群
-
查看哨兵信息
连接到任意哨兵
redis-cli -p 26380
在哨兵交互模式中执行以下命令:
127.0.0.1:26380> SENTINEL masters # 查看监控的主节点
127.0.0.1:26380> SENTINEL slaves mymaster # 查看从节点信息
127.0.0.1:26380> SENTINEL sentinels mymaster # 查看其他哨兵
127.0.0.1:26380> SENTINEL get-master-addr-by-name mymaster # 获取当前主节点
第四步:测试redis故障转移
language
主节点redis宕机
[root@iv-ye4xdz3ncwwh2ypb2dr5 ~]# redis-cli -p 6380 -a 123456 shutdown
可以看到主库已经切换到6381端口
[root@iv-ye4xdz3ncwwh2ypb2dr5 ~]# redis-cli -p 6381 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6381> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=127.0.0.1,port=6382,state=online,offset=6759,lag=1
master_failover_state:no-failover
master_replid:d9c34548ba64278ad3b8364d7a24d22b17bab679
master_replid2:4e9c3ef6d1d4d7c4f9e8d5b8cc001284c49c2acc
master_repl_offset:6759
second_repl_offset:5407
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:15
repl_backlog_histlen:6745
将6380重启后,会自动以从库方式加入到集群
[root@iv-ye4xdz3ncwwh2ypb2dr5 ~]# redis-server /data/6380/redis.conf
[root@iv-ye4xdz3ncwwh2ypb2dr5 ~]#
[root@iv-ye4xdz3ncwwh2ypb2dr5 ~]#
[root@iv-ye4xdz3ncwwh2ypb2dr5 ~]#
[root@iv-ye4xdz3ncwwh2ypb2dr5 ~]# redis-cli -p 6381 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6381> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=127.0.0.1,port=6382,state=online,offset=16335,lag=0
slave1:ip=127.0.0.1,port=6380,state=wait_bgsave,offset=0,lag=0
master_failover_state:no-failover
master_replid:d9c34548ba64278ad3b8364d7a24d22b17bab679
master_replid2:4e9c3ef6d1d4d7c4f9e8d5b8cc001284c49c2acc
master_repl_offset:16335
second_repl_offset:5407
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:15
repl_backlog_histlen:16321
哨兵管理命令
language
# 查看所有监控的主节点
redis-cli -p 26380 SENTINEL masters
# 查看特定主节点的从节点
redis-cli -p 26380 SENTINEL slaves mymaster
# 查看其他哨兵节点
redis-cli -p 26380 SENTINEL sentinels mymaster
# 强制故障转移(危险操作)
redis-cli -p 26380 SENTINEL failover mymaster
# 移除旧主节点(故障转移后)
redis-cli -p 26380 SENTINEL remove mymaster