首页 > 解决方案 > 在 OpenShift 中以 HTTPS 模式配置 python flask 应用程序

问题描述

我正在尝试在 openshift 4.7 中以 https 模式运行 python 烧瓶应用程序。我的 python 烧瓶应用程序在端口 8080 上侦听,该端口通过 dockerFile 配置公开,如下所示。我已经通过 Openshift 控制台中的配置重定向了 httpsService Port Mapping请求https port 443 to pod port 8080。您可以通过转到 OpenshiftConsole > Project > Services > Service Details并选择您的应用程序来进行配置。但是我仍然无法以 https 模式访问该服务。当我尝试以 https 模式访问时,出现错误The application is currently not serving requests at this endpoint. It may not have been started or is still starting.

当我通过 oc cli 进行正常部署并且不通过 openshift 控制台进行任何 ssl 配置时,该应用程序在 http 模式下运行良好。请告知如何在 https 模式下运行它

我的 app.py 代码如下

from flask import Flask, jsonify, request, redirect

app = Flask(__name__)

@app.route('/<string:name>/')
def helloName(name):
    print(request)
    return "Hello " + name

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=8080, debug = True)

我的 Dockerfile 内容如下,我正在从中暴露端口 8080

# set base image 
FROM registry.redhat.io/rhel8/python-38

# copy the dependencies file to the working directory
COPY requirements.txt .

# Configure PIP with local Nexus
ENV PIP_TRUSTED-HOST=nexus.company.com:8443
ENV PIP_INDEX=https://nexus.company.com:8443/repository/pypi-group/pypi
ENV PIP_INDEX-URL=https://nexus.company.com:8443/repository/pypi-group/simple
ENV PIP_NO-CACHE-DIR=false
ENV PIP_TIMEOUT=600

RUN touch ~/.netrc && \
    echo "machine nexus.company.com" >> ~/.netrc && \
    echo "login ${NEXUS_USER}" >> ~/.netrc && \
    echo "password ${NEXUS_TOKEN}" >> ~/.netrc 

# install dependencies
RUN pip install -U pip \ 
pip install -r requirements.txt

# copy the content of the local src directory to the working directory
COPY src/ .

EXPOSE 8080
# command to run on container start
CMD [ "python3", "./app.py" ]

Requirements.txt 只有 Flask

标签: pythonflasksslhttpsopenshift

解决方案


这里有两件事:

  1. 使用 host="0.0.0.0" Flask 侦听它正在运行的机器的 IP。我认为将其更改为 127.0.0.1 应该很好。

    app.run(host='127.0.0.1', port=8080, debug = True)

  2. 可能更重要的.run()是,Flask 中的方法仅用于开发,对于生产,您应该使用 gunicorn 之类的东西。

链接:https ://gunicorn.org/

干杯,T


推荐阅读