Redis Sentinel

sentinel是个分布式的结构,感觉跟zk有些像
sentinel能做下面事情:
1监控
2通知
3自动故障转移
4客户端连接sentinel,sentinel能自动提供正确的服务地址
建议生产环境上至少部署3个sentinel,分别放在不同机器上
运行sentinel
redis-sentinel /path/to/sentinel.conf
redis-server /path/to/sentinel.conf –sentinel sentinel模式下运行
该工具是运行在26379端口下
需要3个实例部署,配置
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel down-after-milliseconds mymaster 60000
sentinel failover-timeout mymaster 180000
sentinel parallel-syncs mymaster 1

sentinel monitor resque 192.168.1.3 6380 4
sentinel down-after-milliseconds resque 10000
sentinel failover-timeout resque 180000
sentinel parallel-syncs resque 5

只需要配置master的信息,slave的信息是自动发现的,上面的配置监控了2组redis实例,quorum是sentinel的数量来确保master不可达,quorum只是用来检测失败,为了进行故障转移,一个sentinel需要被选举出来进行故障转移,如果有5个sentinel进程,quorum被设置成2,如果有2个sentinel同时发现master不可达,那么2个中的1个就会进程故障转移。
如果有3个sentinel可达,故障转移就开始。
下面是用了3个sentinel实例,一个redis master 一个redis slave
port 5000
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel down-after-milliseconds mymaster 5000 –5smasters will be detected as failing as soon as we don’t receive any reply from our pings within this amount of time.

sentinel failover-timeout mymaster 60000
sentinel parallel-syncs mymaster 1
配置如上,另外2个配置就是把5000 改成5001,50002
通过sentinel了解master的状态
redis-cli -p 5000
127.0.0.1:5000> sentinel master mymaster
查看更多的消息
SENTINEL slaves mymaster
SENTINEL sentinels mymaster
获取当前master的地址
127.0.0.1:5000> SENTINEL get-master-addr-by-name mymaster
测试故障转移
redis-cli -p 6379 DEBUG sleep 30 这个命令让master不可达,sleep30s
查看日志,会看到下面的信息
1master的状态是sdown
2稍后变成odown
3投票选出一个sentinel开始故障转移
sentinel的api
PING This command simply returns PONG.
SENTINEL masters Show a list of monitored masters and their state. 显示master的状态
SENTINEL master Show the state and info of the specified master. 显示指定master的状态
SENTINEL slaves Show a list of slaves for this master, and their state. 显示指定master的slave的状态
SENTINEL sentinels Show a list of sentinel instances for this master, and their state. 显示master的sentinel的状态
SENTINEL get-master-addr-by-name Return the ip and port number of the master with that name. If a failover is in progress or terminated successfully for this master it returns the address and port of the promoted slave.
显示master的地址
SENTINEL reset This command will reset all the masters with matching name. The pattern argument is a glob-style pattern. The reset process clears any previous state in a master (including a failover in progress), and removes every slave and sentinel already discovered and associated with the master.
重置匹配的master,清理之前的状态,包含故障转移的过程,移除已经发现的slave信息。
SENTINEL failover Force a failover as if the master was not reachable, and without asking for agreement to other Sentinels (however a new version of the configuration will be published so that the other Sentinels will update their configurations).
强制一次故障转移
SENTINEL ckquorum Check if the current Sentinel configuration is able to reach the quorum needed to failover a master, and the majority needed to authorize the failover. This command should be used in monitoring systems to check if a Sentinel deployment is ok.
来检验配置部署是否正确
SENTINEL flushconfig Force Sentinel to rewrite its configuration on disk, including the current Sentinel state. Normally Sentinel rewrites the configuration every time something changes in its state (in the context of the subset of the state which is persisted on disk across restart). However sometimes it is possible that the configuration file is lost because of operation errors, disk failures, package upgrade scripts or configuration managers. In those cases a way to to force Sentinel to rewrite the configuration file is handy. This command works even if the previous configuration file is completely missing.
强制一个刷新配置文件信息
运行时重新配置sentinel
SENTINEL SET objects-cache-master down-after-milliseconds 1000

在角色转换的过程中可能会出现写入丢失的情况,官方文档中的第二个例子有描述,可以配置下面的2个参数
min-slaves-to-write 1
min-slaves-max-lag 10对应的说明如下:
when acting as a master, will stop accepting writes if it can’t write to at least 1 slave.
但是从库都宕掉了,主库旧没法写了
sentinel之间的通信是怎么样的来?之前好像看到过现在找不到了,唉。。。。。。。

一个部署的连接:
http://blog.csdn.net/donggang1992/article/details/50981341
部署完成后,可以连接到sentinel上,
1查看master状态
127.0.0.1:7505> sentinel master master1
2获取master地址:
127.0.0.1:7505> SENTINEL get-master-addr-by-name master1
1) “127.0.0.1”
2) “7501”
3测试failover,kill掉master后,在连接到sentinel中查看master地址发生了改变
应用连接的一个例子from http://blog.sina.com.cn/s/blog_48c95a190102v6bg.html
package redis.clients.mytest;
import java.util.HashSet;
import java.util.Set;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisSentinelPool;
public class MyJedisSentinelTest {
public static void main(String[] args) {
Set sentinels = new HashSet();
sentinels.add(new HostAndPort(“localhost”, 26379).toString());
sentinels.add(new HostAndPort(“localhost”, 26380).toString());
sentinels.add(new HostAndPort(“localhost”, 26381).toString());
JedisSentinelPool sentinelPool = new JedisSentinelPool(“mymaster”, sentinels);
System.out.println(“Current master: ” + sentinelPool.getCurrentHostMaster().toString());
Jedis master = sentinelPool.getResource();
master.set(“username”,”liangzhichao”);
sentinelPool.returnResource(master);
Jedis master2 = sentinelPool.getResource();
String value = master2.get(“username”);
System.out.println(“username: ” + value);
master2.close();
sentinelPool.destroy();
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值