需要准备两台虚拟服务器
1.准备两台nginx服务器. 172.16.7.221 172.16.7.220 必须安装了nginx
不会安装nginx的可以查看下面的链接
2. 需要在每一个nginx服务器上安装keepalived
yum install keepalived ----安装keepalived
rpm -q -a keepalived ------- 检查是否安装keepalived
keepalived默认安装路径: /etc/keepalived
3.进入keepalived安装路径
cd /etc/keepalived
4.修改keepalived的配置文件内容
vi keepalived.conf
以下配置需要进行修改!!!!!!
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_ server 172.16.7.220
smtp_connect_timeout 30
router_id 172.16.7.220
}vrrp_script chk_http_ port {
script "/usr/local/src/nginx_check.sh"
interval 2
weight 2
}vrrp_instance VI_1 {
state BUCKUP
interface ens33
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth type PASS
auth pass 1111
}
virtual_ipaddress {
172.16.7.50
}
}
5.nginx_check.sh脚本文件
#!/bin/bash
# 检查是否开启nginx
A=`ps -C nginx --no-header |wc -l`
if [ $A -eq 0 ];then
# 开启nginx nginx脚本所在的路径
/usr/soft/nginx/sbin/nginx -----这个需要根据自己的路径进行修改
sleep 2
# 开启nginx失败 则执行关闭keepalived
if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
pkill -9 keepalived
fi
fi
分别在两台服务器上放入指定目录 ------ 分别启动---keepalived
systemctl start keepalived.service
开启两台nginx服务
访问虚拟ip
关闭主节点nginx:
/usr/soft/nginx/sbin/nginx -s stop
systemctl stop keepalived.service
之后进行测试就可以发现会自动使用备用服务器