简体   繁体   English

Google Cloud Run 在 Healthcheck 健康之前启动容器

[英]Google Cloud Run Starts Container Before Healthcheck is Healthy

I have an image that has a relatively lengthy startup time of ~5 seconds.我有一个启动时间相对较长的图像,约为 5 秒。 In other words, the Flask server is up and running, but I load some data into global variables, so the server is not really operational at this point.换句话说,Flask 服务器已启动并正在运行,但我将一些数据加载到全局变量中,因此此时服务器并未真正运行。 If I ping my Google Cloud Run endpoint during this time, the connection will timeout with如果我在此期间 ping 我的 Google Cloud Run 端点,连接将超时

upstream request timeout

To avoid this, I added a docker healthcheck that calls an endpoint in my server.为避免这种情况,我添加了一个 docker 健康检查,它调用我的服务器中的一个端点。 This http request has a timeout of 2 seconds.这个 http 请求的超时时间为 2 秒。 If it times out, it means that the server is still loading those global files, and the endpoint is not ready to receive requests just yet.如果超时,则意味着服务器仍在加载这些全局文件,端点尚未准备好接收请求。 This works fine in development, but not in Cloud Run.这在开发中运行良好,但在 Cloud Run 中却不行。 Cloud Run starts serving traffic to my server before it's done loading - and subsequently, before the container HEALTHCHECK status is actually "healthy". Cloud Run 在完成加载之前开始向我的服务器提供流量 - 随后,在容器 HEALTHCHECK 状态实际上是“健康”之前。

My question我的问题

How can I delay Cloud Run from delivering traffic to my container until it's fully setup?如何延迟 Cloud Run 将流量传输到我的容器,直到它完全设置好?

Edit > answer编辑 > 回答

In my case (using Python + Gunicorn) I was able to solve this using the "application factory" pattern .在我的例子中(使用 Python + Gunicorn)我能够使用“应用程序工厂”模式解决这个问题。 That is, start Gunicorn with也就是说,启动 Gunicorn

$ gunicorn 'test:create_app()'

Where the function create_app() returns the Flask application.其中 function create_app()返回 Flask 应用程序。

My hypothesis as to why this works is because until that function returns, Gunicorn is not yet listening on the port it binds to, and Cloud Run won't start driving traffic to your new running container until that's the case.我对这为何有效的假设是因为在 function 返回之前,Gunicorn 尚未侦听它绑定的端口,并且 Cloud Run 不会开始将流量驱动到您的新运行容器,直到这种情况发生。

rodrigo-silveira's solution: rodrigo-silveira 的解决方案:

In my case (using Python + Gunicorn) I was able to solve this using the "application factory" pattern.就我而言(使用 Python + Gunicorn)我能够使用“应用程序工厂”模式解决这个问题。 That is, start Gunicorn with也就是说,启动 Gunicorn

$ gunicorn 'test:create_app()' Where the function create_app() returns the Flask application. $ gunicorn 'test:create_app()' 其中 function create_app() 返回 Flask 应用程序。

My hypothesis as to why this works is because until that function returns, Gunicorn is not yet listening on the port it binds to, and Cloud Run won't start driving traffic to your new running container until that's the case.我关于为什么会这样的假设是因为在 function 返回之前,Gunicorn 尚未侦听它绑定到的端口,并且 Cloud Run 不会开始将流量引导到您正在运行的新容器,直到出现这种情况。

Note CloudRun now supports liveness and startup probes.注意 CloudRun 现在支持活性和启动探测。
per https://cloud.google.com/run/docs/configuring/healthcheckshttps://cloud.google.com/run/docs/configuring/healthchecks

I was very surprised to learn that CR used to not support standard Kube.netes probes, but it seems that after a recent update.得知 CR 过去不支持标准的 Kube.netes 探针,我感到非常惊讶,但最近更新后似乎是这样。 Not sure when it happened but at the time of this post (Oct 3, 2022) CloudRun health checks are considered to be in "Preview".不确定它是何时发生的,但在这篇文章发布时(2022 年 10 月 3 日)CloudRun 运行状况检查被认为处于“预览”状态。 Readiness probes still aren't a thing, but startup probes are now allowed so the original cool hack solution can now be replaced by standard startup probes.就绪探针仍然不是问题,但现在允许启动探针,因此现在可以用标准启动探针取代原来很酷的 hack 解决方案。

Here's a walkthrough of how to implement / test startup probe: https://stackoverflow.com/a/73942357/2548914以下是如何实施/测试启动探针的演练: https://stackoverflow.com/a/73942357/2548914

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Google Cloud Functions 2 Terraform 部署失败(错误等待创建function,容器健康检查失败) - Google Cloud Functions 2 Terraform fails to deploy (error waiting for creating function, container healthcheck failed) Google Cloud Run - 容器无法启动解决方法 - Google Cloud Run - Container failed to start workarounds Google Cloud Run - Docker building container building but not accessible - Google Cloud Run - Docker building container building but not accessible Github 到 Google Cloud Run:Python Flask API 在 Google Cloud Run 上运行的 WSGI 给出“容器无法启动并侦听端口” - Github to Google Cloud Run: Python Flask API WSGI running on Google Cloud Run gives "Container failed to start and listen on the port" 谷歌云,GitHub pipe 与谷歌云运行 - Google Cloud, GitHub pipe with Google Cloud Run 谷歌云运行端口 - Google Cloud Run Port python 从谷歌云存储中删除以开头的文件 - python delete files from google cloud storage that starts with Docker 容器在本地运行但在 Cloud Run 上失败 - Docker container runs locally but fails on Cloud Run 如何在 Cloud Run 中部署带卷的 docker 容器 - How to Deploy a docker container with volume in Cloud Run 为什么 Google Cloud Run 需要大量容器重启/新实例创建? - Why Google Cloud Run gettings massive container restart / new instance creation?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM