使用 SaaS Runtime 部署服務執行個體
瞭解如何使用 SaaS 執行階段部署服務執行個體。在本快速入門範例中,服務執行個體是 VM。
事前準備
-
登入 Google 帳戶。
如果沒有帳戶,請 申請新帳戶。
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
Enable the SaaS Runtime, Artifact Registry, Infrastructure Manager, Developer Connect, Cloud Build, and Cloud Storage APIs.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.-
Create a service account:
-
Ensure that you have the Create Service Accounts IAM role
(
roles/iam.serviceAccountCreator) and the Project IAM Admin role (roles/resourcemanager.projectIamAdmin). Learn how to grant roles. -
In the Google Cloud console, go to the Create service account page.
Go to Create service account - Select your project.
-
In the Service account name field, enter a name. The Google Cloud console fills in the Service account ID field based on this name.
In the Service account description field, enter a description. For example,
Service account for quickstart. - Click Create and continue.
-
Grant the Project > Owner role to the service account.
To grant the role, find the Select a role list, then select Project > Owner.
- Click Continue.
-
Click Done to finish creating the service account.
-
Ensure that you have the Create Service Accounts IAM role
(
-
安裝 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.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
Enable the SaaS Runtime, Artifact Registry, Infrastructure Manager, Developer Connect, Cloud Build, and Cloud Storage APIs.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.-
Create a service account:
-
Ensure that you have the Create Service Accounts IAM role
(
roles/iam.serviceAccountCreator) and the Project IAM Admin role (roles/resourcemanager.projectIamAdmin). Learn how to grant roles. -
In the Google Cloud console, go to the Create service account page.
Go to Create service account - Select your project.
-
In the Service account name field, enter a name. The Google Cloud console fills in the Service account ID field based on this name.
In the Service account description field, enter a description. For example,
Service account for quickstart. - Click Create and continue.
-
Grant the Project > Owner role to the service account.
To grant the role, find the Select a role list, then select Project > Owner.
- Click Continue.
-
Click Done to finish creating the service account.
-
Ensure that you have the Create Service Accounts IAM role
(
-
安裝 Google Cloud CLI。
-
若您採用的是外部識別資訊提供者 (IdP),請先使用聯合身分登入 gcloud CLI。
-
執行下列指令,初始化 gcloud CLI:
gcloud init
將權限授予 SaaS 執行者服務帳戶
SaaS 執行階段會使用多個服務帳戶。您在上一節中建立服務帳戶,啟用 SaaS 執行階段 API 時,SaaS 執行階段會建立另一個服務帳戶。
SaaS Runtime 建立的服務帳戶稱為 service-PROJECT-NUMBER@gcp-sa-saasservicemgmt.iam.gserviceaccount.com,其中 PROJECT-NUMBER 是您的專案編號。
請按照下列步驟,授予這個服務帳戶必要權限:
依序開啟「SaaS 執行階段」 >「總覽」 >「開始使用」頁面。
在顯示「尚未授予 SaaS 執行階段帳戶的必要權限」的橫幅中,按一下「授予權限」。
使用 Terraform 設定定義服務執行個體
您需要使用 Terraform 設定,定義要為服務執行個體部署的基礎架構。在本快速入門導覽課程中,您將部署 VM。
如要建立定義 VM 的 Terraform 設定,請按照下列步驟操作:
在本機上建立名為
terraform-vm的目錄。在
terraform-vm中,建立下列四個 Terraform 檔案:名為
versions.tf的檔案,其中含有下列內容:terraform { required_version = "1.5.7" required_providers { google = { source = "hashicorp/google" version = "~> 4.0" } } }名為
outputs.tf的檔案,其中含有下列內容:output "instance_name" { description = "Name of the instance" value = google_compute_instance.vm_instance.name } output "instance_external_ip" { description = "External IP of the instance" value = google_compute_instance.vm_instance.network_interface[0].access_config[0].nat_ip } output "instance_self_link" { description = "Self-link of the instance" value = google_compute_instance.vm_instance.self_link }名為
variables.tf的檔案,其中含有下列內容:variable "region" { description = "The Google Cloud region" type = string default = "us-central1" } variable "zone" { description = "The Google Cloud zone" type = string default = "us-central1-a" } variable "instance_name" { description = "Name for the Compute Engine instance" type = string default = "saas-runtime-vm-instance" } variable "machine_type" { description = "Machine type for the Compute Engine instance" type = string default = "e2-medium" } variable "disk_size" { description = "Boot disk size in GB" type = number default = 10 } variable "actuation_sa" { description = "The email of the Actuation Service Account" type = string } variable "tenant_project_id" { description = "The project ID of the tenant project" type = string } variable "tenant_project_number" { description = "The project number of the tenant project" type = number }名為
main.tf的檔案,其中含有下列內容:resource "google_compute_instance" "vm_instance" { project = var.tenant_project_id name = var.instance_name machine_type = var.machine_type zone = var.zone boot_disk { initialize_params { image = "debian-cloud/debian-11" size = var.disk_size } } network_interface { network = "default" access_config { # Ephemeral public IP - empty block is okay here } } tags = ["allow-ssh"] }
如要建立包含這四個 Terraform 設定檔的 zip 封存檔,請在終端機中前往
terraform-vm目錄,然後執行下列指令:zip terraform-files.zip main.tf outputs.tf variables.tf versions.tf您現在有一個名為
terraform-files.zip的壓縮封存檔,其中包含所有四個 Terraform 設定檔。
在 Artifact Registry 中建立存放區
如要使用 SaaS 執行階段,您必須在 Artifact Registry 中建立存放區。如要建立這個存放區,請按照下列步驟操作:
在控制台中,前往「Artifact Registry」。
點選「Create Repository」(建立存放區)。
針對「Name」(名稱) 輸入
vm-quickstart-repo。將「格式」選取為「Docker」。
在「區域」部分,選擇
us-central1 (Iowa)。點選「建立」。
建立 SaaS 產品
您擁有定義要部署 VM 的 Terraform 設定和存放區。您現在可以使用 SaaS Runtime 建立部署單元模型,並部署 VM。
建立 SaaS 產品資源
在控制台中,依序前往「SaaS Runtime」 >「SaaS Offering」。
點選「建立」。
在「SaaS 產品名稱」欄位中,輸入
vm-quickstart-saas-offering。在「Region」(區域) 欄位中選取區域
us-central1,然後按一下「Ok」(確定)。點選「建立」。
您為 SaaS 產品選取的區域,就是 SaaS 產品部署作業的代管位置。在本快速入門導覽課程的範例中,SaaS 產品是單一 VM,因此這些區域是佈建及代管 VM 的位置。
如果使用者會存取這些 VM,他們將存取您在此處指定的區域中部署的 VM。
模擬部署單位
如要模擬 SaaS 產品,請建立名為「單元 kind」的元件。單位 kind 會定義服務中的元件,以供部署及管理。 舉例來說,您可能有一個 VM 的單元 kind,以及在該 VM 上執行的應用程式的第二個單元 kind。
在本快速入門導覽課程中,您會為 VM 建立一個單元 kind。
如要建立單元 kind,請按照下列步驟操作:
在控制台中,依序前往「SaaS Runtime」 >「Unit Kinds」。
選取「建立」。
在「建立藍圖」頁面中:
- 選取「上傳」。
- 如要上傳定義 VM 的 Terraform 設定,請按照下列步驟操作:
- 在「檔案選擇器」欄位中,選取「瀏覽」。
- 瀏覽並選取
terraform-files.zip,這是您先前建立的 zip 封存檔案。 - 選取「開啟」。
- 按一下「下一步:設定藍圖」。
在「儲存藍圖」頁面中:
- 選取 Artifact Registry:
- 在「Select repository from Artifact Registry」(從 Artifact Registry 選取存放區) 欄位中,選取「Browse」(瀏覽)。
- 選取
vm-quickstart-repo,這是您先前建立的存放區。 - 按一下「選取」。
- 在「構件映像檔名稱」欄位中,輸入
vm-quickstart-blueprint。 - 在「Infrastructure Manager terraform version」(Infrastructure Manager Terraform 版本) 中,選取
1.5.7。 - 在「Cloud Build service account」(Cloud Build 服務帳戶) 部分,選取您在「事前準備」部分建立的服務帳戶。
- 按一下「下一步:單元 kind 詳細資料」。
- 選取 Artifact Registry:
在「設定單元 kind 屬性」頁面中,執行下列操作:
- 在「單元 kind 名稱」中輸入
vm-quickstart-unit-kind。 - 在「SaaS Offering」部分,選取「
vm-quickstart-saas-offering」,這是您先前建立的 SaaS 產品資源。 - 按一下「Next: Release configuration」(下一步:版本設定)
- 在「單元 kind 名稱」中輸入
在「版本名稱」中輸入
vm-quickstart-first-release。點選「建立」。
佈建服務執行個體
如要佈建單元種類的資源,請建立單元。 建立單元時,SaaS 執行階段會佈建連結至單元 kind 的 Terraform 設定中定義的資源。資源會在 SaaS 產品提供的每個區域中佈建。
在本快速入門導覽課程的範例中,VM 是在 us-central1 區域中佈建。
建立單元:
在控制台中,依序前往「SaaS Runtime」 >「Units」。
選取「建立」。
在「建立單元」頁面中:
- 在「Unit name」(單位名稱) 輸入:
vm-quickstart-unit。 - 在「SaaS 產品」下方,選取您先前建立的 SaaS 產品資源:
vm-quickstart-saas-offering。 - 在「Region」(地區) 下方,選取「
us-central1」。 - 在「單元 kind」下方,選取您先前建立的單元 kind:
vm-quickstart-unit-kind。 - 選取「建立」。
- 在「Unit name」(單位名稱) 輸入:
如要佈建 VM:
SaaS 執行階段會在您在 SaaS 產品中指定的區域佈建 VM。您可以在單元中指定的任何區域建立單元。在本快速入門導覽課程中,您指定了一個區域 (us-central1),而 VM 就是在這個區域中佈建。
查看已部署的 VM
您現在已使用 SaaS 執行階段部署 VM。
如要查看在本快速入門導覽課程中部署的 VM,請按照下列步驟操作:
在控制台中,依序前往「SaaS Runtime」 >「Units」 >「Unit details」頁面。
按一下機構單位名稱:
vm-quickstart-unit。在「單元詳細資料」頁面中:
確認「狀態」為:
- 如果 VM 已佈建,則為「Ready」(就緒)。
- 如果作業仍在進行中,則為「佈建中」。
展開「變數」部分。
在「輸出變數」中,您可以看到可用於連上執行個體的外部 IP。
您也可以在 Compute Engine 中查看 VM:
在控制台中,依序前往「Compute Engine」 >「VM instances」(VM 執行個體) 頁面。
在「VM 執行個體」下方,查看列出的 VM。
清除所用資源
為了避免系統向您的 Google Cloud 帳戶收取本頁面所用資源的費用,請按照下列步驟操作。
刪除專案
如果您是在新 Google Cloud 專案中部署解決方案,且現在不再需要該專案,請完成下列步驟來刪除專案:
- 前往 Google Cloud 控制台的「Manage resources」(管理資源) 頁面。
- 從專案清單中選取要刪除的專案,然後點選「Delete」(刪除)。
- 收到提示時,請輸入專案 ID,然後點選「Shut down」(關閉)。
後續步驟
- 如要進一步瞭解 SaaS 執行階段,請參閱「SaaS 執行階段總覽」。
- 如要開始使用 SaaS 執行階段,請先建立 SaaS 產品。
- 如要瞭解服務帳戶的使用方式,以及如何授予這些帳戶的精細權限,請參閱「SaaS 執行階段服務帳戶」。
- 如要瞭解如何更新版本,請參閱「推出版本」。