- 安装nginx、keepalived nginx安装 keepalived安装与添加服务
- 在/etc/keepalived目录下新建nginx_check.sh(两台服务器都需要)
配置keepalived.conf:
#配置邮箱
global_defs {notification_email {# acassen@firewall.loc# failover@firewall.loc# sysadmin@firewall.locroot@localhost}#notification_email_from Alexandre.Cassen@firewall.locnotification_email_from root@localhost#smtp_server 192.168.200.1smtp_server localhostsmtp_connect_timeout 30router_id NodeA
}
#检查nginx状态
vrrp_script check_localhost {sicript "/etc/keepalived/nginx_check.sh"interval 2 #每两秒检测另一个nginx状态weight -20 #降低权重
}
vrrp_instance VI_1 {state MASTER #设为主,如果是从的话设置成BACKUPinterface eth0virtual_router_id 51priority 100 #如果是从的话可以设置成比100小的:90advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {192.168.11.16}track_script {check_localhost #调用方法}
}
...其他不变
配置nginx_check.sh
#!/bin/bash
if [ "$(ps -ef | grep "nginx: master process"| grep -v grep )" == "" ]
then
/usr/local/nginx/sbin/nginx
sleep 5
if [ "$(ps -ef | grep "nginx: master process"| grep -v grep )" == "" ]
then
killall keepalived
fi
fi
启动keepalived、nginx,通过ip addr指令查看inet中有vip(虚拟ip)而从服务器没有虚拟ip,当关闭主keepalived时,从inet中出现inet说明配置成功,也可以修改nginx中的html下的index.html主里面加1从里面加2,通过访问配置的虚拟ip来访问页面,如果两个服务都开启的话显示的是
都关闭主keepalived时候(service keepalived stop)显示
如上说明配置成功,然后重启服务器reboot,keepalived开机自启(chkconfig keepalived on)之后就可以保证每两秒心跳检测一次