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

相关推荐
多多*7 小时前
蓝桥杯国赛训练 day1
java·开发语言·数据库·redis·缓存·职场和发展·蓝桥杯
weixin_307779137 小时前
使用Redis作为缓存优化ElasticSearch读写性能
redis·分布式·elasticsearch·缓存·架构
清风~徐~来10 小时前
【Redis】set 类型
java·数据库·redis
书山有路勤为径~11 小时前
Docker 安装 Redis 容器
redis·docker·eureka
小小星球之旅11 小时前
redis缓存常见问题
数据库·redis·学习·缓存
bing_15811 小时前
Redis 缓存粒度如何控制?缓存整个对象还是部分字段?
redis·缓存
纪元A梦11 小时前
Redis最佳实践——性能优化技巧之缓存预热与淘汰策略
redis·缓存·性能优化
未来并未来11 小时前
Redis 缓存问题及其解决方案
java·redis·缓存
码农开荒路14 小时前
Redis线程模型
数据库·redis·缓存
bing_15814 小时前
如何合理设计缓存 Key的命名规范,以避免在共享 Redis 或跨服务场景下的冲突?
数据库·redis·缓存