1. 介绍
2. 准备工作
(1)两台服务器
(2)在两台服务器安装nginx
(3)在两台服务器安装keepalived
yum install keepalived -y
安装之后,在etc里面生成目录keepalived,有文件keepalived.conf
3.完成高可用配置
(1)修改主服务器keepalived.conf
vrrp_script chk_http_port {
script "/usr/local/src/nginx_check.sh"
interval 2 #(检测脚本的执行间隔)
weight 2
}
vrrp_instance VI_1 {
state MASTER # 如果是备份服务器的话将MASTER改为BACKUP
interface enp0s3 # 网卡,通过ifconfig获取
virtual_router_id 51 #主、备机的virtual_router_id必须相同
priority 100 #主、备机取不同的优先级,主机值较大,备份机值较小
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.168.200.16 # 虚拟IP地址
}
}
(2)修改从服务器keepalived.conf
(3)在/usr/local/src添加检测脚本
#!/bin/bash
A='ps -C nginx -no-header |wc -l'
if [ $A -eq 0 ];then
/usr/local/nginx/sbin/nginx
sleep 2
if [ '\ps -C nginx --no-header |wc -l' -eq 0 ];then
killall keepalived
fi
fi
(4)把两台服务器的nginx和keepalived启启来
启动nginx ./nginx
启动keepalived systemctl start keepalived.service
4. 最终测试
访问虚拟IP
将主服务器停掉以后访问虚拟IP