project/markit

[GCP] Google App Engine에 Flask 프로젝트 배포하기

ㅈㅣ니 2023. 12. 1. 03:48

이번에는 Google Cloud Platfrom 에 있는 App Engine 서비스를 이용하여 웹 어플리케이션을 배포할 것이다.

 

App Engine이란 간단하게 말하자면 웹 어플리케이션을 배포, 관리 할 수 있는 서버리스 플랫폼이다.

 

Google Cloud 대시보드에 들어가서 좌측 메뉴의 App Engine 에 들어간다.

보통 App Engine에 처음 들어가면, 시작하기 버튼을 눌러서 애플리케이션을 생성해줘야한다.

나는 Flask 프로젝트를 배포할 예정이라 Language를 python으로 지정해줬다.

애플리케이션이 생성됐다는 가정하에 배포했던 방법을 기록해보겠다.

 

1) 메뉴 상단의 google cloud shell 을 클릭한다.

 

그러면 cloud shell 이 켜진다.

 

2) 깃허브에 올린 프로젝트를 클론해온다. (private 한 repository를 clone해옴)

 

private repository를 클론할 때는 ssh key가 있어야한다.

https://yegenie2.tistory.com/65

 

[Git] Github SSH key 등록하기 (Windows)

보통 repository를 clone할 때 HTTPS 주소를 가져와서 클론하는 경우가 많다. 하지만 private 한 repository는 SSH로 클론해와야한다. 이때, SSH Key를 등록해야 로그인 인증을 할 수 있다. 그래서 이번에는 SSH K

yegenie2.tistory.com

 

github에 프로젝트 올리는 방법

https://yegenie2.tistory.com/66

 

[Git] Github에 프로젝트 올리기

1) 먼저 깃허브에 들어가서 새로운 리파지토리를 생성해준다. 2) Repository name 이랑 공개 여부를 설정해주고 create 해준다. 3) 생성된 저장소의 주소는 뒤에 쓰이니까 기억해둔다. 4) 업로드할 프로

yegenie2.tistory.com

 

위의 과정을 거친 다음에 진행한다.

 

repository 클론해오기 (ssh 로 경로 주소 받아야함 )

git clone git@github.com:경로

위와 같이 입력하면 

Enter passphrase for key ~~~~~ 이렇게 뜨는데,

ssh key를 발급받으면서 설정한 비밀번호를 입력해주면 repository가 clone된다.

 

3) app.py가 있는 디렉토리로 접근

cd Markit_api/source

 

참고로 프로젝트 디렉토리는 아래와 같이 구성하였다.

4) 가상환경 세팅

virtualenv --python python3  ~/envs/source

 

5) 가상환경 activate

source ~/envs/source/bin/activate

 

6) 라이브러리 설치

pip install -r requirements.txt

 

참고로 requirements.txt 파일은 다음과 같이 구성하였다.

[ requirements.txt ]

Flask
google-cloud-vision
google-cloud-aiplatform
gunicorn
Werkzeug

 

라이브러리가 설치 되었으면 app.py를 실행시켜본다.

python app.py

웹 미리보기를 눌러서 웹페이지를 확인해본다.

 

페이지가 잘 나오는 것을 확인하고. 바로 배포를 했다.

7) 배포

gcloud app deploy

 

Do you want to continue (y/n)? 이 나오면 y 누르고 enter

 

결과~~ (배포된 걸 확인할 수 있다)

+ 그리고 프로젝트에 app.yaml 파일에 다음과 같이 가상환경을 스펙을 구성해놨다.

[ app.yaml ]

# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

runtime: python
env: flex
entrypoint: gunicorn -b :$PORT app:app

runtime_config:
  python_version: 3

# This sample incurs costs to run on the App Engine flexible environment.
# The settings below are to reduce costs during testing and are not appropriate
# for production use. For more information, see:
# https://cloud.google.com/appengine/docs/flexible/python/configuring-your-app-with-app-yaml
manual_scaling:
  instances: 2
resources:
  cpu: 2
  memory_gb: 2
  disk_size_gb: 10

 

배포 후 App Engine 대시보드에 들어가서 확인해보면 잘 배포된 것을 확인 할 수 있다.

 

반응형