11.1.5.3 持久化动态修改

其实相对于VIP的切换,动态修改持久化则是比较常见的一个需求,一般在一主多从多Sentinel的HA环境中,为了性能常常在Master上关闭持久化,而在Slave上开启持久化,但是如果发生切换就必须有人工干预才能实现这个功能。可以利用client-reconfig-script自动化该进程,无需人工守护,我们就以RDB的动态控制为例:
Sentinel配置文件如下:

  1. sentinel client-reconfig-script mymaster /redis/script/rdbctl.sh

rdbctl.sh源代码:

  1. #!/bin/bash
  2. _DEBUG="on"
  3. DEBUGFILE="/smsred/redis-3.0.4/log/sentinel_failover.log"
  4. MASTERIP=${6}
  5. MASTERPORT=${7}
  6. SLAVEIP=${4}
  7. SLAVEPORT=${5}
  8. MASK='24'
  9. IFACE='bond0'
  10. MYIP=$(ip -4 -o addr show dev ${IFACE}| grep -v secondary| awk '{split($4,a,"/");print a[1]}')
  11. DEBUG () {
  12. if [ "$_DEBUG" = "on" ]; then
  13. echo `$@` >> ${DEBUGFILE}
  14. fi
  15. }
  16. set -e
  17. DEBUG date
  18. DEBUG echo $@
  19. DEBUG echo "===Begin Failover==="
  20. #If Master
  21. if [ ${MASTERIP} = ${MYIP} ]; then
  22. #Disable RDB
  23. redis-cli -h ${MYIP} -p ${MASTERPORT} -a c1m2b3c4 config set save ""
  24. DEBUG echo ${MYIP}
  25. DEBUG echo "Disable Master RDB:" ${MYIP} ${MASTERPORT}
  26. DEBUG echo "===End Failover==="
  27. exit 0
  28. #Or Slave
  29. else
  30. echo "test5" >> $DEBUGFILE
  31. redis-cli -h ${MYIP} -p ${SLAVEPORT} -a c1m2b3c4 config set save "900 1 300 10 60 100000000"
  32. DEBUG echo ${MYIP}
  33. DEBUG echo "Enable Slave RDB:" ${MYIP} ${SLAVEPORT}
  34. DEBUG echo "===End Failover==="
  35. exit 0
  36. fi
  37. exit 1

原理和VIP切换一节基本一致,不再赘述。