Redis哨兵

2023年 10月 5日 73.7k 0

准备

服务器准备

  • 3个哨兵:自动监控和维护集群,不存放数据
  • 1主2从:用于数据读取和存放

哨兵集群配置

# 创建一个哨兵集群的配置文件夹
[root@node1 ~]# mkdir /usr/local/redis/sentinel

创建哨兵集群所使用的配置文件

[root@node1 ~]# vim /usr/local/redis/sentinel/sential26379.conf

配置文件内容如下
# sentinel节点1配置
[root@node1 ~]# cat /usr/local/redis/sentinel/sential26379.conf
bind 0.0.0.0
daemonize yes
protected-mode no
port 26379
logfile "/usr/local/redis/log/sentinel26379.log"
pidfile /var/run/redis-sentinel26379.pid
dir /usr/local/redis/data
sentinel monitor mymaster 192.168.202.129 6379 2
sentinel auth-pass mymaster 123456

# sentinel节点2配置
[root@node1 ~]# cat /usr/local/redis/sentinel/sential26380.conf
bind 0.0.0.0
daemonize yes
protected-mode no
port 26380
logfile "/usr/local/redis/log/sentinel26380.log"
pidfile /var/run/redis-sentinel26380.pid
dir /usr/local/redis/data
sentinel monitor mymaster 192.168.202.129 6379 2
sentinel auth-pass mymaster 123456

# sentinel节点3配置
[root@node1 ~]# cat /usr/local/redis/sentinel/sential26381.conf
bind 0.0.0.0
daemonize yes
protected-mode no
port 26381
logfile "/usr/local/redis/log/sentinel26381.log"
pidfile /var/run/redis-sentinel26381.pid
dir /usr/local/redis/data
sentinel monitor mymaster 192.168.202.129 6379 2
sentinel auth-pass mymaster 123456

启动一个一主两从的redis集群

参考链接

注意,主从密码配置成一样的,要不然从机或者主机身份变化时候需要拷贝数据的时候会出现验证不通过的情况。

启动sentinel集群

本示例使用了一台机器启动了三个sentinel实例

[root@node1 ~]# redis-sentinel /usr/local/redis/sentinel/sential26379.conf --sentinel
[root@node1 ~]# redis-sentinel /usr/local/redis/sentinel/sential26380.conf --sentinel
[root@node1 ~]# redis-sentinel /usr/local/redis/sentinel/sential26381.conf --sentinel

可以看到redis配置文件发生了变化

# Generated by CONFIG REWRITE
save 3600 1
save 300 100
save 60 10000
latency-tracking-info-percentiles 50 99 99.9
user default on sanitize-payload #8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92 ~* &* +@all

模拟master挂了

将现在的master关闭

[root@node1 ~]# redis-cli -a 123456
Warning: Using a password with '-a' or '-u' 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=192.168.202.130,port=6379,state=online,offset=641582,lag=0
slave1:ip=192.168.202.131,port=6379,state=online,offset=641727,lag=0
master_failover_state:no-failover
master_replid:07c3db3aea823b87fb9e477208f8afa1377af2ec
master_replid2:ed614ba98a153f419741e75e97233f088ee7ee0b
master_repl_offset:641727
second_repl_offset:11999
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:11999
repl_backlog_histlen:629729
127.0.0.1:6379> shutdown
not connected> 

可以看到原来的slave节点变为了master节点

127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.202.130,port=6379,state=online,offset=658546,lag=1
master_failover_state:no-failover
master_replid:5fc98adc5e69818e9e9374d79cef9e804d24088f
master_replid2:07c3db3aea823b87fb9e477208f8afa1377af2ec
master_repl_offset:658546
second_repl_offset:643664
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:11747
repl_backlog_histlen:646800

相关文章

JavaScript2024新功能:Object.groupBy、正则表达式v标志
PHP trim 函数对多字节字符的使用和限制
新函数 json_validate() 、randomizer 类扩展…20 个PHP 8.3 新特性全面解析
使用HTMX为WordPress增效:如何在不使用复杂框架的情况下增强平台功能
为React 19做准备:WordPress 6.6用户指南
如何删除WordPress中的所有评论

发布评论