keepalived nginx 配置

主机配置文件keepalived.conf


! 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 LVS_DEVEL
# vrrp_skip_check_adv_addr
vrrp_garp_interval 0
vrrp_gna_interval 0
}
#vrrp_script checkhaproxy
#{
# script "/etc/keepalived/do_sth.sh"
# interval 5
#}
vrrp_script check_nginx {
script "/etc/keepalived/nginx_check.sh"
interval 2 #监测时间间隔
weight -20 #如果条件城里则权重减20
}


vrrp_instance VI_1 {
state MASTER #设置ECS1实例为主实例
interface eth0 #设置网卡名,本示例配置为eth0
virtual_router_id 51
nopreempt
# preempt_delay 10
priority 89 #设置优先级,数字越大,优先级越高,本示例配置主用实例优先级为100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
unicast_src_ip 172.31.249.221   #设置ECS实例的私网IP地址,本示例配置为192.168.0.209
unicast_peer {
172.31.249.222           #对端ECS实例的私网IP地址,本示例配置为192.168.0.210
}
virtual_ipaddress {
172.31.249.223          #设置HaVip的IP地址,本示例配置为192.168.0.88
}
notify_master "/etc/keepalived/notify_action.sh MASTER"
notify_backup "/etc/keepalived/notify_action.sh BACKUP"
notify_fault "/etc/keepalived/notify_action.sh FAULT"
notify_stop "/etc/keepalived/notify_action.sh STOP"
garp_master_delay 1
garp_master_refresh 5


track_interface {
eth0 #设置ECS实例网卡名,本示例配置为eth0
}
track_script {
check_nginx #vrrp_script
}
}

 

virtual_server 172.31.249.211 80 { ## 虚拟ip和nginx访问端口
delay_loop 6
lb_algo rr
lb_kind NAT
persistence_timeout 5
protocol TCP

real_server 172.31.249.209 80 { ##本机物理主机IP和nginx访问端口
weight 1
notify_down /etc/keepalived/nginx_check.sh ##监测本地nginx进程端口宕掉后,执行脚本,停止keepalived
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80 #监测的NGINX的访问的TCP端口
}
}
}

 

 

从机配置文件keepalived.conf

! 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 LVS_DEVEL
#   vrrp_skip_check_adv_addr
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}
#vrrp_script checkhaproxy
#{
#    script "/etc/keepalived/do_sth.sh"
#    interval 5
#}
vrrp_script check_nginx {
    script "/etc/keepalived/nginx_check.sh"
    interval 2  #监测时间间隔
    weight -20  #如果条件城里则权重减20
}

vrrp_instance VI_1 {
state BACKUP           #设置ECS2实例为备用实例
    interface eth0          #设置网卡名,本示例配置为eth0
    virtual_router_id 51
    nopreempt
#    preempt_delay 10
    priority 90             #设置优先级,数字越大,优先级越高,本示例配置备用实例优先级为10
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    unicast_src_ip 172.31.249.210   #设置ECS实例的私网IP地址,本示例配置为192.168.0.210
    unicast_peer {
        172.31.249.209           #对端ECS实例的私网IP地址,本示例配置为192.168.0.209
    }
    virtual_ipaddress {
        172.31.249.211          #设置HaVip的IP地址,本示例配置为192.168.0.88
    }
    notify_master "/etc/keepalived/notify_action.sh MASTER"
    notify_backup "/etc/keepalived/notify_action.sh BACKUP"
    notify_fault "/etc/keepalived/notify_action.sh FAULT"
    notify_stop "/etc/keepalived/notify_action.sh STOP"
    garp_master_delay 1
    garp_master_refresh 5

        track_interface {
                eth0                #设置ECS实例网卡名,本示例配置为eth0
        }
    track_script {
        check_nginx   #vrrp_script
    }
}


virtual_server 172.31.249.211 80 { ## 虚拟ip和nginx访问端口
delay_loop 6
lb_algo rr
lb_kind NAT
persistence_timeout 5
protocol TCP


real_server 172.31.249.209 80 { ##本机物理主机IP和nginx访问端口
weight 1
notify_down /etc/keepalived/nginx_check.sh ##监测本地nginx进程端口宕掉后,执行脚本,停止keepalived
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80 #监测的NGINX的访问的TCP端口
}
}
}

 

 

 Nginx运行状态监视脚本  nginx_check.sh  

#keepalived  nginx
#!/bin/sh
  
A=`ps -C nginx --no-header |wc -l`
if [ $A -eq 0 ];then
        /www/server/nginx/sbin/nginx
        if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
                exit 1
        else
                exit 0
        fi
else
        exit 0
fi

 

posted on 2022-09-29 16:14  Ben丶大壮  阅读(359)  评论(0编辑  收藏  举报

导航