Google Cloud Run (GCR) 的 Gunicorn (with Flask) 参数 - 在 Dockerfile 中放置啥? [关闭]

Posted

技术标签:

【中文标题】Google Cloud Run (GCR) 的 Gunicorn (with Flask) 参数 - 在 Dockerfile 中放置啥? [关闭]【英文标题】:Gunicorn (with Flask) parameters for Google Cloud Run (GCR) - what to put in Dockerfile? [closed]Google Cloud Run (GCR) 的 Gunicorn (with Flask) 参数 - 在 Dockerfile 中放置什么? [关闭] 【发布时间】:2021-03-01 23:04:43 【问题描述】:

寻求具有实际 GCR 经验的人的指导。你怎么处理这个? 我在 Google Cloud Run 中运行了一个 Docker 容器(大小约为 670mb),里面是我基于 Flask 的 Python 服务器,它目前由 Dockerfile 中的这个命令运行:

CMD exec gunicorn --bind 0.0.0.0:8080 --reload --workers=1 --threads 8 --timeout 0 "db_app.app:create_app()"

假设我需要每小时处理大约 300 个请求。

我应该在我的 exec 命令中指定多少个工作线程,才能最有效地使用 GCR 的功能?

例如,GCR 服务器的基本配置类似于 1 CPU 1gb RAM。

那么我应该如何将我的 Gunicorn 放在那里?也许我也应该使用--preload?指定worker-connections?

正如 Dustin 在他的回答中提到的(见下文),Google 官方文档建议将其写入 Dockerfile:

# 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 :$PORT --workers 1 --threads 8 --timeout 0 main:app

我不知道他们在 GCR 配置中的那个“1 CPU”上有多少个内核,所以我怀疑这个示例代码是否非常准确,它更有可能只是为了演示它的一般工作原理。因此,如果有人将 Gunicorn 服务器打包到 Google Cloud Run 中的容器中,我会非常感激(并且每个人都会)非常感激能够分享一些关于如何正确配置它的信息——基本上是在这个 Dockerfile @987654328 中放入什么@ 行而不是通用示例代码?更真实的东西。

我认为这是一个软件问题,因为我们正在谈论在 Dockerfile 中编写内容(问题已关闭并标记为“非 SO 范围问题”)。

【问题讨论】:

这个问题可能已经结束,因为现在有一个关于 vCPU 的问题,它有很好的解释 here 和 GCP public documentation。因此,通过回答您关于分配给 Cloud Run container instances 的 CPU 的问题,Google 员工提供的答案似乎在这里可能会有所帮助。 【参考方案1】:

guidance from Google 是如下配置:

# 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 :$PORT --workers 1 --threads 8 --timeout 0 main:app

使用--preload 可能会减少冷启动时间,但也可能导致意外行为,这在很大程度上取决于您的应用程序的结构。

您不应在生产中使用--reload

您还应该绑定到$PORT,而不是将8080 硬编码为端口。

【讨论】:

谢谢你的回答,我看到了这个例子,但它太笼统了,我怀疑它在如何使用资源方面真的是最优的。他们确实推荐了一名工人,但同时说根据核心数量增加数量,他们有多少核心?这就是为什么我希望一些在 GCR 上拥有此类/类似服务器的人分享一些有关他们的设置的知识.. 默认情况下,Cloud Run 实例分配有 1 个 vCPU (cloud.google.com/run/docs/reference/container-contract#cpu)。线程数很大程度上取决于您的工作负载,请参阅docs.gunicorn.org/en/stable/design.html#how-many-threads。最好的答案是“从这里开始并根据需要进行调整”。 我明白了.. 那么在线程部分的上方,Gunicorn 文档还建议将工作人员的数量设置为 (2 x $num_cores) + 1。假设最小工人数总是 3 是否正确?尤其是在 Cloud Run 的一个 CPU 的情况下。 由于 Cloud Run 是无服务器的,因此多个实例处理并发请求比每个实例拥有更多工作人员更好/更快/更有效,因为这减少了每个实例的总体内存占用和开销。

以上是关于Google Cloud Run (GCR) 的 Gunicorn (with Flask) 参数 - 在 Dockerfile 中放置啥? [关闭]的主要内容,如果未能解决你的问题,请参考以下文章

Google Cloud Build 超时

Google Cloud 构建条件步骤

GCP - 无法在 Cloud Run 中使用 Google Secret Manager (@google-cloud/secret-manager)

Google Cloud Tasks 无法向 Cloud Run 进行身份验证

从 Google Cloud Run 服务中的 docker 容器到 Google Cloud SQL 的请求最多需要 2 分钟

监控 Google Cloud Run 内存使用情况