快速测试 3节点的redis sentinel集群宕机2个节点以后是否仍能正常使用

有同事问我,三个redis sentinel节点,宕机两个节点以后,是否还能够正常的通过redis sentinel正常访问redis的数据。我想了想,理论上是可以的,但是我没试过,今天有时间就测试了一下。搭建环境和测试代码的郭晨不够一波三折,以下是配置信息。虚拟机使用的是CENTOS7 ,docker是现成的,redis镜像的版本redis_version:7.0.11 , python是python3

使用docker compose 快速启动 redis节点和sentinel集群,这样会比较快。

docker-compose.yml

version: '3.0'
services:master:image: rediscontainer_name: redis-masterports:- 6379:6379# 这个模式不要用 host,会有问题,用bridgenetwork_mode: bridgecommand: redis-server /usr/local/etc/redis/redis.confvolumes:- ./redis1.conf:/usr/local/etc/redis/redis.confslave1:image: rediscontainer_name: redis-slave-1ports:- 6380:6380network_mode: bridgecommand: redis-server /usr/local/etc/redis/redis.conf --slaveof 10.4.7.124 6379volumes:- ./redis2.conf:/usr/local/etc/redis/redis.confslave2:image: rediscontainer_name: redis-slave-2ports:- 6381:6381network_mode: bridgecommand: redis-server /usr/local/etc/redis/redis.conf --slaveof 10.4.7.124 6379volumes:- ./redis3.conf:/usr/local/etc/redis/redis.confsentinel1:image: rediscontainer_name: redis-sentinel-1ports:- 26379:26379network_mode: bridgecommand: redis-sentinel /usr/local/etc/redis/sentinel.confvolumes:- ./sentinel1.conf:/usr/local/etc/redis/sentinel.confsentinel2:image: rediscontainer_name: redis-sentinel-2ports:- 26380:26380network_mode: bridgecommand: redis-sentinel /usr/local/etc/redis/sentinel.confvolumes:- ./sentinel2.conf:/usr/local/etc/redis/sentinel.confsentinel3:image: rediscontainer_name: redis-sentinel-3ports:- 26381:26381network_mode: bridgecommand: redis-sentinel /usr/local/etc/redis/sentinel.confvolumes:- ./sentinel3.conf:/usr/local/etc/redis/sentinel.conf

redis1.conf

bind 0.0.0.0
protected-mode no
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize no
supervised no
pidfile /var/run/redis_6379.pid
loglevel notice
logfile ""
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir ./
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
replica-priority 100
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes

redis2.conf

bind 0.0.0.0
# 这个模式要no,否则不好连接测试
protected-mode no
port 6380
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize no
supervised no
pidfile /var/run/redis_6380.pid
loglevel notice
logfile ""
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir ./
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
replica-priority 100
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes

redis3.conf

bind 0.0.0.0
protected-mode no
port 6381
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize no
supervised no
pidfile /var/run/redis_6381.pid
loglevel notice
logfile ""
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir ./
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
replica-priority 100
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes

sentinel1.conf

port 26379
sentinel monitor mymaster 10.4.7.124 6379 2
sentinel down-after-milliseconds mymaster 10000
sentinel failover-timeout mymaster 180000
sentinel parallel-syncs mymaster 1

sentinel2.conf

port 26380                                                                                                                                                                                                                 
sentinel monitor mymaster 10.4.7.124 6379 2                                                                                                                                                                                
sentinel down-after-milliseconds mymaster 10000                                                                                                                                                                            
sentinel failover-timeout mymaster 180000                                                                                                                                                                                  
sentinel parallel-syncs mymaster 1  

sentinel3.conf

port 26381                                                                                                                                                                                                                 
sentinel monitor mymaster 10.4.7.124 6379 2                                                                                                                                                                                
sentinel down-after-milliseconds mymaster 10000                                                                                                                                                                            
sentinel failover-timeout mymaster 180000                                                                                                                                                                                  
sentinel parallel-syncs mymaster 1      

启动所有服务

docker-compose up -d
docker compose up -d
[root@docker_124 sentinel]# docker compose up -d 
[+] Running 6/6✔ Container redis-slave-2     Started                                                                                                                                                                                0.1s ✔ Container redis-sentinel-1  Started                                                                                                                                                                                0.1s ✔ Container redis-sentinel-2  Started                                                                                                                                                                                0.1s ✔ Container redis-sentinel-3  Started                                                                                                                                                                                0.1s ✔ Container redis-master      Started                                                                                                                                                                                0.1s ✔ Container redis-slave-1     Started    

停止所有服务

docker-compose down
[root@docker_124 sentinel]# docker compose up -d 
[+] Running 6/6✔ Container redis-slave-2     Started                                                                                                                                                                                0.1s ✔ Container redis-sentinel-1  Started                                                                                                                                                                                0.1s ✔ Container redis-sentinel-2  Started                                                                                                                                                                                0.1s ✔ Container redis-sentinel-3  Started                                                                                                                                                                                0.1s ✔ Container redis-master      Started                                                                                                                                                                                0.1s ✔ Container redis-slave-1     Started        

集群启动好以后,测试是否正常启动

[root@docker_124 sentinel]# docker run -it --rm redis redis-cli -h 10.4.7.124 -p 6379 info replication
# Replication
role:master
connected_slaves:2
slave0:ip=172.17.0.1,port=6380,state=online,offset=170743,lag=1
slave1:ip=172.17.0.1,port=6381,state=online,offset=170743,lag=0
master_failover_state:no-failover
master_replid:c85254ee815510a442514fa73aeba46fc8fb018f
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:170743
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:170743[root@docker_124 sentinel]# docker run -it --rm redis redis-cli -h 10.4.7.124 -p 6380
[root@docker_124 sentinel]# docker run -it --rm redis redis-cli -h 10.4.7.124 -p 6381
[root@docker_124 sentinel]# docker run -it --rm redis redis-cli -h 10.4.7.124 -p 26380
[root@docker_124 sentinel]# docker run -it --rm redis redis-cli -h 10.4.7.124 -p 26381
[root@docker_124 sentinel]# docker run -it --rm redis redis-cli -h 10.4.7.124 -p 26379
10.4.7.124:26379> sentinel master mymaster1) "name"2) "mymaster"3) "ip"4) "10.4.7.124"5) "port"6) "6379"7) "runid"8) "8c21a5f4a8fdcabb3677603800a66a2b41c62f3c"9) "flags"
10) "master"……省略……[root@docker_124 sentinel]# docker run -it --rm redis redis-cli -h 10.4.7.124 -p 26379 sentinel slaves mymaster
1)  1) "name"2) "172.17.0.1:6381"3) "ip"4) "172.17.0.1"5) "port"6) "6381"7) "runid"8) "10053461cfeedbf53aec9f0afa66570f079483f6"9) "flags"10) "slave"……省略……

环境正常启动后可以运行Python脚本进行测试

测试python脚本

from redis.sentinel import Sentinel
import time
import random# 生成随机字符串
def generate_random_str(randomlength=16):random_str = ''base_str = 'ABCDEFGHIGKLMNOPQRSTUVWXYZabcdefghigklmnopqrstuvwxyz0123456789'length = len(base_str) - 1for i in range(randomlength):random_str += base_str[random.randint(0, length)]return random_str# 连接Redis Sentinel
sentinel = Sentinel([('10.4.7.124', 26379),('10.4.7.124', 26380),('10.4.7.124', 26381)],socket_timeout=0.5)# 获取主服务器地址
master = sentinel.master_for('mymaster', socket_timeout=0.5, db=0)
w_ret = master.set('foo', 'bar')
print(master)
# 输出:('192.168.31.87', 26379)
# 获取从服务器地址
slave = sentinel.slave_for('mymaster', socket_timeout=0.5, db=0)
print(slave)
# 输出:[('192.168.31.88', 26379)]while True:print('master:', master.get('foo'))# master 向redis 插入随机数据master.set('foo', generate_random_str(10))print('sleep 1s')time.sleep(1)

python 程序启动后会每秒请求sentinel地址获取redis连接设置值,然后我将sentinel集群的节点停掉两个(包括master节点),依然可以正常设置值,在redis的 master节点可以获取最新设置的值,说明sentinel节点停掉两个仍是可用状态。

参考文档

  • Dock-compose 搭建redis-sentinel测试环境

    https://www.cnblogs.com/leffss/p/12082361.html

  • python3连接redis sentinel集群

    https://blog.csdn.net/u012887259/article/details/102425691

  • 解决 WARNING: Published ports are discarded when using host network mode 问题

    https://blog.csdn.net/zengNLP/article/details/127220244

  • 安装 docker compose

    我安装完只能用docker compose 不能用docker-compose,不知道为什么。

    yum -y install docker-compose-plugin
    

    https://blog.csdn.net/pushiqiang/article/details/78682323

    https://zhuanlan.zhihu.com/p/570108654

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

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

教你如何一键生成微信分享卡片

今天说一种非常快捷的方式,快捷到什么程度呢? 1.关注微信公众号“C1N短网址服务” 2.发送你想要的链接内容,即可得到一张微信卡片 使用步骤: 按如下格式发送消息到公众号,公众号自动回复微信卡片,然后就…

YOLO的全面综述:从YOLOv1到最新版本

文章目录 摘要1、简介2、YOLO在不同领域的应用3、目标检测的度量标准和非最大值抑制(NMS)3.1. AP如何工作?3.2. 计算AP3.3、非极大值抑制(NMS) 4、YOLO: You Only Look Once4.1、YOLOv1的工作原理4.2、YOLOv1架构4.3、…

ingress介绍和ingress通过LoadBalancer暴露服务配置

目录 一.ingress基本原理介绍 1.将原有用于暴露服务和负载均衡的服务的三四层负载均衡变为一个七层负载均衡 2.controller和ingress 3.通过下面这个图可能会有更直观的理解 二.为什么会出现ingress 1.NodePort存在缺点 2.LoadBalancer存在缺点 三.ingress三种暴露服务的…

Linux篇:进程间通信

一、进程间通信原理: 1、通信是有成本的:两个或者多个进程,实现数据层面的交互,因为进程独立性的存在,导致进程通信的成本比较高。 2、进程间通信的方式: ①基本数据 ②发送命令 ③某种协同 ④通知 .....…

Google 发布史上最强大模型,能否抗衡 OpenAI?

昨晚 Google 发布了史上最强人工智能模型:Gemini。 Google CEO Sundar Pichai CEO 说 Gemini 时代是 Google 新时代的开始。 Gemini 是 Google 最新的大型语言模型,Pichai 首次在 6 月的 I/O 开发者大会上透露了这一消息,如今正式公开发布。…

利用proteus实现串口助手和arduino Mega 2560的串口通信

本例用到的proteus版本为8.13,ardunio IDE版本为2.2.1,虚拟串口vspd版本为7.2,串口助手SSCOM V5.13.1。软件的下载安装有很多教程,大家可以自行搜索,本文只介绍如何利用这4种软件在proteus中实现arduino Mega 2560的串…

单片机学习13——串口通信

单片机的通信功能: 实现单片机和单片机的信息交换,实现单片机和计算机的信息交换。 计算机通信是指计算机与外部设备或计算机与计算机之间的信息交换。 通信有并行通信和串行通信两种方式。 在多微机系统以及现在测控系统中信息的交换多采用串行通信方…

12.7图欧拉回路与路径,图的一些性质,存储方式,图的遍历(有向图无向图BFSDFS)

欧拉回路与欧拉路径 存在条件 无向图存在欧拉回路的充要条件 一个无向图存在欧拉回路,当且仅当该图所有顶点度数都为偶数,且该图是连通图。 无向图存在欧拉路径的充要条件 当且仅当该图顶点度数为奇数的点的个数为0或者2。 欧拉定理二: 如果一个无向图…

sql注入漏洞--MYSQL两种思路

在学习之前,先自己搭建了一个网站 在前端可以实现与后端数据库的交互查询 创建一个数据库名为wy,表名为users 建立字段,定义类型 插入数据 INSERT INTO users(name, password,photo, money) VALUES ("DuZZ",123456,11,100); IN…

(三)基于高尔夫优化算法GOA求解无人机三维路径规划研究(MATLAB代码)

一、无人机模型简介: 单个无人机三维路径规划问题及其建模_IT猿手的博客-CSDN博客 参考文献: [1]胡观凯,钟建华,李永正,黎万洪.基于IPSO-GA算法的无人机三维路径规划[J].现代电子技术,2023,46(07):115-120 二、高尔夫优化算法GOA简介 高尔夫优化算法…

虚拟数据优化器VDO

本章主要介绍虚拟化数据优化器。 什么是虚拟数据优化器VDO创建VDO设备以节约硬盘空间 了解什么是VDO VDO全称是Virtual Data Optimize(虚拟数据优化),主要是为了节省硬盘空间。 现在假设有两个文件file1和 file2,大小都是10G。file1和 f…

Retrofit嵌套请求与适配器

一、前言: 1. retrofit嵌套请求 在实际开发中,可能会存在:需要先请求A接口,在请求B接口的情况,比如进入“玩android”网页请求获取收藏文章列表,但是需要先登录拿到Cookie才能请求搜藏文章几口&am…