sentinel需要先把redis主从配置好
主从配置:
#从服务器上
vim /etc/redis.conf
#做如下修改,其他配置保持默认即可
slaveof 172.17.16.7 6379
masterauth redispass
requirepass redispass
#主服务器上
vim /etc/redis.conf
#做如下修改,其他配置保持默认即可
masterauth redispass
requirepass redispass
# 查看同步状态
redis-cli -h 127.0.0.1 -a redispass info replication 或
进入cli后再认证
redis-cli -h 127.0.0.1 -p 6379
AUTH redispass
info replication
touch /usr/local/redis/log/sentinel.log
mkdir -p /usr/local/redis/sentinel-data
sentinel.conf
## --------------------------------------
port 5002
dir /usr/local/redis/sentinel-data
pidfile "/usr/local/redis/sentinel.pid"
logfile "/usr/local/redis/log/sentinel.log"
daemonize yes
# 保护模式默认是开启的,需要关闭才能从网络连接sentinel
protected-mode no
# sentinel monitor <master-name> <ip> <redis-port> <quorum>
# master-name:不能包含特殊字符,自定义master名字
# ip: redis master IP
# redis-port: redis master PORT
# quorum: 客观DOWN至少多个sentinel同意
# Tells Sentinel to monitor this master, and to consider it in O_DOWN
# (Objectively Down) state only if at least <quorum> sentinels agree.
#
# Note that whatever is the ODOWN quorum, a Sentinel will require to
# be elected by the majority of the known Sentinels in order to
# start a failover, so no failover can be performed in minority.
#
# Slaves are auto-discovered, so you don't need to specify slaves in
# any way. Sentinel itself will rewrite this configuration file adding
# the slaves using additional configuration options.
# Also note that the configuration file is rewritten when a
# slave is promoted to master.
#
# Note: master name should not include special characters or spaces.
# The valid charset is A-z 0-9 and the three characters ".-_".
sentinel monitor mymaster {master_ip} {redis_port} 2
sentinel auth-pass mymaster {redis_passwd}
sentinel down-after-milliseconds mymaster 5000
# sentinel parallel-syncs <master-name> <numslaves>
#
# How many slaves we can reconfigure to point to the new slave simultaneously
# during the failover. Use a low number if you use the slaves to serve query
# to avoid that all the slaves will be unreachable at about the same
# time while performing the synchronization with the master.
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 60000
## ---------------------
sentinel启动脚本:
#!/bin/bash
#
# redis-sentinel - this script starts and stops the redis-sentinel daemon
#
# chkconfig: - 80 12
# description: Redis is a persistent key-value database
# processname: redis-server
# config: /etc/redis/redis.conf
# pidfile: /var/run/redis.pid
source /etc/init.d/functions
BIN="/usr/local/redis/bin"
CONFIG="/usr/local/redis/etc/sentinel.conf"
PIDFILE="/usr/local/redis/redis-sentinel.pid"
### Read configuration
[ -r "$SYSCONFIG" ] && source "$SYSCONFIG"
RETVAL=0
prog="redis-sentinel"
desc="Redis sentinel"
start() {
if [ -e $PIDFILE ];then
echo "$desc already running...."
exit 1
fi
echo -n $"Starting $desc: "
daemon $BIN/$prog $CONFIG --sentinel 2>&1 >> /var/log/messages &
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog
return $RETVAL
}
stop() {
echo -n $"Stop $desc: "
killproc $prog
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog $PIDFILE
return $RETVAL
}
restart() {
stop
start
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
*)
echo $"Usage: $0 {start|stop|restart}"
RETVAL=1
esac
exit $RETVAL
centos 7启动脚本:
/usr/lib/systemd/system/sentinel.service
###########
[Unit]
Description=Startup script for the Redis sentinel
Documentation=http://redis.io
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/usr/local/redis/sentinel.pid
ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/etc/sentinel.conf --sentinel
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
########
sentinel 查看状态:
redis-cli -p 5002 127.0.0.1:5002> sentinel master mymaster SENTINEL get-master-addr-by-name mymaster sentinel slaves mymaster http://www.cnblogs.com/zhoujinyi/p/5569462.html
Sentinel API
Sentinel默认运行在26379端口上,sentinel支持redis协议,所以可以使用redis-cli客户端或者其他可用的客户端来与sentinel通信。
有两种方式能够与sentinel通信:
一种是直接使用客户端向它发消息
另外一种是使用发布/订阅模式来订阅sentinel事件,比如说failover,或者某个redis实例运行出错,等等。
Sentinel命令
sentinel支持的合法命令如下:
PING
sentinel回复PONG
.SENTINEL masters
显示被监控的所有master以及它们的状态.SENTINEL master <master name>
显示指定master的信息和状态;SENTINEL slaves <master name>
显示指定master的所有slave以及它们的状态;SENTINEL get-master-addr-by-name <master name>
返回指定master的ip和端口,如果正在进行failover或者failover已经完成,将会显示被提升为master的slave的ip和端口。SENTINEL reset <pattern>
重置名字匹配该正则表达式的所有的master的状态信息,清楚其之前的状态信息,以及slaves信息。SENTINEL failover <master name>
强制sentinel执行failover,并且不需要得到其他sentinel的同意。但是failover后会将最新的配置发送给其他sentinel。
动态修改Sentinel配置
从redis2.8.4开始,sentinel提供了一组API用来添加,删除,修改master的配置。
需要注意的是,如果你通过API修改了一个sentinel的配置,sentinel不会把修改的配置告诉其他sentinel。你需要自己手动地对多个sentinel发送修改配置的命令。
以下是一些修改sentinel配置的命令:
SENTINEL MONITOR <name> <ip> <port> <quorum>
这个命令告诉sentinel去监听一个新的masterSENTINEL REMOVE <name>
命令sentinel放弃对某个master的监听SENTINEL SET <name> <option> <value>
这个命令很像Redis的CONFIG SET
命令,用来改变指定master的配置。支持多个<option><value>。例如以下实例:SENTINEL SET objects-cache-master down-after-milliseconds 1000
只要是配置文件中存在的配置项,都可以用SENTINEL SET
命令来设置。这个还可以用来设置master的属性,比如说quorum(票数),而不需要先删除master,再重新添加master。例如:
SENTINEL SET objects-cache-master quorum 5
增加或删除Sentinel
由于有sentinel自动发现机制,所以添加一个sentinel到你的集群中非常容易,你所需要做的只是监控到某个Master上,然后新添加的sentinel就能获得其他sentinel的信息以及master所有的slaves。
如果你需要添加多个sentinel,建议你一个接着一个添加,这样可以预防网络隔离带来的问题。你可以每个30秒添加一个sentinel。最后你可以用SENTINEL MASTER mastername
来检查一下是否所有的sentinel都已经监控到了master。
删除一个sentinel显得有点复杂:因为sentinel永远不会删除一个已经存在过的sentinel,即使它已经与组织失去联系很久了。
要想删除一个sentinel,应该遵循如下步骤:
停止所要删除的sentinel
发送一个
SENTINEL RESET *
命令给所有其它的sentinel实例,如果你想要重置指定master上面的sentinel,只需要把*号改为特定的名字,注意,需要一个接一个发,每次发送的间隔不低于30秒。检查一下所有的sentinels是否都有一致的当前sentinel数。使用
SENTINEL MASTER mastername
来查询。
删除旧master或者不可达slave
sentinel永远会记录好一个Master的slaves,即使slave已经与组织失联好久了。这是很有用的,因为sentinel集群必须有能力把一个恢复可用的slave进行重新配置。
并且,failover后,失效的master将会被标记为新master的一个slave,这样的话,当它变得可用时,就会从新master上复制数据。
然后,有时候你想要永久地删除掉一个slave(有可能它曾经是个master),你只需要发送一个SENTINEL RESET master
命令给所有的sentinels,它们将会更新列表里能够正确地复制master数据的slave。
发布/订阅
注意:
如果原来的 redis master停止后,并已经选取出新的master后,原来停止的master启动前需要指定
redis.conf
slaveof {目前master_ip} {redis_port}
紧急恢复处理
当紧急恢复后,连接上一redis实例,执行
sentinel failover mymaste
重启选举master,哨兵会通知所连接客户新选举出来的master信息
转载于:https://blog.51cto.com/tianshili/1759289