Redis-6.2.9 Sentinel 哨兵配置

目录

[1 操作系统信息和redis软件版本](#1 操作系统信息和redis软件版本)

[2 集群架构图](#2 集群架构图)

[3 部署redis主从](#3 部署redis主从)

[4 sentinel 配置文件](#4 sentinel 配置文件)

[5 运维管理](#5 运维管理)

[6 go编写应用业务测试](#6 go编写应用业务测试)


哨兵核心功能:能够后台监控redis主机是否故障,如果故障了根据投票自动将从库转换为主库

1 操作系统信息和redis软件版本

root@u24-redis-120:~# cat /etc/issue

Ubuntu 24.04.2 LTS \n \l

root@u24-redis-120:~# redis-server --version

Redis server v=6.2.9 sha=00000000:0 malloc=libc bits=64 build=56edd385f7ce4c9b

2 集群架构图

2.1 redis主从集群

192.168.254.120 u24-redis-120 #主库

192.168.254.121 u24-redis-121 #从库

192.168.254.122 u24-redis-122 #从库

2.2 sentinel 集群

192.168.254.120 u24-redis-120 #端口26379

192.168.254.121 u24-redis-121 #端口26379

192.168.254.122 u24-redis-122 #端口26379

3 部署redis主从

源码编译安装参考:https://blog.csdn.net/zyb378747350/article/details/148295180

在redis主库上

#将redis软件拷贝到从库

rsync -r /usr/local/redis-6.2.9 192.168.254.121:/usr/local/

rsync -r /usr/local/redis-6.2.9 192.168.254.122:/usr/local/

#拷贝redis的rdb数据

rsync -r /redis 192.168.254.121:/

rsync -r /redis 192.168.254.122:/

#redis配置

主库

root@u24-redis-120:~# cat /usr/local/redis-6.2.9/etc/redis.conf

bind 0.0.0.0

daemonize yes

pidfile /redis/data/redis_6379.pid

loglevel notice

logfile "/redis/log/redis_6379.log"

databases 16

dir /redis/data

从库

root@u24-redis-121:~# cat /usr/local/redis-6.2.9/etc/redis.conf

bind 0.0.0.0

daemonize yes

pidfile /redis/data/redis_6379.pid

loglevel notice

logfile "/redis/log/redis_6379.log"

databases 16

dir /redis/data

slaveof 192.168.254.120 6379 #配置主从参数

从库

root@u24-redis-122:~# cat /usr/local/redis-6.2.9/etc/redis.conf

bind 0.0.0.0

daemonize yes

pidfile /redis/data/redis_6379.pid

loglevel notice

logfile "/redis/log/redis_6379.log"

databases 16

dir /redis/data

slaveof 192.168.254.120 6379 #配置主从参数

启动redis服务

/usr/local/redis-6.2.9/bin/redis-server /usr/local/redis-6.2.9/etc/redis.conf

查看信息

root@u24-redis-120:~# redis-cli

127.0.0.1:6379> info replication

Replication

role:master

connected_slaves:2

slave0:ip=192.168.254.121,port=6379,state=online,offset=4176,lag=0 #从库

slave1:ip=192.168.254.122,port=6379,state=online,offset=4176,lag=1

master_failover_state:no-failover

master_replid:62750ed102cf2ec85d3bc812702eee5ea17e7568

master_replid2:0000000000000000000000000000000000000000

master_repl_offset:4176

second_repl_offset:-1

repl_backlog_active:1

repl_backlog_size:1048576

repl_backlog_first_byte_offset:1

repl_backlog_histlen:4176

4 sentinel 配置文件

三台sentinel配置和操作都一样

vi sentinel.conf

port 26379

daemonize yes

pidfile /redis/sentinel/sentinel_26379.pid

logfile "/redis/sentinel/sentinel_26379.log"

dir /redis/sentinel

sentinel monitor mymaster 192.168.254.120 6379 2

sentinel down-after-milliseconds mymaster 30000

sentinel parallel-syncs mymaster 1

sentinel failover-timeout mymaster 18000

创建目录

mkdir /redis/sentinel -p

#启动

/usr/local/redis-6.2.9/bin/redis-server /usr/local/redis-6.2.9/etc/sentinel.conf --sentinel

5 运维管理

5.1 查看sentinel统计信息

root@u24-redis-121:~# redis-cli -p 26379 info sentinel

Sentinel

sentinel_masters:1

sentinel_tilt:0

sentinel_running_scripts:0

sentinel_scripts_queue_length:0

sentinel_simulate_failure_flags:0

master0:name=mymaster,status=ok,address=192.168.254.122:6379,slaves=2,sentinels=3

root@u24-redis-121:~# redis-cli -p 26379 sentinel masters

    1. "name"
  1. "mymaster"

  2. "ip"

  3. "192.168.254.122"

5.2 主从故障切换测试

root@u24-redis-121:~# redis-cli -p 26379 info sentinel

Sentinel

sentinel_masters:1

sentinel_tilt:0

sentinel_running_scripts:0

sentinel_scripts_queue_length:0

sentinel_simulate_failure_flags:0

master0:name=mymaster,status=ok,address=192.168.254.122:6379,slaves=2,sentinels=3

关闭主库192.168.254.122

root@u24-redis-122:~# redis-cli -p 6379 shutdown

root@u24-redis-122:~# ps aux|grep redis

root 3218 1.1 0.1 34012 4008 ? Ssl 16:30 0:12 /usr/local/redis-6.2.9/bin/redis-server *:26379 [sentinel]

root 3255 0.0 0.0 6544 2164 pts/0 S+ 16:49 0:00 grep --color=auto redis

验证信息

root@u24-redis-122:~# redis-cli -p 26379 info sentinel

Sentinel

sentinel_masters:1

sentinel_tilt:0

sentinel_running_scripts:0

sentinel_scripts_queue_length:0

sentinel_simulate_failure_flags:0

master0:name=mymaster,status=ok,address=192.168.254.120:6379,slaves=2,sentinels=3

主库修改为192.168.254.120

root@u24-redis-120:~# redis-cli -p 6379 info replication

Replication

role:master

connected_slaves:1

#只有一台从库

slave0:ip=192.168.254.121,port=6379,state=online,offset=302113,lag=0

master_failover_state:no-failover

master_replid:765b5275e77329cc6ab0f387968e1cde6044092f

master_replid2:f6f50ad20d4efc8e969894c46bef3620873314c5

master_repl_offset:302258

second_repl_offset:268372

repl_backlog_active:1

repl_backlog_size:1048576

repl_backlog_first_byte_offset:153637

repl_backlog_histlen:148622

#重启已经关闭redis服务

root@u24-redis-122:~# /usr/local/redis-6.2.9/bin/redis-server /usr/local/redis-6.2.9/etc/redis.conf

root@u24-redis-120:~# redis-cli -p 6379 info replication

Replication

role:master

connected_slaves:2

#已经转变为从库

slave0:ip=192.168.254.121,port=6379,state=online,offset=322707,lag=0

slave1:ip=192.168.254.122,port=6379,state=online,offset=322707,lag=0

master_failover_state:no-failover

master_replid:765b5275e77329cc6ab0f387968e1cde6044092f

master_replid2:f6f50ad20d4efc8e969894c46bef3620873314c5

master_repl_offset:322707

second_repl_offset:268372

repl_backlog_active:1

repl_backlog_size:1048576

repl_backlog_first_byte_offset:153637

repl_backlog_histlen:169071

5.3 强制主从切换

root@u24-redis-121:~# redis-cli -p 26379 info sentinel

Sentinel

sentinel_masters:1

sentinel_tilt:0

sentinel_running_scripts:0

sentinel_scripts_queue_length:0

sentinel_simulate_failure_flags:0

master0:name=mymaster,status=ok,address=192.168.254.120:6379,slaves=2,sentinels=3

#强制提升主库

root@u24-redis-121:~# redis-cli -p 26379 sentinel failover mymaster

OK

root@u24-redis-121:~# redis-cli -p 26379 info sentinel

Sentinel

sentinel_masters:1

sentinel_tilt:0

sentinel_running_scripts:0

sentinel_scripts_queue_length:0

sentinel_simulate_failure_flags:0

#由源码192.168.254.120转换192.168.254.122

master0:name=mymaster,status=ok,address=192.168.254.122:6379,slaves=2,sentinels=3

6 go编写应用业务测试

package main

import (

"context"

"fmt"

"github.com/redis/go-redis/v9"

)

func main() {

rdb := redis.NewFailoverClusterClient(&redis.FailoverOptions{

MasterName: "mymaster",

SentinelAddrs: []string{"192.168.254.120:26379", "192.168.254.121:26379", "192.168.254.122:26379"},

})

ctx := context.Background()

//写入redis

rdb.Set(ctx, "key02", "value-02", 0)

//读取redis

val, err := rdb.Get(ctx, "key02").Result()

if err != nil {

fmt.Printf("操作错误")

return

}

fmt.Printf(val)

}

测试结果:

redis: 2025/06/01 18:15:16 sentinel.go:746: sentinel: selected addr=192.168.254.121:26379 masterAddr=192.168.254.122:6379

value-02

Process finished with the exit code 0

相关推荐
半新半旧5 小时前
python 整合使用 Redis
redis·python·bootstrap
daixin88487 小时前
什么是缓存雪崩?缓存击穿?缓存穿透?分别如何解决?什么是缓存预热?
java·开发语言·redis·缓存
daixin88489 小时前
Redis过期数据的删除策略是什么?有哪些?
数据库·redis·缓存
幻灭行度12 小时前
通过redis_exporter监控redis cluster
数据库·redis·缓存
冷崖16 小时前
Redis缓存策略以及bigkey的学习(九)
redis·学习·缓存
chen1108____1 天前
用 Docker 一键部署 Flask + Redis 微服务
redis·docker·flask
失散132 天前
大型微服务项目:听书——10 缓存+分布式锁优化根据专辑id查询专辑详情接口
redis·分布式·缓存·微服务
Aeside12 天前
Redis的线程模型
redis
white camel2 天前
分布式方案 一 分布式锁的四大实现方式
redis·分布式·zookeeper·分布式锁
大佐不会说日语~2 天前
Redis高可用架构演进面试笔记
redis·面试·架构