介紹完了各種套件安裝,今天來講講實際的部署
我們試著將之前寫好的專案部署到 Google Cloud Platform 上面看看
雲端容器化的專案部署,基本概念通常都是,根據某個專案,我們建立某個容器,然後將可運作的檔案傳輸進去
首先,我們先到 https://console.cloud.google.com/welcome 裡面,建立一個專案
建立好了之後,GCP 和一些其他的雲端部署概念不同的地方是,GCP 很依賴使用指令工具進行操作
所以我們要先安裝 gcloud CLI,然後之後的部署都是透過指令工具進行
安裝 gcloud CLI 的方法,可以參考網站 https://cloud.google.com/sdk/docs/install?hl=zh-tw
我這邊本地開發使用的是 macOS 64 位元(ARM64, Apple silicon),執行的指令是
tar -xf google-cloud-cli-darwin-arm.tar.gz
下載並解壓縮後,執行安裝的指令 install.sh
./google-cloud-sdk/install.sh
安裝完成之後,要再進行初始化
./google-cloud-sdk/bin/gcloud init
運作完畢後,我們可以執行 gcloud --version
確認是否安裝完成
gcloud --version
Google Cloud SDK 538.0.0
beta 2025.09.05
bq 2.1.23
core 2025.09.05
gcloud-crc32c 1.0.0
gsutil 5.35
安裝完成之後,就可以開始進行本地的設置了!
首先我們要將本地和雲端的專案進行連接
假設你的專案 ID 是 ithome2025
,我們可以設置如下
gcloud config set project ithome2025
設置完成後,我們要開啟一些 API 的權限
首先我們開啟 Cloud Run Admin API
gcloud services enable run.googleapis.com
接著開啟能建立容器服務的 Cloud Build API
gcloud services enable cloudbuild.googleapis.com
接著確認專案可以付款,因為容器化服務是根據用量進行計價,所以必須要事先綁定一個付款方式,否則不會允許建立新容器
要確認付款,可以透過網頁或者指令進行檢查
gcloud beta billing projects describe ithome2025
billingAccountName: ---
billingEnabled: true
name: projects/ithome2025/billingInfo
projectId: ithome2025
確認有對應的付款帳戶之後,我們就可以開始試著建立容器了
我們要告訴 GCP 我們希望建立的容器裡面要包含什麼服務
以 Ktor 專案來說,其實非常單純,我們只需要建立的容器可以運作 Java RunTime 即可
我們在專案內建立一個 project.toml
檔案
[[build.env]]
name = "GOOGLE_RUNTIME_VERSION"
value = "17"
做到這一步,對應的專案設定完成,該部署的環境設定了,付款方式也說明了
剩下就只是實際的部署了
gcloud run deploy --source .
會看到一串選項,下面一一進行說明。
首先設置服務名稱,我們可以直接用預設的專案名稱
Service name (ktor-health-sample):
接著是選擇容器部署的地區,asia-east1 對應台灣,所以我選擇這個選項
Please specify a region:
[1] africa-south1
[2] asia-east1
[3] asia-east2
[4] asia-northeast1
[5] asia-northeast2
[6] asia-northeast3
[7] asia-south1
[8] asia-south2
[9] asia-southeast1
[10] asia-southeast2
[11] australia-southeast1
[12] australia-southeast2
[13] europe-central2
[14] europe-north1
[15] europe-north2
[16] europe-southwest1
[17] europe-west1
[18] europe-west10
[19] europe-west12
[20] europe-west2
[21] europe-west3
[22] europe-west4
[23] europe-west6
[24] europe-west8
[25] europe-west9
[26] me-central1
[27] me-central2
[28] me-west1
[29] northamerica-northeast1
[30] northamerica-northeast2
[31] northamerica-south1
[32] southamerica-east1
[33] southamerica-west1
[34] us-central1
[35] us-east1
[36] us-east4
[37] us-east5
[38] us-south1
[39] us-west1
[40] us-west2
[41] us-west3
[42] us-west4
[43] cancel
Please enter numeric choice or text value (must exactly match list item): 2
下面是再次確認,點選 Y
To make this the default region, run `gcloud config set run/region asia-east1`.
Deploying from source requires an Artifact Registry Docker repository to store built containers. A repository named [cloud-run-source-deploy] in region [asia-east1]
will be created.
Do you want to continue (Y/n)? Y
再來要確認是否允許所有人進入這個服務,我們這個服務作為一個範例,先選取不需要認證也可進入
Allow unauthenticated invocations to [ktor-health-sample] (y/N)? Y
接著就是等待的時間了。gcloud CLI 會負責將專案編譯,將對應的內容送到網路,開啟新容器的一連串流程。
部署完畢之後,就會看到下面的訊息
Done.
Service [ktor-health-sample] revision [ktor-health-sample-00001-r6r] has been deployed and is serving 100 percent of traffic.
Service URL: https://ktor-health-sample-540459471940.asia-east1.run.app
到對應的網址,我們就可以看到專案已經部署上去了。
今天部署的流程就說到這邊,我們明天見!