首页 > 其他 > 详细

Redis 之 哨兵模式

时间:2019-07-22 22:01:54      阅读:119      评论:0      收藏:0      [点我收藏+]
一、哨兵模式的概念

哨兵模式是一种特殊的模式,首先Redis提供了哨兵的命令,哨兵是一个独立的进程,作为进程,它会独立运行。其原理是哨兵通过发送命令,等待Redis服务器响应,从而监控运行的多个Redis实例。
技术分享图片

二、实验环境

技术分享图片

三、安装redis服务

1、指定外部安装源
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
2、安装redis
yum -y install redis

四、修改master主库的配置文件

1、vi /etc/redis.conf

#修改绑定IP地址为本机IP
bind 10.3.152.78   

#以守护进程在后台运行
daemonize yes

五、修改两台从库的redis.conf配置文件

1、vi /etc/redis.conf

#分别修改绑定的对应主机IP地址
bind 10.3.151.34

#以守护进程在后台运行
daemonize yes

#指定主库IP地址与端口
slaveof 10.3.152.78 6379

六、启动三台服务器的redis服务

1、service redis start
2、在主库上检查redis同步状态

[root@ops-site redis]# redis-cli -h 10.3.152.78
10.3.152.78:6379> info replication
# Replication
role:master       #  当前主机为master角色
connected_slaves:2        #有两个从库连接过来
slave0:ip=10.3.151.86,port=6379,state=online,offset=228889,lag=1       #slave0信息
slave1:ip=10.3.151.34,port=6379,state=online,offset=228889,lag=1       #slave1信息
master_repl_offset:229163
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:229162

七、在主库编辑redis-sentinel.conf 文件

bind 0.0.0.0
port 26379
dir /tmp
#重点 10.3.152.78 为主库,6379为主库端口,2为当两个哨兵同时成立
sentinel monitor mymaster 10.3.152.78 6379 2
sentinel down-after-milliseconds mymaster 5000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 5000
logfile /var/log/redis/sentinel.log

八、把当前主库的redis-sentinel.conf 配置文件复制到从库中

九、启动三台服务器的redis-sentinel服务

service redis-sentinel start

十、查看当前sentinel状态(可在任何一台服务器上运行)

[root@ops-site ~]# redis-cli -h 10.3.152.78 -p 26379
10.3.152.78:26379> info sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=10.3.152.78:6379,slaves=2,sentinels=3

十一、验证检查哨兵是否成功,主从是否自动切换

1、分别在主库上停止redis服务,看是否会切换到别的服务器作为master
2、启动主库的redis,看是否会自动从当前的主库中同步数据。

Redis 之 哨兵模式

原文:https://blog.51cto.com/12965094/2422588

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!