keepalived实现某服务的HA(高可用)

本文旨在说明如何用 keepalived 实现某个服务的 主从热备 模式的高可用,主要提供了相关配置文件,配置文件中以 Nginx 服务为例。

一 keepalived 实现高可用的基本原理
keepalived 借助目标服务状态检查脚本判断是否进行代理漂移。
目标服务状态检查脚本中的逻辑是:如果目标服务没有运行,则尝试启动目标服务,等待一定时间后,如果目标服务仍未启动,则关闭对应的 keepalived ,进行 keepalived 代理漂移。

二 keepalived 的相关配置文件

(1)master上的配置:

! Configuration File for keepalived
global_defs {
    notification_email {
        1807479153@qq.com
    }
    notification_email_from pve@example.com
    smtp_server mail.example.com
    smtp_connect_timeout 30
    router_id LVS_DEVEL 
}
 
 
vrrp_script chk_nginx {
    script "/etc/keepalived/nginx_check.sh" 
    interval 2
    weight -5
    fall 3
    rise 2
}
 
 
vrrp_instance VI_1 {
    state MASTER  
    interface eth0  
    virtual_router_id 2 
    priority 101 
    advert_int 2
    authentication {
        auth_type PASS 
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.207.210  
    }
    track_script {
       chk_nginx
    }
 
}

(2)slave上的配置:

! Configuration File for keepalived
global_defs {
    notification_email {
     1807479153@qq.com
    }
    notification_email_from pve@example.com
    smtp_server mail.example.com
    smtp_connect_timeout 30
    router_id LVS_DEVEL
    }

vrrp_script chk_nginx {
    script "/etc/keepalived/nginx_check.sh"
    interval 2
    weight -5
    fall 3
    rise 2
    }


vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 2
    priority 100
    advert_int 2
    authentication {
    auth_type PASS
    auth_pass 1111
    }
virtual_ipaddress {
    192.168.207.210
    }
track_script {
      chk_nginx
    }

}

(3) 目标服务状态监测脚本

processCount=`ps -ef | grep nginx | grep -v grep | wc -l`
if [ $processCount -eq 0 ];then
    # try to start Nginx
    nginx
    # suspended time 2 seconds
    sleep 2
    if [ `ps -ef | grep nginx | grep -v grep | wc -l` -eq 0 ];then
        # kill keepalived and floating the VIP
        ps -ef|grep keepalived|grep -v grep|awk '{print $2}'|xargs kill -9
    fi
 
fi

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值