docker-compose搭建redis环境:哨兵模式(一主两重两哨兵)

2024-04-24 04:12

本文主要是介绍docker-compose搭建redis环境:哨兵模式(一主两重两哨兵),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

  • 0.BG
  • 1. 编写docker-compose.yml文件
  • 2. 哨兵配置文件sentinel.conf
  • 3.启动容器
  • 4.模拟故障转移

0.BG

redis环境有多中模式,包括Standalone,Cluster和Sentinel模式等。这里介绍一种简单搭建Sentinel模式的方法,搭建一个一主两重两哨兵的redis环境。
在这里插入图片描述

1. 编写docker-compose.yml文件

version: '3.8'services:redis-master:container_name: redis-masterimage: redis:6.0networks:redis-yy:ipv4_address: 172.28.5.2ports:- "6379:6379"command: redis-server --appendonly yesredis-slave-1:container_name: redis-slave-1image: redis:6.0networks:redis-yy:ipv4_address: 172.28.5.3ports:- "6380:6379"command: redis-server --slaveof redis-master 6379 --appendonly yesdepends_on:- redis-masterredis-slave-2:container_name: redis-slave-2image: redis:6.0networks:redis-yy:ipv4_address: 172.28.5.4ports:- "6381:6379"command: redis-server --slaveof redis-master 6379 --appendonly yesdepends_on:- redis-masterredis-sentinel-1:container_name: redis-sentinel-1image: redis:6.0networks:redis-yy:ipv4_address: 172.28.5.5ports: - "26379:26379"command: redis-sentinel /etc/redis/sentinel.confvolumes:- $PWD/sentinel.conf:/etc/redis/sentinel.confdepends_on:- redis-master- redis-slave-1- redis-slave-2redis-sentinel-2:container_name: redis-sentinel-2image: redis:6.0networks:redis-yy:ipv4_address: 172.28.5.6ports: - "26380:26379"command: redis-sentinel /etc/redis/sentinel.confvolumes:- $PWD/sentinel.conf:/etc/redis/sentinel.confdepends_on:- redis-master- redis-slave-1- redis-slave-2               networks:redis-yy:ipam:driver: defaultconfig:- subnet: 172.28.0.0/16ip_range: 172.28.5.0/24gateway: 172.28.5.254              

简单介绍一下上述文件:

  • 只是给容器命名,指定了其redis镜像的版本。
  • Command是容器启动后执行的指令:
    • master节点只是以AOF方式启动。
    • slave节点要指明其master节点是哪个。
    • sentinel节点启动指明了配置文件,具体配置内容在配置文件中。
    • 这里都没有设置密码

2. 哨兵配置文件sentinel.conf

sentinel monitor redis-yy 172.28.5.2 6379 2
sentinel down-after-milliseconds redis-yy 30000
sentinel parallel-syncs redis-yy 1
sentinel failover-timeout redis-yy 180000

sentinel monitor redis-yy 172.30.0.2 6379 2
这条指令的意思是哨兵监控一个名为redis-yy的redis,这个redis的IP与端口号是172.30.0.2与6379。最后一个2表示的是,由主观下线转变为客观下线的条件,即当两个哨兵认为当前master节点下线,那么就客观认为当前master节点已经下线。

sentinel down-after-milliseconds redis-yy 30000
要down超过30s才会认为其主观下线

sentinel down-after-milliseconds redis-yy 30000
选举超时时间

3.启动容器

使用如下指令启动docker容器

docker-compose -f docker-compose.yml up -d
Running 5/5✔ Container redis-master      Started                                                                                                                1.3s✔ Container redis-slave-2     Started                                                                                                                1.5s✔ Container redis-slave-1     Started                                                                                                                1.4s✔ Container redis-sentinel-1  Started                                                                                                                1.6s✔ Container redis-sentinel-2  Started

看到上述结果就表示容器已经启动成功。
可以连接到master节点查看信息:

➜  ~ docker exec -it 6473c293bda4 bash
root@6473c293bda4:/data# redis-cli
127.0.0.1:6379> info
# Server
redis_version:6.0.20
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:7cb942700da8c107
redis_mode:standalone
os:Linux 5.15.49-linuxkit-pr x86_64
arch_bits:64
multiplexing_api:epoll
atomicvar_api:atomic-builtin
gcc_version:12.2.0
process_id:1
run_id:0f2dc11b814f4b3d39edc15554a3b8e7fe182d30
tcp_port:6379
uptime_in_seconds:6775
uptime_in_days:0
hz:10
configured_hz:10
lru_clock:2576091
executable:/data/redis-server
config_file:
io_threads_active:0# Clients
connected_clients:5
client_recent_max_input_buffer:8
client_recent_max_output_buffer:0
blocked_clients:0
tracking_clients:0
clients_in_timeout_table:0# Memory
used_memory:2041584
used_memory_human:1.95M
used_memory_rss:9568256
used_memory_rss_human:9.12M
used_memory_peak:2143616
used_memory_peak_human:2.04M
used_memory_peak_perc:95.24%
used_memory_overhead:1995264
used_memory_startup:803160
used_memory_dataset:46320
used_memory_dataset_perc:3.74%
allocator_allocated:2101520
allocator_active:2428928
allocator_resident:5189632
total_system_memory:8345645056
total_system_memory_human:7.77G
used_memory_lua:36864
used_memory_lua_human:36.00K
used_memory_scripts:0
used_memory_scripts_human:0B
number_of_cached_scripts:0
maxmemory:0
maxmemory_human:0B
maxmemory_policy:noeviction
allocator_frag_ratio:1.16
allocator_frag_bytes:327408
allocator_rss_ratio:2.14
allocator_rss_bytes:2760704
rss_overhead_ratio:1.84
rss_overhead_bytes:4378624
mem_fragmentation_ratio:4.79
mem_fragmentation_bytes:7569184
mem_not_counted_for_evict:0
mem_replication_backlog:1048576
mem_clients_slaves:41024
mem_clients_normal:102496
mem_aof_buffer:8
mem_allocator:jemalloc-5.1.0
active_defrag_running:0
lazyfree_pending_objects:0# Persistence
loading:0
rdb_changes_since_last_save:0
rdb_bgsave_in_progress:0
rdb_last_save_time:1713845348
rdb_last_bgsave_status:ok
rdb_last_bgsave_time_sec:0
rdb_current_bgsave_time_sec:-1
rdb_last_cow_size:339968
aof_enabled:1
aof_rewrite_in_progress:0
aof_rewrite_scheduled:0
aof_last_rewrite_time_sec:-1
aof_current_rewrite_time_sec:-1
aof_last_bgrewrite_status:ok
aof_last_write_status:ok
aof_last_cow_size:0
module_fork_in_progress:0
module_fork_last_cow_size:0
aof_current_size:93
aof_base_size:93
aof_pending_rewrite:0
aof_buffer_length:0
aof_rewrite_buffer_length:0
aof_pending_bio_fsync:0
aof_delayed_fsync:0# Stats
total_connections_received:13
total_commands_processed:17403
instantaneous_ops_per_sec:3
total_net_input_bytes:841071
total_net_output_bytes:4686757
instantaneous_input_kbps:0.11
instantaneous_output_kbps:0.01
rejected_connections:0
sync_full:2
sync_partial_ok:6
sync_partial_err:0
expired_keys:0
expired_stale_perc:0.00
expired_time_cap_reached_count:0
expire_cycle_cpu_milliseconds:58
evicted_keys:0
keyspace_hits:0
keyspace_misses:0
pubsub_channels:1
pubsub_patterns:0
latest_fork_usec:349
migrate_cached_sockets:0
slave_expires_tracked_keys:0
active_defrag_hits:0
active_defrag_misses:0
active_defrag_key_hits:0
active_defrag_key_misses:0
tracking_total_keys:0
tracking_total_items:0
tracking_total_prefixes:0
unexpected_error_replies:0
total_reads_processed:17089
total_writes_processed:23546
io_threaded_reads_processed:0
io_threaded_writes_processed:0# Replication
role:master
connected_slaves:2
slave0:ip=172.28.5.4,port=6379,state=online,offset=480313,lag=0
slave1:ip=172.28.5.3,port=6379,state=online,offset=480313,lag=0
master_replid:c092ef83b572c548e189eeae4c2cb6da8d64e616
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:480313
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:480313# CPU
used_cpu_sys:7.974892
used_cpu_user:3.743266
used_cpu_sys_children:0.006901
used_cpu_user_children:0.002612# Modules# Cluster
cluster_enabled:0# Keyspace
127.0.0.1:6379>

从上面的结果可以看到,当前master节点以Standalone mode在运行,其replication部分显示了只是个master节点,以及连接到master节点的两个slave节点,节点的IP、port、state和offset以及lag都可以看到。

127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=172.28.5.4,port=6379,state=online,offset=540257,lag=0
slave1:ip=172.28.5.3,port=6379,state=online,offset=540257,lag=0
master_replid:c092ef83b572c548e189eeae4c2cb6da8d64e616
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:540257
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:540257

4.模拟故障转移

如果这个时候,master节点宕机了,那么哨兵会在判断master节点客观下线后,从slave节点中选举出新的master节点。

➜  ~ docker ps
CONTAINER ID   IMAGE       COMMAND                  CREATED       STATUS       PORTS                                NAMES
0e6b49cc97f2   redis:6.0   "docker-entrypoint.s…"   2 hours ago   Up 2 hours   6379/tcp, 0.0.0.0:26380->26379/tcp   redis-sentinel-2
0c3f8f8f4dbd   redis:6.0   "docker-entrypoint.s…"   2 hours ago   Up 2 hours   6379/tcp, 0.0.0.0:26379->26379/tcp   redis-sentinel-1
951e5d81fde6   redis:6.0   "docker-entrypoint.s…"   2 hours ago   Up 2 hours   0.0.0.0:6381->6379/tcp               redis-slave-2
e3be7632c5f9   redis:6.0   "docker-entrypoint.s…"   2 hours ago   Up 2 hours   0.0.0.0:6380->6379/tcp               redis-slave-1
6473c293bda4   redis:6.0   "docker-entrypoint.s…"   2 hours ago   Up 2 hours   0.0.0.0:6379->6379/tcp               redis-master
➜  ~ docker stop 6473c293bda4
6473c293bda4
➜  ~ docker ps
CONTAINER ID   IMAGE       COMMAND                  CREATED       STATUS       PORTS                                NAMES
0e6b49cc97f2   redis:6.0   "docker-entrypoint.s…"   2 hours ago   Up 2 hours   6379/tcp, 0.0.0.0:26380->26379/tcp   redis-sentinel-2
0c3f8f8f4dbd   redis:6.0   "docker-entrypoint.s…"   2 hours ago   Up 2 hours   6379/tcp, 0.0.0.0:26379->26379/tcp   redis-sentinel-1
951e5d81fde6   redis:6.0   "docker-entrypoint.s…"   2 hours ago   Up 2 hours   0.0.0.0:6381->6379/tcp               redis-slave-2
e3be7632c5f9   redis:6.0   "docker-entrypoint.s…"   2 hours ago   Up 2 hours   0.0.0.0:6380->6379/tcp               redis-slave-1

此时已经master节点宕机,这个时候的查看sentinel的日志,可以看到:

2024-04-23 15:40:25 1:X 23 Apr 2024 07:40:25.416 # +odown master redis-yy 172.28.5.2 6379 #quorum 2/2
2024-04-23 15:40:25 1:X 23 Apr 2024 07:40:25.416 # +new-epoch 1
2024-04-23 15:40:25 1:X 23 Apr 2024 07:40:25.416 # +try-failover master redis-yy 172.28.5.2 6379
2024-04-23 15:40:25 1:X 23 Apr 2024 07:40:25.421 # Could not rename tmp config file (Device or resource busy)
2024-04-23 15:40:25 1:X 23 Apr 2024 07:40:25.421 # WARNING: Sentinel was not able to save the new configuration on disk!!!: Device or resource busy
2024-04-23 15:40:25 1:X 23 Apr 2024 07:40:25.421 # +vote-for-leader ac58eb282da2eedff5843b98043caf42576dc273 1
2024-04-23 15:40:25 1:X 23 Apr 2024 07:40:25.427 # 7e1f67f1ed0cbc2763d8fa9b66913d3972d5c7c6 voted for ac58eb282da2eedff5843b98043caf42576dc273 1
2024-04-23 15:40:25 1:X 23 Apr 2024 07:40:25.493 # +elected-leader master redis-yy 172.28.5.2 6379
2024-04-23 15:40:25 1:X 23 Apr 2024 07:40:25.493 # +failover-state-select-slave master redis-yy 172.28.5.2 6379
2024-04-23 15:40:25 1:X 23 Apr 2024 07:40:25.584 # +selected-slave slave 172.28.5.3:6379 172.28.5.3 6379 @ redis-yy 172.28.5.2 6379
2024-04-23 15:40:25 1:X 23 Apr 2024 07:40:25.584 * +failover-state-send-slaveof-noone slave 172.28.5.3:6379 172.28.5.3 6379 @ redis-yy 172.28.5.2 6379
2024-04-23 15:40:25 1:X 23 Apr 2024 07:40:25.667 * +failover-state-wait-promotion slave 172.28.5.3:6379 172.28.5.3 6379 @ redis-yy 172.28.5.2 6379
2024-04-23 15:40:25 1:X 23 Apr 2024 07:40:25.951 # Could not rename tmp config file (Device or resource busy)
2024-04-23 15:40:25 1:X 23 Apr 2024 07:40:25.951 # WARNING: Sentinel was not able to save the new configuration on disk!!!: Device or resource busy
2024-04-23 15:40:25 1:X 23 Apr 2024 07:40:25.951 # +promoted-slave slave 172.28.5.3:6379 172.28.5.3 6379 @ redis-yy 172.28.5.2 6379
2024-04-23 15:40:25 1:X 23 Apr 2024 07:40:25.951 # +failover-state-reconf-slaves master redis-yy 172.28.5.2 6379
2024-04-23 15:40:26 1:X 23 Apr 2024 07:40:26.012 * +slave-reconf-sent slave 172.28.5.4:6379 172.28.5.4 6379 @ redis-yy 172.28.5.2 6379
2024-04-23 15:40:26 1:X 23 Apr 2024 07:40:26.514 # -odown master redis-yy 172.28.5.2 6379
2024-04-23 15:40:26 1:X 23 Apr 2024 07:40:26.970 * +slave-reconf-inprog slave 172.28.5.4:6379 172.28.5.4 6379 @ redis-yy 172.28.5.2 6379
2024-04-23 15:40:26 1:X 23 Apr 2024 07:40:26.970 * +slave-reconf-done slave 172.28.5.4:6379 172.28.5.4 6379 @ redis-yy 172.28.5.2 6379
2024-04-23 15:40:27 1:X 23 Apr 2024 07:40:27.022 # +failover-end master redis-yy 172.28.5.2 6379
2024-04-23 15:40:27 1:X 23 Apr 2024 07:40:27.022 # +switch-master redis-yy 172.28.5.2 6379 172.28.5.3 6379
2024-04-23 15:40:27 1:X 23 Apr 2024 07:40:27.022 * +slave slave 172.28.5.4:6379 172.28.5.4 6379 @ redis-yy 172.28.5.3 6379
2024-04-23 15:40:27 1:X 23 Apr 2024 07:40:27.022 * +slave slave 172.28.5.2:6379 172.28.5.2 6379 @ redis-yy 172.28.5.3 6379

从日志中已经可以看到,master节点已经切换到172.28.5.3这个节点。那么登录节点确认一下:

# redis-cli
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=172.28.5.4,port=6379,state=online,offset=90354,lag=0
master_replid:3f4312a3678a4e771d6a9a2668728b7202198c05
master_replid2:459d6b396219637670fa0b38dfe7a4ecf4227f89
master_repl_offset:90354
second_repl_offset:24036
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:90354
127.0.0.1:6379> 

可以确认,目前这个节点role已经是master了。目前只有一个slave节点。
此时,再重新启动172.28.5.2这个已经宕机的节点,它会再加入集群中,成为新的slave节点:

127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=172.28.5.4,port=6379,state=online,offset=110283,lag=0
slave1:ip=172.28.5.2,port=6379,state=online,offset=110283,lag=0
master_replid:3f4312a3678a4e771d6a9a2668728b7202198c05
master_replid2:459d6b396219637670fa0b38dfe7a4ecf4227f89
master_repl_offset:110283
second_repl_offset:24036
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:110283
127.0.0.1:6379> 

这篇关于docker-compose搭建redis环境:哨兵模式(一主两重两哨兵)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/930723

相关文章

Linux系统配置NAT网络模式的详细步骤(附图文)

《Linux系统配置NAT网络模式的详细步骤(附图文)》本文详细指导如何在VMware环境下配置NAT网络模式,包括设置主机和虚拟机的IP地址、网关,以及针对Linux和Windows系统的具体步骤,... 目录一、配置NAT网络模式二、设置虚拟机交换机网关2.1 打开虚拟机2.2 管理员授权2.3 设置子

使用Python实现快速搭建本地HTTP服务器

《使用Python实现快速搭建本地HTTP服务器》:本文主要介绍如何使用Python快速搭建本地HTTP服务器,轻松实现一键HTTP文件共享,同时结合二维码技术,让访问更简单,感兴趣的小伙伴可以了... 目录1. 概述2. 快速搭建 HTTP 文件共享服务2.1 核心思路2.2 代码实现2.3 代码解读3.

MySQL双主搭建+keepalived高可用的实现

《MySQL双主搭建+keepalived高可用的实现》本文主要介绍了MySQL双主搭建+keepalived高可用的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,... 目录一、测试环境准备二、主从搭建1.创建复制用户2.创建复制关系3.开启复制,确认复制是否成功4.同

Docker镜像pull失败两种解决办法小结

《Docker镜像pull失败两种解决办法小结》有时候我们在拉取Docker镜像的过程中会遇到一些问题,:本文主要介绍Docker镜像pull失败两种解决办法的相关资料,文中通过代码介绍的非常详细... 目录docker 镜像 pull 失败解决办法1DrQwWCocker 镜像 pull 失败解决方法2总

Redis 中的热点键和数据倾斜示例详解

《Redis中的热点键和数据倾斜示例详解》热点键是指在Redis中被频繁访问的特定键,这些键由于其高访问频率,可能导致Redis服务器的性能问题,尤其是在高并发场景下,本文给大家介绍Redis中的热... 目录Redis 中的热点键和数据倾斜热点键(Hot Key)定义特点应对策略示例数据倾斜(Data S

SpringBoot如何通过Map实现策略模式

《SpringBoot如何通过Map实现策略模式》策略模式是一种行为设计模式,它允许在运行时选择算法的行为,在Spring框架中,我们可以利用@Resource注解和Map集合来优雅地实现策略模式,这... 目录前言底层机制解析Spring的集合类型自动装配@Resource注解的行为实现原理使用直接使用M

通过Docker Compose部署MySQL的详细教程

《通过DockerCompose部署MySQL的详细教程》DockerCompose作为Docker官方的容器编排工具,为MySQL数据库部署带来了显著优势,下面小编就来为大家详细介绍一... 目录一、docker Compose 部署 mysql 的优势二、环境准备与基础配置2.1 项目目录结构2.2 基

redis+lua实现分布式限流的示例

《redis+lua实现分布式限流的示例》本文主要介绍了redis+lua实现分布式限流的示例,可以实现复杂的限流逻辑,如滑动窗口限流,并且避免了多步操作导致的并发问题,具有一定的参考价值,感兴趣的可... 目录为什么使用Redis+Lua实现分布式限流使用ZSET也可以实现限流,为什么选择lua的方式实现

Redis中管道操作pipeline的实现

《Redis中管道操作pipeline的实现》RedisPipeline是一种优化客户端与服务器通信的技术,通过批量发送和接收命令减少网络往返次数,提高命令执行效率,本文就来介绍一下Redis中管道操... 目录什么是pipeline场景一:我要向Redis新增大批量的数据分批处理事务( MULTI/EXE

Redis中高并发读写性能的深度解析与优化

《Redis中高并发读写性能的深度解析与优化》Redis作为一款高性能的内存数据库,广泛应用于缓存、消息队列、实时统计等场景,本文将深入探讨Redis的读写并发能力,感兴趣的小伙伴可以了解下... 目录引言一、Redis 并发能力概述1.1 Redis 的读写性能1.2 影响 Redis 并发能力的因素二、