准备四台配有静态IP地址的虚拟机
nginx-01 192.168.20.11
nginx-02 192.168.20.12
tomcat-01 192.168.20.21
tomcat-02 192.168.20.22
tomcat-03 192.168.20.23
配置:
防火墙开放8080端口
firewall-cmd --permanent --add-port=8080/tcp
firewall-cmd --reload
代理java工程
cd /usr/local/tomcat/webapps
rm -rf *
# 把对应的war包放入该目录中
设置EasyBuy项目默认访问路径
vim /usr/local/tomcat/conf/server.xml
<Context path="" docBase="EasyBuy" reloadable="true"/>
开启tomcat服务
systemctl start tomcat
结果:
浏览器访问192.168.20.21:8080 易买网-服务器一
浏览器访问192.168.20.22:8080 易买网-服务器二
浏览器访问192.168.20.23:8080 易买网-服务器三
配置:
防火墙开放8080端口
firewall-cmd --permanent --add-port=8080/tcp
firewall-cmd --reload
监听8080端口
vim /usr/local/nginx/conf/nginx.conf
把80端口改为8080端口
listen 8080;
配置nginx反向代理
vim /usr/local/nginx/conf/nginx.conf
设置服务器集群
upstream tomcat{
server 192.168.20.21:8080 weight=6;
server 192.168.20.22:8080 weight=3;
server 192.168.20.23:8080 weight=1;
}
指定代理到服务器集合
server{
.....
location / {
proxy_pass http://tomcat;
}
.....
}
结果:
浏览器访问192.168.20.11:8080 随机落在一台服务器上
浏览器访问192.168.20.12:8080 随机落在一台服务器上
配置:
在两台nginx服务器上安装keepalived
yum -y install keepalived
设置主备服务器
vim /etc/keepalived/keepalived.conf
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.20.100
}
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.20.100
}
}
设置跟踪策略,并用keepalived监听nginx
# 在vrrp_instance VI_1上面
vrrp_script check_nginx {
script "/etc/keepalived/check_nginx.sh"
interval 1
weight -20
}
# 在密码设置下面的位置
track_script {
check_nginx
}
删除这两行
13 vrrp_skip_check_adv_addr
14 vrrp_strict
防火墙开启vrrp(解决出现主备都获取到虚拟IP的情况)
keepalived是基于vrrp做虚拟IP漂移的,不开启的话,主备均会认为对方down掉了,会造成主备都能获取到虚拟IP
firewall-cmd --add-rich-rule='rule protocol value="vrrp" accept' --permanent
firewall-cmd --reload