Redis-6.2.9 Sentinel 哨兵配置

news2025/6/4 14:39:46

目录

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

2 集群架构图

3 部署redis主从

4 sentinel 配置文件

5 运维管理

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)  1) "name"
    2) "mymaster"
    3) "ip"
    4) "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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2395968.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

基于TMC5160堵转检测技术的夹紧力控制系统设计与实现

点击下面图片带您领略全新的嵌入式学习路线 🔥爆款热榜 90万阅读 1.6万收藏 一、技术背景与系统原理 在工业自动化领域,夹紧力控制是精密装配、机床夹具等场景的核心需求。传统方案多采用压力传感器伺服电机的闭环控制方式,但存在系统复杂…

XCTF-web-fileclude

解析如下 <?php include("flag.php"); // 包含敏感文件&#xff08;通常包含CTF挑战的flag&#xff09; highlight_file(__FILE__); // 高亮显示当前PHP文件源代码&#xff08;方便查看代码逻辑&#xff09;if(isset($_GET["file1"]…

OpenShift AI - 启用过时版本的 Notebook 镜像

《OpenShift / RHEL / DevSecOps 汇总目录》 说明&#xff1a;本文已经在 OpenShift 4.18 OpenShift AI 2.19 的环境中验证 文章目录 查看可用 Notebook 镜像控制台查看命令行查看 Notebook 镜像、Image Stream 和 Image Registry Repository 对应关系启用老版本的 Notebook 镜…

Redis 缓存穿透、缓存击穿、缓存雪崩详解与解决方案

在分布式系统中&#xff0c;Redis 凭借高性能和高并发处理能力&#xff0c;成为常用的缓存组件。然而&#xff0c;在实际应用中&#xff0c;缓存穿透、缓存击穿、缓存雪崩这三大问题会严重影响系统的性能与稳定性。本文将详细解析这三个问题的成因&#xff0c;并提供对应的解决…

DQN和DDQN(进阶版)

来源&#xff1a; *《第五章 深度强化学习 Q网络》.ppt --周炜星、谢文杰 一、前言 Q表格、Q网络与策略函数 Q表格是有限的离散的&#xff0c;而神经网络可以是无限的。 对于动作有限的智能体来说&#xff0c;使用Q网络获得当下状态的对于每个动作的 状态-动作值 。那么 a…

【组件】翻牌器效果

目录 效果组件代码背景素材 效果 组件代码 <template><divclass"card-flop":style"{height: typeof height number ? ${height}px : height,--box-width: typeof boxWidth number ? ${boxWidth}px : boxWidth,--box-height: typeof boxHeight nu…

CentOS 7 环境中部署 LNMP(Linux + Nginx + MySQL 5.7 + PHP)

在 CentOS 7 环境中部署 LNMP&#xff08;Linux Nginx MySQL 5.7 PHP&#xff09; 环境的详细步骤如下。此方案确保各组件版本兼容&#xff0c;并提供完整的配置验证流程。 1. 更新系统 sudo yum update -y 2. 安装 MySQL 5.7 2.1 添加 MySQL 官方 YUM 仓库 由于MySQL并不…

NX811NX816美光颗粒固态NX840NX845

NX811NX816美光颗粒固态NX840NX845 美光NX系列固态硬盘颗粒深度解析&#xff1a;技术、性能与市场全景透视 一、技术架构与核心特性解析 1. NX811/NX816&#xff1a;入门级市场的平衡之选 技术定位&#xff1a;基于176层TLC&#xff08;Triple-Level Cell&#xff09;3D NAN…

捋捋wireshark

本猿搬砖时会用到wireshark分析pcap包&#xff0c;但频率不高&#xff0c;记过一些笔记&#xff0c;今天捋捋&#xff0c;希望能给初学者节省一点时间。 wireshark是个网络封包分析软件&#xff08;network packet analyzer&#xff09;&#xff0c;可以用来抓流量包&#xff…

c++学习之---模版

目录 一、函数模板&#xff1a; 1、基本定义格式&#xff1a; 2、模版函数的优先匹配原则&#xff1a; 二、类模板&#xff1a; 1、基本定义格式&#xff1a; 2、类模版的优先匹配原则&#xff08;有坑哦&#xff09;&#xff1a; 3、缺省值的设置&#xff1a; 4、ty…

第十六章 EMQX黑名单与连接抖动检测

系列文章目录 第一章 总体概述 第二章 在实体机上安装ubuntu 第三章 Windows远程连接ubuntu 第四章 使用Docker安装和运行EMQX 第五章 Docker卸载EMQX 第六章 EMQX客户端MQTTX Desktop的安装与使用 第七章 EMQX客户端MQTTX CLI的安装与使用 第八章 Wireshark工具的安装与使用 …

新编辑器编写指南--给自己的备忘

欢迎使用Markdown编辑器 你好&#xff01; 这是你第一次使用 Markdown编辑器 所展示的欢迎页。如果你想学习如何使用Markdown编辑器, 可以仔细阅读这篇文章&#xff0c;了解一下Markdown的基本语法知识。 新的改变 我们对Markdown编辑器进行了一些功能拓展与语法支持&#x…

鸿蒙网络数据传输案例实战

一、案例效果截图 二、案例运用到的知识点 核心知识点 网络连接管理&#xff1a;connection模块HTTP数据请求&#xff1a;http模块RPC数据请求&#xff1a;rcp模块文件管理能力&#xff1a;fileIo模块、fileUri模块 其他知识点 ArkTS 语言基础V2版状态管理&#xff1a;Comp…

【JavaEE】-- 网络原理

文章目录 1. 网络发展史1.1 广域网1.2 局域网 2. 网络通信基础2.1 IP地址2.2 端口号2.3 认识协议2.4 五元组2.5 协议分层2.5.1 分层的作用2.5.2 OSI七层模型&#xff08;教科书&#xff09;2.5.3 TCP/IP五层&#xff08;或四层&#xff09;模型&#xff08;工业中常用&#xff…

1.RV1126-OPENCV 交叉编译

一.下载opencv-3.4.16.zip到自己想装的目录下 二.解压并且打开 opencv 目录 先用 unzip opencv-3.4.16.zip 来解压 opencv 的压缩包&#xff0c;并且进入 opencv 目录(cd opencv-3.4.16) 三. 修改 opencv 的 cmake 脚本的内容 先 cd platforms/linux 然后修改 arm-gnueabi.to…

PySide6 GUI 学习笔记——常用类及控件使用方法(标签控件QLabel)

文章目录 标签控件QLabel及其应用举例标签控件QLabel的常用方法及信号应用举例Python 代码示例1Python 代码示例2 小结 标签控件QLabel及其应用举例 QLabel 是 PySide6.QtWidgets 模块中的一个控件&#xff0c;用于在界面上显示文本或图像。它常用于作为标签、提示信息或图片展…

CSS (mask)实现服装动态换色:创意与技术的完美融合

在网页开发中&#xff0c;我们常常会遇到需要对图片元素进行个性化处理的需求&#xff0c;比如改变图片中特定部分的颜色。今天&#xff0c;我们就来探讨一种通过 CSS 和 JavaScript 结合&#xff0c;实现服装动态换色的有趣方法。 一、代码整体结构分析 上述代码构建了一个完…

基于51单片机的音乐盒汽车喇叭调音量proteus仿真

地址&#xff1a; https://pan.baidu.com/s/1l3CSSMi4uMV5-XLefnKoSg 提取码&#xff1a;1234 仿真图&#xff1a; 芯片/模块的特点&#xff1a; AT89C52/AT89C51简介&#xff1a; AT89C51 是一款常用的 8 位单片机&#xff0c;由 Atmel 公司&#xff08;现已被 Microchip 收…

彻底理解Spring三级缓存机制

文章目录 前言一、Spring解决循环依赖时&#xff0c;为什么要使用三级缓存&#xff1f; 前言 Spring解决循环依赖的手段&#xff0c;是通过三级缓存&#xff1a; singletonObjects&#xff1a;存放所有生命周期完整的单例对象。&#xff08;一级缓存&#xff09;earlySingleto…

【产品经理从0到1】自媒体端产品设计

后台的定义 “后台” 与“前台”都是相对独立的平台&#xff0c;前台是服务于互联网用户的平台 &#xff0c;后台主要是支撑前台页面内容、数据及对前台业务情况的统计分析的系统&#xff1b; 后台与前台的区别 第1&#xff1a;使用用户不同 前台用户&#xff1a;互联网用户…