【问题标题】:Cloud Run/Gunicorn giving 502 error after one minuteCloud Run/Gunicorn 在一分钟后给出 502 错误
【发布时间】:2021-03-05 03:26:03
【问题描述】:

我正在使用 Gunicorn 的 Google Cloud Run 中部署一个 python 应用程序。我的 gunicorn 和 cloud run 超时都设置为 900 秒,这也是 Cloud Run 的超时。奇怪的是,当我调用该函数时,如果应用程序运行时间超过 60 秒,我会从 Cloud Run 收到 502 错误,而如果运行时间少于 60 秒,则不会。例如,下面部署的函数抛出了这个错误:

def process_file(request=request):
    time.sleep(61)
    ...
    return handle_response()      

但是,如果我将睡眠时间改为 40 秒:

def process_file(request=request):
    time.sleep(40)
    ...
    return handle_response() 

没有 502 错误。起初我以为问题是由 nginx 引起的,它有 60 秒的默认超时时间,但似乎 nginx 并没有默认使用 docker 或 cloud run 部署,所以这似乎不是问题的原因。我的 Dockerfile 如下:

FROM continuumio/miniconda3

# Install production dependencies.
RUN conda install numpy==1.17.2
RUN conda install xlsxwriter==1.1.2
RUN conda install pandas==0.25.1
RUN conda install -c conda-forge ciso8601
RUN pip install gunicorn flask gevent flask_mail flask-cors pyjwt firebase_admin networkx datefinder google-cloud-pubsub 

# Copy local code to the container image.
COPY app.py .
RUN mkdir backend/
COPY backend/ /backend/

# Service must listen to $PORT environment variable.
# This default value facilitates local development.
ENV PORT 8080

# Run the web service on container startup. Here we use the gunicorn
# webserver, with one worker process and 8 threads.
# For environments with multiple CPU cores, increase the number of workers
# to be equal to the cores available.
CMD exec gunicorn --bind 0.0.0.0:$PORT --workers 1 app:app --timeout 900 --log-level debug

我在前端使用axios 调用云运行,据我了解,它没有超时,所以我不认为这应该是一个问题。任何帮助表示赞赏,谢谢!

编辑:这是 chrome 控制台中错误消息的图像 - 不过似乎不是很有帮助:

【问题讨论】:

  • 默认的 Cloud Run 超时为 5 分钟。使用--log-level debug 运行 Gunicorn 以查看 gunicorn 是问题还是其他原因。 Cloud Run 不使用 Nginx 作为前端。 GFE(谷歌前端)位于 Cloud Run 的前面。 Cloud Run 支持--timeout=N。尝试将其设置为特定值,例如 120。查看您的 Cloud Run 服务实例的 Stackdriver 日志。用你的发现编辑你的问题。
  • 我将超时设置为 15 分钟(900 秒),这样就不会出现问题;我会用这个更新问题。正如您从 dockerfile 中看到的那样,我已经添加了调试和超时标志 - 日志中没有显示任何有用的信息。
  • Stackdriver 没有显示 502?
  • 不,502 只显示在前端。在堆栈驱动程序中没有它的踪迹。我可能应该注意到,云运行函数实际上确实成功完成了执行。
  • 这意味着 Cloud Run 本身可能不是罪魁祸首。查看浏览器调试器。有没有出现可能指向问题的东西?您是否在 Cloud Run 前使用负载平衡器? HTTP 错误 502 表示网关错误。以我的经验,这意味着前端和后端之间的故障(后端无法及时响应)。编辑您的问题,详细了解您的架构。

标签: docker google-cloud-platform timeout google-cloud-run http-status-code-502


【解决方案1】:

我们遇到了类似的问题。可能您的云运行前的 GCP 内部负载均衡器无法将请求传递给实例。这意味着某些进程使云运行实例在 60 秒后停止,因此它没有收到任何请求。根据this 的帖子,这可能与云运行干扰gunicorn 工人有关。由于云运行(托管)是无服务器环境,因此加载和关闭工作程序和代码的顺序很重要。您可以尝试设置--preload--timeout=0。另一个article 提出了类似的建议。

【讨论】:

  • 尝试添加所有这些标志,仍然有问题:(
【解决方案2】:

解决了这个问题。我正在向 Firebase 托管域发送 HTTP POST 请求。 Firebase 托管的域 POST 请求在 60 秒后超时(请参阅 Firebase-Hosted Cloud Function retrying on any request that takes 60s, even when timeout is >60s) - 解决方案是直接调用 Cloud Run url。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-07
    • 2011-09-07
    • 2016-08-19
    • 2016-06-23
    • 2017-10-01
    • 2012-05-28
    • 2017-05-17
    • 2018-12-05
    相关资源
    最近更新 更多