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
相关推荐
阿豪啊4 小时前
记一次 Nginx 跨域配置踩坑与优化:从嵌套 If 报错到 Map 指令最佳实践
nginx
成为你的宁宁12 小时前
【Prometheus Operator监控K8S Nginx】
nginx·kubernetes·prometheus
abcy07121313 小时前
centos7 nginx代理kafka集群
nginx
難釋懷14 小时前
Nginx对上游服务器使用keepalive
服务器·nginx·github
sbjdhjd14 小时前
Tomcat(下) 集群高可用实战:反向代理・负载均衡・分布式 Session
运维·前端·云原生·开源·tomcat·负载均衡·memcached
2401_834636991 天前
Nginx 从入门到实战:静态 / 动态站点、PHP 部署与反向代理全解析
运维·nginx·php
回忆2012初秋1 天前
【Nginx】优雅地走进高性能 Web 服务器世界(1)
服务器·前端·nginx
難釋懷1 天前
Nginx-KeepAlive
运维·nginx
2401_834636991 天前
Keepalived + LVS (DR) + Nginx + NFS 高可用 Web 集群部署实战手册
前端·nginx·lvs
NCU_wander2 天前
LB HA(high avaliablity)和nginx
运维·nginx