Nginx+Keepalive实现VIP
主机备机安装配置方式基本一样,部分配置需要修改,已在文中备注
1-安装,并设置开机自启
yum install -y nginx keepalived
systemctl start nginx.service
systemctl start keepalived.service
2-创建nginx检测脚本
cat >> /usr/bin/check_nginx_alive.sh << EOF
#!/bin/sh
PATH=/bin:/sbin:/user/bin:/user/sbin
A=`ps -C nginx --no-header|wc -l`
if [ $A -eq 0 ]
then
echo "nginx server is died"
killall keepalived
fi
EOF
chmod 777 /usr/bin/check_nginx_alive.sh
3-修改keepalived配置(注意网卡名)
mv /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak
cat >> /etc/keepalived/keepalived.conf << EOF
! Configuration File for keepalived
global_defs {
router_id LVS_DEVEL
}
vrrp_script check_nginx_alive {
script "/usr/bin/check_nginx_alive.sh"
interval 3
weight -10
}
global_defs {
## 设置router_id
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER
interface ens33 # ifconfig查看网卡名
virtual_router_id 51 # VRRP 路由 ID实例,每个实例是唯一的
priority 100 # 优先级,备服务器设置 90
advert_int 1 # 指定VRRP 心跳包通告间隔时间,默认1秒
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.0.20
}
track_script {
check_nginx_alive
}
}
EOF
3-重启服务,并查看ip是否成功
systemctl restart nginx keepalived
ip a
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/a126779ec13eb36a4166067d58eb9cd1.png)