Redis+Sentinel的高可用在主从切换时无法执行client-reconfig-script脚本进行VIP飘移
已经给予脚本执行权限。
关闭一台redis进行主从切换后,VIP漂移没有执行(不清楚脚本是否执行了)。单独运行脚本可以漂移。
配置如下
Sentinel配置(部分代码是主从切换后自动生成)
# Example sentinel.conf
# *** IMPORTANT ***
#
# By default Sentinel will not be reachable from interfaces different than
# localhost, either use the 'bind' directive to bind to a list of network
# interfaces, or disable protected mode with "protected-mode no" by
# adding it to this configuration file.
#
# Before doing that MAKE SURE the instance is protected from the outside
# world via firewalling or other means.
#
# For example you may use one of the following:
#
# bind 127.0.0.1 192.168.1.1
#
# protected-mode no
protected-mode no
# port <sentinel-port>
# The port that this sentinel instance will run on
daemonize yes
port 26379
logfile "/var/log/redis/sentinel.log"
pidfile "/var/run/sentinel.pid"
# sentinel announce-ip <ip>
# sentinel announce-port <port>
#
# The above two configuration directives are useful in environments where,
# because of NAT, Sentinel is reachable from outside via a non-local address.
#
# When announce-ip is provided, the Sentinel will claim the specified IP address
# in HELLO messages used to gossip its presence, instead of auto-detecting the
# local address as it usually does.
#
# Similarly when announce-port is provided and is valid and non-zero, Sentinel
# will announce the specified TCP port.
#
# The two options don't need to be used together, if only announce-ip is
# provided, the Sentinel will announce the specified IP and the server port
# as specified by the "port" option. If only announce-port is provided, the
# Sentinel will announce the auto-detected local IP and the specified port.
#
# Example:
#
# sentinel announce-ip 1.2.3.4
# dir <working-directory>
# Every long running process should have a well-defined working directory.
# For Redis Sentinel to chdir to /tmp at startup is the simplest thing
# for the process to don't interfere with administrative tasks such as
# unmounting filesystems.
dir "/tmp"
# sentinel monitor <master-name> <ip> <redis-port> <quorum>
#
# 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 myid 7267fb13d88570855d5b6e47915520d3e625afd5
# sentinel auth-pass <master-name> <password>
#
# Set the password to use to authenticate with the master and slaves.
# Useful if there is a password set in the Redis instances to monitor.
#
# Note that the master password is also used for slaves, so it is not
# possible to set a different password in masters and slaves instances
# if you want to be able to monitor these instances with Sentinel.
#
# However you can have Redis instances without the authentication enabled
# mixed with Redis instances requiring the authentication (as long as the
# password set is the same for all the instances requiring the password) as
# the AUTH command will have no effect in Redis instances with authentication
# switched off.
#
# Example:
#
# sentinel auth-pass mymaster MySUPER--secret-0123passw0rd
sentinel monitor mymaster 10.0.0.42 6379 2
# sentinel down-after-milliseconds <master-name> <milliseconds>
#
# Number of milliseconds the master (or any attached slave or sentinel) should
# be unreachable (as in, not acceptable reply to PING, continuously, for the
# specified period) in order to consider it in S_DOWN state (Subjectively
# Down).
#
# Default is 30 seconds.
sentinel client-reconfig-script mymaster /usr/opt/notify_master.sh
# 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 auth-pass mymaster dell@123
# sentinel failover-timeout <master-name> <milliseconds>
#
# Specifies the failover timeout in milliseconds. It is used in many ways:
#
# - The time needed to re-start a failover after a previous failover was
# already tried against the same master by a given Sentinel, is two
# times the failover timeout.
#
# - The time needed for a slave replicating to a wrong master according
# to a Sentinel current configuration, to be forced to replicate
# with the right master, is exactly the failover timeout (counting since
# the moment a Sentinel detected the misconfiguration).
#
# - The time needed to cancel a failover that is already in progress but
# did not produced any configuration change (SLAVEOF NO ONE yet not
# acknowledged by the promoted slave).
#
# - The maximum time a failover in progress waits for all the slaves to be
# reconfigured as slaves of the new master. However even after this time
# the slaves will be reconfigured by the Sentinels anyway, but not with
# the exact parallel-syncs progression as specified.
#
# Default is 3 minutes.
sentinel config-epoch mymaster 254
# SCRIPTS EXECUTION
#
# sentinel notification-script and sentinel reconfig-script are used in order
# to configure scripts that are called to notify the system administrator
# or to reconfigure clients after a failover. The scripts are executed
# with the following rules for error handling:
#
# If script exits with "1" the execution is retried later (up to a maximum
# number of times currently set to 10).
#
# If script exits with "2" (or an higher value) the script execution is
# not retried.
#
# If script terminates because it receives a signal the behavior is the same
# as exit code 1.
#
# A script has a maximum running time of 60 seconds. After this limit is
# reached the script is terminated with a SIGKILL and the execution retried.
# NOTIFICATION SCRIPT
#
# sentinel notification-script <master-name> <script-path>
#
# Call the specified notification script for any sentinel event that is
# generated in the WARNING level (for instance -sdown, -odown, and so forth).
# This script should notify the system administrator via email, SMS, or any
# other messaging system, that there is something wrong with the monitored
# Redis systems.
#
# The script is called with just two arguments: the first is the event type
# and the second the event description.
#
# The script must exist and be executable in order for sentinel to start if
# this option is provided.
#
# Example:
#
# sentinel notification-script mymaster /var/redis/notify.sh
# CLIENTS RECONFIGURATION SCRIPT
#
# sentinel client-reconfig-script <master-name> <script-path>
#
# When the master changed because of a failover a script can be called in
# order to perform application-specific tasks to notify the clients that the
# configuration has changed and the master is at a different address.
#
# The following arguments are passed to the script:
#
# <master-name> <role> <state> <from-ip> <from-port> <to-ip> <to-port>
#
# <state> is currently always "failover"
# <role> is either "leader" or "observer"
#
# The arguments from-ip, from-port, to-ip, to-port are used to communicate
# the old address of the master and the new address of the elected slave
# (now a master).
#
# This script should be resistant to multiple invocations.
#
# Example:
#
# sentinel client-reconfig-script mymaster /var/redis/reconfig.sh
sentinel leader-epoch mymaster 254
# Generated by CONFIG REWRITE
sentinel known-slave mymaster 10.0.0.40 6379
sentinel known-slave mymaster 10.0.0.41 6379
sentinel known-sentinel mymaster 10.0.0.40 26379 8ed286e02e81d6946e0d007f569e164a9404c03f
sentinel known-sentinel mymaster 10.0.0.41 26379 8e1529d6b3ce060ea4282d99b671330546d723ab
sentinel current-epoch 254
不同点是sentinel client-reconfig-script mymaster /usr/opt/notify_master.sh 这一句在主从切换后被移到了上面,我写的时候写在最后一行
Sentinel日志(含主从切换和原来的主重新启动)
3314:X 02 Apr 10:14:07.069 # +sdown master mymaster 10.0.0.41 6379
3314:X 02 Apr 10:14:07.159 # +new-epoch 254
3314:X 02 Apr 10:14:07.161 # +vote-for-leader 8ed286e02e81d6946e0d007f569e164a9404c03f 254
3314:X 02 Apr 10:14:07.679 # +config-update-from sentinel 8ed286e02e81d6946e0d007f569e164a9404c03f 10.0.0.40 26379 @ mymaster 10.0.0.41 6379
3314:X 02 Apr 10:14:07.679 # +switch-master mymaster 10.0.0.41 6379 10.0.0.42 6379
3314:X 02 Apr 10:14:07.680 * +slave slave 10.0.0.40:6379 10.0.0.40 6379 @ mymaster 10.0.0.42 6379
3314:X 02 Apr 10:14:07.680 * +slave slave 10.0.0.41:6379 10.0.0.41 6379 @ mymaster 10.0.0.42 6379
3314:X 02 Apr 10:14:37.742 # +sdown slave 10.0.0.41:6379 10.0.0.41 6379 @ mymaster 10.0.0.42 6379
3314:X 02 Apr 10:48:36.099 # -sdown slave 10.0.0.41:6379 10.0.0.41 6379 @ mymaster 10.0.0.42 6379
3314:X 02 Apr 10:48:46.056 * +convert-to-slave slave 10.0.0.41:6379 10.0.0.41 6379 @ mymaster 10.0.0.42 6379
VIP漂移脚本
#!/bin/bash
MASTER_IP=$6 #第六个参数是新主redis的ip地址
LOCAL_IP=`ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:"` #本地ip
VIP='10.0.0.31'
NETMASK='24'
INTERFACE='eno16777736' #网卡接口设备名称
if [ ${MASTER_IP} = ${LOCAL_IP} ];then
sudo /usr/sbin/ip addr add ${VIP}/${NETMASK} dev ${INTERFACE} #将VIP绑定到该服务器上
sudo /usr/sbin/arping -q -c 3 -A ${VIP} -I ${INTERFACE}
exit 0
else
sudo /usr/sbin/ip addr del ${VIP}/${NETMASK} dev ${INTERFACE} #将VIP从该服务器上删除
exit 0
fi
exit 1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我把sudo去掉了就可以了。但是为什么呢?
我把sudo去掉后VIP也不会漂移
Apr 7 18:01:28 master redis-sentinel: 10954:X 07 Apr 18:01:28.335 * +slave slave 192.168.11.13:6379 192.168.11.13 6379 @ def_master 192.168.11.10 6379
Apr 7 18:01:28 master redis-sentinel: 10954:X 07 Apr 18:01:28.335 * +slave slave 192.168.11.12:6379 192.168.11.12 6379 @ def_master 192.168.11.10 6379
Apr 7 18:01:28 master redis-sentinel: 10954:X 07 Apr 18:01:28.464 # -script-error /var/lib/redis/failover.sh 0 2
Apr 7 18:01:33 master redis-sentinel: 10954:X 07 Apr 18:01:33.371 # +sdown slave 192.168.11.12:6379 192.168.11.12 6379 @ def_master 192.168.11.10 6379
Apr 7 18:01:34 master redis-sentinel: 10954:X 07 Apr 18:01:34.600 # +sdown sentinel 63e8dad4e625ce6ac20ee4221b8c22c5443b3433 192.168.11.12 26379 @ def_master 192.168.11.10 6379
Apr 7 18:02:48 master redis-sentinel: 10954:X 07 Apr 18:02:48.303 # -sdown slave 192.168.11.12:6379 192.168.11.12 6379 @ def_master 192.168.11.10 6379
Apr 7 18:02:52 master redis-sentinel: 10954:X 07 Apr 18:02:52.493 # -sdown sentinel 63e8dad4e625ce6ac20ee4221b8c22c5443b3433 192.168.11.12 26379 @ def_master 192.168.11.10 6379
这是centos7的message日志
最后问题解决了么?我也遇到同样的问题,希望给点指导谢谢!!!
日志:
16587:X 29 Jun 01:14:40.545 # Sentinel ID is 6e912d3910d2394ef472b5fd8daa6548ec628d24
16587:X 29 Jun 01:14:40.545 # +monitor master mymaster 172.16.24.240 6379 quorum 2
16587:X 29 Jun 01:15:37.248 # +sdown master mymaster 172.16.24.240 6379
16587:X 29 Jun 01:15:37.330 # +new-epoch 33
16587:X 29 Jun 01:15:37.332 # +vote-for-leader 0794d1aacc84fc57af48da252767b14e2e79f8a5 33
16587:X 29 Jun 01:15:37.333 # +odown master mymaster 172.16.24.240 6379 #quorum 2/2
16587:X 29 Jun 01:15:37.333 # Next failover delay: I will not start a failover before Fri Jun 29 01:16:08 2018
16587:X 29 Jun 01:15:37.682 # +config-update-from sentinel 0794d1aacc84fc57af48da252767b14e2e79f8a5 172.16.24.242 26379 @ mymaster 172.16.24.240 6379
16587:X 29 Jun 01:15:37.682 # +switch-master mymaster 172.16.24.240 6379 172.16.24.242 6379
16587:X 29 Jun 01:15:37.682 * +slave slave 172.16.24.241:6379 172.16.24.241 6379 @ mymaster 172.16.24.242 6379
16587:X 29 Jun 01:15:37.683 * +slave slave 172.16.24.240:6379 172.16.24.240 6379 @ mymaster 172.16.24.242 6379
16587:X 29 Jun 01:15:37.771 # -script-error /usr/notify_master.sh 0 2
16587:X 29 Jun 01:15:42.765 # +sdown slave 172.16.24.240:6379 172.16.24.240 6379 @ mymaster 172.16.24.242 6379