๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Back-end & Server/FastAPI

[FastAPI] ๋ฐฐํฌ

728x90
๋ฐ˜์‘ํ˜•

 

๐Ÿฐ FastAPI ๊ณต์‹๋ฌธ์„œ๋ฅผ ๋ณด๋ฉด์„œ ๊ฐœ์ธ์ ์œผ๋กœ ์ •๋ฆฌํ•œ ๊ธ€ ์ž…๋‹ˆ๋‹ค.

 

 

FastAPI์˜ ๋ฐฐํฌ๋Š” ๋น„๊ต์  ์‰ฝ๋‹ค. 

 

๋ฒ„์ „

FastAPI, Starlette, Pydantic์˜ ๋ฒ„์ „์ด ํ˜ธํ™˜๋˜๋„๋ก ์„ค์ •ํ•œ๋‹ค.

 

Release Notes - FastAPI

FastAPI framework, high performance, easy to learn, fast to code, ready for production

fastapi.tiangolo.com

 

 

๋ฐฐํฌ

๋ฐฐํฌ์— ์•ž์„œ ์ด ๋ถ€๋ถ„๋“ค์„ ์‹ ๊ฒฝ์จ์•ผํ•œ๋‹ค.

  • Security - HTTPS
  • Running on Start Up
  • Restarts
  • Replication(the number of processes running)
  • Memory
  • Previous steps before starting

 

Security -HTTPS

์ด ๋ถ€๋ถ„์€ SSL ๊ฐ™์€ ๋ณด์•ˆ ์„ธํŒ…์„ ๊ตฌ๋งคํ•˜์—ฌ ํ˜ธ์ŠคํŒ… ์„œ๋ฒ„ ๋“ฑ์— ์ ์šฉ ํ•˜๋ฉด๋˜๋Š” ๋ถ€๋ถ„์ด๋ฏ€๋กœ ๋„˜์–ด๊ฐ€๊ฒ ๋‹ค.

 

 

Running on Start UP

์›๊ฒฉ ์„œ๋ฒ„(ํด๋ผ์šฐ๋“œ, ๊ฐ€์ƒ๋จธ์‹  ๋“ฑ)๋ฅผ ์„ค์ • ํ•  ๋•Œ, Uvicorn(๋˜๋Š” ์œ ์‚ฌ ์„œ๋ฒ„)์„ ์ˆ˜๋™์œผ๋กœ ์‹คํ–‰ํ•˜๊ฒŒ ์„ธํŒ…ํ•œ๋‹ค. 

๊ทธ๋Ÿฌ๋‚˜ ์„œ๋ฒ„ ์—ฐ๊ฒฐ์ด ๋Š์–ด์ง€๋ฉด ์‹คํ–‰ ์ค‘์ธ ํ”„๋กœ์„ธ์Šค๊ฐ€ ์ข…๋ฃŒ๋  ์ˆ˜ ์žˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ์„œ๋ฒ„๊ฐ€ ๋‹ค์‹œ ์‹œ์ž‘๋˜๋ฉด ํ”„๋กœ์„ธ์Šค๋ฅผ ๋‹ค์‹œ ์‹œ์ž‘ํ•˜๊ธฐ ์ „๊นŒ์ง€๋Š” API ์„œ๋ฒ„๋Š” ์ฃฝ์€ ์ƒํƒœ๋กœ ์œ ์ง€๋œ๋‹ค.

 

์ผ๋ฐ˜์ ์œผ๋กœ ์„œ๋ฒ„ ๊ตฌ์ถ• ์‹œ ์‚ฌ๋žŒ์˜ ๊ฐœ์ž… ์—†์ด ํ”„๋กœ์„ธ์Šค๊ฐ€ ํ•ญ์ƒ API(Uvicorn ๊ฐ™์€ FastAPI ์•ฑ์„ ์‹คํ–‰ํ•˜๋Š” ๊ฒƒ)์™€ ํ•จ๊ป˜ ์‹คํ–‰๋˜๊ธฐ๋ฅผ ์›ํ•œ๋‹ค.

์ด๋ฅผ ์œ„ํ•ด๋Š” ๋ณ„๋„์˜ ํ”„๋กœ๊ทธ๋žจ์„ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

์ด ๋ณ„๋„์˜ ํ”„๋กœ๊ทธ๋žจ์€ DB์™€ ๊ฐ™์€ ๋‹ค๋ฅธ ๊ตฌ์„ฑ ์š”์†Œ๋‚˜ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜๋„ ๊ฐ™์ด ์‹คํ–‰๋˜๋„๋ก ํ•ด์•ผํ•œ๋‹ค.

 

์˜ˆ์ œ ๋„๊ตฌ

  • Docker
  • Kubernetes
  • Docker Compose
  • Docker in Swarm Mode
  • Systemd
  • Supervisor
  • Hadled internally by a cloud provider as part of their services
  • Others....

 

Restarting

FastAPI App์ด ์‹คํ–‰ ์ค‘์— ์ธ๊ฐ„์˜ ์‹ค์ˆ˜ ๋˜๋Š” ์–ด๋–ค ์ด์œ ๋กœ ๋ฒ„๊ทธ๋กœ ์ธํ•ด ์˜ค๋ฅ˜๋ฅผ ๋ฐœ์ƒ ์‹œํ‚จ๋‹ค. 

 

์ž‘์€ ์˜ค๋ฅ˜๋Š” FastAPI ์ž์ฒด์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•œ๋‹ค.

  • ์ฝ”๋“œ์— ์˜ค๋ฅ˜๊ฐ€ ์žˆ์œผ๋ฉด FastAPI๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ ์˜ค๋ฅ˜๋ฅผ ๋ฐœ์ƒ์‹œํ‚จ ๋‹จ์ผ ์š”์ฒญ์— ํ•ด๋‹น ์˜ค๋ฅ˜๋ฅผ ํฌํ•จํ•œ๋‹ค.
  • ํด๋ผ์ด์–ธํŠธ๋Š” ํ•ด๋‹น ์š”์ฒญ์— ๋Œ€ํ•ด 500 Error๋ฅผ ๋ฐ›๊ฒŒ ๋˜์ง€๋งŒ App์€ ์™„์ „ํžˆ ์ถฉ๋Œํ•˜๋Š” ๋Œ€์‹  ๋‹ค์Œ ์š”์ฒญ์— ๋Œ€ํ•ด ๊ณ„์† ์ž‘์—…์„ ํ•œ๋‹ค.

 

์ด๋Ÿฌํ•œ ํŠน์„ฑ์—๋„ ๋ถˆ๊ตฌํ•˜๊ณ  ์ „์ฒด App์„ ์ถฉ๋Œ ์‹œํ‚ค๋Š” ์ผ๋ถ€ ์ฝ”๋“œ๋กœ ์ธํ•˜์—ฌ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜์—ฌ ์„œ๋ฒ„๊ฐ€ ์ค‘๋‹จ๋˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ์„ ๊ฒƒ์ด๋‹ค.

 

์ด๋•Œ๋Š” ์ˆ˜์ • ํ›„ ์„œ๋ฒ„๋ฅผ ๋‹ค์‹œ ์‹œ์ž‘ํ•˜๋Š” ๊ณผ์ •์ด ํ•„์š”ํ•  ๊ฒƒ ์ด๋‹ค.

 

์ด๋Ÿฌํ•œ ์—ญํ• ์„ ํ•˜๋Š” ๋„๊ตฌ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

  • Docker
  • Kubernetes
  • Docker Compose
  • Docker in Swarm Mode
  • Systemd
  • Supervisor
  • Hadled internally by a cloud provider as part of their services
  • Others....

 

 

Replication(๋ณต์ œ)

FastAPI App์„ ์‚ฌ์šฉํ•˜๋ฉด Uvicorn๊ณผ ๊ฐ™์€ ์„œ๋ฒ„ ํ”„๋กœ๊ทธ๋žจ์„ ์‚ฌ์šฉํ•˜์—ฌ ํ•˜๋‚˜์˜ ํ”„๋กœ์„ธ์Šค์—์„œ ํ•œ ๋ฒˆ ์‹คํ–‰ํ•˜๋ฉด ์—ฌ๋Ÿฌ ํด๋ผ์ด์–ธํŠธ์— ๋™์‹œ์— ์„œ๋ฒ„์Šค๋ฅผ ์ œ๊ณตํ•  ์ˆ˜ ์žˆ๋‹ค.

 

๊ทธ๋Ÿฌ๋‚˜ ๋Œ€๋ถ€๋ถ„์˜ ๊ฒฝ์šฐ ์—ฌ๋Ÿฌ ํ”„๋กœ์„ธ์Šค๋ฅผ ๋™์‹œ์— ์‹คํ–‰ํ•˜๊ณ  ์‹ถ์„ ๊ฒƒ์ด๋‹ค.

 

๋‹จ์ผ ํ”„๋กœ์„ธ์Šค๊ฐ€ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ๋ณด๋‹ค ๋” ๋งŽ์€ ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์žˆ๊ณ , ์„œ๋ฒ„์˜ CPU๊ฐ€ ์—ฌ๋Ÿฌ ์ฝ”์–ด๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ, ๋™์ผํ•œ App์œผ๋กœ ๋™์‹œ์— ์—ฌ๋Ÿฌ ํ”„๋กœ์„ธ์Šค๋ฅผ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๊ณ  ๋ชจ๋“  ์š”์ฒญ์„ ํ”„๋กœ์„ธ์Šค๋“ค์—๊ฒŒ ๋ถ„๋ฐฐํ•œ๋‹ค.

 

๋™์ผํ•œ API ํ”„๋กœ๊ทธ๋žจ์˜ ์—ฌ๋Ÿฌ ํ”„๋กœ์„ธ์Šค๋ฅผ ์‹คํ–‰ํ•˜๋Š” ๊ฒฝ์šฐ ์ด๋ฅผ ์ผ๋ฐ˜์ ์œผ๋กœ "Workers"๋ผ๊ณ  ํ•œ๋‹ค.

 

์„œ๋ฒ„์˜ ํ•˜๋‚˜์˜ ํฌํŠธ์™€ IP ์ฃผ์†Œ ์กฐํ•ฉ์„ ํ•˜๋‚˜์˜ ํ”„๋กœ์„ธ์Šค๋งŒ ์ˆ˜์‹ ํ•  ์ˆ˜ ์žˆ๋‹ค.

๋”ฐ๋ผ์„œ ๋™์‹œ์— ์—ฌ๋Ÿฌ ํ”„๋กœ์„ธ์Šค๋ฅผ ๊ฐ€์ง€๋ ค๋ฉด ์–ด๋–ค ๋ฐฉ์‹์œผ๋กœ๋“  ๊ฐ "Worker ํ”„๋กœ์„ธ์Šค"์— ํ†ต์‹ ์„ ์ „์†กํ•˜๋Š” ํฌํŠธ๋ฅผ ์ˆ˜์‹ ํ•˜๋Š” ๋‹จ์ผ ํ”„๋กœ์„ธ์Šค๊ฐ€ ์žˆ์–ด์•ผ ํ•œ๋‹ค.

์ด๋Ÿฐ ๋™์ž‘์„ ํ•˜๋Š” ํ”„๋กœ์„ธ์Šค๋ฅผ Process Manager๋ผ๊ณ  ํ•œ๋‹ค.

 

๋‹ค์ค‘ ํ”„๋กœ์„ธ์Šค์˜ ์˜ˆ์‹œ

์ถœ์ฒ˜ : FastAPI Docs

 

์ด๋Ÿฐ ๋‹ค์ค‘ ํ”„๋กœ์„ธ์Šค ๋ฐฉ์‹์„ ๋‹ฌ์„ฑํ•˜๋ ค๋ฉด ์—ฌ๋Ÿฌ ๋„๊ตฌ๋“ค์„ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

  • Gunicorn์€ Uvicorn Worker๋ฅผ ๊ด€๋ฆฌํ•œ๋‹ค.
    • Gunicorn์€ IP ๋ฐ Port๋ฅผ ์ˆ˜์‹ ํ•˜๋Š” Process Manager๊ฐ€ ๋˜๋ฉฐ ๋ณต์ œ๋Š” ์—ฌ๋Ÿฌ Uvicorn Worker Process๋ฅผ ์‚ฌ์šฉํ•จ์œผ๋กœ์จ ๊ฐ€๋Šฅํ•˜๋‹ค.

 

  • Uvicorn Wokers๋“ค์„ ๊ด€๋ฆฌํ•˜๋Š” Uvicorn
    • ํ•˜๋‚˜์˜ Uvicorn Process Manager๋Š” IP ๋ฐ Port๋ฅผ ์ˆ˜์‹ ํ•˜๊ณ  ์—ฌ๋Ÿฌ Uvicorn Worker Process๋ฅผ ์‹œ์ž‘ํ•œ๋‹ค.

 

  • Kubernetes ๋ฐ ๊ธฐํƒ€ ๋ถ„์‚ฐ ์ปจํ…Œ์ด๋„ˆ ์‹œ์Šคํ…œ
    • Kubernetes ๊ณ„์ธต์˜ ๋ฌด์–ธ๊ฐ€๊ฐ€ IP ๋ฐ Port๋ฅผ ์ˆ˜์‹ ํ•œ๋‹ค.
    • ๋ณต์ œ๋Š” ๊ฐ๊ฐ์˜ ํ•˜๋‚˜ Uvicorn Process๊ฐ€ ์‹คํ–‰๋˜๋Š” ์—ฌ๋Ÿฌ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ๊ฐ€์ง์œผ๋กœ์จ ์ด๋ฃจ์–ด์ง„๋‹ค.

 

  • ๋ณต์ œ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ฃผ๋Š” ํด๋ผ์šฐ๋“œ ์„œ๋น„์Šค
    • ํด๋ผ์šฐ๋“œ ์„œ๋น„์Šค ์ž์ฒด์ ์œผ๋กœ ๋ณต์ œ์ฒ˜๋ฆฌ๋ฅผ ํ•  ๊ฒƒ์ด๋‹ค.
    • ์‹คํ–‰ํ•  ํ”„๋กœ์„ธ์Šค๋‚˜ ์‚ฌ์šฉํ•  ์ปจํ…Œ์ด๋„ˆ ์ด๋ฏธ์ง€๋ฅผ ์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

Server Worker - Gunicorn, Uvicorn

Gunicorn์€ WSGI ํ‘œ์ค€์„ ์ฃผ๋กœ ์‚ฌ์šฉํ•˜๋Š” App ์„œ๋ฒ„์ด๋‹ค. (Flask, Django์™€ ๊ฐ™์€ App์„ ์ œ๊ณตํ•  ์ˆ˜ ์žˆ๋‹ค.)

Gunicorn ์ž์ฒด๋Š” ASGI ํ‘œ์ค€์„ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— FastAPI์™€ ํ˜ธํ™˜๋˜์ง€ ์•Š๋Š”๋‹ค. 

 

๊ทธ๋Ÿฌ๋‚˜ Gunicorn์€ Process Manager ์—ญํ• ์„ ํ•˜๊ณ  ์‚ฌ์šฉ์ž์—๊ฒŒ ํŠน์ • Worker Process์˜ Class๋ฅผ ์•Œ๋ ค์ค€๋‹ค.

๊ทธ๋Ÿฐ ๋‹ค์Œ Gunicorn์€ ํ•ด๋‹น Class๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•˜๋‚˜ ์ด์ƒ์˜ Worker Process๋ฅผ ์‹œ์ž‘ํ•œ๋‹ค.

 

์ด ์กฐํ•ฉ์„ ์‚ฌ์šฉํ•˜์—ฌ Gunicorn์€ Process Manager์˜ ์—ญํ• ์„ ํ•˜๋ฉฐ Port์™€ IP๋ฅผ ๊ด€์ฐฐํ•˜๊ณ , Uvicorn Class๋ฅผ ์‹คํ–‰ํ•˜๋Š” Worker Process๋กœ ํ†ต์‹  ์ •๋ณด๋ฅผ ์ „์†กํ•œ๋‹ค.

๊ทธ๋ฆฌ๊ณ  Gunicorn๊ณผ ํ˜ธํ™˜๋˜๋Š” Uvicorn Worker Class๋Š” Gunicorn์ด ๋ณด๋‚ธ ๋ฐ์ดํ„ฐ๋ฅผ FastAPI์—์„œ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•œ ASGI ํ‘œ์ค€์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ์ผ์„ ๋‹ด๋‹นํ•œ๋‹ค.

 

Gunicorn, Uvicorn ์„ค์น˜

pip install "uvicorn[standard]" gunicorn

 

Gunicorn ์‹คํ–‰ํ•˜๊ธฐ

4๊ฐœ์˜ Process Worker ์ƒ์„ฑ ํ›„ ์‹คํ–‰

python3 -m gunicorn main:app --workers 4 --worker-class uvicorn.workers.UvicornWorker --bind 0.0.0.0:80

 

worker 4๊ฐœ๊ฐ€ ์‹คํ–‰๋˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

Process Manager์˜ PID๋Š” 80863์ด๋‹ค.

 

gunicorn ์˜ต์…˜

  • --workers : ํ”„๋กœ์„ธ์Šค ๊ฐฏ์ˆ˜๋‹ค (์ตœ๋Œ€ vcpu ๊ฐฏ์ˆ˜ * 2 ๋งŒํผ ์„ค์ •ํ•˜๊ธฐ๋ฅผ ๊ถŒ์žฅ)
  • --worker-class : ํ”„๋กœ์„ธ์Šค๋ฅผ ๋‹ค์ค‘์œผ๋กœ ์‹คํ–‰ํ•˜๋ ค๋ฉด ํ•„์š”ํ•œ ์˜ต์…˜์ด๋‹ค.(uvicorn.workers.UvicornWorker)
  • --daemon : ๋ฐฑ๊ทธ๋ผ์šด๋“œ๋กœ ์‹คํ–‰ํ•œ๋‹ค.
  • --access-logfile ./log.log : log.log ์ด๋ฆ„์œผ๋กœ ๋กœ๊ทธ๋ฅผ ๊ธฐ๋กํ•œ๋‹ค.

 

์˜ˆ์‹œ

gunicorn main:app --workers 2 --worker-class uvicorn.workers.UvicornWorker --daemon --access-logfile ./log.log

 

 

์ปจํ…Œ์ด๋„ˆ ๋ฐฐํฌ

 

'ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๊ณต๋ถ€/Docker' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๊ธ€ ๋ชฉ๋ก

๊ฐœ๋ฐœ ๊ณต๋ถ€ํ•˜๋Š” ๋ธ”๋กœ๊ทธ!

pupbani.tistory.com

Docker ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ํด๋ผ์šฐ๋“œ ๊ฐ™์€ ์„œ๋ฒ„์— ๋ฐฐํฌํ•˜๋ฉด ๋œ๋‹ค.

728x90
๋ฐ˜์‘ํ˜•

'Back-end & Server > FastAPI' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[FastAPI] Metadata Docs, Testing, Debugging  (0) 2024.02.24
[FastAPI] Template  (0) 2024.02.23
[FastAPI] Background Task  (0) 2024.02.23
[FastAPI] ํŒŒ์ผ ๋ถ„ํ•   (0) 2024.02.23
[FastAPI] Relational Database  (0) 2024.02.22