快速入門導覽課程:建構 Node.js 網路應用程式並部署至 Cloud Run
瞭解如何在 Cloud Run 使用單一指令,依據程式碼範例建構及部署「Hello World」網頁應用程式。 Google Cloud
按照本快速入門導覽課程的步驟操作時,Cloud Run 會在您從原始碼部署時,自動為您建構 Dockerfile。
事前準備
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Verify that billing is enabled for your Google Cloud project.
-
Make sure that you have the following role or roles on the project: Cloud Run Admin, Cloud Run Source Developer, Logs Viewer
Check for the roles
-
In the Google Cloud console, go to the IAM page.
Go to IAM - Select the project.
-
In the Principal column, find all rows that identify you or a group that you're included in. To learn which groups you're included in, contact your administrator.
- For all rows that specify or include you, check the Role column to see whether the list of roles includes the required roles.
Grant the roles
-
In the Google Cloud console, go to the IAM page.
前往「身分與存取權管理」頁面 - 選取專案。
- 按一下 「授予存取權」。
-
在「New principals」(新增主體) 欄位中,輸入您的使用者 ID。 這通常是 Google 帳戶的電子郵件地址。
- 在「Select a role」(選取角色) 清單中,選取角色。
- 如要授予其他角色,請按一下 「新增其他角色」,然後新增每個其他角色。
- 按一下 [Save]。
Install the Google Cloud CLI.
如果您使用外部識別資訊提供者 (IdP),請先 使用聯合身分登入 gcloud CLI。
如要初始化 gcloud CLI,請執行下列指令:
gcloud init
-
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Verify that billing is enabled for your Google Cloud project.
-
Make sure that you have the following role or roles on the project: Cloud Run Admin, Cloud Run Source Developer, Logs Viewer
Check for the roles
-
In the Google Cloud console, go to the IAM page.
Go to IAM - Select the project.
-
In the Principal column, find all rows that identify you or a group that you're included in. To learn which groups you're included in, contact your administrator.
- For all rows that specify or include you, check the Role column to see whether the list of roles includes the required roles.
Grant the roles
-
In the Google Cloud console, go to the IAM page.
前往「身分與存取權管理」頁面 - 選取專案。
- 按一下 「授予存取權」。
-
在「New principals」(新增主體) 欄位中,輸入您的使用者 ID。 這通常是 Google 帳戶的電子郵件地址。
- 在「Select a role」(選取角色) 清單中,選取角色。
- 如要授予其他角色,請按一下 「新增其他角色」,然後新增每個其他角色。
- 按一下 [Save]。
Install the Google Cloud CLI.
如果您使用外部識別資訊提供者 (IdP),請先 使用聯合身分登入 gcloud CLI。
如要初始化 gcloud CLI,請執行下列指令:
gcloud init
-
- 如要為 Cloud Run 服務設定預設專案:
將 PROJECT_ID 替換為您的 Google Cloud 專案 ID。gcloud config set project PROJECT_ID
啟用 Cloud Run Admin API 和 Cloud Build API:
gcloud services enable run.googleapis.com \ cloudbuild.googleapis.com
啟用 Cloud Run Admin API 後,系統會自動建立 Compute Engine 預設服務帳戶。
-
請確認您已獲得服務身分的服務帳戶使用者角色。根據預設,服務身分是 Compute Engine 預設服務帳戶。
授予角色
如要授予服務身分資源的存取權,請使用
gcloud iam service-accounts add-iam-policy-binding
指令,並將醒目顯示的變數替換為適當的值:gcloud iam service-accounts add-iam-policy-binding SERVICE_ACCOUNT_EMAIL \ --member="PRINCIPAL" \ --role="roles/iam.serviceAccountUser"
更改下列內容:
- SERVICE_ACCOUNT_EMAIL:您用來做為服務身分的服務帳戶電子郵件地址,例如:
- Compute Engine 預設服務帳戶:
PROJECT_NUMBER-compute@developer.gserviceaccount.com
- 您建立的服務帳戶:
SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com
- Compute Engine 預設服務帳戶:
- PRINCIPAL:使用者 ID。這通常是 Google 帳戶的電子郵件地址。
- SERVICE_ACCOUNT_EMAIL:您用來做為服務身分的服務帳戶電子郵件地址,例如:
- 將下列 IAM 角色授予 Cloud Build 服務帳戶。
按一下即可查看 Cloud Build 服務帳戶的必要角色
除非您覆寫這項行為,否則 Cloud Build 會自動使用 Compute Engine 預設服務帳戶做為預設的 Cloud Build 服務帳戶,建構您的原始碼和 Cloud Run 資源。如要讓 Cloud Build 建構來源,請要求管理員將 Cloud Run Builder (
roles/run.builder
) 授予專案中的 Compute Engine 預設服務帳戶:gcloud projects add-iam-policy-binding PROJECT_ID \ --member=serviceAccount:PROJECT_NUMBER-compute@developer.gserviceaccount.com \ --role=roles/run.builder
請將
PROJECT_NUMBER
替換為專案編號,並將PROJECT_ID
替換為專案 ID。 Google CloudGoogle Cloud如需如何找出專案 ID 和專案編號的詳細操作說明,請參閱「建立及管理專案」。將 Cloud Run 建構者角色授予 Compute Engine 預設服務帳戶後,需要幾分鐘才能傳播。
- 查看 Cloud Run 定價,或使用 Pricing Calculator 估算費用。
建立名為
helloworld
的新目錄,然後將目錄變更為該目錄:mkdir helloworld cd helloworld
使用以下內容建立
package.json
檔案:在同一個目錄中建立
index.js
檔案,然後將以下幾行複製到檔案中:這段程式碼會建立基本的網路伺服器,用於監聽
PORT
環境變數定義的通訊埠。在原始碼目錄中,使用下列指令部署目前資料夾:
gcloud run deploy --source .
系統提示您輸入服務名稱時,請按下 Enter 鍵接受預設名稱,例如
helloworld
。如果系統提示您在專案中啟用其他 API (例如 Artifact Registry API),請按
y
回應。系統提示您選擇地區時,請選取所需地區,例如
europe-west1
。如果系統提示您在指定區域建立存放區,請按下
y
回應。如果系統提示你允許公開存取: 請回覆
y
。如果網域限制機構政策禁止顯示這項提示,您可能不會看到提示。詳情請參閱「事前準備」一節。
然後稍等片刻,等待部署成功。成功完成後,指令列會顯示服務網址。
在網路瀏覽器中開啟服務網址,以造訪您所部署的服務。
asia-east1
(臺灣)asia-northeast1
(東京)asia-northeast2
(大阪)asia-south1
(印度孟買)europe-north1
(芬蘭)二氧化碳排放量低
europe-north2
(斯德哥爾摩)二氧化碳排放量低
europe-southwest1
(馬德里)二氧化碳排放量低
europe-west1
(比利時)二氧化碳排放量低
europe-west4
(荷蘭)二氧化碳排放量低
europe-west8
(米蘭)europe-west9
(巴黎)二氧化碳排放量低
me-west1
(特拉維夫)northamerica-south1
(墨西哥)us-central1
(愛荷華州)二氧化碳排放量低
us-east1
(南卡羅來納州)us-east4
(北維吉尼亞州)us-east5
(哥倫布)us-south1
(達拉斯)二氧化碳排放量低
us-west1
(奧勒岡州)二氧化碳排放量低
africa-south1
(約翰尼斯堡)asia-east2
(香港)asia-northeast3
(韓國首爾)asia-southeast1
(新加坡)asia-southeast2
(雅加達)asia-south2
(印度德里)australia-southeast1
(雪梨)australia-southeast2
(墨爾本)europe-central2
(波蘭華沙)europe-west10
(柏林)二氧化碳排放量低
europe-west12
(杜林)europe-west2
(英國倫敦)二氧化碳排放量低
europe-west3
(德國法蘭克福)europe-west6
(瑞士蘇黎世)二氧化碳排放量低
me-central1
(杜哈)me-central2
(達曼)northamerica-northeast1
(蒙特婁)二氧化碳排放量低
northamerica-northeast2
(多倫多)二氧化碳排放量低
southamerica-east1
(巴西聖保羅)二氧化碳排放量低
southamerica-west1
(智利聖地牙哥)二氧化碳排放量低
us-west2
(洛杉磯)us-west3
(鹽湖城)us-west4
(拉斯維加斯)前往 Google Cloud 控制台的 Cloud Run:
在服務清單中找出您要刪除的服務,然後按一下核取方塊來選取。
按一下 [Delete] (刪除),如此便會刪除服務的所有修訂版本。
- SERVICE:服務名稱。
- REGION:服務的 Google Cloud 區域。
- In the Google Cloud console, go to the Manage resources page.
- In the project list, select the project that you want to delete, and then click Delete.
- In the dialog, type the project ID, and then click Shut down to delete the project.
撰寫範例服務
如要建立及部署 Node.js 服務,請按照下列步驟操作:
您的應用程式已完成,可以開始部署。
從來源部署至 Cloud Run
重要事項:本快速入門導覽課程假設您在用於快速入門導覽課程的專案中,具備擁有者或編輯者角色。否則,請參閱 Cloud Run Source Developer 角色,瞭解從來源部署 Cloud Run 資源所需的權限。
從來源部署功能會自動從原始碼建構容器映像檔,並部署該映像檔。
如要從來源部署:
Cloud Run 位置
Cloud Run 具有「地區性」,這表示執行 Cloud Run 服務的基礎架構位於特定地區,並由 Google 代管,可為該地區內所有區域提供備援功能。
選擇 Cloud Run 服務的執行地區時,請將延遲時間、可用性或耐用性需求做為主要考量。一般而言,您可以選擇最靠近使用者的地區,但您應考量 Cloud Run 服務所使用的其他 Google Cloud
產品位置。使用分散在不同位置的 Google Cloud 產品,可能會影響服務的延遲時間和費用。
Cloud Run 可在下列地區使用:
採用級別 1 定價
採用級別 2 定價
如果您已建立 Cloud Run 服務,即可在 Google Cloud 控制台的 Cloud Run 資訊主頁中查看地區。
清除所用資源
如要避免系統向您的 Google Cloud 帳戶收取額外費用,請刪除您透過本快速入門導覽課程部署的所有資源。
刪除存放區
部署的服務閒置時,Cloud Run 不會向您收費。不過,您可能仍須支付在 Artifact Registry 中儲存容器映像檔的費用。如要刪除 Artifact Registry 存放區,請按照 Artifact Registry 說明文件中的「刪除存放區」一節操作。
刪除服務
Cloud Run 服務收到要求後才會開始計費。如要刪除 Cloud Run 服務,請按照下列步驟操作:
控制台
如要刪除服務:
gcloud
如要刪除服務,請執行下列指令:
gcloud run services delete SERVICE --region REGION
更改下列內容:
刪除測試專案
刪除 Google Cloud 專案後,系統就會停止對該專案中的所有資源收取費用。如要釋出專案中的所有 Google Cloud 資源,請按照下列步驟操作:
後續步驟
如要進一步瞭解如何從程式碼來源建構容器並推送至存放區,請參閱: