redis与keepalive合用,检测机制修订

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了redis与keepalive合用,检测机制修订相关的知识,希望对你有一定的参考价值。

近期又被自己造的keepalive检测机制搞了,直接上最新优化后的脚本:

############start scripts
killall -0 redis-server
if [ "$?" -eq 0 ]; then
        echo good
        exit 0
else
        LOGFILE=/var/log/keepalived-redis-state.log
        echo "[check_fail_log]" >> $LOGFILE
        date >> $LOGFILE
        killall -0 redis-server
        if [ "$?" -ne 0 ]; then
                echo killall_bad_two >> /var/log/keepalived-redis-state.log
                numprocess1=`netstat -tnpl | grep 6379 -c`
                numprocess2=`ps -ef | grep redis-server | grep -c 6379`
                if [[ ${numprocess1} -lt 1 ]] && [[ ${numprocess2} -lt 1 ]]; then
                        echo process_bad_three >> /var/log/keepalived-redis-state.log
                        #start judge status
                        /etc/init.d/redis status
                        if [[ $? -eq 0 ]]; then
                                echo good
                                exit 0
                        else
                                echo status_bad_four >> /var/log/keepalived-redis-state.log
                                /etc/init.d/redis status >> /var/log/keepalived-redis-state.log
                                ####start last judge
                                ALIVE=`/usr/bin/redis-cli -p 6379 PING`
                                if [ "$ALIVE" != "PONG" ]; then
                                        echo ping_bad_five >> /var/log/keepalived-redis-state.log
                                        exit 1
                                else
                                        exit 0
                                fi
                                ###end last judge
                        fi
                        #end judge status
                        else
                        exit 0
                fi
        else
                exit 0
        fi
fi
############end scripts

目前的判断为:
第一层: 重试两次 进程信号判断。
第二层: netstat 及 ps -ef 判断
第三层: /etc/init.d/redis status 返回码判断。
第四层: redis ping   返回码判断。

以上每一层失败,都会打印日志。失败后说明redis进程及业务访问出现问题,正式开始启动切换操作。
测试环境测试效果。
日志记录:
[check_fail_log]
Thu May 19 09:46:23 CST 2016
killall_bad_two
process_bad_three
status_bad_four
redis-server is stopped
ping_bad_five


本文出自 “linux系统维护” 博客,请务必保留此出处http://linuxadmin.blog.51cto.com/2683824/1774987

以上是关于redis与keepalive合用,检测机制修订的主要内容,如果未能解决你的问题,请参考以下文章

Redis高可用架构—Keepalive+VIP

keepalive初窥及实现

TCP的KeepAlive探测详解

keepalive与redis主从

TCP_KEEPALIVE机制简述

nginx的keepalive源码分析