flask - 在Google Kubernetes Engine中运行Flask Web应用程序

标签 flask web-applications kubernetes reverse-proxy wsgi

网络上有大量的Flask在开发状态下运行的教程和文档。在开发模式下,日志如下所示:

* Serving Flask app "app" (lazy loading)
 * Environment: production
   WARNING: Do not use the development server in a production environment.
   Use a production WSGI server instead.
 * Debug mode: on
 * Running on http://0.0.0.0:5555/ (Press CTRL+C to quit)

我想了解更多有关如何使其准备就绪的信息。我还看到了有关使用生产就绪的WSGI服务器和nginx作为反向代理的文档。但是有人可以告诉我为什么需要WSGI和反向代理吗?

如果我的Flask应用程序已被docker化并在Google Kubernetes Engine中运行,那么是否有必要? GKE会不会照顾WSGI和反向代理的目的?

最佳答案

Flask's documentation所述:

Flask’s built-in server is not suitable for production



为什么选择WSGI?部署Python Web应用程序是a standard way,它为您提供了选择服务器时的选项(即,您可以选择最适合您的应用程序/工作流程而无需更改应用程序),并且可以将扩展方面的负担转移到服务器上。

为什么要使用反向代理?这取决于服务器。这是Gunicorn's rationale:

... we strongly advise that you use Nginx. If you choose another proxy server you need to make sure that it buffers slow clients when you use default Gunicorn workers. Without this buffering Gunicorn will be easily susceptible to denial-of-service attacks.



这是Waitress's rationale:

Often people will set up "pure Python" web servers behind reverse proxies, especially if they need TLS support (Waitress does not natively support TLS). Even if you don't need TLS support, it's not uncommon to see Waitress and other pure-Python web servers set up to only handle requests behind a reverse proxy; these proxies often have lots of useful deployment knobs.



反向代理的其他实际原因可能包括需要多个后端(其中一些可能不是Python Web应用程序)的反向代理,缓存响应以及提供静态内容(例如Nginx擅长的事情)。并非所有WSGI服务器都需要反向代理:uWSGICherryPy将其视为可选。

附言Google App Engine似乎符合WSGI,并且不需要任何其他配置。

关于flask - 在Google Kubernetes Engine中运行Flask Web应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54743125/

相关文章:

javascript - 模糊测试(框架)网络应用程序?

docker - 使 Kubernetes 服务可从外部访问

kubernetes - 将k8迁移到RBAC时遇到问题

kubernetes - 设置并行度> 1时如何获取1个initContainer

python - Flask:为什么我会收到 404 错误?

python - Flask app config.py vs dotenv 访问环境变量

css - 如何为 Ruby on Rails 中的所有 Controller 使用 application.css.scss?

java - 从开发人员到 Web 开发人员再到 Web 设计师

python - 改变 flask.flash 信息的颜色

python - 使用 Swagger 生成的新代码更新 Flask 代码