Redis哨兵模式(Sentinel)的搭建与配置

数据库 Redis
创建三个Redis实例所需的目录,生产环境需独立部署在不同主机上,提高稳定性。

Redis 哨兵模式(Sentinel)是一个自动监控处理 redis 间故障节点转移工作的一个redis服务端实例,它不提供数据存储服务,只进行普通 redis 节点监控管理,使用redis哨兵模式可以实现redis服务端故障的自动化转移。

一、搭建redis主从集群

1、创建3个redis实例

关于redis的搭建,可以参考历史文章。

​https://mp.weixin.qq.com/s/RaWy0sqRxcAti1qbv-GbZQ​

如果有编译好的二进制文件,则直接部署redis实例即可。

创建三个redis实例所需的目录,生产环境需独立部署在不同主机上,提高稳定性。

mkdir   -p /data/redis
cd /data/redis/
mkdir redis6379 redis6380 redis6381
cd redis6379
vim redis.conf
# 添加如下配置
bind 0.0.0.0
protected-mode no
port 6379
tcp-backlog 511
timeout 30
tcp-keepalive 300
daemonize yes
supervised no
pidfile /data/redis/redis6379/redis_6379.pid
loglevel notice
logfile "/data/redis/redis6379/redis6379.log"
databases 16
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /data/redis/redis6379
masterauth 123456
slave-serve-stale-data yes
slave-read-only yes
# 将配置文件拷贝到其他2个实例的目录下
cp redis.conf ../redis6380.conf
cp redis.conf ../redis6381.conf
sed -i "s#6379#6380#g" ../redis6380/redis.conf
sed -i "s#6379#6381#g" ../redis6381/redis.conf
# redis实例不建议使用root账号启动,单独创建一个redis用户,并修改redis相关目录的权限
useradd redis
chown -R redis:redis /data/redis
su - redis
# 启动三个redis实例
redis-server /data/redis/redis6379/redis.conf
redis-server /data/redis/redis6380/redis.conf
redis-server /data/redis/redis6381/redis.conf

2、配置主从同步

进入2个实例,配置同步,配置完成后去主节点检查一下是否正常。

[redis@test redis6379]$ redis-cli -p 6380 -a "123456"
Warning: Using a password with '-a' option on the command line interface may not be safe.
127.0.0.1:6380> slaveof 127.0.0.1 6379
OK
127.0.0.1:6380> exit
[redis@test redis6379]$ redis-cli -p 6381 -a "123456"
Warning: Using a password with '-a' option on the command line interface may not be safe.
127.0.0.1:6381> slaveof 127.0.0.1 6379
OK
127.0.0.1:6381> exit
[redis@test redis6379]$ redis-cli -p 6379 -a "123456"
Warning: Using a password with '-a' option on the command line interface may not be safe.
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=127.0.0.1,port=6380,state=online,offset=42,lag=0
slave1:ip=127.0.0.1,port=6381,state=online,offset=42,lag=1
master_replid:b8a19f5afae13d3da38b359244dc0f560df03176
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:42
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:42
127.0.0.1:6379>

可见 当前主从同步已建立。

二、哨兵模式搭建

1、创建3个哨兵实例

mkdir -p   /data/redis/redis_sentinel/
cd /data/redis/redis_sentinel/
mkdir sentinel26379 sentinel26380 sentinel26381
cd sentinel26379
# 初始化配置文件如下
vim redis_sentinel_26379.conf
bind 0.0.0.0
port 26379
daemonize yes
dir "/data/redis/redis_sentinel/sentinel26379"
pidfile "/data/redis/redis_sentinel/sentinel26379/redis_sentinel26379.pid"
logfile "/data/redis/redis_sentinel/sentinel26379/redis_sentinel26379.log"
# Generated by CONFIG REWRITE
sentinel deny-scripts-reconfig yes
sentinel monitor testdb 127.0.0.1 6379 2
sentinel down-after-milliseconds testdb 5000
sentinel auth-pass testdb 123456
# 配置文件拷贝至另2个实例
cp redis_sentinel_26379.conf ../sentinel26380/redis_sentinel_26380.conf
sed -i "s#26379#26380#g" ../sentinel26380/redis_sentinel_26380.conf
cp redis_sentinel_26379.conf ../sentinel26381/redis_sentinel_26381.conf
sed -i "s#26379#26381#g" ../sentinel26381/redis_sentinel_26381.conf

配置文件主要参数说明:

参数名

说明

bind

绑定的可以访问的主机IP,0.0.0.0 代表不限制

port

哨兵实例的端口

sentinel monitor testdb 127.0.0.1 6379 1

testdb任意定义,哨兵集群名称,127.0.0.1 6379 redis实例主节点 ;1 代表当1个哨兵实例判断主库不可用则进行转移,生产环境节点数要配置多一点

sentinel down-after-milliseconds testdb 5000

testdb同上,down-after-milliseconds代表 master 最长响应时间,超过这个时间就主观判断它下线,5000 代表5000ms,即5s

sentinel auth-pass testdb 123456

123456 是redis实例的登录密码

2、启动哨兵实例

redis-sentinel  /data/redis/redis_sentinel/sentinel26379/redis_sentinel_26379.conf 
redis-sentinel /data/redis/redis_sentinel/sentinel26380/redis_sentinel_26380.conf
redis-sentinel /data/redis/redis_sentinel/sentinel26381/redis_sentinel_26381.conf

启动后配置文件会自动新增如下红框中的内容。

登录哨兵实例查看。

redis-cli -p  26379

3、测试

测试将主节点down机。

redis-cli -p 6379 -a 123456  shutdown

再查看哨兵找那个的master结果,如下:

日志信息如下:

1895:X 29 Apr 23:57:31.778 # +sdown master testdb 127.0.0.1 6379
1895:X 29 Apr 23:57:31.779 # +odown master testdb 127.0.0.1 6379 #quorum 1/1
1895:X 29 Apr 23:57:31.779 # +new-epoch 1
1895:X 29 Apr 23:57:31.779 # +try-failover master testdb 127.0.0.1 6379
1895:X 29 Apr 23:57:31.795 # +vote-for-leader 4928b4d4dfd762cd50fa540b7a0903d2be3b0f95 1
1895:X 29 Apr 23:57:31.796 # +elected-leader master testdb 127.0.0.1 6379
1895:X 29 Apr 23:57:31.796 # +failover-state-select-slave master testdb 127.0.0.1 6379
1895:X 29 Apr 23:57:31.862 # +selected-slave slave 127.0.0.1:6380 127.0.0.1 6380 @ testdb 127.0.0.1 6379
1895:X 29 Apr 23:57:31.862 * +failover-state-send-slaveof-noone slave 127.0.0.1:6380 127.0.0.1 6380 @ testdb 127.0.0.1 6379
1895:X 29 Apr 23:57:31.991 * +failover-state-wait-promotion slave 127.0.0.1:6380 127.0.0.1 6380 @ testdb 127.0.0.1 6379
1895:X 29 Apr 23:57:32.223 # +promoted-slave slave 127.0.0.1:6380 127.0.0.1 6380 @ testdb 127.0.0.1 6379
1895:X 29 Apr 23:57:32.223 # +failover-state-reconf-slaves master testdb 127.0.0.1 6379
1895:X 29 Apr 23:57:32.273 * +slave-reconf-sent slave 127.0.0.1:6381 127.0.0.1 6381 @ testdb 127.0.0.1 6379
1895:X 29 Apr 23:57:33.231 * +slave-reconf-inprog slave 127.0.0.1:6381 127.0.0.1 6381 @ testdb 127.0.0.1 6379
1895:X 29 Apr 23:57:33.231 * +slave-reconf-done slave 127.0.0.1:6381 127.0.0.1 6381 @ testdb 127.0.0.1 6379
1895:X 29 Apr 23:57:33.296 # +failover-end master testdb 127.0.0.1 6379
1895:X 29 Apr 23:57:33.296 # +switch-master testdb 127.0.0.1 6379 127.0.0.1 6380
1895:X 29 Apr 23:57:33.297 * +slave slave 127.0.0.1:6381 127.0.0.1 6381 @ testdb 127.0.0.1 6380
1895:X 29 Apr 23:57:33.297 * +slave slave 127.0.0.1:6379 127.0.0.1 6379 @ testdb 127.0.0.1 6380
1895:X 29 Apr 23:57:38.356 # +sdown slave 127.0.0.1:6379 127.0.0.1 6379 @ testdb 127.0.0.1 6380

再把6379端口启动,可以看到节点自动加入集群,且作为从节点。

责任编辑:姜华 来源: 今日头条
相关推荐

2021-03-31 05:57:40

集群搭建哨兵集群Redis

2020-02-07 09:44:30

Redis哨兵数据库

2022-06-28 07:31:11

哨兵模式redis

2019-09-16 16:05:13

Redis集群模式

2021-04-01 08:50:54

SentinelRedis 集群原理

2022-11-06 21:31:11

云原生Sentinel集群模式

2020-04-14 21:12:42

Redis集群Linux

2022-02-11 08:41:19

WindowsRedis集群

2023-03-15 08:30:37

2022-12-05 08:41:39

Redis调试环境源码

2023-07-31 21:56:54

哨兵系统redis

2023-09-27 06:26:07

2022-05-16 13:46:38

Redis高可用Sentinel

2023-10-26 07:47:53

Redis哨兵集群

2022-05-17 22:20:41

哨兵Redis机制

2020-09-04 06:35:28

Redis复制哨兵

2020-09-02 17:28:26

Spring Boot Redis集成

2019-07-22 09:35:23

RedisSentinel

2022-12-21 09:50:41

2023-11-21 17:36:04

OpenFeignSentinel
点赞
收藏

51CTO技术栈公众号