Backend & 인프라

[Nginx] 로드 밸런싱(Load balancing)

UnoCoding 2022. 7. 5. 09:03
오늘은 로드 밸런싱(Load balancing)에 대해 알아 보도록 하자.

🧙 우리의 목표!!

✅ 단일 서버 개념 🔎 

위와 같이

FastAPI 서버 가 한개라면 한곳에 부하가 걸리겠져

이떄 부하Load라고 합니다.

 

위 그림처럼 사용자가 늘어서 서버가 힘들어 하겠져

 

이러한 부하를 고르게(Balance) 분배 하려면 Load Balancing를 하면 됩니다.

 

👉👉 소스코드는 이전 글인 [FastApi] CPU를 많이 사용하는 APP 만들기 (CPU Bound)

👉👉 Git Actions는 이전 글인 [Git Actions] SSH으로 배포하기

⚡ 실습 1(Nginx로 Load Balancing) 

nginx를 설치 하셨을 경우 
설정 파일 경로 : sudo vi /etc/nginx/nginx.conf
upstream cpu-bound-app {
  server {instance_1번의_ip}:8080 weight=100 max_fails=3 fail_timeout=3s;
  server {instance_2번의_ip}:8080 weight=100 max_fails=3 fail_timeout=3s;
  server {instance_3번의_ip}:8080 weight=100 max_fails=3 fail_timeout=3s;
}

location / {
  proxy_pass http://cpu-bound-app;
  proxy_http_version 1.1;
  proxy_set_header Upgrade $http_upgrade;
  proxy_set_header Connection 'upgrade';
  proxy_set_header Host $host;
  proxy_cache_bypass $http_upgrade;
}

proxy_set_header Upgrade 

proxy_set_header Connection

proxy_set_header Host

에 대한 내용은 아래 블로그를 참조 하면 좋다.

https://hyeon9mak.github.io/nginx-web-socket-proxy-configuration/

 

NGINX 웹 소켓 프록시 설정

🥃 summary Nginx는 버전 1.3부터 ​​WebSocket을 지원하며, WebSocket의 로드 밸런싱 을 수행 할 수 있다.

hyeon9mak.github.io

upstream 만들기

upstream <업스트림 이름> {
    <로드밸런스 타입: defulat는 round-robin>
    server <host1>:<port1>
    ...
    server <host2>:<port2>
}

실제 적용 nginx.conf

😵‍💫 혹시나 실행을 했을 때

I tried installing uvicorn through "pip install "uvicorn[standard]" ", got the following error:

이런 문구가 있으신 분들은

 

프로젝트의 requirements.txt에 websockets를 설치 하시기 바랍니다.

pip install websockets

 

💟 websockets

 

⚡ 실습 2(Git Actions로 배포 하기) 

name: deploy-every-fastapi
on:
  push:
    branches: master

jobs:
  main-deploy:
    runs-on: ubuntu-latest
    steps:
      - name: Deploy FastApi MainServer(master)
        uses: appleboy/ssh-action@master
        with:
          host: ${{ secrets.SERVER_HOST }}
          username: ${{ secrets.SERVER_USERNAME }}
          password: ${{ secrets.SERVER_PASSWORD }}
          port: ${{ secrets.SERVER_PORT }}
          script: |
            cd ~/Fastapi-cpu-bound 
            git pull
            docker build -t ehghksvjscl/fastapi-cpu-bound .
            docker push ehghksvjscl/fastapi-cpu-bound
            docker rm fastapi-test --force
            docker rmi ehghksvjscl/fastapi-cpu-bound:latest
            nohup docker run -p 8888:8888 --name fastapi-test  ehghksvjscl/fastapi-cpu-bound > nohup.out 2>&1 &
  sub-deplo1y:
    runs-on: ubuntu-latest
    needs: main-deploy
    steps:
      - name: Deploy FastApi Server1(sub)
        uses: appleboy/ssh-action@master
        with:
          host: ${{ secrets.SERVER2_HOST }}
          username: ${{ secrets.SERVER_USERNAME }}
          password: ${{ secrets.SERVER_PASSWORD }}
          port: ${{ secrets.SERVER2_PORT }}
          script: |
            docker rm fastapi-test --force
            docker rmi ehghksvjscl/fastapi-cpu-bound:latest
            nohup docker run -p 8888:8888 --name fastapi-test  ehghksvjscl/fastapi-cpu-bound > nohup.out 2>&1 &
      
  sub-deplo2y:
    runs-on: ubuntu-latest
    needs: main-deploy
    steps:
      - name: Deploy FastApi Server2(sub)
        uses: appleboy/ssh-action@master
        with:
          host: ${{ secrets.SERVER3_HOST }}
          username: ${{ secrets.SERVER_USERNAME }}
          password: ${{ secrets.SERVER_PASSWORD }}
          port: ${{ secrets.SERVER_PORT }}
          script: |
            docker rm fastapi-test --force
            docker rmi ehghksvjscl/fastapi-cpu-bound:latest
            nohup docker run -p 8888:8888 --name fastapi-test  ehghksvjscl/fastapi-cpu-bound > nohup.out 2>&1 &

⚡ 확인