【问题标题】:502 bad gateway error with upstream prematurely closed connection?上游过早关闭连接的 502 错误网关错误?
【发布时间】:2018-11-22 06:55:23
【问题描述】:

我正在尝试使用 AWS sagemaker、Flask、Nginx 和 Gunicorn 在 docker 容器上运行机器学习推理服务器。我曾尝试在 AWS sagemaker 上运行 c5.xlarge 实例和 c5.4xlarge 实例,但在 c5.xlarge 实例上运行时它总是会中断。

当请求通过加载大约 300 mb 的 ML 模型来检查应用程序的运行状况时。当调用推理端点时,它会检查模型是否在工作器中启动并运行,如果没有启动 ML 模型,然后使用数据运行预测。我通常用

Nginx 配置:

worker_processes auto;
daemon off; # Prevent forking


pid /tmp/nginx.pid;
error_log /var/log/nginx/error.log;

events {
  # defaults
}

http {
  include /etc/nginx/mime.types;
  default_type application/octet-stream;
  access_log /var/log/nginx/access.log combined;

  upstream gunicorn {
    server unix:/tmp/gunicorn.sock;
  }

  server {
    listen 8080 deferred;
    client_max_body_size 5m;

    keepalive_timeout 10000;

    location ~ ^/(ping|invocations) {
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_redirect off;
      proxy_pass http://gunicorn;
    }

    location / {
      return 404 "{}";
    }
  }
}

独角兽:

subprocess.Popen(['gunicorn',
                             '--timeout', str(model_server_timeout),
                             '-k', 'gevent',
                             '-b', 'unix:/tmp/gunicorn.sock',
                             '-w', str(model_server_workers),
                             '--error-logfile', '-',
                             '--access-logfile', '-',
                             '--preload',
                             'wsgi:app'])

我查看了超时(gunicorn 已经设置为 60 秒),尝试预加载应用程序,并且在读取错误响应时,抛出到标准输出的日志只有上游过早关闭的连接。

【问题讨论】:

    标签: python nginx flask gunicorn amazon-sagemaker


    【解决方案1】:

    您的容器通常会在多长时间内响应请求?如果您在托管端点中使用容器,则容器必须在 60 秒内响应请求。将 gunicorn 超时设置为略低于 60 秒可能会有所帮助。 https://docs.aws.amazon.com/sagemaker/latest/dg/API_runtime_InvokeEndpoint.html

    看起来响应时间取决于实例类型。如果是这种情况,并且您确实想使用 c5.xlarge 实例类型,您可以尝试创建批量转换作业,而不是使用实时推理端点。批量转换作业确实允许每个请求的响应时间超过 60 秒。 https://docs.aws.amazon.com/sagemaker/latest/dg/API_CreateTransformJob.html

    希望这会有所帮助!

    -韩

    【讨论】:

      猜你喜欢
      • 2019-05-15
      • 2019-04-04
      • 2022-11-27
      • 1970-01-01
      • 2015-12-04
      • 1970-01-01
      • 2014-04-02
      • 1970-01-01
      • 2017-03-28
      相关资源
      最近更新 更多