Redis 7.2.x 主从复制+哨兵模式

IP操作系统服务版本
192.168.140.153CentOS 7redis-master,sentinel7.2.5
192.168.140.156CentOS 7redis-slave,sentinel7.2.5
192.168.140.159CentOS 7redis-slave,sentinel7.2.5

一、安装Redis + 配置主从复制

参考下面文档:

Redis 7.2.x 主从复制-CSDN博客文章浏览阅读59次。搭建 Redis 7.2.x 主从复制,一主一从https://blog.csdn.net/weixin_44295677/article/details/139475019?spm=1001.2014.3001.5502

二、配置哨兵

1、修改内核参数
vi /etc/sysctl.conf

vm.overcommit_memory = 1

# 0 (默认值): 启发式内存提交策略。内核会尝试猜测进程是否会真正使用它们请求的所有内存。是一个很好的默认设置,允许进程请求大量内存,而实际上可能并不需要这么多。
# 1: 总是允许内存提交。无论物理内存和交换空间是否足够,内核都会允许进程提交内存。会导致在内存不足时,系统无法分配更多的内存,并导致OOM(Out-Of-Memory)被激活,杀死一些进程以释放内存。
# 2: 使用/proc/meminfo中的CommitLimit值来决定是否允许内存提交。如果请求的内存加上当前已提交的内存超过了CommitLimit,那么内存提交会被拒绝。
2、修改配置
cp /opt/redis-7.2.5/sentinel.conf /opt/redis/
vi /opt/redis/sentinel.conf

# 修改内容如下
## 端口
port 26379
## 允许后台运行
daemonize yes
## 进程号存放位置
pidfile "/opt/redis/redis-sentinel.pid"
## 日志级别,如果想要更详细的可以设置成debug
loglevel notice
## 日志存放路径
logfile "/opt/redis/logs/redis-sentinel.log"
## 工作目录
dir "/opt/redis"
## 监听的主节点信息
sentinel monitor mymaster 192.168.140.153 6379 2
## sentinel判断redis主服务器下线所需要的时间,单位毫秒
sentinel down-after-milliseconds mymaster 10000
## redis主服务器故障转移的最大超时时间
sentinel failover-timeout mymaster 60000
## 故障转移时,最多允许几个从服务器同时向新的主服务器转移数据
sentinel parallel-syncs mymaster 2
## 向主服务器认证所需要的密码
sentinel auth-pass mymaster final123
3、创建服务脚本
vi /etc/systemd/system/sentinel.service

[Unit]
Description=Sentinel service
After=network.target

[Service]
Type=simple
User=root
Group=root
ExecStart=/opt/redis/bin/redis-sentinel /opt/redis/sentinel.conf --sentinel --daemonize no
PrivateTmp=true
Restart=always

[Install]
WantedBy=multi-user.target
4、启动服务
systemctl daemon-reload

systemctl start sentinel.service

systemctl enable sentinel.service

三、测试

1、主服务器查看 主从复制相关信息

可以看到当前master是192.168.140.153

[root@localhost redis]# redis-cli -h 192.168.140.153
192.168.140.153:6379> auth final123
OK
192.168.140.153:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.140.156,port=6379,state=online,offset=957428,lag=1
slave1:ip=192.168.140.159,port=6379,state=online,offset=957283,lag=1
master_failover_state:no-failover
master_replid:9ec1820323cd826f2955176a75e99dbdfde51f30
master_replid2:2840abf8606e7ea7237bfa1f03225898e634d2a4
master_repl_offset:957428
second_repl_offset:396279
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:97903
repl_backlog_histlen:859526
2、启停主服务器的redis服务
# 停止主服务器的redis服务
systemctl stop redis.service

# 间隔10秒,等待故障转移完成,启动服务
systemctl start redis.service
3、查看sentinel日志

可以看到当前主节点已经转移到192.168.140.159机器上

[root@localhost redis]# tail -f logs/redis-sentinel.log
1460:X 05 Jun 2024 23:17:20.936 # +sdown slave 192.168.140.153:6379 192.168.140.153 6379 @ mymaster 192.168.140.159 6379
4、查看主节点是否转移

在159服务器查看redis主从信息,发现159的角色已经变为master,故障转移成功

[root@localhost redis]# redis-cli -h 192.168.140.159
192.168.140.159:6379> auth final123
OK
192.168.140.159:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.140.156,port=6379,state=online,offset=1074384,lag=0
slave1:ip=192.168.140.153,port=6379,state=online,offset=1074384,lag=0
master_failover_state:no-failover
master_replid:401449cc3f667d6032124da09b2002dbd1a36a52
master_replid2:9ec1820323cd826f2955176a75e99dbdfde51f30
master_repl_offset:1074384
second_repl_offset:967527
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:408285
repl_backlog_histlen:666100

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值