使用 Docker Compose 部署 20 个 Splash 实例并做到负载均衡。

解读

国内面试问这道题,不是让你背 docker-compose.yml 模板,而是考察四点:

  1. 是否理解 Splash 作为 无状态 JS 渲染服务 的横向扩容模型;
  2. 能否在 单台 4C8G 云主机 上把 20 个实例稳稳跑起来(端口、资源、健康检查);
  3. 是否知道 国产云厂商内网 SLB + 安全组 的套路,以及 Docker Compose 本身没有 LB 功能,必须外挂 Nginx;
  4. 能否给出 一键启停、日志归集、监控告警 的工程化方案,让面试官相信你不是在笔记本上跑玩具 demo。

一句话:把 20 个 Splash 做成“可交付”的集群,而不是跑起来就完事。

知识点

  • Splash 官方镜像 openresty+qt5 的体积 1.3 GB,20 个实例并发拉镜像会占满内网带宽,需提前 docker pull 或私有仓库。
  • 单容器建议 200 m CPU + 256 MB 内存,20 个实例≈4C5G,宿主机必须留 20 % 余量给系统+Nginx,否则 OOM 杀进程直接 502。
  • Compose v3.7 版以上才能用 deploy.resources.limits,但 deploy 键只在 Swarm 模式生效;单机场景用 cpus/mem_limit 兼容键。
  • Nginx upstream 默认轮询,但 Splash 返回 502 时不会自动剔除,需加 max_fails=2 fail_timeout=10s 做被动健康检查;生产环境建议用 OpenResty + lua-resty-healthcheck 做主动探测 /health 接口。
  • 国产云主机内网带宽 1 Gbps 封顶,20 个实例同时出网相当于 20 个 Chrome 内核,很容易打满,务必开启 --disable-gpu 与 --max-timeout 30 降低流量。
  • 日志必须落盘到宿主,否则 docker-compose logs 只能看一头一尾;国内合规要求保存 6 个月,用 json-file + max-size=50m,max-file=10 轮转,再丢到 阿里云 SLS 或腾讯云 CLS。
  • 端口段规划:Splash 默认 8050,单机递增 8050–8069;Nginx 统一监听 80/443,对外只暴露 80,安全组只放行 80 和 22,8050-8069 不放公网,防止被扫。
  • Scrapy 侧用 SPLASH_URL=http://nginx/splash 即可,无需感知后端 20 个实例;配合 scrapy-splash 的 DUPEFILTER_CLASS 与缓存中间件,可削峰 30 % 流量。

答案

  1. 前置准备
    a. 购买 8 vCPU 16 GiB 的国内云主机(按量付费约 0.8 元/小时),系统盘 100 GB。
    b. 安装 Docker 20.10+ 与 docker-compose 2.6+,配置 阿里云镜像加速器,mkdir -p /data/splash/logs。

  2. 编写 .env 文件

    SPLASH_IMAGE=harbor.xxx.com/public/splash:3.5
    SPLASH_NUM=20
    SPLASH_START_PORT=8050
    NGINX_IMAGE=nginx:1.25-alpine
    
  3. 编写 docker-compose.yml

    version: '3.7'
    services:
      splash:
        image: ${SPLASH_IMAGE}
        deploy:
          replicas: ${SPLASH_NUM}
        cpus: 0.2
        mem_limit: 256m
        command: >
          --max-timeout 30
          --disable-lua-sandbox
          --disable-gpu
        healthcheck:
          test: ["CMD", "curl", "-f", "http://localhost:8050/health"]
          interval: 10s
          timeout: 3s
          retries: 3
        logging:
          driver: json-file
          options:
            max-size: "50m"
            max-file: "10"
            labels: "service=splash"
        networks:
          - splash-net
    
      nginx:
        image: ${NGINX_IMAGE}
        ports:
          - "80:80"
        volumes:
          - ./nginx.conf:/etc/nginx/nginx.conf:ro
        depends_on:
          - splash
        networks:
          - splash-net
    
    networks:
      splash-net:
        driver: bridge
    
  4. 编写 nginx.conf(关键片段)

    upstream splash {
        least_conn;
        server splash:8050 max_fails=2 fail_timeout=10s;
        # Compose 会把 splash 服务名自动解析到 20 个容器 IP
    }
    server {
        listen 80;
        location /splash {
            proxy_pass http://splash;
            proxy_set_header Host $host;
            proxy_connect_timeout 5s;
            proxy_read_timeout 30s;
        }
        location /nginx-status {
            stub_status on;
            access_log off;
            allow 172.16.0.0/12;
            deny all;
        }
    }
    
  5. 启动与验证

    docker-compose up -d
    # 等待 30 s 健康检查全部通过
    curl http://localhost/splash/render.html?url=https://www.baidu.com
    # 返回 200 即成功
    
  6. 监控与扩缩

    • Prometheus + node-exporter + cadvisor 采集容器 CPU、内存、网络;
    • Grafana 模板 179 看板实时展示 20 个实例 QPS、502 数量;
    • 如需再扩容到 40 个,改 SPLASH_NUM=40 && docker-compose up -d --scale splash=40 即可,Nginx 会自动发现新 IP。

拓展思考

  1. 多机横向:Compose 单机有天花板,生产环境用 Docker Swarm 或 Kubernetes;Swarm 模式把上述 compose 文件加 deploy.placement 约束,3 台 8C16G 节点可跑 60 个 Splash,再用 阿里云 SLB 4 层转发 替代单机 Nginx,带宽可提到 5 Gbps。
  2. Session 保持:Splash 默认无状态,但某些站把 Cookie 写死到渲染进程,需开启 Splash 的 --slots=1 强制 1:1 绑定,此时负载均衡要改 ip_hash,代价是失去弹性。
  3. 反爬对抗:国内大厂对 20 个固定出口 IP 极易封段,给每台宿主机绑定弹性公网 EIP 池(最多 20 个),用 Docker network macvlan 让容器直接拿 EIP 出网,Scrapy 侧通过 SPLASH_COOKIES_DEBUG 与随机 User-Agent 中间件 降低特征。
  4. 成本优化:夜间低峰期用 阿里云弹性伸缩定时任务 缩到 5 个实例,节省 75 % 费用;配合 Spot 实例 + 自动漂移脚本,单条渲染成本可压到 0.0003 元/次。