Keepalived高可用+邮件告警

Keepalived高可用

IP 主机名 备注
192.168.117.14 keepalived-master 主节点
192.168.117.15 keepalived-slaver 备节点
192.168.117.100 VIP

1.主备节点均安装keepalived

# yum install -y keepalived httpd

2.主备节点均修改keepalived日志存放路径

# vim /etc/sysconfig/keepalived  //修改如下
KEEPALIVED_OPTIONS="-D -d -S 0"
# vim /etc/rsyslog.conf  //在local7.*一行下加一行配置
local0.*                                                /var/log/keepalived.log
# systemctl restart rsyslog  //重启rsyslog以生效

3.进入主节点,编辑keepalived配置文件

[root@keepalived-master ~]# vim /etc/keepalived/keepalived.conf

global_defs {
   notification_email {
      [email protected]  //接收告警的人
   }
   notification_email_from [email protected]  //发邮件地址
   smtp_server 127.0.0.1    //发邮件地址
   smtp_connect_timeout 30
   router_id LVS_DEVEL
   vrrp_skip_check_adv_addr
   vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

#此处添加一段监控httpd服务的配置
vrrp_script check_httpd {
    script "/usr/local/sbin/check_httpd.sh"
    interval 3              //脚本执行间隔,单位:秒
}

vrrp_instance VI_1 {
    state MASTER            //节点身份
    interface ens33         //对应监听VIP的网卡
    virtual_router_id 51
    priority 100            //权重,master的值要比backup大
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 12345678  //密码
    }
    virtual_ipaddress {
        192.168.117.100     //设置VIP
    }
    #此处添加一段定义监控脚本
    track_script {
        check_httpd
    }
}

4.编写监控httpd服务的脚本,赋予执行权限

[root@keepalived-master ~]# vim /usr/local/sbin/check_httpd.sh
#!/bin/bash

DATE=`date +'%F_%T'`
PROC=`ps -C httpd --no-heading | wc -l`

if [ $PROC -eq "0" ]; then
    systemctl start httpd
    PROC2=`ps -C httpd --no-heading | wc -l`
    if [ $PROC2 -eq "0" ]; then
        echo "$DATE httpd down,keepalived will stop!" >> /var/log/check_httpd.log
        systemctl stop keepalived
    fi
fi

[root@keepalived-master ~]# chmod +x /usr/local/sbin/check_httpd.sh

5.启动httpd服务和keepalived服务,查看是否自动为网卡配置上了VIP

[root@keepalived-master ~]# systemctl start httpd
[root@keepalived-master ~]# systemctl start keepalived
[root@keepalived-master ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:52:42:6f brd ff:ff:ff:ff:ff:ff
    inet 192.168.117.14/24 brd 192.168.117.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet 192.168.117.100/32 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::421a:43b7:1f73:55d9/64 scope link noprefixroute
       valid_lft forever preferred_lft forever

6.进入备节点,编辑keepalived配置文件,与主节点的配置基本相同,备注的几处不同

[root@keepalived-slaver ~]# vim /etc/keepalived/keepalived.conf
global_defs {
   notification_email {
      [email protected]
   }
   notification_email_from [email protected]
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
   vrrp_skip_check_adv_addr
   vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

vrrp_script check_httpd {
    script "/usr/local/sbin/check_httpd.sh"
    interval 3
}

vrrp_instance VI_1 {
    state BACKUP            //节点身份
    interface ens33         //对应监听VIP的网卡
    virtual_router_id 51    
    priority 90             //权重,要比主节点的值低
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 12345678
    }
    virtual_ipaddress {
        192.168.117.100
    }

    track_script {
        check_httpd
    }
}

7.同样需要配置监控脚本

[root@keepalived-slaver ~]# vim /usr/local/sbin/check_httpd.sh
#!/bin/bash

DATE=`date +'%F_%T'`
PROC=`ps -C httpd --no-heading | wc -l`

if [ $PROC -eq "0" ]; then
    systemctl start httpd
    PROC2=`ps -C httpd --no-heading | wc -l`
    if [ $PROC2 -eq "0" ]; then
        echo "$DATE httpd down,keepalived will stop!" >> /var/log/check_httpd.log
        systemctl stop keepalived
    fi
fi

[root@keepalived-slaver ~]# chmod +x /usr/local/sbin/check_httpd.sh

8.启动keepalived服务

[root@keepalived-slaver ~]# systemctl start keepalived

9.测试

测试1:httpd服务自动启动
[root@keepalived-master ~]# systemctl stop httpd  //主节点关闭httpd
[root@keepalived-master ~]# ps -C httpd  //发现很快httpd又启动了
   PID TTY          TIME CMD
23513 ?        00:00:00 httpd
23515 ?        00:00:00 httpd
23516 ?        00:00:00 httpd
23517 ?        00:00:00 httpd
23518 ?        00:00:00 httpd
23519 ?        00:00:00 httpd
测试2:模拟主节点宕机
[root@keepalived-master ~]# systemctl stop keepavlied  //主节点关闭keepalived,模拟宕机
[root@keepalived-slaver ~]# ip a  //进入备节点查看ens33网卡,发现自动配置上了VIP
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:d4:b5:f5 brd ff:ff:ff:ff:ff:ff
    inet 192.168.117.15/24 brd 192.168.117.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet 192.168.117.100/32 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::e075:8f2b:ed7c:ca78/64 scope link noprefixroute
       valid_lft forever preferred_lft forever

配置邮件告警(主备节点都以相同方式配置,以主节点为例)

1.安装sendmail

[root@keepalived-master ~]# yum install -y sendmail* mailx

2.修改邮件配置

[root@keepalived-master ~]# vim /etc/mail.rc  //添加如下配置
set from=[email protected]
set smtp=smtp.qq.com
set smtp-auth-user=[email protected]
set smtp-auth-password=xxxxxxxxxxxxxxxx  //授权码
set smtp-auth=login

3.编写邮件告警脚本

[root@keepalived-master ~]# vim /usr/local/sbin/check_keepalived.sh
#!/bin/bash

EMAIL='[email protected]'
DATE=`date +'%F_%T'`
TITLE="`hostname` is $1 now"
CONTENT="${DATE}: `hostname` to be $1"

if (($1=='master'|'backup')); then
    echo "${CONTENT}" | mail -s "${TITLE}" ${EMAIL}
else
    echo "something error!" | mail -s "${TITLE}" ${EMAIL}
fi

4.修改keepalived配置文件

[root@keepalived-master ~]# vim /etc/keepalived/keepalived.conf  //在vrrp_instance VI_1这段配置尾部加上两行邮件告警配置
    notify_master "/bin/bash /usr/local/sbin/check_keepalived.sh master"
    notify_backup "/bin/bash /usr/local/sbin/check_keepalived.sh backup"

5.重启sendmail服务及keepalived服务

[root@keepalived-master ~]# systemctl restart sendmail
[root@keepalived-master ~]# systemctl restart keepalived

6.关闭主节点测试邮件告警
Keepalived高可用+邮件告警_第1张图片

你可能感兴趣的:(Keepalived高可用+邮件告警)