LVS 工作在 TCP/IP 四层(传输层),基于 IP 地址和端口转发流量,具备高并发、高可用、低资源消耗的特性;
Nginx 工作在 TCP/IP 七层(应用层),基于 HTTP 协议特征(如域名、URL、Cookie)转发流量,同时具备反向代理、缓存、SSL 卸载等能力。
两者结合可实现 "四层高可用承载 + 七层灵活调度" 的企业级负载架构。
✅核心架构:1 台 Director (LVS)→2 台物理机(机 A / 机 B)→每机 2 个 RS→每 RS1 个 Nginx(共 4 个独立 Nginx),DR 模式,全程步骤落地可直接复制✅统一环境:CentOS 7.x,所有节点同网段(示例网段 192.168.1.x),VIP=192.168.1.100,机 A=192.168.1.10,机 B=192.168.1.11
一、 全节点前置准备(必做,一步不落)
所有节点(Director + 机 A + 机 B)统一执行,避免依赖缺失
- 关闭防火墙 + SELinux(永久禁用,避免端口拦截)
bash
# 临时关闭
systemctl stop firewalld && setenforce 0
# 永久禁用,重启不失效
systemctl disable firewalld
sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
- 安装基础依赖(LVS 工具 + Nginx 依赖)
bash
yum install -y ipvsadm keepalived gcc gcc-c++ make pcre-devel zlib-devel wget
- Director 额外开 IP 转发(RS 机不用)
bash
echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
sysctl -p
二、 Director 节点(LVS)详细配置(纯转发,不装 Nginx)
- 配置 VIP(eth0 网卡,永久生效,重启不掉)
bash
# 创建VIP网卡配置文件
cat > /etc/sysconfig/network-scripts/ifcfg-eth0:0 <<EOF
TYPE=Ethernet
BOOTPROTO=static
NAME=eth0:0
DEVICE=eth0:0
IPADDR=192.168.1.100
NETMASK=255.255.255.255
ONBOOT=yes
USERCTL=no
EOF
# 启动VIP网卡
ifup eth0:0
# 验证:ip addr 能看到eth0:0绑定192.168.1.100
- 配置 LVS 规则(绑定 4 个 RS,DR 模式必用 - g,调度算法 WRR)
bash
# 1. 清空旧规则(避免冲突)
ipvsadm -C
# 2. 添加VIP服务:对外暴露VIP:80,加权轮询(性能均衡)
ipvsadm -A -t 192.168.1.100:80 -s wrr
# 3. 绑定机A 2个RS(8080/8081端口=2个Nginx)
ipvsadm -a -t 192.168.1.100:80 -r 192.168.1.10:8080 -g
ipvsadm -a -t 192.168.1.100:80 -r 192.168.1.10:8081 -g
# 4. 绑定机B 2个RS(8080/8081端口=2个Nginx)
ipvsadm -a -t 192.168.1.100:80 -r 192.168.1.11:8080 -g
ipvsadm -a -t 192.168.1.100:80 -r 192.168.1.11:8081 -g
# 5. 保存规则(开机自启)
ipvsadm-save > /etc/sysconfig/ipvsadm
# 6. 启动ipvsadm并设置开机自启
systemctl enable --now ipvsadm
# 验证:ipvsadm -Ln 能看到4个RS,状态都是UP
- Keepalived 高可用配置(可选,防 Director 单点故障)
bash
# 覆盖默认配置
cat > /etc/keepalived/keepalived.conf <<EOF
global_defs {
router_id LVS_DIRECTOR # 唯一标识,备机可改LVS_DIRECTOR_BACKUP
}
vrrp_instance VI_1 {
state MASTER # 备机改BACKUP
interface eth0 # 绑定网卡
virtual_router_id 51 # 主备一致
priority 100 # 备机改90,优先级低
advert_int 1 # 心跳间隔1秒
authentication {
auth_type PASS
auth_pass 1111 # 主备一致
}
virtual_ipaddress {
192.168.1.100/32 dev eth0 label eth0:0 # VIP配置
}
}
# 4个RS健康检查(关键:对应端口)
virtual_server 192.168.1.100 80 {
delay_loop 6 # 检查间隔6秒
lb_algo wrr # 加权轮询
lb_kind DR # DR模式
persistence_timeout 0 # 关闭会话保持
protocol TCP
# 机A RS1 8080
real_server 192.168.1.10 8080 {
weight 1 # 权重,越高分发越多
TCP_CHECK {
connect_port 8080
connect_timeout 3 # 超时3秒
retry 3 # 重试3次
}
}
# 机A RS2 8081
real_server 192.168.1.10 8081 {
weight 1
TCP_CHECK {
connect_port 8081
connect_timeout 3
retry 3
}
}
# 机B RS3 8080
real_server 192.168.1.11 8080 {
weight 1
TCP_CHECK {
connect_port 8080
connect_timeout 3
retry 3
}
}
# 机B RS4 8081
real_server 192.168.1.11 8081 {
weight 1
TCP_CHECK {
connect_port 8081
connect_timeout 3
retry 3
}
}
}
EOF
# 启动Keepalived并开机自启
systemctl enable --now keepalived
# 验证:ip addr 能看到VIP,systemctl status keepalived 状态running
三、 RS 物理机(机 A / 机 B)详细配置(2 机配置完全一致,照抄)
第一步:RS 机基础配置(适配 LVS,必做)
机 A(192.168.1.10)和机 B(192.168.1.11)完全执行相同命令
- 回环网卡绑 VIP(关键:和 Director 一致,不对外广播)
bash
# 创建回环VIP配置
cat > /etc/sysconfig/network-scripts/ifcfg-lo:0 <<EOF
TYPE=Ethernet
BOOTPROTO=static
NAME=lo:0
DEVICE=lo:0
IPADDR=192.168.1.100
NETMASK=255.255.255.255
ONBOOT=yes
USERCTL=no
EOF
# 启动回环VIP
ifup lo:0
# 验证:ip addr 能看到lo:0绑定192.168.1.100
- 禁用 ARP(核心避坑,防止 VIP 冲突,永久生效)
bash
# 添加ARP参数
cat >> /etc/sysctl.conf <<EOF
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
EOF
# 生效配置
sysctl -p
第二步:RS 机安装 2 个独立 Nginx(每机 2RS=2 个 Nginx,端口 8080/8081,完全隔离)
机 A、机 B 均执行,全程分步教安装,零踩坑,用源码安装保证独立,避免 yum 安装冲突
前置:创建 Nginx 目录(区分 2 个实例)
bash
# 统一目录,方便管理
mkdir -p /usr/local/nginx8080 /usr/local/nginx8081 # 2个Nginx安装目录
mkdir -p /data/nginx/html8080 /data/nginx/html8081 # 2个Nginx网站目录
mkdir -p /var/log/nginx8080 /var/log/nginx8081 # 2个Nginx日志目录
步骤 1:下载 Nginx 源码(统一版本,稳定无兼容问题)
bash
cd /usr/local/src
wget https://nginx.org/download/nginx-1.24.0.tar.gz # 稳定版
tar -zxvf nginx-1.24.0.tar.gz
cd nginx-1.24.0
步骤 2:编译安装第一个 Nginx(RS1/RS3,监听 8080 端口)
bash
# 配置编译参数:指定安装目录、端口8080
./configure \
--prefix=/usr/local/nginx8080 \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-http_sub_module \
--with-stream \
--http-port=8080 \
--pid-path=/var/run/nginx8080.pid \
--error-log-path=/var/log/nginx8080/error.log \
--http-log-path=/var/log/nginx8080/access.log
# 编译+安装
make && make install
# 验证安装:/usr/local/nginx8080/sbin/nginx -v 显示版本即成功
步骤 3:编译安装第二个 Nginx(RS2/RS4,监听 8081 端口)
同一源码包直接编译,无需重新下载,仅改目录和端口
bash
# 配置编译参数:指定安装目录、端口8081
./configure \
--prefix=/usr/local/nginx8081 \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-http_sub_module \
--with-stream \
--http-port=8081 \
--pid-path=/var/run/nginx8081.pid \
--error-log-path=/var/log/nginx8081/error.log \
--http-log-path=/var/log/nginx8081/access.log
# 编译+安装
make && make install
# 验证安装:/usr/local/nginx8081/sbin/nginx -v 显示版本即成功
步骤 4:配置 2 个 Nginx(独立配置,业务统一,差异化页面便于验证)
1. 配置 8080 端口 Nginx(RS1/RS3)
bash
# 编辑配置文件
cat > /usr/local/nginx8080/conf/nginx.conf <<EOF
worker_processes auto; # 自动匹配CPU核数,不抢资源
worker_rlimit_nofile 65535; # 调大文件句柄,扛高并发
events {
use epoll; # 高效IO模型
worker_connections 65535;
}
http {
include mime.types;
default_type application/octet-stream;
charset utf-8;
# 日志格式
log_format main '\$remote_addr - \$remote_user [\$time_local] "\$request" '
'\$status \$body_bytes_sent "\$http_referer" '
'"\$http_user_agent" "\$http_x_forwarded_for"';
access_log /var/log/nginx8080/access.log main;
error_log /var/log/nginx8080/error.log warn;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
gzip on;
server {
listen 8080; # 固定端口,和LVS规则对应
server_name _; # 匹配所有请求(对应VIP)
location / {
root /data/nginx/html8080; # 网站根目录
index index.html index.htm;
}
# 健康检查接口(方便排查)
location /health {
stub_status on;
access_log off;
}
# 错误页面
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
EOF
2. 配置 8081 端口 Nginx(RS2/RS4)
bash
# 编辑配置文件,仅改端口和目录,其余一致
cat > /usr/local/nginx8081/conf/nginx.conf <<EOF
worker_processes auto;
worker_rlimit_nofile 65535;
events {
use epoll;
worker_connections 65535;
}
http {
include mime.types;
default_type application/octet-stream;
charset utf-8;
log_format main '\$remote_addr - \$remote_user [\$time_local] "\$request" '
'\$status \$body_bytes_sent "\$http_referer" '
'"\$http_user_agent" "\$http_x_forwarded_for"';
access_log /var/log/nginx8081/access.log main;
error_log /var/log/nginx8081/error.log warn;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
gzip on;
server {
listen 8081; # 固定端口,和LVS规则对应
server_name _;
location / {
root /data/nginx/html8081; # 网站根目录
index index.html index.htm;
}
location /health {
stub_status on;
access_log off;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
EOF
步骤 5:创建差异化页面(验证负载分发,4 个 Nginx 一目了然)
机 A(192.168.1.10)创建页面
bash
# 机A RS1(8080)
echo "【物理机A - RS1 - Nginx8080】" > /data/nginx/html8080/index.html
# 机A RS2(8081)
echo "【物理机A - RS2 - Nginx8081】" > /data/nginx/html8081/index.html
机 B(192.168.1.11)创建页面
bash
# 机B RS3(8080)
echo "【物理机B - RS3 - Nginx8080】" > /data/nginx/html8080/index.html
# 机B RS4(8081)
echo "【物理机B - RS4 - Nginx8081】" > /data/nginx/html8081/index.html
步骤 6:启动 2 个 Nginx + 设置开机自启(独立启停,互不影响)
1. 临时启动(先验证)
bash
# 启动8080端口Nginx
/usr/local/nginx8080/sbin/nginx
# 启动8081端口Nginx
/usr/local/nginx8081/sbin/nginx
# 验证启动:ss -tlnp | grep nginx 能看到8080、8081端口监听
# 验证访问:本机curl http://127.0.0.1:8080 和 http://127.0.0.1:8081 能看到对应页面
2. 开机自启(永久生效,重启不丢)
创建 systemd 服务文件,支持 systemctl 管理
bash
# 8080端口Nginx服务
cat > /usr/lib/systemd/system/nginx8080.service <<EOF
[Unit]
Description=Nginx 8080 Service
After=network.target
[Service]
Type=forking
PIDFile=/var/run/nginx8080.pid
ExecStart=/usr/local/nginx8080/sbin/nginx
ExecReload=/usr/local/nginx8080/sbin/nginx -s reload
ExecStop=/usr/local/nginx8080/sbin/nginx -s stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
# 8081端口Nginx服务
cat > /usr/lib/systemd/system/nginx8081.service <<EOF
[Unit]
Description=Nginx 8081 Service
After=network.target
[Service]
Type=forking
PIDFile=/var/run/nginx8081.pid
ExecStart=/usr/local/nginx8081/sbin/nginx
ExecReload=/usr/local/nginx8081/sbin/nginx -s reload
ExecStop=/usr/local/nginx8081/sbin/nginx -s stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
# 重新加载服务,设置开机自启
systemctl daemon-reload
systemctl enable --now nginx8080 nginx8081
# 验证自启:systemctl status nginx8080 nginx8081 均为running
四、 整体使用 & 验证(必做,确认架构生效)
1. 基础使用命令(常用,运维必备)
✅ Director 节点命令
bash
# 查看LVS规则+RS状态(核心)
ipvsadm -Ln
# 查看LVS流量统计(看分发是否均匀)
ipvsadm -Ln --stats
# 新增RS(比如机A加8082端口)
ipvsadm -a -t 192.168.1.100:80 -r 192.168.1.10:8082 -g
# 删除RS(比如机A8081端口)
ipvsadm -d -t 192.168.1.100:80 -r 192.168.1.10:8081
# 重启LVS
systemctl restart ipvsadm
✅ RS 物理机(机 A / 机 B)命令
bash
# 启停单个Nginx(互不影响)
systemctl start/stop/restart/status nginx8080
systemctl start/stop/restart/status nginx8081
# 重载Nginx配置(不重启)
/usr/local/nginx8080/sbin/nginx -s reload
/usr/local/nginx8081/sbin/nginx -s reload
# 查看Nginx日志(排查问题)
tail -f /var/log/nginx8080/access.log
tail -f /var/log/nginx8081/error.log
2. 完整验证(3 步确认架构没问题)
步骤 1:基础分发验证(核心)
客户端(同网段任意机器)访问 http://192.168.1.100(VIP:80),多次刷新,会轮询显示 4 个页面:✅ 【物理机 A - RS1 - Nginx8080】✅ 【物理机 A - RS2 - Nginx8081】✅ 【物理机 B - RS3 - Nginx8080】✅ 【物理机 B - RS4 - Nginx8081】→ 说明 LVS 分发成功
步骤 2:RS 故障验证(容灾能力)
- 机 A 停掉 8080 端口 Nginx:
systemctl stop nginx8080 - 客户端再访问 VIP,不再出现机 A8080 页面,其余 3 个正常
- 重启机 A8080:
systemctl start nginx8080,页面恢复轮询→ 说明 LVS 健康检查生效
步骤 3:物理机故障验证(双层容灾)
- 直接关机物理机 A
- 客户端访问 VIP,仅轮询机 B 的 2 个页面,服务不中断
- 开机物理机 A,自动恢复轮询 4 个页面→ 说明物理机级容灾生效
五、 本方案核心优缺点(精准总结)
✅ 核心优点(比基础架构强 2 倍)
- 双层容灾极致:物理机互备(单机宕机不影响)+ 单机 RS 互备(单 Nginx 宕机不影响),服务零中断
- 并发拉满:4 个独立 Nginx 并行,并发上限 = 4 倍单 Nginx,Director 内核态转发支撑百万级接入
- 资源利用率最高:单机 2 个 Nginx,充分榨干 CPU / 内存 / 网卡,比 1 机 1RS 省 50% 服务器成本
- 无缝扩容:可加物理机(每机仍 2RS),或单机加 RS(如 8082 端口),Director 仅加 1 行规则即可
- 分层安全:Director 纯转发无业务暴露,RS 藏后端,Nginx 管业务,抗攻击能力强
❌ 核心缺点(避坑重点)
- 部署复杂度高:比单 Nginx 多 4 倍配置,需注意端口隔离、Nginx 独立安装,新手易出错
- 单机资源竞争:高并发下 1 机 2 个 Nginx 会抢 CPU / 内存,需限制 worker_processes(建议 = CPU 核数 / 2)
- 网络约束严格:DR 模式必须同网段,跨网段需用 NAT 模式,性能下降 10%-30%
- 运维成本高:需监控 1Director+2 物理机 + 4Nginx,故障排查需定位到具体 RS 端口
- 仍有上限:整体上限 = 2 台物理机资源总和,超上限需新增物理机
六、 生产避坑优化建议(必看)
- 资源限制:给每个 Nginx 设
worker_processes = CPU核数/2,避免抢核;worker_connections设 65535 即可 - 网卡优化:RS 机配双网卡 bonding(聚合),提升带宽,避免网卡瓶颈
- 日志切割:4 个 Nginx 日志单独切割,避免日志过大占满磁盘
- 监控告警:监控 Director 的 LVS 状态、RS 的 Nginx 端口和连接数,异常及时告警
- 会话保持:若业务需要,Director 可开 persistence_timeout(建议 30 秒),避免会话丢失