【发布时间】:2022-10-07 02:59:44
【问题描述】:
我在 AWS Fargate 上使用 Python 3.10.1 和 Application Load Balancer 部署了 FastAPI 0.81.0 + uvicorn 0.18.3。服务器在我的本地 Docker 中无限期地运行(如预期的那样),但是在 AWS 上,应用程序总是在 1-2 分钟后关闭。
这是 Docker 中的 uvicorn 调用:
CMD [\"uvicorn\", \"--host\", \"0.0.0.0\", \"--port\", \"8000\", \"--log-level\", \"trace\", \"app.main:app\"]
我的 FastAPI 应用程序如下所示:
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
app = FastAPI()
app = FastAPI()
origins = [
\"*\"
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=[\"*\"],
allow_headers=[\"*\"],
)
@app.get(\"/\")
async def root():
return {\"Hello\": \"World\"}
这可能与负载均衡器有关,因为我的 Fargate 服务的 RAM 使用率并不太高:
通常的嫌疑人似乎是health checks via TCP instead of HTTP,但是在 Fargate 任务定义或 EC2 目标组中,默认情况下,AFAIK 已经分别通过 HTTP 进行健康检查。
这是我的 Fargate 任务的日志:
2022-09-22 18:43:46 INFO: Finished server process [1]
2022-09-22 18:43:46 INFO: Waiting for application shutdown.
2022-09-22 18:43:46 TRACE: ASGI [1] Receive {\'type\': \'lifespan.shutdown\'}
2022-09-22 18:43:46 TRACE: ASGI [1] Send {\'type\': \'lifespan.shutdown.complete\'}
2022-09-22 18:43:46 TRACE: ASGI [1] Completed
2022-09-22 18:43:46 INFO: Application shutdown complete.
2022-09-22 18:43:45 INFO: Shutting down
2022-09-22 18:43:39 TRACE: 172.31.21.3:16662 - ASGI [7] Send {\'type\': \'http.response.body\', \'body\': \'<17 bytes>\'}
2022-09-22 18:43:39 TRACE: 172.31.21.3:16662 - ASGI [7] Completed
2022-09-22 18:43:39 TRACE: 172.31.21.3:16662 - HTTP connection lost
2022-09-22 18:43:39 INFO: 172.31.21.3:16662 - \"GET / HTTP/1.1\" 200 OK
2022-09-22 18:43:39 TRACE: 172.31.21.3:16662 - HTTP connection made
2022-09-22 18:43:39 TRACE: 172.31.21.3:16662 - ASGI [7] Started scope={\'type\': \'http\', \'asgi\': {\'version\': \'3.0\', \'spec_version\': \'2.3\'}, \'http_version\': \'1.1\', \'server\': (\'172.31.30.157\', 8000), \'client\': (\'172.31.21.3\', 16662), \'scheme\': \'http\', \'method\': \'GET\', \'root_path\': \'\', \'path\': \'/\', \'raw_path\': b\'/\', \'query_string\': b\'\', \'headers\': \'<...>\'}
2022-09-22 18:43:39 TRACE: 172.31.21.3:16662 - ASGI [7] Send {\'type\': \'http.response.start\', \'status\': 200, \'headers\': \'<...>\'}
2022-09-22 18:43:39 INFO: 172.31.47.71:3856 - \"GET / HTTP/1.1\" 200 OK
2022-09-22 18:43:39 TRACE: 172.31.47.71:3856 - ASGI [6] Send {\'type\': \'http.response.body\', \'body\': \'<17 bytes>\'}
2022-09-22 18:43:39 TRACE: 172.31.47.71:3856 - ASGI [6] Completed
2022-09-22 18:43:39 TRACE: 172.31.47.71:3856 - HTTP connection lost
2022-09-22 18:43:39 TRACE: 172.31.47.71:3856 - HTTP connection made
2022-09-22 18:43:39 TRACE: 172.31.47.71:3856 - ASGI [6] Started scope={\'type\': \'http\', \'asgi\': {\'version\': \'3.0\', \'spec_version\': \'2.3\'}, \'http_version\': \'1.1\', \'server\': (\'172.31.30.157\', 8000), \'client\': (\'172.31.47.71\', 3856), \'scheme\': \'http\', \'method\': \'GET\', \'root_path\': \'\', \'path\': \'/\', \'raw_path\': b\'/\', \'query_string\': b\'\', \'headers\': \'<...>\'}
2022-09-22 18:43:39 TRACE: 172.31.47.71:3856 - ASGI [6] Send {\'type\': \'http.response.start\', \'status\': 200, \'headers\': \'<...>\'}
2022-09-22 18:43:09 TRACE: 172.31.21.3:39448 - ASGI [5] Completed
2022-09-22 18:43:09 TRACE: 172.31.21.3:39448 - HTTP connection lost
2022-09-22 18:43:09 TRACE: 172.31.21.3:39448 - ASGI [5] Send {\'type\': \'http.response.body\', \'body\': \'<17 bytes>\'}
2022-09-22 18:43:09 TRACE: 172.31.21.3:39448 - HTTP connection made
2022-09-22 18:43:09 TRACE: 172.31.21.3:39448 - ASGI [5] Started scope={\'type\': \'http\', \'asgi\': {\'version\': \'3.0\', \'spec_version\': \'2.3\'}, \'http_version\': \'1.1\', \'server\': (\'172.31.30.157\', 8000), \'client\': (\'172.31.21.3\', 39448), \'scheme\': \'http\', \'method\': \'GET\', \'root_path\': \'\', \'path\': \'/\', \'raw_path\': b\'/\', \'query_string\': b\'\', \'headers\': \'<...>\'}
2022-09-22 18:43:09 TRACE: 172.31.21.3:39448 - ASGI [5] Send {\'type\': \'http.response.start\', \'status\': 200, \'headers\': \'<...>\'}
2022-09-22 18:43:09 INFO: 172.31.21.3:39448 - \"GET / HTTP/1.1\" 200 OK
2022-09-22 18:43:09 TRACE: 172.31.47.71:50778 - ASGI [4] Completed
2022-09-22 18:43:09 TRACE: 172.31.47.71:50778 - HTTP connection lost
2022-09-22 18:43:09 TRACE: 172.31.47.71:50778 - ASGI [4] Send {\'type\': \'http.response.start\', \'status\': 200, \'headers\': \'<...>\'}
2022-09-22 18:43:09 INFO: 172.31.47.71:50778 - \"GET / HTTP/1.1\" 200 OK
2022-09-22 18:43:09 TRACE: 172.31.47.71:50778 - ASGI [4] Send {\'type\': \'http.response.body\', \'body\': \'<17 bytes>\'}
2022-09-22 18:43:09 TRACE: 172.31.47.71:50778 - HTTP connection made
2022-09-22 18:43:09 TRACE: 172.31.47.71:50778 - ASGI [4] Started scope={\'type\': \'http\', \'asgi\': {\'version\': \'3.0\', \'spec_version\': \'2.3\'}, \'http_version\': \'1.1\', \'server\': (\'172.31.30.157\', 8000), \'client\': (\'172.31.47.71\', 50778), \'scheme\': \'http\', \'method\': \'GET\', \'root_path\': \'\', \'path\': \'/\', \'raw_path\': b\'/\', \'query_string\': b\'\', \'headers\': \'<...>\'}
2022-09-22 18:42:39 INFO: 172.31.47.71:55984 - \"GET / HTTP/1.1\" 200 OK
2022-09-22 18:42:39 TRACE: 172.31.47.71:55984 - ASGI [3] Send {\'type\': \'http.response.start\', \'status\': 200, \'headers\': \'<...>\'}
2022-09-22 18:42:39 TRACE: 172.31.47.71:55984 - ASGI [3] Send {\'type\': \'http.response.body\', \'body\': \'<17 bytes>\'}
2022-09-22 18:42:39 TRACE: 172.31.47.71:55984 - ASGI [3] Completed
2022-09-22 18:42:39 TRACE: 172.31.47.71:55984 - HTTP connection lost
2022-09-22 18:42:39 TRACE: 172.31.21.3:59240 - HTTP connection lost
2022-09-22 18:42:39 TRACE: 172.31.47.71:55984 - ASGI [3] Started scope={\'type\': \'http\', \'asgi\': {\'version\': \'3.0\', \'spec_version\': \'2.3\'}, \'http_version\': \'1.1\', \'server\': (\'172.31.30.157\', 8000), \'client\': (\'172.31.47.71\', 55984), \'scheme\': \'http\', \'method\': \'GET\', \'root_path\': \'\', \'path\': \'/\', \'raw_path\': b\'/\', \'query_string\': b\'\', \'headers\': \'<...>\'}
2022-09-22 18:42:39 TRACE: 172.31.21.3:59240 - ASGI [2] Completed
2022-09-22 18:42:39 TRACE: 172.31.47.71:55984 - HTTP connection made
2022-09-22 18:42:39 INFO: 172.31.21.3:59240 - \"GET / HTTP/1.1\" 200 OK
2022-09-22 18:42:39 TRACE: 172.31.21.3:59240 - ASGI [2] Send {\'type\': \'http.response.body\', \'body\': \'<17 bytes>\'}
2022-09-22 18:42:39 TRACE: 172.31.21.3:59240 - ASGI [2] Started scope={\'type\': \'http\', \'asgi\': {\'version\': \'3.0\', \'spec_version\': \'2.3\'}, \'http_version\': \'1.1\', \'server\': (\'172.31.30.157\', 8000), \'client\': (\'172.31.21.3\', 59240), \'scheme\': \'http\', \'method\': \'GET\', \'root_path\': \'\', \'path\': \'/\', \'raw_path\': b\'/\', \'query_string\': b\'\', \'headers\': \'<...>\'}
2022-09-22 18:42:39 TRACE: 172.31.21.3:59240 - ASGI [2] Send {\'type\': \'http.response.start\', \'status\': 200, \'headers\': \'<...>\'}
2022-09-22 18:42:39 TRACE: 172.31.21.3:59240 - HTTP connection made
2022-09-22 18:42:30 INFO: Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit)
2022-09-22 18:42:30 INFO: Waiting for application startup.
2022-09-22 18:42:30 TRACE: ASGI [1] Started scope={\'type\': \'lifespan\', \'asgi\': {\'version\': \'3.0\', \'spec_version\': \'2.0\'}}
2022-09-22 18:42:30 TRACE: ASGI [1] Receive {\'type\': \'lifespan.startup\'}
2022-09-22 18:42:30 TRACE: ASGI [1] Send {\'type\': \'lifespan.startup.complete\'}
2022-09-22 18:42:30 INFO: Application startup complete.
2022-09-22 18:42:30 INFO: Started server process [1]
关于如何解决这个问题的任何建议?谢谢!
-
您介意提供您的 python 版本和完整的 Dockerbuild 文件吗?
-
我在
python:3.10.1下运行它。 Dockerbuild 文件还仅包含 pip 和诗歌安装。 -
是的。最大值永远不会超过 30%。
标签: amazon-web-services fastapi aws-fargate aws-application-load-balancer uvicorn