Keepalived+Nginx 高可用集群(主从模式)
准备工作
-
需要两台服务器 192.168.17.129 和 192.168.17.131
-
在两台服务器安装 nginx
检测
-
在两台服务器安装 keepalived
#使用 yum 命令进行安装 yum install keepalived –y
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-wXYvm236-1623572025562)(F:\Nginx.assets\image-20210613153353688.png)]
配置
keepalived.conf
global_defs { notification_email {
acassen@firewall.loc failover@firewall.loc sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc smtp_server 192.168.17.129
smtp_connect_timeout 30 router_id LVS_DEVEL
}
vrrp_script chk_http_port {
script "/usr/local/src/nginx_check.sh" interval 2 #(检测脚本执行的间隔)
weight 2
}
vrrp_instance VI_1 {
state BACKUP # 备份服务器上将 MASTER 改为 BACKUP
interface ens33 //网卡
virtual_router_id 51 # 主、备机的 virtual_router_id 必须相同
priority 90 # 主、备机取不同的优先级,主机值较大,备份机值较小
advert_int 1
authentication {
auth_type PASS auth_pass 1111
}
virtual_ipaddress {
192.168.17.50 // VRRP H 虚拟地址
}
}
检测脚本
在/usr/local/src /nginx_check.sh添加
#!/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
测试
把两台服务器上 nginx 和 keepalived 启动
#启动 nginx:
./nginx
启动 keepalived:
#systemctl start keepalived.service
在浏览器地址栏输入 虚拟 ip 地址 192.168.17.50
把主服务器(192.168.17.129)nginx 和 keepalived 停止,再输入 192.168.17.50
Keepalived+Nginx 高可用集群(双主模式)
keepalived.conf
[root@LB-01 ~]# vim /etc/keepalived/keepalived.conf //编辑配置文件,增加一段新的vrrp_instance 规则
! Configuration File for keepalived
global_defs { notification_email { 381347268@qq.com
}
smtp_server 192.168.200.1
smtp_connect_timeout 30 router_id LVS_DEVEL
}
vrrp_instance VI_1 { state MASTER interface ens33 virtual_router_id 51
priority 150
advert_int 1 authentication {
auth_type PASS auth_pass 1111
}
virtual_ipaddress {
192.168.1.110/24 dev ens33 label ens33:1
}
}
#
vrrp_instance VI_2 { state BACKUP interface ens33 virtual_router_id 52
priority 100
advert_int 1 authentication { auth_type PASS auth_pass 2222
}
virtual_ipaddress {
192.168.1.210/24 dev ens33 label ens33:2
}
}
节点,同样还是正常访问;到此,keepalived+nginx 高可用集群(双主模式)就搭建完成了。