Flask在Azure上部署失败,503 -服务器不可用



在Azure上我试图在Linux上托管的web应用程序上部署一个flask API。app.py的编码如下:

app = Flask(__name__)
CORS(app)
basedir = os.path.abspath(os.path.dirname(__file__))
from db_helpers import init_db
app.config[
'SQLALCHEMY_DATABASE_URI'
] = my_microsoft_azure_database_connection.database_connection_uri
app.config['DEBUG'] = False
app.config['TESTING'] = False
app.config['CSRF_ENABLED'] = True
app.config['SECRET_KEY'] = 'needs-to-be-changed'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db_engine = init_db()
db = SQLAlchemy(app, metadata=BaseDeclarative.metadata)
api = Api(app, default_mediatype='application/json')
api.add_resource(
ListAssessmentCaseEndpoint, '/assessments', endpoint='list_assessment_cases'
)
api.add_resource(
AssessmentCaseEndpoint, '/assessments/<uuid:uuid>', endpoint='assessment_cases'
)
api.add_resource(ListAuditLogEndpoint, '/auditing', endpoint='list_audit_logs')
api.add_resource(AuditLogEndpoint, '/auditing/<uuid:uuid>', endpoint='audit_logs')
api.add_resource(HealthCheckEndpoint, '/healthcheck', endpoint='health_check')

# Add Swagger documentation to Flask app
Swagger(app, template_file='swagger.yml')
为了在Azure上部署应用程序,我们首先使用以下ARM模板构建工件(zip文件):
trigger:
- develop
variables:
webAppName: 'test-api'
environmentName: 'test-api'
projectRoot: $(System.DefaultWorkingDirectory)/WebApi
pythonVersion: '3.7'
stages:
- stage: Build
displayName: Build stage
jobs:
- job: BuildJob
steps:
- task: UsePythonVersion@0
inputs:
versionSpec: '$(pythonVersion)'
displayName: 'Use Python $(pythonVersion)'

- script: |
python3 -m venv .venv
source .venv/bin/activate
pip install --upgrade pip
pip install waitress==2.1.2
pip install setup
pip install -r requirements.txt
workingDirectory: $(projectRoot)
displayName: "Install requirements"
- task: ArchiveFiles@2
displayName: 'Archive files'
inputs:
rootFolderOrFile: '$(projectRoot)'
includeRootFolder: false
archiveType: zip
archiveFile: $(Build.ArtifactStagingDirectory)/$(webAppName).zip
replaceExistingArchive: true
- publish: $(Build.ArtifactStagingDirectory)/$(webAppName).zip
displayName: 'Upload package'
artifact: drop

然后我们将工件部署到Web应用程序上(使用Python 3.7托管在linux上)。为此,我们使用以下模板:

steps:
- task: AzureWebApp@1
displayName: 'Azure Web App Deploy: test-api'
inputs:
azureSubscription: 'azurerm-sp-apps-dev'
appType: webAppLinux
appName: 'testt-api'
package: '$(System.DefaultWorkingDirectory)/test-api-WebApi/drop/test-api.zip'
runtimeStack: 'PYTHON|3.7'
startUpCommand: 'python3.7 -m pip install -r requirements.txt && python3.7 -m pip install gunicorn && python3.7 -m gunicorn --bind=0.0.0.0:80 --workers=4 app:app'
configurationStrings: '-StartupCommand startup.sh'
deploymentMethod: zipDeploy

,我们还使用一个应用网关,配置如下:

steps:
- task: test-AppGateway.appGatewayCreate-task.test-AppGateway-CreateUpdate@1
displayName: 'Create or Update Application Gateway Config API'
inputs:
connectedServiceName: 'azurerm-sp-apps-dev'
AppName: 'test-api'
HealthProbeUri: /healthcheck
timeoutInMinutes: 120
retryCountOnTaskFailure: 10

但是在部署时,我们有以下奇怪的行为:

1 +在应用程序日志中(在可用性和性能菜单中),我们可以看到gunicorn服务器已经正确启动,并且它正在监听http://0.0.0.0:80,并且4个worker已经启动…

但在平台日志中,我们有以下错误:

Platform logs contain errors or warnings
Container test-api_0_a8607b6c for site test-api did not start within expected time limit.
Container test-api_0_a8607b6c didn't respond to HTTP pings on port: 8000, failing site start

我们已经调查了这个问题,这里有一些我们尝试过的解决方案,但没有成功:

  • 增加WEBSITES_CONTAINER_START_TIME_LIMIT的值:目前我们将其设置为1800
  • WEBSITES_PORT的值改为8000和80
  • 修改用于启动flask rest api的启动命令:tried waitress-serve, gunicorn, and flask so far
  • 我们还尝试更改端口号以启动flask服务器:80,5000,8000m 8081

3 +这导致/healthcheck端点不响应(503服务器不可用,导致502应用程序网关坏)

4 +遇到的另一个问题是,由于某种原因,在构建期间创建的虚拟环境在部署阶段无法识别:

  • 我们收到以下命令当我们试图激活它:/opt/startup/startup.sh: startup.sh: not found name source not found
  • 我们还注意到部署期间使用的python版本与我们定义的虚拟环境不同

这是部署内部使用的以下docker命令:

docker run -d -p 9286:8000 --expose=49494 --name test-api_0_f5ccb0ab -e WEBSITE_CORS_ALLOWED_ORIGINS=https://test.dev.app.test.com,https://test.eastus2-dev-platform-ase1.appserviceenvironment.net -e WEBSITE_CORS_SUPPORT_CREDENTIALS=True -e WEBSITES_PORT=80 -e WEBSITE_SITE_NAME=test-api -e WEBSITE_AUTH_ENABLED=False -e WEBSITE_ROLE_INSTANCE_ID=0 -e WEBSITE_HOSTNAME=test-api.eastus2-dev-platform-ase1.appserviceenvironment.net -e WEBSITE_INSTANCE_ID=MY-ID -e HTTP_LOGGING_ENABLED=1 -e WEBSITE_USE_DIAGNOSTIC_SERVER=True appsvc/python:3.7_20221128.12.tuxprod python3.7 -m pip install -r requirements.txt && python3.7 -m pip install gunicorn && python3.7 -m gunicorn --bind=0.0.0.0:80 --workers=4 app:app

你知道怎么让它工作吗?

在花费大量时间试图找到问题的根源之后;通过将启动命令更改为以下内容,我最终使其余api正常工作:

gunicorn -w 2 -k uvicorn.workers.UvicornWorker app:app

现在所有的错误都消失了,web应用程序功能齐全。此外,您必须将以下内容添加到requirements.txt:

gunicorn~=20.1.0
uvicorn~=0.20.0

相关内容

  • 没有找到相关文章

最新更新