iT邦幫忙

2023 iThome 鐵人賽

DAY 1
0
自我挑戰組

Django系列 第 17

Day17~Django 漫漫長路-踏上雲的路途,架構GCP Appengine

  • 分享至 

  • xImage
  •  

大家好,我是Leo
今天要來講解,django在gcp appengine的建置/images/emoticon/emoticon30.gif
OK~~~ Let's go now!!!


註冊GCP帳號並建立一個project

目前GCP帳號擁有90天300美元的免費額度,以下是GCP註冊連結。
https://cloud.google.com/


建立db

選擇要建立資料庫的類型,在這邊我選擇的是mysql

step1

https://ithelp.ithome.com.tw/upload/images/20230329/20154853A728mO8mGC.png

step2

https://ithelp.ithome.com.tw/upload/images/20230329/20154853TerCzDXB60.png

step3 選擇國家

https://ithelp.ithome.com.tw/upload/images/20230329/20154853J1RKpIxtmF.png

step4 選擇存儲空間大小

https://ithelp.ithome.com.tw/upload/images/20230329/20154853gfRvfct2K3.png

step5 選擇開放的ip

在這邊0.0.0.0/0 全開放
https://ithelp.ithome.com.tw/upload/images/20230329/20154853bSzKt6R2ii.png

step6 選擇db是否防刪除模式

https://ithelp.ithome.com.tw/upload/images/20230329/20154853UH2fsx10f3.png

step7 按下建立

step8 新增使用者

https://ithelp.ithome.com.tw/upload/images/20230329/20154853lNJCRPjnvk.png

step9 storage 開放url顯示storage內資料權限

如果有圖片的專案,可以在此設定
https://ithelp.ithome.com.tw/upload/images/20230330/20154853m1VHqnOz15.png

step10 storage 讀取權限

(點選專案->權限->賦予存取權->cloud storage)
https://ithelp.ithome.com.tw/upload/images/20230330/20154853w0XH9P3BmM.png


下載 cloud_sql_proxy(windows)

https://storage.googleapis.com/cloud-sql-connectors/cloud-sql-proxy/v2.0.0/cloud-sql-proxy.x64.exe

(Linux)

$ wget https://dl.google.com/cloudsql/cloud_sql_proxy.linux.amd64 -O cloud_sql_proxy
$ chmod +x cloud_sql_proxy

建立服務帳戶

https://ithelp.ithome.com.tw/upload/images/20230321/20154853raHYKybNhS.png

設定服務帳戶角色

https://ithelp.ithome.com.tw/upload/images/20230321/201548535Wv7oxepKM.png

https://ithelp.ithome.com.tw/upload/images/20230329/20154853VnMpcyKXFb.png

建立金鑰(將下載好的json檔,改名為credential.json放置django專案內)

https://ithelp.ithome.com.tw/upload/images/20230321/20154853FzHYzyiN2f.png

https://ithelp.ithome.com.tw/upload/images/20230321/20154853Cdi0UMcuJk.png

https://ithelp.ithome.com.tw/upload/images/20230321/201548535nS19G7lgS.png


透過 Cloud SQL Proxy 連線至資料庫

.\cloud_sql_proxy -instances=<INSTANCE_CONNECTION_NAME>=tcp:3306 -credential_file=<PATH_TO_KEY_FILE> &

INSTANCE_CONNECTION_NAME : 請看圖一
PATH_TO_KEY_FILE: json檔路徑
測試Cloud SQL Proxy 是否能夠連線資料庫
如3306 Port被佔用,可以先行去服務停止,待測試成功在開啟

圖一
https://ithelp.ithome.com.tw/upload/images/20230322/20154853iz9NOaEjJC.png

https://ithelp.ithome.com.tw/upload/images/20230322/20154853Vu0iER3E5W.png

看到以下畫面代表測試成功
https://ithelp.ithome.com.tw/upload/images/20230321/20154853em2yT88ysI.png


在專案目錄下建立一個 app.yaml 檔

# [START django_app]
# [START gaestd_py_django_app_yaml]
runtime: python39

env_variables:
  # This setting is used in settings.py to configure your ALLOWED_HOSTS
  # APPENGINE_URL: PROJECT_ID.uc.r.appspot.com
  INSTANCE_UNIX_SOCKET: /cloudsql/<INSTANCE_CONNECTION_NAME>
  DB_USER: <YOUR DB_USER>
  DB_PASS: <YOUR DB_PASS>
  DB_NAME:<YOUR DB_NAME>

handlers:
# This configures Google App Engine to serve the files in the app's static
# directory.
- url: /static
  static_dir: static/

# This handler routes all requests not caught above to your main app. It is
# required when static routes are defined, but can be omitted (along with
# the entire handlers section) when there are no static files defined.
- url: /.*
  script: auto
# [END gaestd_py_django_app_yaml]
# [END django_app]

在專案目錄下建立一個 main.py

from mysite.wsgi import application

# App Engine by default looks for a main.py file at the root of the app
# directory with a WSGI-compatible object called app.
# This file imports the WSGI-compatible object of your Django app,
# application from mysite/wsgi.py and renames it app so it is discoverable by
# App Engine without additional configuration.
# Alternatively, you can add a custom entrypoint field in your app.yaml:
# entrypoint: gunicorn -b :$PORT mysite.wsgi
app = application

修改setting.py

DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'HOST': '/cloudsql/<INSTANCE_CONNECTION_NAME>',
            'USER': '<YOUR DB_USER>',
            'PASSWORD': '<YOUR DB_PASSWORD>',
            'NAME': '<YOUR DB_NAME>',
       }
}

from google.oauth2 import service_account
GS_CREDENTIALS = service_account.Credentials.from_service_account_file(
    os.path.join(BASE_DIR, 'credential.json'),
)

DEFAULT_FILE_STORAGE = 'mysite.gcloud.GoogleCloudMediaFileStorage'
GS_PROJECT_ID = 'PROJECT_ID'
GS_BUCKET_NAME = 'PROJECT_ID.appspot.com'
MEDIA_ROOT = 'media/'
MEDIA_URL = 'https://storage.googleapis.com/{}/'.format(GS_BUCKET_NAME)

專案目錄

  • 表示有修改或新增
mysite/
    * app.yaml
    * main.py
    manage.py
    mysite/
        __init__.py
        * settings.py
        urls.py
        wsgi.py

連線至gcp db

create database DB_NAME

初始化資料庫

python manage.py makemigrations
python manage.py migrate

下載 GoogleCloudSDK

類似google版的cmd 可以輸入指令
https://dl.google.com/dl/cloudsdk/channels/rapid/GoogleCloudSDKInstaller.exe


打開GoogleCloudSDK

gcloud init

照著步驟登入自己的gcp帳號
位置
https://ithelp.ithome.com.tw/upload/images/20230329/20154853uL2s6QJVhW.png


佈署

cd project
gcloud app deploy
之後跑完會跳出這行
Created [url]

ctrl點下url 會跳出網址 | gcloud app browse


今天主要是django 部署至 gcp appengine,透過以次計費的方式,使用越多付費越多,相對不常使用的情控下也比較節省,那我們明天會講的是object的create的一些功能。
我們明天見,各位掰掰~~~/images/emoticon/emoticon29.gif


上一篇
Day16~Django 漫漫長路-當tableName 成為變數
下一篇
Day18~Django 漫漫長路-objects create資料創建必備技能
系列文
Django30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言