将 Django 部署到 App Engine 柔性环境 - 超时错误响应:[4]

Deploying Django to App Engine Flexible Environment - Timeout Error Response: [4]

我正在尝试将我的应用程序部署到灵活的环境中。 Docker 图像构建良好,但当我认为它试图使服务上线时,该过程失败了。我的构建超时设置为 1200,这是值得的。

如何进一步查询此错误? 我正在努力寻找日志/GCP 系统中的哪个位置可以准确找出卡住的进程。这似乎是一个完全不透明的错误,没有任何迹象表明到底出了什么问题。应用程序是否存在错误(在本地运行良好)?如果是这样,我希望它仍会部署,但只是在我访问该网站时显示错误。

非常感谢任何帮助。

错误:

OperationError: Error Response: [4] Your deployment has failed to become healthy in the allotted time and therefore was rolled back. If you believe this was an error, try adjusting the 'app_start_timeout_sec' setting in the 'readiness_check' section.
ERROR: (gcloud.app.deploy) Error Response: [4] Your deployment has failed to become healthy in the allotted time and therefore was rolled back. If you believe this was an error, try adjusting the 'app_start_timeout_sec' setting in the 'readiness_check' section.

这是我的Docker文件:

FROM gcr.io/google-appengine/python

RUN apt-get update && apt-get install software-properties-common -y
RUN add-apt-repository ppa:ubuntugis/ppa

RUN apt-get install -y gdal-bin


# Create a virtualenv for dependencies. This isolates these packages from
# system-level packages.
# Use -p python3 or -p python3.7 to select python version. Default is version 2.
RUN virtualenv /env -p python3.7



# Setting these environment variables are the same as running
# source /env/bin/activate.
ENV VIRTUAL_ENV /env
ENV PATH /env/bin:$PATH

# Copy the application's requirements.txt and run pip to install all
# dependencies into the virtualenv
COPY requirements.txt /tmp
WORKDIR /tmp
RUN pip install -r requirements.txt

# Add the application source code.
ADD . /

EXPOSE 8080
# Run a WSGI server to serve the application. gunicorn must be declared as
# a dependency in requirements.txt.
#CMD gunicorn -b :$PORT main:app

这是我的 app.yaml:

runtime: custom
env: flex

runtime_config:
  # You can also specify 2 for Python 2.7
  python_version: 3.7

我几乎可以肯定这是由 gunicorn 超时引起的。

要禁用 gunicorn 的超时行为,请将 Dockerfile 中的最后一条命令更改为:

CMD exec gunicorn --bind :$PORT --workers 1 --threads 8 main:app --timeout 0

其中:-- workers 1 --threads 8表示一个工作进程和8个线程。 (如果您不手动指定资源,则默认为 1 CPU 核心) 如果您决定使用更多内核,则相应地更改工作线程和线程,但这有点超出了这个问题的范围。

重要的部分是 --timeout 0,它基本上可以防止 gunicorn 超时。

如果您仍然看到错误,那么有一个小的补充很可能会修复它。 启动 gunicorn 时也使用 --preload 标志。所以 Dockerfile 中的最后一个命令将是:

CMD exec gunicorn --bind :$PORT --workers 1 --threads 8 main:app --timeout 0 --preload

这将基本上确保所有需要的导入和预处理都将在创建托管您的 docker 容器的实例时完成。当您使用需要花费大量时间进行一些一次性预处理的应用程序时,这非常有用。 这样,一旦请求到来,一切都已加载并准备好为该请求提供服务。

为了最大限度地发挥 --preload 的优势,您还需要将所有导入等移动到主应用程序的最开头,并避免在路由处理程序中调用导入。

另外,在 app.yaml 和 Dockerfile 中都有入口点命令是没有意义的。在我看来,最好将它保存在 Dockerfile 中。

另外:

我会将 EXPOSE 8080 移到 FROM 行之后,因为这将确保您的容器与外界有正确的连接。