使用 SaaS 运行时部署服务实例

了解如何使用 SaaS 运行时部署服务实例。在此快速入门示例中,您的服务实例是一个虚拟机。

准备工作

  1. 登录您的 Google 账号。

    如果您还没有 Google 账号,请 注册新账号

  2. 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 the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. 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 the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the APIs

  5. Create a service account:

    1. 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.
    2. In the Google Cloud console, go to the Create service account page.

      Go to Create service account
    3. Select your project.
    4. 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.

    5. Click Create and continue.
    6. Grant the Project > Owner role to the service account.

      To grant the role, find the Select a role list, then select Project > Owner.

    7. Click Continue.
    8. Click Done to finish creating the service account.

  6. 安装 Google Cloud CLI。

  7. 如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI

  8. 如需初始化 gcloud CLI,请运行以下命令:

    gcloud init
  9. 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 the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  10. Verify that billing is enabled for your Google Cloud project.

  11. 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 the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the APIs

  12. Create a service account:

    1. 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.
    2. In the Google Cloud console, go to the Create service account page.

      Go to Create service account
    3. Select your project.
    4. 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.

    5. Click Create and continue.
    6. Grant the Project > Owner role to the service account.

      To grant the role, find the Select a role list, then select Project > Owner.

    7. Click Continue.
    8. Click Done to finish creating the service account.

  13. 安装 Google Cloud CLI。

  14. 如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI

  15. 如需初始化 gcloud CLI,请运行以下命令:

    gcloud init

向 SaaS Runner 服务帐号授予权限

SaaS 运行时使用多个服务帐号。您在上一部分中创建了一个服务账号,并且在启用 SaaS 运行时 API 时,SaaS 运行时会创建另一个服务帐号。

SaaS 运行时创建的服务帐号称为 service-PROJECT-NUMBER@gcp-sa-saasservicemgmt.iam.gserviceaccount.com,其中 PROJECT-NUMBER 是您的项目编号。

如需向此服务帐号授予所需权限,请执行以下操作:

  1. 打开SaaS 运行时 > 概览 > 使用入门 页面。

    前往 SaaS 运行时使用入门

  2. 在显示消息尚未授予 SaaS 运行时 账号所需权限 的横幅中, 点击 授予权限

使用 Terraform 配置定义服务实例

您需要使用 Terraform 配置来定义要为服务实例部署的基础架构。在此快速入门中,您将部署一个虚拟机。

如需创建定义虚拟机的 Terraform 配置,请执行以下操作:

  1. 在本地机器上创建一个名为 terraform-vm 的目录。

  2. terraform-vm 中,创建以下四个 Terraform 文件:

    1. 一个名为 versions.tf 的文件,其中包含以下内容:

      terraform {
        required_version = "1.5.7"
        required_providers {
          google = {
            source  = "hashicorp/google"
            version = "~> 4.0"
          }
        }
      }
      
    2. 一个名为 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
      }
      
    3. 一个名为 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
      }
      
    4. 一个名为 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"]
      }
      
  3. 如需创建包含这四个 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 中拥有一个代码库。如需创建此代码库,请执行以下操作:

  1. 在控制台中,前往 Artifact Registry

    前往 Artifact Registry

  2. 点击创建代码库

  3. 对于名称,输入vm-quickstart-repo

  4. 格式 保持选择为 Docker

  5. 对于 区域,选择 us-central1 (Iowa)

  6. 点击创建

创建 SaaS 产品

您已拥有定义要部署的虚拟机的 Terraform 配置和一个代码库。现在,您可以使用 SaaS 运行时对部署单元进行建模并部署虚拟机。

创建 SaaS 产品资源

  1. 在控制台中,前往 SaaS 运行时 > SaaS 产品

    前往 SaaS 产品

  2. 点击创建

  3. SaaS 产品名称 字段中,输入:vm-quickstart-saas-offering

  4. 区域 字段中,选择区域 us-central1,然后点击确定

  5. 点击创建

您为 SaaS 产品选择的区域是 SaaS 产品部署的托管位置。在此快速入门示例中,SaaS 产品是单个虚拟机,因此这些区域是虚拟机预配和托管的位置。

如果有最终用户访问这些虚拟机,他们将访问您在此处指定的区域中部署的这些虚拟机。

对部署单元进行建模

如需对 SaaS 产品进行建模,您可以创建称为“单元种类”的组件。 单元种类定义了服务中要部署和管理的组件。 例如,您可能有一个用于虚拟机的单元种类,以及一个用于在该虚拟机上运行的应用的单元种类。

在此快速入门中,您将为虚拟机创建一个单元种类。

如需创建单元种类,请执行以下操作:

  1. 在控制台中,前往 SaaS 运行时 > 单元种类

    前往单元种类

  2. 选择创建

  3. 创建蓝图 页面上:

    1. 选择上传
    2. 如需上传定义虚拟机的 Terraform 配置,请执行以下操作:
      1. 文件选择器 字段中,选择浏览
      2. 找到并选择 terraform-files.zip,这是您之前创建的 zip 归档文件。
      3. 选择打开
    3. 点击下一步:配置蓝图
  4. 存储蓝图 页面上:

    1. 选择 Artifact Registry:
      1. 从 Artifact Registry 中选择代码库字段中,选择 浏览
      2. 选择 vm-quickstart-repo,这是您之前创建的代码库。
      3. 点击选择
    2. 制品映像名称 字段中,输入 vm-quickstart-blueprint
    3. 对于 Infrastructure Manager 的 Terraform 版本 ,选择 1.5.7
    4. 对于 Cloud Build 服务账号 ,选择您在 准备工作 部分中创建的服务 账号。
    5. 点击下一步:单元种类详情
  5. 配置单元种类属性 页面上,执行以下操作:

    1. 对于单元种类名称 ,输入 vm-quickstart-unit-kind
    2. 对于 SaaS 产品 ,选择 vm-quickstart-saas-offering,这是您之前创建的 SaaS 产品资源。
    3. 点击下一步:发布配置
  6. 对于发布名称,输入vm-quickstart-first-release

  7. 点击创建

预配服务实例

如需预配属于单元种类的资源,您可以创建“单元”。 创建单元时,SaaS 运行时会预配在连接到单元种类的 Terraform 配置中定义的资源。这些资源会在属于 SaaS 产品的每个区域中预配。

在此快速入门示例中,虚拟机在 us-central1 区域中预配。

  1. 创建单元:

    1. 在控制台中,前往 SaaS 运行时 > 单元

      前往单元

    2. 选择创建

    3. 创建单元 页面上:

      1. 对于单元名称,输入:vm-quickstart-unit
      2. SaaS 产品 下,选择您之前创建的 SaaS 产品资源:vm-quickstart-saas-offering
      3. 区域 下,选择 us-central1
      4. 单元种类 下,选择您之前创建的单元种类: vm-quickstart-unit-kind
      5. 选择创建
  2. 如需预配虚拟机,请执行以下操作:

    1. 单元详情 页面上,选择预配
    2. 对于发布 字段,选择 vm-quickstart-first-release
    3. 对于服务账号,选择您在准备工作部分中创建的服务 账号。
    4. 添加租户项目:

      1. 选择添加租户项目变量
      2. 选择您在此 快速入门中使用的 Google Cloud 项目。SaaS 运行时部署虚拟机时,会将其部署到此项目。
    5. 选择预配

SaaS 运行时会在您在 SaaS 产品中指定的区域中预配虚拟机。您可以在单元中指定的任何区域中创建单元。在此快速入门中,您指定了一个区域 (us-central1),虚拟机在此区域中预配。

查看已部署的虚拟机

您现在已使用 SaaS 运行时部署虚拟机。

如需查看您在此快速入门中部署的虚拟机,请执行以下操作:

  1. 在控制台中,前往 SaaS 运行时 > 单元 > 单元详情 页面。

    前往单元

  2. 点击单元的名称:vm-quickstart-unit

  3. 单元详情 页面上:

    1. 查看状态 是否为:

      • 如果虚拟机已预配,则为 Ready
      • 如果操作仍在进行中,则为 Provisioning
    2. 展开变量 部分。

    3. 输出变量 中,您可以看到可用于访问实例的外部 IP。

  4. 您还可以在 Compute Engine 中查看虚拟机:

    1. 在控制台中,前往 Compute Engine > 虚拟机实例 页面。

      前往虚拟机实例

    2. 查看列在虚拟机实例 下的虚拟机。

清理

为避免因本页中使用的资源导致您的 Google Cloud 账号产生费用,请按照以下步骤操作。

删除项目

如果您在新 Google Cloud 项目中部署了解决方案,并且不再需要该 项目,请按照以下步骤将其删除:

  1. 在 Google Cloud 控制台中,前往管理资源页面。

    转到“管理资源”

  2. 在项目列表中,选择要删除的项目,然后点击删除
  3. 在提示符处,输入项目 ID,然后点击关停

后续步骤

  • 如需详细了解 SaaS 运行时,请参阅 SaaS 运行时概览
  • 如需开始使用 SaaS 运行时,请先创建 SaaS 产品
  • 如需了解服务账号的使用方式以及如何为这些账号授予精细权限,请参阅 SaaS 运行时服务账号
  • 如需了解更新版本的步骤,请参阅 推出版本