单个虚拟机

架构

单个虚拟机堆栈会创建具有智能默认设置的单个虚拟机,但也能够自定义该虚拟机。它使用:

  • 计算 - 虚拟机 - Compute Engine

Example 设置了一个简单的单个虚拟机,并允许您通过 SSH 连接到该虚拟机。


开始使用

点击以下链接即可转到 Cloud Shell 中的源代码副本。之后,只需一个命令即可在项目中启动应用的工作副本。

在 Cloud Shell 中打开

查看 GitHub 上的源代码


单个虚拟机组件

单一虚拟机架构使用一个关键产品。下面重点介绍了该产品以及更多信息,包括相关视频链接、产品文档和互动演示。
视频 文档 演示
Compute Engine Compute Engine 是 Google Cloud 的虚拟技术。借助 Cloud Spanner,您可以启动许多不同的虚拟机配置,以适应您的任何计算需求。

脚本

安装脚本使用 go 和 Terraform CLI 工具编写的可执行文件接受一个空项目,并在其中安装应用。输出结果应该是正常运行的应用以及负载均衡 IP 地址的网址。

./main.tf

启用服务

默认情况下,Google Cloud 服务在项目中处于停用状态。若要使用此处的任何解决方案,我们必须启用以下内容:

  • Compute Engine - 虚拟机和网络
variable "gcp_service_list" {
    description = "The list of apis necessary for the project"
    type        = list(string)
    default = [
        "compute.googleapis.com",
    ]
}

resource "google_project_service" "all" {
  for_each                   = toset(var.gcp_service_list)
  project                    = var.project_number
  service                    = each.key
  disable_dependent_services = false
  disable_on_destroy         = false
}

创建虚拟机

创建虚拟机。

resource "google_compute_instance" "instance" {
    name         = var.instance-name
    machine_type = var.instance-machine-type
    zone         = var.zone
    project      = var.project_id
    tags         = var.instance-tags

    boot_disk {
        auto_delete = true
        device_name = var.instance-name
        initialize_params {
        image = var.instance-image
        size  = var.instance-disksize
        type  = var.instance-disktype
        }
    }

    network_interface {
        network = "default"
        access_config {
        // Ephemeral public IP
        }
    }

    depends_on = [google_project_service.all]
    }

总结

运行后,您现在应该有一个虚拟机。您可以通过 SSH 连接到该服务器,并按需进行配置。此外,您应具备所有代码,以便修改或扩展此解决方案,使其适合您的环境。