redis哨兵模式

哨兵简介

哨兵(sentinel) 是一个分布式系统,用于对主从结构中的每台服务器进行监控,当出现故障时通过投票机制选择新的 master并将所有slave连接到新的master。

哨兵的作用

监控

  不断的检查master和slave是否正常运行。

  master存活检测、master与slave运行情况检测

通知(提醒)

  当被监控的服务器出现问题时,向其他(哨兵间,客户端)发送通知。

自动故障转移

  断开master与slave连接,选取一个slave作为master,将其他slave连接到新的master,并告知客户端新的服 务器地址

注意: 哨兵也是一台redis服务器,只是不提供数据服务 通常哨兵配置数量为单数

启用哨兵模式

配置一拖二的主从结构

master配置(redis-6379.conf)

port 6379
daemonize no
#logfile "log_6379.log"
dir ./data
save 60 2
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename "dump-6379.rdb"

appendonly yes
appendfsync always
appendfilename "appendonly-6379.aof"
bind 127.0.0.1
View Code

slave配置(redis-6380.conf、redis-6381.conf)

port 6380
daemonize no
#logfile "log_6380.log"
dir ./data
slaveof 127.0.0.1 6379 
View Code

通过以下命令快速将redis-6380.conf内容复制到redis-6381.conf中,并将内容中包含的6380改为6381

sed 's/6380/6381/g' redis-6380.conf > redis-6381.conf

启动服务

redis-server redis-config/redis-6379.conf

redis-server redis-config/redis-6380.conf

redis-server redis-config/redis-6381.conf

配置三个哨兵(配置相同,端口不同)

将redis解压目录下sentinel.conf内容过滤并复制到指定目录下

cat sentinel.conf |grep -v "#" | grep -v "^$" > /usr/local/bin/redis-config/sentinel-26379.conf

查看

[root@localhost redis-config]# cat sentinel-26379.conf 
port 26379
daemonize no
#pidfile /var/run/redis-sentinel-26379.pid #以守护进程方式运行时,系统默认会把pid写入/var/run/redis-sentinel-26379.pid
#logfile "" #以守护进程方式运行时,log文件的路径
dir /tmp
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel down-after-milliseconds mymaster 30000
acllog-max-len 128
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes
SENTINEL resolve-hostnames no
SENTINEL announce-hostnames no

通过以下命令快速创建文件,将源文件的内容替换为指定内容并复制创建的文件中

[root@localhost redis-config]# sed 's/26379/26380/g' sentinel-26379.conf > sentinel-26380.conf
[root@localhost redis-config]# sed 's/26380/26381/g' sentinel-26380.conf > sentinel-26381.conf

启动哨兵

启动哨兵前,先将主从服务启动

定位到sentinel-26379.conf所在目录下

redis-sentinel sentinel-26379.conf

redis-sentinel sentinel-26380.conf

redis-sentinel sentinel-26381.conf

测试

master服务停止(Ctrl + C)

 

posted @ 2021-09-24 10:56  一杯水M  阅读(227)  评论(0编辑  收藏  举报