配置文件
keepalived只有一个配置文件keepalived.conf,里面主要包括以下几个配置区域,分别是global_defs、static_ipaddress、
vrrp_script、vrrp_instance和virtual_server.
1、global_defs区域
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id 192.168.224.206
vrrp_skip_check_adv_addr
vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
notification_email 故障发生时给谁发邮件通知
notification_email_from 通知邮件从哪个地址发出
smtp_server 通知邮件的smtp地址
smtp_connect_timeout 连接smtp服务器的超时时间
enable_traps开启SNMP(Simple Network Management Protocol)陷阱
router_id 标志本节点的字符串,通常为ip地址,故障发生时邮件会通知到 必须填写
2、vrrp_script区域
用来做健康检查的,当检查失败时会将vrrp_instance的priority减少相应的值,
vrrp_script chk_nginx {
script "/usr/local/keepalived-1.3.4/nginx_check.sh"
interval 2
weight -20
}
script:自己写的监测脚本。
interval 2:每2s监测一次
weight -20:监测失败,则相应的vrrp_instance的优先级会减少20个点
3、vrrp_instance
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 51
mcast_src_ip 192.168.224.206
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.224.208
}
track_script{
chk_nginx
}
}
state:只有BACKUP和MASTER。MASTER为工作状态,BACKUP是备用状态
interface:为网卡接口:可通过ip addr查看自己的网卡接口
virtual_router_id:虚拟路由标志。同组的virtual_router_id应该保持一致。它将决定多播的MAC地址。
priority:设置本节点的优先级,优先级高的为master
advert_int:MASTER与BACKUP同步检查的时间间隔
virtual_ipaddress:这就是传说中的虚拟ip
[root@server1 ~]# cat /usr/local/sbin/check_ng.sh
#!/bin/bash
#时间变量,用于记录日志
d=`date --date today +%Y%m%d_%H:%M:%S`
#计算nginx进程数量
n=`ps -C nginx --no-heading|wc -l`
#如果进程为0,则启动nginx,并且再次检测nginx进程数量,
#如果还为0,说明nginx无法启动,此时需要关闭keepalived
if [ $n -eq "0" ]; then
/etc/init.d/nginx start
n2=`ps -C nginx --no-heading|wc -l`
if [ $n2 -eq "0" ]; then
echo "$d nginx down,keepalived will stop" >> /var/log/check_ng.log
# systemctl stop keepalived
/etc/init.d/keepalived stop
fi
fi