Martin.xu

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

简单介绍下Redis-sentinel:

Redis-sentinel是Redis实例的监控管理、通知和实例失效备援服务,是Redis集群的管理工具。在一般的分布式中心节点数据库中,Redis-sentinel的作用是中心节点的工作,监控各个其他节点的工作情况并且进行故障恢复,来提高集群的高可用性。

 

集群架构:

三次服务器:L1:192.168.100.30,L2:192.168.100.31,L3:192.168.100.32

每台服务器上分别放3个Redis服务,生成三组集群

如:L1 7000,L2 7000,L3 7000 属于一组集群

 

1、安装Redis:

打开redis官方下载页面:http://redis.io/download
cd /usr/local/src
wget http://download.redis.io/releases/redis-3.0.6.tar.gz
tar xzf redis-3.0.6.tar.gz
ln -s redis-3.0.6 redis
cd redis
make prefix=/usr/local install

2、创建集群目录:

mkdir -p /data/redis/7000
mkdir -p /data/redis/7001
mkdir -p /data/redis/7002

3、拷贝redis.conf 和 sentinel.conf 到集群目录

cd /usr/local/src/redis
cp redis.conf /data/redis/7000/redis.conf
cp redis.conf /data/redis/7000/sentinel.conf
cp redis.conf /data/redis/7001/redis.conf
cp redis.conf /data/redis/7001/sentinel.conf
cp redis.conf /data/redis/7002/redis.conf
cp redis.conf /data/redis/7002/sentinel.conf

4、设置redis配置文件,并设置访问密码

假设主机的ip是192.168.100.30,注意后文从机也会设置主机一样的密码,此处主机的配置中也要设置masterauth参数,否则主机宕掉重启后不能正常连接从机

#cd /data/redis/7000
#vim redis.conf
daemonize yes
pidfile /var/run/redis_7000.pid
port 7000
bind 192.168.100.30
requirepass pass***
masterauth pass***
dir "/data/redis/7000"
appendonly yes


#cd /data/redis/7001
#vim redis.conf
daemonize yes
pidfile /var/run/redis_7001.pid
port 7001
bind 192.168.100.30
requirepass pass***
masterauth pass***
dir "/data/redis/7001"
appendonly yes

#cd /data/redis/7002
#vim redis.conf
daemonize yes
pidfile /var/run/redis_7002.pid
port 7002
bind 192.168.100.30
requirepass pass***
masterauth pass***
dir "/data/redis/7002"
appendonly yes

设置sentinel配置文件,设置监控的master和访问密码

#vim /data/redis/7000/sentinel.conf
port 17000
logfile "/data/redis/7000/sentinel.log"
sentinel monitor mymaster 192.168.100.30 7000 2 #主节点
sentinel auth-pass mymaster pass***
sentinel down-after-milliseconds mymaster 15000
sentinel failover-timeout mymaster 900000


#vim /data/redis/7001/sentinel.conf
port 17001
logfile "/data/redis/7001/sentinel.log"
sentinel monitor mymaster 192.168.100.30 7001 2 #主节点
sentinel auth-pass mymaster pass***
sentinel down-after-milliseconds mymaster 15000
sentinel failover-timeout mymaster 900000


#vim /data/redis/7002/sentinel.conf
port 17002
logfile "/data/redis/7002/sentinel.log"
sentinel monitor mymaster 192.168.100.30 7002 2 #主节点
sentinel auth-pass mymaster pass***
sentinel down-after-milliseconds mymaster 15000
sentinel failover-timeout mymaster 900000

protected-mode no

现在启动master服务
redis-server /data/redis/7000/redis.conf > /data/redis/7000/redis.log 2>&1 &
redis-server /data/redis/7001/redis.conf > /data/redis/7001/redis.log 2>&1 &
redis-server /data/redis/7002/redis.conf > /data/redis/7002/redis.log 2>&1 &
redis-sentinel /data/redis/7000/sentinel.conf > /data/redis/7000/sentinel.log 2>&1 --sentinel &
redis-sentinel /data/redis/7001/sentinel.conf > /data/redis/7001/sentinel.log 2>&1 --sentinel &
redis-sentinel /data/redis/7002/sentinel.conf > /data/redis/7002/sentinel.log 2>&1 --sentinel &

5、启用redis从机和其他哨兵进程

现在主要的redis和哨兵进程已经启动了,接下来要启用redis从机和其他哨兵进程,我们换一台服务器进行也是照旧先拷贝redis.conf和sentinel.conf

cd /usr/local/src/redis
cp redis.conf /data/redis/7000/redis.conf
cp redis.conf /data/redis/7000/sentinel.conf
cp redis.conf /data/redis/7001/redis.conf
cp redis.conf /data/redis/7001/sentinel.conf
cp redis.conf /data/redis/7002/redis.conf
cp redis.conf /data/redis/7002/sentinel.conf

接下来设置redis从机配置文件,同样设置访问密码,设置master主机访问权限

假设从机的ip是192.168.100.31

#cd /data/redis/7000
#vim redis.conf
daemonize yes
pidfile /var/run/redis_7000.pid
port 7000
bind 192.168.100.31
requirepass pass***
masterauth pass***
dir "/data/redis/7000"
appendonly yes
slaveof 192.168.100.30 7000

#cd /data/redis/7001 #vim redis.conf daemonize yes pidfile /var/run/redis_7001.pid port 7001 bind 192.168.100.31 requirepass pass*** masterauth pass*** dir "/data/redis/7001" appendonly yes slaveof 192.168.100.30 7001
#cd /data/redis/7002 #vim redis.conf daemonize yes pidfile /var/run/redis_7002.pid port 7001 bind 192.168.100.31 requirepass pass*** masterauth pass*** dir "/data/redis/7002" appendonly yes slaveof 192.168.100.30 7002

设置sentinel配置文件,设置监控的master和访问密码

#vim /data/redis/7000/sentinel.conf
port 17000
logfile "/data/redis/7000/sentinel.log"
sentinel monitor mymaster 192.168.100.31 7000 2
sentinel auth-pass mymaster pass***
sentinel down-after-milliseconds mymaster 15000
sentinel failover-timeout mymaster 900000

#vim /data/redis/7001/sentinel.conf port 17001 logfile "/data/redis/7001/sentinel.log" sentinel monitor mymaster 192.168.100.31 7001 2 sentinel auth-pass mymaster pass*** sentinel down-after-milliseconds mymaster 15000 sentinel failover-timeout mymaster 900000
#vim /data/redis/7002/sentinel.conf port 17002 logfile "/data/redis/7002/sentinel.log" sentinel monitor mymaster 192.168.100.31 7002 2 sentinel auth-pass mymaster pass*** sentinel down-after-milliseconds mymaster 15000 sentinel failover-timeout mymaster 900000

现在启动slave服务

redis-server /data/redis/7000/redis.conf >> /data/redis/7000/redis.log 2>&1 &
redis-server /data/redis/7001/redis.conf >> /data/redis/7001/redis.log 2>&1 &
redis-server /data/redis/7002/redis.conf >> /data/redis/7002/redis.log 2>&1 &
redis-sentinel /data/redis/7000/sentinel.conf >> /data/redis/7000/sentinel.log 2>&1 --sentinel &
redis-sentinel /data/redis/7001/sentinel.conf >> /data/redis/7001/sentinel.log 2>&1 --sentinel &
redis-sentinel /data/redis/7002/sentinel.conf >> /data/redis/7002/sentinel.log 2>&1 --sentinel &

6、按第5步相同的操作(仅用调整bind 地址),完成192.168.100.32 Redis从配置

7、测试

接下来可以手动测试一下redis挂掉的情况,很简单,把主机上的redis master kill掉就行了
pkill redis-server
pkill redis-sentinel
如果此时telnet到主机的17000端口,就能看到sentinel的输出信息
10914:X 31 May 17:58:26.108 # +sdown master mymaster 10.66.1.9 6379
10914:X 31 May 17:58:26.242 # +new-epoch 2
10914:X 31 May 17:58:26.245 # +vote-for-leader 62bb8d23d060c4c445d623e03acf6304ac4a0aea 2
10914:X 31 May 17:58:27.276 # +odown master mymaster 10.66.1.9 6379 #quorum 2/2
10914:X 31 May 17:58:27.276 # Next failover delay: I will not start a failover before Tue May 31 18:28:26 2016
10914:X 31 May 17:58:27.444 # +config-update-from sentinel 10.66.1.98:36379 10.66.1.98 36379 @ mymaster 10.66.1.9 6379
10914:X 31 May 17:58:27.444 # +switch-master mymaster 10.66.1.9 6379 10.66.1.98 6379

sdown,主观下线(Subjectively Down, 简称 SDOWN)指的是单个 Sentinel 实例对服务器做出的下线判断

odown,客观下线(Objectively Down, 简称 ODOWN)指的是多个 Sentinel 实例在对同一个服务器做出 SDOWN 判断, 并且通过SENTINEL is-master-down-by-addr 命令互相交流之后, 得出的服务器下线判断。 (一个 Sentinel 可以通过向另一个 Sentinel 发送 SENTINEL is-master-down-by-addr 命令来询问对方是否认为给定的服务器已下线。)

switch-master,表示开始发送切换master指令了

这里就切换完成了,telnet到从机查看

info Replication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.100.31,port=7000,state=online,offset=176179,lag=1
slave1:ip=192.168.100.32,port=7000,state=online,offset=176036,lag=1

已经变成主了

再次启动本来的redis主机,并且telnet到本来的主机查看

info Replication
Replication
role:slave
master_host:192.168.100.32
master_port:7000
master_link_status:up

本来的主机已经变成从了

 

接下来编辑redis-sentinel的client-reconfig-script脚本,client-reconfig-script脚本如果配置了,在sentinel failover时会执行,用于手动做一些主从切换的操作,比如切换VIP(虚拟IP)指向,发送短信通知等。

本文通过添加host实现ip指向的定义:

#vim /etc/hosts  
10.66.1.9  redis.zhibo.com  

client-reconfig-script脚本内容:

echo "Master move from $4:$5 to $6:$7" >> /Data/logs/redis/sentinel.log  
sed -i '/redis.zhibo.com/d' /etc/hosts  
sed -i '$a '$6' redis.zhibo.com' /etc/hosts  

client-reconfig-script脚本会在选举出做switch操作的sentinel机上执行一次,在其他运行sentinel的集群成员机上也会各执行一次,这样所有集群机上访问redis.zhibo.com这个域名都会指向正常运行的redis的ip上去

另一种方法是,客户端代码服务器设立一个可访问的http api接口,调用client-reconfig-script脚本时访问api,通知redis可用地址切换,由代码服务器自行处理ip切换

 

登录:

redis-cli -h 192.168.1.183 -p 7002

检测节点主从状态:

info Replication

 

检测集群状态:

redis-cli -h 192.168.1.209 -p 17002 info Sentinel

posted on 2017-03-22 13:52  Martin.xu  阅读(238)  评论(0编辑  收藏  举报