首页 > 数据库 >Redis 哨兵 Sentinel 配置

Redis 哨兵 Sentinel 配置

时间:2024-12-17 08:58:35浏览次数:3  
标签:16 Redis 192.168 哨兵 2024 sentinel master 16379 Sentinel

节点规划

redis-01 192.168.174.108   16379
redis-02 192.168.174.112   16379
redis-03 192.168.174.117   16379
chown -R redis:redis /usr/local/redis/etc/

redis 主从配置

在所有节点执行

sed -i -e 's@port 6379@port 16379@' -e 's@bind 127.0.0.1@bind 0.0.0.0@g' -e '/# requirepass /a requirepass 123456' -e 's@pidfile /var/run/redis_6379.pid@pidfile /data/apps/redis/redis.pid@' -e 's@dir ./@dir /data/apps/redis/data@' -e 's@logfile ""@logfile /data/apps/redis/logs/redis.log@' -e  's/^# masterauth .*/masterauth 123456/' /usr/local/redis/etc/redis.conf

在 slave 节点执行

echo "replicaof 192.168.174.108 16379" >> /usr/local/redis/etc/redis.conf

redis 服务重启

systemctl restart redis-server 

查看 redis 主从状态

连接 redis 

/usr/local/redis/bin/redis-cli -p 16379 -a 123456 
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:16379> 

查看 master 状态

127.0.0.1:16379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.174.112,port=16379,state=online,offset=56,lag=0
slave1:ip=192.168.174.117,port=16379,state=online,offset=56,lag=1
master_failover_state:no-failover
master_replid:97c2e4f7cd3b2c0809d9ee2213e348929fc33ccb
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:56
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:56

查看 slave 状态

127.0.0.1:16379> info replication
# Replication
role:slave
master_host:192.168.174.108
master_port:16379
master_link_status:up
master_last_io_seconds_ago:7
master_sync_in_progress:0
slave_read_repl_offset:154
slave_repl_offset:154
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:97c2e4f7cd3b2c0809d9ee2213e348929fc33ccb
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:154
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:57
repl_backlog_histlen:98

Sentinel 配置

sentinel.conf

port 26379 
daemonize yes
pidfile /var/run/redis-sentinel.pid
logfile ""
dir /tmp

# mymaster 是集群的名称,此行指定当前mymaster集群中master服务器的地址和端口 2为法定人数限制(quorum),即有几个sentinel认为master down了就进行故障转移,一般此值是所有sentinel节点(一般总数是>=3的 奇数,如:3,5,7等)的一半以上的整数值,比如,总数是3,即3/2=1.5,取整为2,是master的ODOWN客观下线的依据
sentinel monitor mymaster 127.0.0.1 6379 2

# master 节点的密码 
sentinel auth-pass mymaster 123456

# 判断 mymaster 所有节点主观下线时间。 单位: 毫秒
sentinel down-after-milliseconds mymaster 30000

# 发生故障转移后,可以同时向新的master节点同步数据的slave的数量,数字越小总同步时间越长,但可以减轻master节点压力
sentinel parallel-syncs mymaster 1

# 所有slave节点指向新master节点的超时时间。默认:3分钟
sentinel failover-timeout mymaster 180000

# 禁止修改脚本
sentinel deny-scripts-reconfig yes

命令行修改

sed -i -e 's@^pidfile .*@pidfile /data/apps/redis/redis-sentinel.pid@' -e 's@^logfile .*@logfile /data/apps/redis/logs/redis-sentinel.log@' -e 's@dir /tmp@dir /data/apps/redis/data@' -e 's@sentinel monitor mymaster 127.0.0.1 6379 2@sentinel monitor mymaster 192.168.174.108 16379 2@' -e '/# sentinel auth-pass mymaster/a sentinel auth-pass mymaster 123456' -e 's/sentinel down-after-milliseconds mymaster 30000/sentinel down-after-milliseconds mymaster 3000/' /usr/local/redis/etc/sentinel.conf

redis-sentinel.service

cat > /lib/systemd/system/redis-sentinel.service <<EOF
[Unit]
Description=Redis persistent key-vaue database
After=network.target

[Service]
Restart=always
ExecStart=/usr/local/redis/bin/redis-sentinel /usr/local/redis/etc/sentinel.conf --supervised systemd
ExecStop=/bin/ki11 -S QUIT 
Type=notify 
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755
LimitNOFILE=1000000  
TimeoutStartSec=30
TimeoutStopSec=30

[Install]
WantedBy=multi-user.target

EOF
systemctl enable redis-sentinel --now

验证 sentinel 服务

查看端口信息

netstat -tnlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:26379           0.0.0.0:*               LISTEN      1910/redis-sentinel 
tcp        0      0 0.0.0.0:16379           0.0.0.0:*               LISTEN      1479/redis-server 0 

查看 sentinel id 信息

master 节点

grep myid /usr/local/redis/etc/sentinel.conf
sentinel myid 1be7ad30ff9a6b761b91362502124a777ea1908c

slave-01 节点

grep myid /usr/local/redis/etc/sentinel.conf
sentinel myid 4e075ba63eb20145b9dd0e5ed19eef0c1ecd585c

slave-02 节点

grep myid /usr/local/redis/etc/sentinel.conf
sentinel myid 88fad7df3bad3f474960131587fcddcbba39b333

查看 sentinel 日志信息

master 节点日志

cat redis-sentinel.log
1910:X 16 Dec 2024 08:59:05.756 * Supervised by systemd. Please make sure you set appropriate values for TimeoutStartSec and TimeoutStopSec in your service unit.
1910:X 16 Dec 2024 08:59:05.756 * oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1910:X 16 Dec 2024 08:59:05.756 * Redis version=7.4.1, bits=64, commit=00000000, modified=0, pid=1910, just started
1910:X 16 Dec 2024 08:59:05.756 * Configuration loaded
1910:X 16 Dec 2024 08:59:05.757 * monotonic clock: POSIX clock_gettime
1910:X 16 Dec 2024 08:59:05.759 * Running mode=sentinel, port=26379.
1910:X 16 Dec 2024 08:59:05.762 * Sentinel new configuration saved on disk
1910:X 16 Dec 2024 08:59:05.763 * Sentinel ID is 1be7ad30ff9a6b761b91362502124a777ea1908c
1910:X 16 Dec 2024 08:59:05.763 # +monitor master mymaster 192.168.174.108 16379 quorum 2
1910:X 16 Dec 2024 08:59:05.765 * +slave slave 192.168.174.112:16379 192.168.174.112 16379 @ mymaster 192.168.174.108 16379
1910:X 16 Dec 2024 08:59:05.769 * Sentinel new configuration saved on disk
1910:X 16 Dec 2024 08:59:05.769 * +slave slave 192.168.174.117:16379 192.168.174.117 16379 @ mymaster 192.168.174.108 16379
1910:X 16 Dec 2024 08:59:05.771 * Sentinel new configuration saved on disk
1910:X 16 Dec 2024 08:59:07.783 * +sentinel sentinel 88fad7df3bad3f474960131587fcddcbba39b333 192.168.174.117 26379 @ mymaster 192.168.174.108 16379
1910:X 16 Dec 2024 08:59:07.784 * Sentinel new configuration saved on disk
1910:X 16 Dec 2024 08:59:07.785 * +sentinel sentinel 4e075ba63eb20145b9dd0e5ed19eef0c1ecd585c 192.168.174.112 26379 @ mymaster 192.168.174.108 16379
1910:X 16 Dec 2024 08:59:07.787 * Sentinel new configuration saved on disk

slave 节点日志

cat redis-sentinel.log
6973:X 16 Dec 2024 08:59:05.764 * Supervised by systemd. Please make sure you set appropriate values for TimeoutStartSec and TimeoutStopSec in your service unit.
6973:X 16 Dec 2024 08:59:05.764 * oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
6973:X 16 Dec 2024 08:59:05.764 * Redis version=7.4.1, bits=64, commit=00000000, modified=0, pid=6973, just started
6973:X 16 Dec 2024 08:59:05.764 * Configuration loaded
6973:X 16 Dec 2024 08:59:05.765 * monotonic clock: POSIX clock_gettime
6973:X 16 Dec 2024 08:59:05.767 * Running mode=sentinel, port=26379.
6973:X 16 Dec 2024 08:59:05.770 * Sentinel new configuration saved on disk
6973:X 16 Dec 2024 08:59:05.770 * Sentinel ID is 88fad7df3bad3f474960131587fcddcbba39b333
6973:X 16 Dec 2024 08:59:05.770 # +monitor master mymaster 192.168.174.108 16379 quorum 2
6973:X 16 Dec 2024 08:59:05.774 * +slave slave 192.168.174.112:16379 192.168.174.112 16379 @ mymaster 192.168.174.108 16379
6973:X 16 Dec 2024 08:59:05.777 * Sentinel new configuration saved on disk
6973:X 16 Dec 2024 08:59:05.777 * +slave slave 192.168.174.117:16379 192.168.174.117 16379 @ mymaster 192.168.174.108 16379
6973:X 16 Dec 2024 08:59:05.779 * Sentinel new configuration saved on disk
6973:X 16 Dec 2024 08:59:07.784 * +sentinel sentinel 4e075ba63eb20145b9dd0e5ed19eef0c1ecd585c 192.168.174.112 26379 @ mymaster 192.168.174.108 16379
6973:X 16 Dec 2024 08:59:07.786 * Sentinel new configuration saved on disk
6973:X 16 Dec 2024 08:59:07.797 * +sentinel sentinel 1be7ad30ff9a6b761b91362502124a777ea1908c 192.168.174.108 26379 @ mymaster 192.168.174.108 16379
6973:X 16 Dec 2024 08:59:07.800 * Sentinel new configuration saved on disk

查看 sentinel 状态

连接 sentinel

 /usr/local/redis/bin/redis-cli -p 26379 
127.0.0.1:26379> 

sentinel 状态

127.0.0.1:26379> info sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_tilt_since_seconds:-1
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=192.168.174.108:16379,slaves=2,sentinels=3

sentinel 故障转移

查看 redis master 节点 

127.0.0.1:26379> info sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_tilt_since_seconds:-1
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=192.168.174.108:16379,slaves=2,sentinels=3

停止 redis master 节点

systemctl stop redis-server

查看 sentinel 日志

tail -f /data/apps/redis/logs/redis-sentinel.log
6973:X 16 Dec 2024 09:16:21.032 # +sdown master mymaster 192.168.174.108 16379
6973:X 16 Dec 2024 09:16:21.095 # +odown master mymaster 192.168.174.108 16379 #quorum 2/2
6973:X 16 Dec 2024 09:16:21.096 # +new-epoch 1
6973:X 16 Dec 2024 09:16:21.096 # +try-failover master mymaster 192.168.174.108 16379
6973:X 16 Dec 2024 09:16:21.098 * Sentinel new configuration saved on disk
6973:X 16 Dec 2024 09:16:21.098 # +vote-for-leader 88fad7df3bad3f474960131587fcddcbba39b333 1
6973:X 16 Dec 2024 09:16:21.106 * 1be7ad30ff9a6b761b91362502124a777ea1908c voted for 88fad7df3bad3f474960131587fcddcbba39b333 1
6973:X 16 Dec 2024 09:16:21.107 * 4e075ba63eb20145b9dd0e5ed19eef0c1ecd585c voted for 88fad7df3bad3f474960131587fcddcbba39b333 1
6973:X 16 Dec 2024 09:16:21.162 # +elected-leader master mymaster 192.168.174.108 16379
6973:X 16 Dec 2024 09:16:21.162 # +failover-state-select-slave master mymaster 192.168.174.108 16379
6973:X 16 Dec 2024 09:16:21.253 # +selected-slave slave 192.168.174.112:16379 192.168.174.112 16379 @ mymaster 192.168.174.108 16379
6973:X 16 Dec 2024 09:16:21.253 * +failover-state-send-slaveof-noone slave 192.168.174.112:16379 192.168.174.112 16379 @ mymaster 192.168.174.108 16379
6973:X 16 Dec 2024 09:16:21.331 * +failover-state-wait-promotion slave 192.168.174.112:16379 192.168.174.112 16379 @ mymaster 192.168.174.108 16379
6973:X 16 Dec 2024 09:16:21.718 * Sentinel new configuration saved on disk
6973:X 16 Dec 2024 09:16:21.718 # +promoted-slave slave 192.168.174.112:16379 192.168.174.112 16379 @ mymaster 192.168.174.108 16379
6973:X 16 Dec 2024 09:16:21.718 # +failover-state-reconf-slaves master mymaster 192.168.174.108 16379
6973:X 16 Dec 2024 09:16:21.771 * +slave-reconf-sent slave 192.168.174.117:16379 192.168.174.117 16379 @ mymaster 192.168.174.108 16379
6973:X 16 Dec 2024 09:16:22.201 # -odown master mymaster 192.168.174.108 16379
6973:X 16 Dec 2024 09:16:22.726 * +slave-reconf-inprog slave 192.168.174.117:16379 192.168.174.117 16379 @ mymaster 192.168.174.108 16379
6973:X 16 Dec 2024 09:16:22.726 * +slave-reconf-done slave 192.168.174.117:16379 192.168.174.117 16379 @ mymaster 192.168.174.108 16379
6973:X 16 Dec 2024 09:16:22.818 # +failover-end master mymaster 192.168.174.108 16379
6973:X 16 Dec 2024 09:16:22.818 # +switch-master mymaster 192.168.174.108 16379 192.168.174.112 16379
6973:X 16 Dec 2024 09:16:22.818 * +slave slave 192.168.174.117:16379 192.168.174.117 16379 @ mymaster 192.168.174.112 16379
6973:X 16 Dec 2024 09:16:22.818 * +slave slave 192.168.174.108:16379 192.168.174.108 16379 @ mymaster 192.168.174.112 16379
6973:X 16 Dec 2024 09:16:22.820 * Sentinel new configuration saved on disk
6973:X 16 Dec 2024 09:16:25.843 # +sdown slave 192.168.174.108:16379 192.168.174.108 16379 @ mymaster 192.168.174.112 16379

验证故障转移

查看 sentinel 状态

/usr/local/redis/bin/redis-cli -p 26379 
127.0.0.1:26379> info sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_tilt_since_seconds:-1
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=192.168.174.112:16379,slaves=2,sentinels=3

查看 redis 状态

/usr/local/redis/bin/redis-cli -p 16379 -a 123456
127.0.0.1:16379> info replication
# Replication
role:slave
master_host:192.168.174.112
master_port:16379
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_read_repl_offset:332529
slave_repl_offset:332529
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:b36ee5353ca96257081fe29516dcb5948a15f153
master_replid2:97c2e4f7cd3b2c0809d9ee2213e348929fc33ccb
master_repl_offset:332529
second_repl_offset:227445
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:57
repl_backlog_histlen:332473

查看 redis 配置文件

grep ^replicaof  /usr/local/redis/etc/redis.conf
replicaof 192.168.174.112 16379

查看 sentinel 配置文件

grep "^sentinel monitor"  /usr/local/redis/etc/sentinel.conf
sentinel monitor mymaster 192.168.174.112 16379 2

故障节点从新上线

重启 redis 服务

systemctl restart redis-server

在故障节点查看 redis 信息

127.0.0.1:16379> info replication
# Replication
role:slave
master_host:192.168.174.112
master_port:16379
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_read_repl_offset:392060
slave_repl_offset:392060
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:b36ee5353ca96257081fe29516dcb5948a15f153
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:392060
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:361182
repl_backlog_histlen:30879

在新 master 节点查看 redis 信息

127.0.0.1:16379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.174.117,port=16379,state=online,offset=408072,lag=0
slave1:ip=192.168.174.108,port=16379,state=online,offset=408072,lag=0
master_failover_state:no-failover
master_replid:b36ee5353ca96257081fe29516dcb5948a15f153
master_replid2:97c2e4f7cd3b2c0809d9ee2213e348929fc33ccb
master_repl_offset:408072
second_repl_offset:227445
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:15
repl_backlog_histlen:408058

查看 sentinel 日志

6973:X 16 Dec 2024 09:26:24.579 # -sdown slave 192.168.174.108:16379 192.168.174.108 16379 @ mymaster 192.168.174.112 16379

参考文档

https://redis.io/docs/latest/operate/oss_and_stack/management/sentinel/#obtaining-sentinel

标签:16,Redis,192.168,哨兵,2024,sentinel,master,16379,Sentinel
From: https://www.cnblogs.com/wangguishe/p/18610407

相关文章

  • Redis 哨兵与集群基础:数据冗余与备份的主从舞蹈
    1、redis主从配置1.1、简介像MySQL一样,redis是支持主从同步的,而且也支持一主多从以及多级从结构。 主从结构,一是为了纯粹的冗余备份,二是为了提升读性能,比如很消耗性能的SORT就可以由从服务器来承担。 redis的主从同步是异步进行的,这意味着主从同步不会影响主逻辑,也不会降低......
  • Redis应用—4.在库存里的应用
    大纲1.库存模块设计2.库存缓存分片和渐进式同步方案3.基于缓存分片的下单库存扣减方案4.商品库存设置流程与异步落库的实现6.库存入库时"缓存分片写入+渐进式写入+写入失败进行MQ补偿"的实现7.库存扣减时"基于库存分片依次扣减+合并扣减+扣不了返还+异步落库"的实......
  • Docker安装Redis和Elasticsearch
    本章将和大家分享在Docker中如何安装Redis和Elasticsearch。废话不多说,下面我们直接进入主题。一、Docker安装Redis1、拉取最新版的redis镜像dockerpullredis2、查看本地镜像dockerimages3、从官网获取redis.conf配置文件创建并进入指定目录(可自定义)://进入......
  • Redis:从安装到“数据不见了,快来救我!”史上最全讲解之安装配置启动
    本着上班找工作为目的,从0开始学redis,从安装部署开始。centos9作为实验机目录1、redis下载安装2、redis配置并启动3、Redis命令3.1、设置值(key)3.2、获取值3.3、EX选项3.4、PX选项3.5、TTL获取剩余生存时间(秒)3.6、NX选项3.7、XX选项3.8、del删除键4、redis工具......
  • Redis与关系型数据库:在不同场景下的数据库选择与优化策略
    在现代应用开发中,数据库作为核心组件之一,承载着数据存储、检索和管理的重要任务。根据不同的应用需求,开发人员通常需要在关系型数据库(如MySQL)和NoSQL数据库(如Redis)之间做出选择。每种数据库都有其独特的优势和适用场景,合理的数据库选择和优化策略能够显著提高系统的性能和可扩......
  • Redis 哨兵 Sentinel 介绍
    RedisSentinel说明RedisSentinel为非集群Redis提供高可用解决方案。它能够在主节点发生故障时,自动切换主从角色,实现系统的高可用性。RedisSentinel还提供其他附带任务,例如监控、通知,并充当客户端的配置提供程序。RedisSentinel功能1.监控(Monitoring)分布式......
  • 学习下Redis内存模型
    作者:京东零售吴佳前言redis,对于一个java开发工程师来讲,其实算不得什么复杂新奇的技术,但可能也很少人去深入了解学习它的底层的一些东西。下面将通过对内存统计、内存划分、存储细节、对象类型&内部编码这四个模块来学习学习redis的内存模型,手字笔录,潜心修行。一、redis的内存......
  • Redis应用—3.在购物车里的应用
    大纲1.社区电商购物车的读多写多场景分析2.购物车的复杂缓存与异步落库(SortedSet+Hash->hPut+zadd)3.购物车异步落库与完整加入流程(缓存雪崩+MQ异步出现问题)4.购物车的阈值检查与重复加入逻辑(hGet+hLen+hFieldExists)5.购物车加入商品多线程并发问题解决(分......
  • redis 数据查看
    1.打开命令行终端。2.输入以下命令连接到Redis服务器:redis-cli-hhost-pport其中host是Redis服务器的IP地址,port是Redis服务器的监听端口号。4.当服务器要求输入密码时,在命令行中输入以下命令进行身份验证:AUTHpassword其中password是你设置的密码。5.......
  • 2025全网最全Redis面试题合集
    1.使用Redis有哪些好处?(1)速度快,因为数据存在内存中,类似于HashMap,HashMap的优势就是查找和操作的时间复杂度都是O(1)(2)支持丰富数据类型,支持string,list,set,sortedset,hash(3)支持事务,操作都是原子性,所谓的原子性就是对数据的更改要么全部执行,要么全部不执行(4)丰富的......