概述

在部署redis 的时候,如果redis宕机,缓存将不可用,redis提供了哨兵模式保证redis实现高可用。

即一台主机两台从机,三台哨兵主机,如果主实例宕机,哨兵将将一台从机升级为主机。实现高可用。

配置方法

1.IP地址配置如下

主 127.0.0.1 6001

从 127.0.0.1 6002

从 127.0.0.1 6003

哨兵

127.0.0.1 16001

127.0.0.1 16002

127.0.0.1 16002

2.修改配置

将 redis.con 拷贝两份 redis1.conf redis2.conf

修改配置如下:

编辑 redis.conf

bind 192.168.1.88 127.0.0.1

protected-mode no

daemonize yes

port 6001

pidfile "/var/run/redis_6001.pid"

编辑 redis1.conf

bind 192.168.1.88 127.0.0.1

protected-mode no

daemonize yes

port 6002

pidfile "/var/run/redis_6002.pid"

slaveof 127.0.0.1 6001

编辑 redis2.conf 

bind 192.168.1.88 127.0.0.1

protected-mode no

port 6003

daemonize yes

pidfile "/var/run/redis_6003.pid"

slaveof 127.0.0.1 6001

 

编辑 哨兵文件

将哨兵文件拷贝两份

sentinel.conf sentinel1.conf sentinel2.conf

编辑 sentinel.conf

port 16001

daemonize yes

sentinel monitor mymaster 127.0.0.1 6001 2

编辑 sentinel1.conf

port 16002

daemonize yes

sentinel monitor mymaster 127.0.0.1 6001 2

编辑 sentinel2.conf

port 16003

daemonize yes

sentinel monitor mymaster 127.0.0.1 6001 2

配置完成

 

注意

这里如果需要使用哨兵模式连接的话,注意不能使用 127.0.0.1 需要使用对外的IP地址。

 

3.启动 redis

./bin/redis-server etc/redis.conf 

./bin/redis-server etc/redis1.conf

./bin/redis-server etc/redis2.conf  

启动哨兵

./bin/redis-sentinel ./etc/sentinel.conf 

./bin/redis-sentinel ./etc/sentinel1.conf 

./bin/redis-sentinel ./etc/sentinel2.conf 

 

 

验证

新开一个命令行窗口进入redis的src目录,用redis-cli工具登录其中一个哨兵

./bin/redis-cli -p 16001

连接成功后运行如下命令

sentinel master mymaster

我们可以看到主机端口为 6001

我们手工关闭 6001的实例。

可以看到 6001 断开了。

可以看到6002 变为主机了。可以看到 6002 的配置文件 slaveof 127.0.0.1 6001 没有了。

测试设置数据:

连接到 6002

./bin/redis-cli -p 6002

set name redis

连接到6001

get name 

可以获取到数据 redis

我们连接到6003 ,设置数据,我们可以从机是不能设置数据的。

sentinel 作用

A、Master 状态监测

B、如果Master 异常,则会进行Master-slave 转换,将其中一个Slave作为Master,将之前的Master作为Slave 

C、Master-Slave切换后,redis.conf、redis1.conf和redis2.conf,sentinel.conf 的内容都会发生改变,sentinel.conf的监控目标会随之调换 

sentinel monitor mymaster 127.0.0.1 6002 2

 

posted on 2018-12-30 12:02  自由港  阅读(9904)  评论(0编辑  收藏  举报