ARTICLE DETAIL

建站实战干货

来自一线的建站与推广经验沉淀,每一条都经过真实交付验证。

LVS + Keepalived + Nginx 高可用集群部署 项目实战(后端使用(Spring Boot 3.3 + Java 17)

2026/8/6 18:49:04 拓冰建站 浏览量
LVS + Keepalived + Nginx 高可用集群部署 项目实战(后端使用(Spring Boot 3.3 + Java 17) 效果图展示内网软件商店 (Software Store)企业内网 B/S 架构软件包管理系统支持 x86 / ARM 双架构。目录结构software-store/ ├── backend/ # 后端源码 (Spring Boot 3.3 Java 17) │ ├── pom.xml # Maven 构建文件 │ ├── build.gradle # Gradle 构建文件备选 │ └── src/main/ │ ├── java/com/softstore/ │ │ ├── SoftStoreApplication.java # 启动入口 │ │ ├── config/ │ │ │ ├── WebConfig.java # Web MVC 配置 拦截器注册 │ │ │ └── AuthInterceptor.java # 认证拦截器 │ │ ├── controller/ │ │ │ ├── AuthController.java # 登录/登出/改密码 │ │ │ ├── PackageController.java # 软件包 CRUD 下载 │ │ │ └── RequestController.java # 需求提交/管理 │ │ ├── model/ │ │ │ ├── SoftwarePackage.java # 软件包实体 │ │ │ └── SoftwareRequest.java # 需求实体 │ │ ├── repository/ │ │ │ ├── PackageRepository.java │ │ │ └── RequestRepository.java │ │ └── service/ │ │ ├── AdminConfigService.java # 管理员配置持久化 │ │ └── PackageService.java # 软件包业务逻辑 │ └── resources/ │ └── application.yml # 默认配置内置 JAR ├── frontend/ # 前端源码 (Vue 3 Element Plus) │ ├── package.json │ ├── vite.config.js │ ├── index.html │ └── src/ │ ├── main.js # 入口 │ ├── App.vue # 根组件 │ ├── api/index.js # API 封装axios │ ├── router/index.js # 路由 守卫 │ └── views/ │ ├── StoreFront.vue # 商店首页 │ ├── Login.vue # 登录页 │ └── AdminPanel.vue # 管理后台 ├── db/ # 数据库脚本 │ ├── init.sql # 建库 建表 测试数据 │ └── add_table.sql # 新增需求表单独执行 ├── deploy/ # 部署配置文件 │ ├── nginx-softstore.conf # Nginx 配置模板 │ ├── softstore.service # systemd 服务文件 │ └── setup.sh # 一键部署脚本 └── README.md # 本文件一、环境要求组件版本要求JDK17Maven3.8推荐 3.9.xNode.js18Nginx1.20MySQL8.0二、构建步骤2.1 构建前端cd frontend npm install npm run build产物在frontend/dist/目录。2.2 构建后端cd backend mvn clean package -DskipTests产物在backend/target/softstore.jar约 47MB。三、部署步骤3.1 部署目录结构服务器路径/opt/software-store/ ├── webapp/ # 前端静态文件dist/ 内容拷贝过来 ├── backend/ │ ├── softstore.jar # 后端 JAR │ ├── application.yml # 外部配置文件优先于 JAR 内置 │ └── admin-config.json # 管理员密码自动生成 ├── db/ │ └── init.sql # 数据库初始化脚本 ├── nginx-softstore.conf # Nginx 配置 ├── softstore.service # systemd 服务 └── scripts/ └── setup.sh # 部署脚本 ​ /data/packages/ # 上传的软件包文件存储目录3.2 初始化数据库mysql -u root -p # 输入密码后执行 source /path/to/db/init.sql3.3 部署前端cp -r frontend/dist/* /opt/software-store/webapp/3.4 部署后端cp backend/target/softstore.jar /opt/software-store/backend/3.5 配置外部 application.yml创建/opt/software-store/backend/application.ymlserver: port: 8080 ​ spring: datasource: url: jdbc:mysql://127.0.0.1:3306/software_store?useSSLfalseallowPublicKeyRetrievaltrueserverTimezoneAsia/ShanghaicharacterEncodingUTF-8 username: root password: yangHAO0902.. driver-class-name: com.mysql.cj.jdbc.Driver ​ jpa: hibernate: ddl-auto: update show-sql: false properties: hibernate: dialect: org.hibernate.dialect.MySQLDialect format_sql: true ​ servlet: multipart: max-file-size: 1024MB max-request-size: 1024MB ​ softstore: upload: dir: /data/packages admin: username: admin password: admin1233.6 配置 NginxNginx 配置/etc/nginx/conf.d/softstore.conf或/usr/local/nginx/conf/nginx.confserver { listen 80; server_name _; ​ root /opt/software-store/webapp; index index.html; ​ # Vue SPA 前端路由 location / { try_files $uri $uri/ /index.html; } ​ # 后端 API 反向代理 location /api/ { proxy_pass http://127.0.0.1:8080; 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_set_header X-Forwarded-Proto $scheme; proxy_read_timeout 300s; proxy_request_buffering off; proxy_buffering off; } ​ # 大文件上传限制 client_max_body_size 1024M; server_tokens off; }3.7 配置 systemd 服务创建/etc/systemd/system/softstore.service[Unit] Description内网软件商店后端服务 Afternetwork.target mysqld.service ​ [Service] Typesimple Userroot WorkingDirectory/opt/software-store/backend ExecStart/usr/bin/java -jar /opt/software-store/backend/softstore.jar Restarton-failure RestartSec5 LimitNOFILE65536 ​ [Install] WantedBymulti-user.targetsystemctl daemon-reload systemctl enable softstore systemctl start softstore3.8 验证# 后端健康检查 curl http://127.0.0.1:8080/api/packages/health # 返回: {service:Software Store Backend,status:UP} ​ # 软件包列表 curl http://127.0.0.1:8080/api/packages # 返回 JSON 数组 ​ # 前端页面 curl http://127.0.0.1/ # 返回 HTML四、Nginx 配置详解Nginx 找到的两种情况情况 1通过 yum/dnf 安装# 配置目录 /etc/nginx/conf.d/softstore.conf # 重启 systemctl reload nginx情况 2手动编译安装如 /usr/local/nginx# 直接编辑主配置文件 vi /usr/local/nginx/conf/nginx.conf # 重启 /usr/local/nginx/sbin/nginx -s reload完整 Nginx 配置参考见deploy/nginx-softstore.conf五、更换 MySQL 服务器如果 MySQL 地址变更为192.168.113.77按以下步骤操作5.1 修改后端配置文件编辑/opt/software-store/backend/application.yml找到spring.datasource.urlspring: datasource: # 把 127.0.0.1 改为 192.168.113.77 url: jdbc:mysql://192.168.113.77:3306/software_store?useSSLfalseallowPublicKeyRetrievaltrueserverTimezoneAsia/ShanghaicharacterEncodingUTF-8 username: root password: yangHAO0902..如果新 MySQL 的密码不同一并修改password字段。5.2 重启后端服务systemctl restart softstore5.3 新 MySQL 服务器初始化在新 MySQL 服务器上执行建库脚本mysql -u root -p db/init.sql5.4 验证连接# 查看后端日志确认连接成功 journalctl -u softstore -n 50 --no-pager | grep -i mysql\|datasource\|hikari ​ # 健康检查 curl http://127.0.0.1:8080/api/packages/health5.5 防火墙放行确保 MySQL 服务器允许远程连接# MySQL 服务器上执行 mysql -u root -p -e CREATE USER IF NOT EXISTS root192.168.113.% IDENTIFIED BY yangHAO0902..;GRANT ALL PRIVILEGES ON software_store.* TO root192.168.113.%; FLUSH PRIVILEGES;新服务器防火墙放行 3306 端口firewall-cmd --add-port3306/tcp --permanent firewall-cmd --reload六、日常管理查看日志# 后端日志 journalctl -u softstore -f ​ # Nginx 日志 tail -f /var/log/nginx/access.log修改管理员密码方式一后台 UI 修改登录管理后台 → 点击用户名旁的「修改密码」→ 输入旧密码/新密码方式二直接编辑配置文件vi /opt/software-store/backend/admin-config.json内容格式{ username: admin, password: admin123 }修改后重启生效systemctl restart softstore重新部署# 停止服务 systemctl stop softstore ​ # 替换 JAR cp new-softstore.jar /opt/software-store/backend/softstore.jar ​ # 替换前端 rm -rf /opt/software-store/webapp/* cp -r dist/* /opt/software-store/webapp/ ​ # 启动服务 systemctl start softstore备份数据库mysqldump -u root -p software_store /opt/software-store-backup.sql七、默认账号项目值管理后台 URLhttp://服务器IP/admin登录页 URLhttp://服务器IP/login默认用户名admin默认密码admin123八、技术栈前端: Vue 3 Element Plus Axios Vite 5后端: Spring Boot 3.3 Spring Data JPA Hibernate数据库: MySQL 8.4服务器: Nginx 1.24 JDK 17 Maven 3.8部署: systemd 服务自动重启LVS Keepalived Nginx 高可用集群部署一、项目概述本次基于openEuler 24.03 LTS SP3系统搭建了一套LVS Keepalived Nginx 高可用负载均衡集群。采用Keepalived VRRP协议实现LVS负载均衡节点高可用LVS DR模式实现四层高性能负载均衡Nginx双Real Server提供Web服务实现业务节点冗余最终实现LVS节点故障自动切换Web节点负载均衡单节点故障业务不中断二、集群架构服务器规划角色IP地址服务LVS-Master192.168.113.77Keepalived Master LVSLVS-Backup192.168.113.88Keepalived Backup LVSNginx-Web01192.168.113.66Nginx Real ServerNginx-Web02192.168.113.11Nginx Real Server虚拟IPVIP:192.168.113.100三、整体架构图Client ​ | | ​ VIP 192.168.113.100 ​ | ​ ---------------------------- ​ | | ​ 192.168.113.77 192.168.113.88 ​ LVS-Master LVS-Backup ​ Keepalived Keepalived ​ IPVS IPVS ​ | | ​ ---------------------------- ​ | ​ LVS DR模式 ​ | ​ ---------------------------- ​ | | ​ 192.168.113.66 192.168.113.11 ​ Nginx-Web01 Nginx-Web02 ​ Real Server Real Server四、部署主要步骤总结1、系统初始化完成主机名规划网络配置关闭防火墙SELinux调整例如systemctl disable --now firewalld ​ setenforce 02、LVS节点部署两台LVS服务器安装dnf install -y keepalived ipvsadm作用keepalived实现VIP漂移ipvsadm管理LVS规则3、Nginx节点部署两台Web服务器安装dnf install -y nginx配置测试页面Web01Nginx Web01 192.168.113.66Web02Nginx Web02 192.168.113.11用于验证负载均衡效果。4、Keepalived高可用配置LVS-Master配置state MASTER ​ priority 100负责持有VIP接收客户端请求LVS-Backup配置state BACKUP ​ priority 90负责监听Master状态Master故障后接管VIP5、LVS DR模式配置负载算法rr轮询。模式DRDirect Routing。后端Real Server192.168.113.66:80 ​ 192.168.113.11:806、Real Server配置VIP两台Nginx服务器配置ip addr add 192.168.113.100/32 dev lo原因LVS DR模式只修改MAC地址不修改目标IP。客户端访问VIP:80转发到Nginx后目标IP仍然是192.168.113.100所以Real Server必须识别VIP。7、ARP抑制配置Real Server配置vim /etc/sysctl.conf增加net.ipv4.conf.lo.arp_ignore1 ​ net.ipv4.conf.lo.arp_announce2 ​ net.ipv4.conf.all.arp_ignore1 ​ net.ipv4.conf.all.arp_announce2执行sysctl -p作用防止Real Server响应VIP ARP请求。避免网络中出现VIP地址冲突。五、测试验证结果1、VIP漂移测试查看Masterip addr正常192.168.113.100绑定在192.168.113.77停止Mastersystemctl stop keepalivedBackup检查ip addr结果VIP漂移192.168.113.100出现在192.168.113.88说明✅ Keepalived高可用成功2、LVS负载均衡测试查看ipvsadm -Ln显示TCP 192.168.113.100:80 rr ​ ​ -192.168.113.66:80 Route ​ -192.168.113.11:80 Route说明两个Nginx节点加入负载池。3、Web访问测试访问http://192.168.113.100结果第一次Nginx Web01第二次Nginx Web02说明✅ LVS轮询正常4、Web节点故障测试停止192.168.113.66systemctl stop nginxLVS健康检查自动发现节点异常。业务访问http://192.168.113.100仍然正常。说明✅ Real Server故障自动摘除六、核心技术总结1、Keepalived作用提供VIP管理VRRP心跳检测Master/Backup切换核心参数参数作用state节点角色priority优先级virtual_router_idVRRP组编号advert_int心跳周期virtual_ipaddressVIP地址2、LVS作用四层负载均衡。支持模式NATDRTUN本项目DR模式优点性能高并发能力强请求响应不经过LVS3、Nginx作用提供Web服务。作为Real Server负责HTTP请求处理静态资源响应七、面试总结回答面试官介绍一下你做过的LVS高可用项目回答我基于openEuler系统搭建过LVSKeepalivedNginx高可用集群前端通过Keepalived实现VIP漂移LVS采用DR模式进行四层负载均衡后端部署两台Nginx Real Server通过健康检查实现故障节点自动摘除。项目实现了负载均衡、高可用以及故障自动恢复。