首页 > 代码库 > keepalived双机热备实现故障时发送邮件通知

keepalived双机热备实现故障时发送邮件通知

眼下项目已经採用nginx来实现负载均衡,可是nginx调度器仅仅有一台,上次还意外的down机一次,导致整个服务应用所有瘫痪,这次准备再加一个调度器来实现网站高可用性,也就是常说的双机热备了。


master: 192.168.1.241
backup: 192.168.1.242
漂移IP:192.168.1.243
1。主备调度器都安装keepalived软件
安装步骤例如以下:
./configure --prefix=/usr/local/keepalived && make && make install
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
mkdir /etc/keepalived
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
service keepalived start
可以先用命令 service keepalived start 启动进行測试,看进程可以启动。注意查看log.
2,主调度器配置例如以下:
! Configuration File for keepalived
global_defs {
    router_id LVS_DEVEL
}
vrrp_sync_group VG_1 {
    group {
        VI_1
    }
    notify_master /etc/keepalived/sendmail.pl
}
vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.1.243
    }
}
2,备调度器配置例如以下:
! Configuration File for keepalived
global_defs {
   router_id LVS_DEVEL
}
vrrp_sync_group VG_1 {
    group {
    VI_1
    }
    notify_master /etc/keepalived/sendmail.pl
}
vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 99
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.1.243
    }
}
3。发送邮件的perl脚本sendmail.pl内容例如以下:
注意主备发送邮件的标题是不一致的。仅仅要你能识别漂移IP在哪台server上就可以。


#!/usr/bin/perl -w
use Net::SMTP_auth;
use strict;
my $mailhost = ‘smtp.163.com‘;
my $mailfrom = ‘test@163.com‘;
my @mailto   = (‘123456@139.com‘);
my $subject  = ‘keepalived up on backup‘;
my $text = "正文\n第二行位于此。";  
my $user   = ‘test@163.com‘;
my $passwd = ‘xxxxxxx‘;
&SendMail();
##############################
# Send notice mail
##############################
sub SendMail() {
    my $smtp = Net::SMTP_auth->new( $mailhost, Timeout => 120, Debug => 1 )
      or die "Error.\n";
    $smtp->auth( ‘LOGIN‘, $user, $passwd );
    foreach my $mailto (@mailto) {
        $smtp->mail($mailfrom);
        $smtp->to($mailto);
        $smtp->data();
        $smtp->datasend("To: $mailto\n");
        $smtp->datasend("From:$mailfrom\n");
        $smtp->datasend("Subject: $subject\n");
        $smtp->datasend("\n");
        $smtp->datasend("$text\n\n"); 
        $smtp->dataend();
    }
    $smtp->quit;
}

说明:
a、因为keeplived自带的发送邮件机制是个鸡肋,假设本地不启动25port就无法实现邮件发送,就琢磨着能不能通过自己定义脚本来实现。真的是非常幸运,就採用了认证的方式。
b、其它的配置说明就不具体讲了。网上非常多资料。
4。測试keepalived

主备调度器都开启80port,两台server上的測试内容不一致,这样更方便測试。


##########################

#所需安装模块

#use Net::SMTP

#Authen::SASL

##########################

#$stmp->auth(‘user‘,‘pass‘);

#大部分SMTPserver为了防止 spam /垃圾邮件,就须要用户验证身份。

#此方法须要另外安装模块:Authen::SASL, 此模块可能系统不自带

##########################

#Debug => 1

#此段代码用于測试之用,所以开启了Debug。一般測试一次完成,正式使用的话会关闭它。


注:可在命令行界面直接运行:/etc/keepalived/sendmail.pl,看看是否能发送邮件成功,假设失败的话则须要安装Net::SMTP_auth模块

安装方法:

yum -y install perl-CPAN
cpan Net::SMTP_auth

keepalived双机热备实现故障时发送邮件通知