1、 配置yum源,关闭防火墙,selinux
[root@localhost ~]#yum install epel-release -y
[root@localhost ~]#yum -y install keepalived
2、关闭selinux
[root@localhost ~]#getenforce(查看selinux状态)
[root@localhost ~]#setenforce 0(临时修改SELinux 成为permissive模式)
[root@localhost ~]#vim /etc/sysconfig/selinux
将SELINUX=enforcing改为SELINUX=disabled
3、关闭防火墙或者开放端口
https://blog.csdn.net/weixin_34274029/article/details/92722173
4、修改配置文件
[root@localhost ~]#vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
script_user root
}
vrrp_script chk_nginx {
script "/usr/local/nginx/nginx_notify.sh"
interval 2
weight 21
user root
}
vrrp_instance VI_1 {
state MASTER(从机调整为BACKUP)
interface eth0
virtual_router_id 51 (主从的id值必须为一样)
priority 100 (从机的值小于主机的值,可调为80)
unicast_src_ip 192.168.1.11 (master IP,从机上换成backup IP)
unicast_peer {
192.168.1.12(backup IP,从机上换成master IP)
}
advert_int 1
authentication {
auth_type PASS
auth_pass 123456 (主从master一样)
}
virtual_ipaddress {
192.168.1.13/24 brd 192.168.1.255 dev eth0 label eth0:1 (虚拟IP)
}
notify_master "/usr/local/nginx/start_nginx.sh"
track_interface {
eth0
}
track_script {
chk_nginx
}
}
5、编写检测nginx进程脚本
[root@localhost ~]#vim /usr/local/nginx/nginx_notify.sh
#!/bin/bash
/usr/bin/killall -0 nginx
6、编写启动脚本
[root@localhost ~]#vim /usr/local/nginx/start_nginx.sh
#!/bin/bash
killall -0 nginx
if [ $? == 0 ] ; then
/usr/local/nginx/sbin/nginx -s reload && echo "nginx reload success!"
else
/usr/local/nginx/sbin/nginx
fi
7、执行以下命令
[root@localhost ~]#chmod +x /usr/local/nginx/nginx_notify.sh
[root@localhost ~]#chmod +x /usr/local/nginx/start_nginx.sh
[root@localhost ~]#yum -y install psmisc.x86_64
[root@localhost ~]#systemctl start keepalived.service
8、主机上停掉nginx,验证VIP会不会漂移