美文网首页
【CentOS实用篇】之lvs高可用

【CentOS实用篇】之lvs高可用

作者: Yi_Feng | 来源:发表于2017-11-05 22:24 被阅读0次

    1 概述

    在lvs的集群设计中,存在两个地方不可用的问题,Director不可用 和RS不可用

    A)Director不可用

    Director不可用整个系统将不可用;SPoF Single Point of Failure,单点故障导致

    解决方案:通过keepalived heartbeat/corosync实现高可用

    B)某RS不可用时

    当后端服务器存在某一台RS不可用,Director依然会调度请求至此RS,导致请求不能被处理,服务失败

    解决方案:由Director对各RS健康状态进行检查,失败时禁用,成功时启用

    工具有keepalived heartbeat/corosync, ldirectord

    同时,也可以通过脚本对后端RS监控状态进行监控

    检测方式:
    (a)网络层检测,icmp
    (b)传输层检测,端口探测
    (c)应用层检测,请求某关键资源
    RS全不可用时,可以通过back server或者sorryserver来提示用户

    本文将介绍ldirectord软件和通过脚本的方式来实现对后端服务器的监控

    2 ldirectord

    ldirectord:监控和控制LVS守护进程,可管理LVS规则,该软件解决了ipvs不能监控后端服务器的状态的问题。
    原理是根据配置文件设定好的规则,去检查服务器端的应用是否正常。通过配置文件配置服务后,只要启动该软件就会按设定的规则进行配置和监测

    service ldirectord  start
    

    那么该软件就会根据配置文件的规则创建lvs集群类型,添加RS服务器,并进行监控,如果后端服务失败,就移除对应的RS,如果RS服务恢复正常,会自动将RS加入调度计划里。
    如后端监控http服务时,该软件会通过抓取后端服务器指定页面的关键字来决定后端http服务是否正常运行。

    .包名:ldirectord-3.9.6-0rc1.1.1.x86_64.rpm,该服务包在base源中没有,要另外下载,存在依赖性,下载后用yum安装,解决依赖性。

    .软件相关文件:

    /etc/ha.d/ldirectord.cf主配置文件
    
    /usr/share/doc/ldirectord-3.9.6/ldirectord.cf配置模版
    
    /usr/lib/systemd/system/ldirectord.service服务
    
    /usr/sbin/ldirectord主程序
    
    /var/log/ldirectord.log 日志
    
    /var/run/ldirectord.ldirectord.pidpid文件
    

    Ldirectord配置文件示例

    checktimeout=3 #多长时间为超时时间,如3s没回应,表示超时
    
    checkinterval=1 # 检查的间隔
    
    autoreload=yes #更改策略后,不需要重启服务就自动生效
    
    fallback=127.0.0.1:80 #这里是定义sorry server,当后端的RS都宕机了,本机给用户提示信息
    
    logfile=“/var/log/ldirectord.log“#日志文件
    
    quiescent=no #down时yes权重为0,no为删除
    
    virtual=5#指定VS的FWM或IP:port
    
    real=172.16.0.7:80 gate 2 # gate表示dr模式,2是权重
    
    real=172.16.0.8:80 gate 1
    
    fallback=127.0.0.1:80 gate#sorryserver
    
    service=http
    
    scheduler=wrr #调度算法
    
    checktype=negotiate #默认就可以
    
    checkport=80 #检查端口,这样会给服务器的负载加大。因为对外提供服务是80端口。可以另外在监听一个http的端口,如listen8080但是这里有个矛盾,万一80异常了,但是8080还是正常的,所以就导致了检查结果不准确
    
    request="index.html" #监控的主页面
    
    receive=“Test Ldirectord" #抓到默认的几个字符,就认为服务是正常的。大小写敏感
    

    例子

    监控后端的http服务是否正常,通过抓取后端服务器index.html页面的关键字centos

    cp  /usr/share/doc/ldirectord-3.9.6/ldirectord.cf /etc/ha.d/ldirectord.cf
    
    vim /etc/ha.d/ldirectord.cf
    
    virtual=192.168.32.66:80
    
    real=192.168.32.63:80 gate
    
    real=192.168.32.73:80 gate
    
    fallback=127.0.0.1:80 gate
    
    service=http
    
    scheduler=wrr
    
    #persistent=600
    
    #netmask=255.255.255.255
    
    protocol=tcp
    
    checktype=negotiate
    
    checkport=80
    
    request="index.html"
    
    receive="centos"
    

    3 自动化脚本

    脚本使用需要注意事项:

    脚本中的VIP,RIP,RW(权重)监控端口VPORT和RPORT这些变量需要根据实际情况进行调整

    脚本设置了循环监测,建议使用如下语句执行脚本

    nohup /PATH/TO/script/monitorRS.sh > /root/RSout.file 2>&1 &
    

    脚本默认是3s对后端的RS进行一次监测,该值可以根据实际情况调整,命令在脚本后sleep 3.调整数字3即可。

    一键监控脚本如下

    ​#!/bin/bash
    
    #
    
    #******************************************************************************
    
    #Author:              Sunny
    
    #Date:                2017-10-23
    
    #FileName:            monitorRS.sh
    
    #version:              1.0
    
    #Your change info:
    
    #Description:          For auto monitor RS status
    
    #Copyright(C):        2017  All rights reserved
    
    #*****************************************************************************
    
    echo "This is a script to auto monitor RS status,if you want to run the scirpt ,suggest you to excute cmd below"
    
    echo
    
    echo " nohup /PATH/TO/script/monitorRS.sh > /root/RSout.file 2>&1 & "
    
    echo
    
    echo "If you want to stop the script,you should run two cmds below,first you find the PID,then kill it"
    
    echo
    
    echo "ps -ef | grep monitorRS.sh"
    
    echo  "kill -9 PID"
    
    VIP=10.10.10.10
    
    VPORT=80
    
    RS=("192.168.32.63" "192.168.32.73")
    
    RW=("3" "1")
    
    RPORT=80
    
    TYPE=g
    
    LOG=/var/log/monitorRS.log
    
    [ -e /var/log/monitorRS.log ] || touch /var/log/monitorRS.log
    
    addrs() {
    
    ipvsadm -a -t $VIP:$VPORT -r $1:$RPORT -$TYPE -w $2
    
    [ $? -eq 0 ] && return 0 || return 1
    
    }
    
    delrs() {
    
    ipvsadm -d -t $VIP:$VPORT -r $1:$RPORT
    
    [ $? -eq 0 ] && return 0 || return 1
    
    }
    
    while true; do
    
    let COUNT=0
    
    for rip in ${RS[*]}; do
    
    if ipvsadm -Ln | grep "$rip:$RPORT" &> /dev/null ; then
    
    RS_status=online
    
    else
    
    RS_status=offline
    
    fi
    
    if $(curl --connect-timeout 1 http://$rip &>/dev/null) ; then
    
    RS_test=yes
    
    else
    
    RS_test=no
    
    fi
    
    case $RS_test in
    
    yes)
    
    case ${RS_status} in
    
    online)
    
    echo "`date +'%F %H:%M:%S'`, $rip is work nice now." >> $LOG
    
    ;;
    
    offline)
    
    addrs $rip ${RW[$COUNT]} &>/dev/null;
    
    addstatus=$?
    
    if  [ $? -eq 0 ] && RS_status=online ;
    
    then
    
    echo "`date +'%F %H:%M:%S'`, $rip has been added to work." >> $LOG
    
    else
    
    echo "something wrong when add $rip back to work,please check,maybe your should do it manual"
    
    echo "`date +'%F %H:%M:%S'`, $rip is added failed." >> $LOG
    
    fi
    
    ;;
    
    *)
    
    echo "Something wrong when read RS_status"
    
    ;;
    
    esac
    
    ;;
    
    no)
    
    case ${RS_status} in
    
    online)
    
    delrs $rip &>/dev/null;
    
    [ $? -eq 0 ] && RS_status=offline && echo "`date +'%F %H:%M:%S'`, $rip is out of work,it is delete." >> $LOG
    
    ;;
    
    offline)
    
    echo "`date +'%F %H:%M:%S'`,$rip is still out of  work" >> $LOG
    
    ;;
    
    *)
    
    echo "Something wrong when read RS_status"
    
    ;;
    
    esac
    
    ;;
    
    *)
    
    echo "Something wrong when read RS_test"
    
    ;;
    
    esac
    
    let COUNT++
    
    done
    
    sleep 3
    
    done
    

    4 总结

    本文通过介绍工具ldirectord 和 编写了自己设计的脚本对后端的RS进行监控,但是监控的方式都是对后端RS进行轮询访问,这种方式会对服务器造成一定的压力,因此,使用时要权衡。总体来说,如果要使用LVS进行调度,建议是要对后端RS进行监控,否则当RS异常时,将导致服务不可用。

    相关文章

      网友评论

          本文标题:【CentOS实用篇】之lvs高可用

          本文链接:https://www.haomeiwen.com/subject/yqzhmxtx.html