Keepalived + Nginx 搭建高可用负载均衡服务器

Keepalived Nginx 负载均衡 About 1,690 words

Keepalived 安装

https://www.zhangbj.com/p/351.html

添加监控 Nginx 脚本

Keepalived 配置文件

vrrp_script chk_nginx {
    script "/usr/local/src/check_nginx.sh"    #检测nginx是否存活脚本
    interval 2                          #检测脚本执行的间隔,单位:秒
    weight 2
}

vrrp_instance VI_1 {
    track_script {
        chk_nginx # 调用vrrp_script指定的脚本
    }
}

监控 Nginx 脚本

#!/bin/bash
#时间变量,用于记录日志
d=`date --date today +%Y/%m/%d-%H:%M:%S`
#计算nginx进程数量
n=`ps -C nginx --no-heading|wc -l`
#如果进程为0,则启动nginx,并且再次检测nginx进程数量,
if [ $n -eq "0" ]; then
        /etc/init.d/nginx start # 需确认/etc/init.d/目录下是否有ningx可执行文件
        n2=`ps -C nginx --no-heading|wc -l`
        #如果还为0,说明nginx无法启动,此时需要关闭keepalived
        if [ $n2 -eq "0"  ]; then
                echo "$d nginx down,keepalived will stop" >> /var/log/check_ng.log
                service keepalived stop # 停止keepalived
        fi
fi

参考配置

global_defs {
   notification_email {
    test@qq.com
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id LVS_01
   #vrrp_strict  # 严格遵守vvrp协议,不注释访问不了VIP
}

vrrp_script chk_nginx {
    script "/usr/local/src/check_nginx.sh"    #检测nginx是否存活脚本
    interval 2                          #检测脚本执行的间隔,单位:秒
    weight 2
}

vrrp_instance VI_1 {
    state MASTER # 指定keepalived的角色,MASTER为主,BACKUP为备
    interface eth0
    virtual_router_id 51 # 虚拟路由编号,主从要一致
    priority 150 # 优先级,数值越大,获取处理请求的优先级越高,step建议为50
    advert_int 1 # 检查间隔,默认为1s(vrrp组播周期秒数)
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {
        chk_nginx # 调用vrrp_script指定的脚本
    }
    virtual_ipaddress {
        10.0.0.17/24 # 定义虚拟ip(VIP),可多设,每行一个
    }
}
Views: 4,430 · Posted: 2019-08-07

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

扫描下方二维码关注公众号和小程序↓↓↓

扫描下方二维码关注公众号和小程序↓↓↓


Today On History
Browsing Refresh