global_defs {
notification_email {
test@163.com
}
notification_email_from keepalived@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_MASTER
}
vrrp_script chk_http_port {
script "/usr/local/src/check_nginx_pid.sh"
interval 2 #(检测脚本执行的间隔)
weight 2
}
vrrp_instance VI_1 {
state MASTER
interface bond0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_http_port #(调用检测脚本)
}
virtual_ipaddress {
192.168.15.135
}
}
备:
global_defs {
notification_email {
test@163.com
}
notification_email_from keepalived@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_BACKUP
}
vrrp_script chk_http_port {
script "/usr/local/src/check_nginx_pid.sh"
interval 2 #(检测脚本执行的间隔)
weight 2
}
vrrp_instance VI_1 {
state BACKUP
interface bond0
virtual_router_id 51
priority 66
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_http_port #(调用检测脚本)
}
virtual_ipaddress {
192.168.15.135
}
}
以下是针对nginx状态进行检测的脚本,第一次nginx服务死掉时,会重新启动,如果Nginx服务无法正常启动,则杀掉keepalived进程
vim /usr/local/src/check_nginx_pid.sh
#!/bin/bash
A=`ps -C nginx --no-header |wc -l`
if [ $A -eq 0 ];then
/usr/local/nginx/sbin/nginx
if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
killall keepalived
fi
fi
Ok,开始nginx负载均衡测试,停掉其中一台的任何服务,不影响整个系统的运作。
注:两台LBServer也可分别添加一个VIP①②(Keepalived心跳监控,服务不可用或者宕机,VIP①被备LBServer接管),外部使用智能DNS轮询两个VIP①②,提高硬件资源利用率。
更多Nginx相关教程见以下内容:
搭建基于Linux6.3+Nginx1.2+PHP5+MySQL5.5的Web服务器全过程