Google Cloud Run (GCR) 的 Gunicorn(带 Flask)参数 - 在 Dockerfile 中放入什么?

Amy*_*ong 12 python gunicorn google-cloud-platform google-cloud-run

寻求具有 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()"
Run Code Online (Sandbox Code Playgroud)

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

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

例如,GCR 服务器的基本配置是 1 个 CPU 1GB RAM。

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

正如达斯汀在他的回答中引用的那样(见下文),谷歌官方文档建议在 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
Run Code Online (Sandbox Code Playgroud)

我不知道 GCR 配置中的“1 个 CPU”上有多少个内核,所以我怀疑这个示例代码是否非常准确,它更有可能只是演示它的一般工作原理。CMD因此,如果有人在 Google Cloud Run 中将可用的 Gunicorn 服务器打包到容器中,并且能够分享一些有关如何正确配置它的信息 - 基本上将哪些内容放入此 Dockerfile行中,我将(以及处于我这种情况的每个人)非常感激通用示例代码?更符合现实生活的东西。

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

Dus*_*ram 6

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
Run Code Online (Sandbox Code Playgroud)

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

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

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

  • 由于 Cloud Run 是无服务器的,因此多个实例来服务并发请求比每个实例拥有更多工作线程更好/更快/更高效,因为这减少了每个实例的总体内存占用和开销。 (4认同)
  • 默认情况下,Cloud Run 实例分配 1 个 vCPU (https://cloud.google.com/run/docs/reference/container-contract#cpu)。线程数量很大程度上取决于您的工作负载,请参阅 https://docs.gunicorn.org/en/stable/design.html#how-many-threads。最好的答案是“从这里开始并根据需要进行调整”。 (3认同)