负载均衡+反向代理+高可用(nginx+keepalived+tomcat)
负载均衡+反向代理+高可用(nginx+keepalived+tomcat)********
环境
环境:虚拟机
ip:10.0.1.0
子网掩码:255.255.255.0
网关:10.0.1.2
1.tomcat部署(1/2)
#步骤
nginx1:10.0.1.139
tomcat1:10.0.1.130
tomcat2: 10.0.1.135
1.上传jdk源码 解压源码
cd /data
tar zxvf jdk-8u161-linux-x64.tar.gz
mv jdk1.8.0_161/ jdk1.8
2.添加环境变量
vi /etc/profile
在末尾加入如下代码
JAVA_HOME=/data/jdk1.8
JRE_HOME=/data/jdk1.8
PATH=$JAVA_HOME/bin:$PATH
CLASSPATH=$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export JAVA_HOME
export PATH
export CLASSPATH
3.生效环境变量-检查是否安装成功
source /etc/profile
java -version
4.上传tamcat源码-解压源码
cd /data
rz
tar zxvf apache-tomcat-8.5.43.tar.gz
mv apache-tomcat-8.5.43 tomcat
5.启动tomcat
cd /data/tomcat/bin
./startup.sh
6.发布war包(一般是都是开发会给你包的,你直接解压到发布目录就可以了)
cd /data/tomcat/webapps/ROOT
rz
unzip jpress-web-newest.war
7.查看实时日志(将来可以排错)
cd /data/tomcat/logs
tail -100 catalina.out 看后100行
tail -f catalina.out 看实时日志
8.查看测试页面
10.0.1.130:8080
10.0.1.135:8080
2.编译nginx
1.安装依赖包
yum -y install pcre-devel openssl-devel zlib-devel gcc
groupadd nginx
useradd -g nginx -s /sbin/nologin nginx
2.切换到解压后的nginx目录中执行:
cd /usr/local/src
wget http://nginx.org/download/nginx-1.18.0.tar.gz
tar zxvf nginx-1.18.0.tar.gz
cd nginx-1.18.0.tar.gz/
3.编译nginx
./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module
make
make install
ln -s /usr/local/nginx/sbin/nginx /bin/nginx
启动nginx:/usr/local/nginx/sbin/nginx
关闭nginx:/usr/local/nginx/sbin/nginx -s stop
检查nginx 是否异常:/usr/local/nginx/sbin/nginx -t
重启nginx: nginx -s reload
每次都需要输入路径才可以重启 检查等操作比较繁琐
用软连接定义
ln -s /usr/local/nginx/sbin/nginx /bin/nginx
这样就可以了
启动 : nginx
停止:nginx -t stop
重启 : nginx -s reload
检查: nginx -t
4.测试
10.0.1.139
3.负载均衡(反向代理)(nginx)
#步骤
负载均衡配置模板
upstream tomcat {
server 10.0.1.139:8080;
server 10.0.1.130:8080;
}
server {
listen 80;
server_name www.yunwei.com;
location / {
proxy_pass http://tomcat;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_next_upstream error timeout http_404 http_502 http_403;
}
}
1.配置主nginx
cd /usr/local/nginx/conf
mkdir vhost
vi nginx.conf
#tips:这是nginx主配置文件,生产环境中可能nginx上,不止一个网站,主配置文件里面一个网站,在创建一个子配置文件,不过这里需要再主配置文件里面引用子配置文件,不然不会生效的,注意这里的端口号,也不可以一样,如果主配置文件里,有一个网站,他的端口是80 ,那么新建的tomcat配置文件里,就不能与他冲突,必须错开
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 81;
server_name www.it.com;
location / {
root /html/www;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
include /usr/local/nginx/conf/vhost/*.conf; #主要添加的就是这里,这里如果你只有一个网站,不需要配置,如果你要配置多个tomcat的如负载均衡,就必须创建一个子配置文件夹,在文件夹里,创建配置文件,在主配置文件里,引用一下子配置文件
}
mkdir vhost
cd vhost/
2.添加负载均衡模块配置
cd vhost/
vi lvs.conf
upstream tomcat_backend {
server 10.0.1.130:8080; # Tomcat服务器的IP地址和端口
server 10.0.1.139:8080;
}
server {
listen 80; # Nginx监听的端口,可根据需求更改
server_name yourdomain.com; # 替换为实际的域名或使用localhost进行本地测试
location / {
proxy_pass http://tomcat_backend; # 请求将被转发至Tomcat服务器
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_read_timeout 300s;
proxy_connect_timeout 300s;
}
}
3.写入测试页面内容(nginx+tomcat)
tomcat1
cd /data/tomcat1/webapps/ROOT
rm -rf *
echo 1 > index.jsp
tomcat2
cd /data/tomcat1/webapps/ROOT
rm -rf *
echo 2 > index.jsp
#tips:这里你有多少tomcat实例,就要写入多个tomcat的内容,来进行测试
4.测试
http://10.0.1.139/
#tips :如果页面打开时,跳转内容是1 2 1 2 代表成功
4.keepalive故障转移
nginx+php/tomcat动静分离架构配置
为什么要动静分离
实现效果:nginx处理静态页面,php处理动态页面
nginx动静分离配置文件
upstream www{
server 127.0.0.1:88;
keepalive 64;
}
server {
listen 80;
server_name localhost;
location ~ .*\.(html|gif|jpg|jpeg|png|js|css|bmp|bmp|ico|htm)$
{ root /data/web;
}
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_buffering off;
proxy_pass http://www;
}
location ~ .*\.(php|jsp|cgi)$ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Nginx-Proxy true;
proxy_set_header Connection "";
proxy_pass http://www;
}
}
环境准备:
nginx1:10.0.1.139
nginx2:10.0.1.136
tomcat1:10.0.1.130
tomcat2:10.0.1.135
#步骤
1.安装二台nginx和二台tomcat
2.添加测试页面(tomcat1+tomcat2)
tomcat1:10.0.1.130
tomcat2:10.0.1.135
tomcat1:
cd /data/tomcat1/webapps/ROOT
rm -rf *
echo zhangsan > index.jsp
tomcat2:
cd /data/tomcat1/webapps/ROOT
rm -rf *
echo lisi > index.jsp
3.配置负载均衡
nginx1:10.0.1.139
nginx2:10.0.1.136
nginx1:
cd /usr/local/nginx/conf/vhost
vim lvs.conf
upstream tomcat_backend {
server 10.0.1.130:8080; # Tomcat服务器的IP地址和端口
server 10.0.1.135:8080;
}
server {
listen 80; # Nginx监听的端口,可根据需求更改
server_name yourdomain.com; # 替换为实际的域名或使用localhost进行本地测试
location / {
proxy_pass http://tomcat_backend; # 请求将被转发至Tomcat服务器
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_read_timeout 300s;
proxy_connect_timeout 300s;
}
}
nginx -t
nginx -s reload
http://10.0.1.139/
nginx2:10.0.1.136
scp -r -P 22 /usr/local/nginx/conf/ root@10.0.1.136:/usr/local/nginx/
#tips :这里我为了省事,直接拷贝过去了,当然你也可以重新新建lvs.conf配置文件
nginx -t
nginx -s reload
http://10.0.1.136/
4.在二台nginx上安装keepalived软件
yum -y install keepalived
systemctl start keepalived.service
ps aux |grep keepalived
5.配置keepalived
第一台nginx
cd /etc/keepalived/
vim keepalived.conf
! Configuration File for keepalived
vrrp_script check_nginx {
script "/etc/keepalived/check_nginx.sh"
interval 2
weight 2
}
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL2
}
vrrp_instance VI_1 {
state MASTER
interface eth0 #网卡名别填错了,你自己的网卡名
virtual_router_id 51
priority 100 #设置一下权重
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.1.100/24 dev eth0 #设置虚拟ip
}
track_script {
check_nginx
}
}
systemctl restart keepalived
systemctl status keepalived
ip a
第二台nginx
cd /etc/keepalived/
vim keepalived.conf
! Configuration File for keepalived
vrrp_script check_nginx {
script "/etc/keepalived/check_nginx.sh"
interval 2
weight 2
}
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL2
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.1.100/24 dev eth0
}
track_script {
check_nginx
}
}
systemctl restart keepalived
systemctl status keepalived
ip a
访问虚拟ip
10.0.1.100
#tips:此时用户访问的这个虚拟ip,显示的是tomcat1和Tomcat2上的内容,但是我们又不想让他知道我们的真正ip,因此在nginx上做了个,虚拟ip映射了一下,提高安全性,不过主要的功能是故障切换,实现ip漂移
6.测试 -把nginx1 关机,看还能不能访问虚拟ip10.0.1.100 ,还有没有内容,keepalived能不能实现ip漂移
7.编写心跳脚本
tips:如果nginx死了,还能不能实现故障切换?
nginx1
cd /etc/keepalived/
vim check_nginx.sh
#!/bin/bash
nginxpid=`ps -C nginx --no-header | wc -l`
if [ $nginxpid -eq 0 ];then
systemctl stop keepalived
pkill -9 keepalived
Fi
chmod +x check_nginx.sh
nginx2:
#tips:直接对拷过去
scp check_nginx.sh 10.0.1.136:/etc/keepalived/
systemctl restart keepalived
8.测试nginx 挂了,可不可以实现自动漂移
nginx1
ip a
nginx -s stop
nginx2
ip a
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南