FastAPI+Uvicorn部署AWS Fargate后运行1-2分钟自动关闭求助
AWS Fargate上FastAPI服务自动关闭问题求助
我在AWS Fargate上通过Application Load Balancer部署了FastAPI 0.81.0 + uvicorn 0.18.3(基于Python 3.10.1)的服务。该服务在本地Docker中可正常持续运行,但在AWS环境下总会在1-2分钟后自动关闭。
Docker启动命令
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服务的内存占用并不高,问题可能与负载均衡有关。常见的原因是使用TCP而非HTTP进行健康检查,但据我所知,Fargate任务定义或EC2目标组默认已采用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]
恳请各位提供解决该问题的建议,谢谢!
内容的提问来源于stack exchange,提问作者JoN
相关产品推荐
相关产品推荐

