当前位置: 首页 > news >正文

Redis-6.2.9 Sentinel 哨兵配置

目录

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.xdnf.cn/news/755641.html

相关文章:

  • WSL2 安装与Docker安装
  • 基于微信小程序的scratch学习系统
  • 图像数据如何表示为概率单纯形
  • Github 2025-06-01开源项目月报 Top20
  • 构建系统maven
  • 水系电池的“再登场”
  • 使用lighttpd和开发板进行交互
  • Node.js 全栈技术栈的开发者,Web3 面试题
  • 从架构视角设计统一网络请求体系 —— 基于 uni-app 的前后端通信模型
  • 如何使用 Docker 部署grafana和loki收集vllm日志?
  • IDEA常用快捷键
  • 6.1 数学复习笔记 23
  • Freefilesync配置windows与windows,windows与linux之间同步
  • LTSPICE仿真电路:(三十二)差动放大器电流源
  • openssl 怎么生成吊销列表
  • 【论文解读】DETR | End-to-End Object Detection with Transformers
  • 分类预测 | Matlab实现CNN-LSTM-Attention高光谱数据分类
  • 多线程——线程池
  • Linux下目录递归拷贝的单进程实现
  • C++读写锁以及实现方式
  • 深入理解短链服务:原理、设计与实现全解析
  • 6级翻译学习
  • 蓝桥云课ROS学习C++教程cpp-geeksforgeeks版本
  • Vue-1-前端框架Vue基础入门之一
  • 6、在树莓派上安装 NTP(Network Time Protocol )服务的步骤
  • Linux --进程状态
  • 论文阅读笔记——Quo Vadis, Action Recognition? A New Model and the Kinetics Dataset
  • 《Python语言程序设计》2018 第4章第9题3重量和价钱的对比,利用第7章的概念来解答你
  • 广东WordPress开发公司及服务
  • 2025年渗透测试面试题总结-奇安信[实习]安全服务工程师(题目+回答)