Master配置文件 注意iptables防火墙(见上个随笔)和健康检查脚本
keepalived 只是高可用,如果keepalived宕机后,ip会飘到备份主机,但是如果nginx掉了,keepalived是不会stop掉的,所以需要脚本做健康检查,代码如下:
! Configuration File for keepalived
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 web1}vrrp_script check_nginx {
script "/etc/keepalived/check_nginx.sh" interval 2 weight -5 fall 2 rise 1}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.120 }track_script {
check_nginx }}# virtual_router_id 51必须一致
脚本代码
#!/bin/bash A=$(ss -aunpt | grep nginx | wc -l) if [ $A -eq 0 ] ; then /usr/bin/kill -15 `cat /var/run/keepalived.pid` echo -e "keeplived is stoped" else exit 0 fi
##注意脚本要加执行权限+x vrrp_script
vrrp_script
interval 2 : 每2s检测一次 weight -5 : 检测失败(脚本返回非0)则优先级 -5 fall 2 : 检测连续 2 次失败才算确定是真失败。会用weight减少优先级(1-255之间) rise 1 : 检测 1 次成功就算成功。但不修改优先级
注意关闭的keepalived关闭后,要起来,需要先启nginx,然后启keepalived
由于本人只有两台高可用,就不写备份机的代码和脚本了。