Docker 环境下的 Nginx 负载均衡(vllm)

背景

在linux上实现负载均衡,我在其他服务器上四个vllm的端口,端口分别时8001/v1,8002/v1,8003/v1,8004/v1,需要配置一个监听,使用9000端口,对四个端口进行转发,同时某些端口有时可能不能访问。

在 Ubuntu 上使用 Docker 实现负载均衡,最稳妥且高效的方案是使用 Nginx。Nginx 的 upstream 模块专门用于分发流量,并且内置了基础的健康检查机制(当某个后端端口不可用时,它会自动重试其他端口并暂时剔除不可用节点)。

准备目录结构

bash 复制代码
mkdir nginx-lb && cd nginx-lb
touch docker-compose.yml
touch nginx.conf

配置 Nginx 负载均衡策略

bash 复制代码
events {
    worker_connections 2048;
}

http {
    client_max_body_size 100m;
    # 基础日志格式定义
    log_format main '$remote_addr - $remote_user [$time_local] "$request" '
                    '$status $body_bytes_sent "$http_referer" '
                    '"$http_user_agent" "$upstream_addr" $request_time';

    access_log /var/log/nginx/access.log main;
    error_log /var/log/nginx/error.log warn;

    upstream vllm_servers {
        # 进阶点 1:最少连接算法。LLM 推理时间差异很大,
        # 将新请求发给当前正在处理请求最少的后端,比轮询更科学。
        least_conn;

        server 127.0.0.1:8001 max_fails=2 fail_timeout=30s;
        server 127.0.0.1:8002 max_fails=2 fail_timeout=30s;
        server 127.0.0.1:8003 max_fails=2 fail_timeout=30s;
        server 127.0.0.1:8004 max_fails=2 fail_timeout=30s;

        # 进阶点 2:保持与后端的长连接,减少 TCP 握手开销
        keepalive 32;
    }

    server {
        listen 9000;

        location / {
            proxy_pass http://vllm_servers;

            # 进阶点 3:禁用缓冲 (关键!)。
            # vLLM 通常使用流式输出 (Stream),开启缓冲会导致内容攒够了一大块才显示。
            proxy_buffering off;
            proxy_cache off;
            
            # 设置长连接头
            proxy_http_version 1.1;
            proxy_set_header Connection "";
            
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

            # 超时控制:LLM 生成可能很慢,给够 10 分钟
            proxy_read_timeout 600s;
            proxy_connect_timeout 10s;

            # 容错:当其中一个端口返回错误或超时,自动切换到下一个
            proxy_next_upstream error timeout http_500 http_502 http_503 http_504;
        }

        # 状态监控页面 (可选)
        # 访问 http://IP:9000/nginx_status 可以看到当前连接数
        location /nginx_status {
            stub_status on;
            allow 127.0.0.1; # 安全起见,只允许特定 IP 访问或干脆注释掉
            # allow 你的管理IP;
            # deny all;
        }
    }
}

配置docker-compose.yml

bash 复制代码
services:
  vllm-proxy:
    image: nginx:alpine  # 使用更轻量级的 alpine 版本
    container_name: vllm-load-balancer
    restart: unless-stopped
    ports:
      - "9000:9000"
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf:ro
      - ./logs:/var/log/nginx
    networks:
      - vllm-net
    # 限制容器资源,防止 Nginx 在极端流量下影响主机
    deploy:
      resources:
        limits:
          cpus: '0.5'
          memory: 512M

networks:
  vllm-net:
    driver: bridge

启动

在 vllm-lb 目录下执行

bash 复制代码
docker-compose up -d

热重载

bash 复制代码
docker exec vllm-load-balancer nginx -s reload

查看状态

bash 复制代码
docker logs -f vllm-load-balancer

查看配置是否正确

bash 复制代码
docker exec vllm-load-balancer cat /etc/nginx/nginx.conf

关闭

bash 复制代码
docker-compose down
相关推荐
cyber_两只龙宝7 分钟前
haproxy--使用socat工具实现对haproxy权重配置的热更新
linux·运维·负载均衡·haproxy·socat
萧曵 丶35 分钟前
Nginx常用配置
运维·nginx
有代理ip40 分钟前
后端服务安全加固:Nginx 反向代理配置教程
运维·nginx·安全
养多肉1 小时前
宝塔面板绑定域名(不带端口)
服务器·nginx·安全
Fᴏʀ ʏ꯭ᴏ꯭ᴜ꯭.17 小时前
Nginx构建PC站点:root与alias详解
运维·chrome·nginx
哆啦code梦21 小时前
Kong vs Nginx:微服务网关选型指南
nginx·kong·微服务网关
Fᴏʀ ʏ꯭ᴏ꯭ᴜ꯭.1 天前
Nginx性能调优与压测实战指南
运维·nginx
岁岁种桃花儿1 天前
流量入口Nginx动态发现K8s Ingress Controller实操指南
nginx·架构·kubernetes
运维行者_1 天前
用Applications Manager监控HAProxy:保障负载均衡高效稳定
运维·开发语言·前端·数据库·tcp/ip·负载均衡·服务器监控
qq_312920111 天前
一款轻量级 Nginx 访问日志分析与可视化面板,支持实时统计、IP 归属地解析与客户端识别
运维·nginx