单个虚拟机

使用集合让一切井井有条 根据您的偏好保存内容并对其进行分类。

架构

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

  • 计算 - 虚拟机 - Compute Engine

Example 会设置一个简单的虚拟机,并可让您通过 SSH 连接到该虚拟机。


开始

点击以下链接,转到 Cloud Shell 中源代码的副本。完成后,一条命令将启动项目中应用的工作副本。

在 Cloud Shell 中打开

在 GitHub 上查看源代码


单个虚拟机组件

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

脚本

安装脚本使用用 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 登录,然后根据需要对其进行配置。此外,您应该能够根据自己的环境修改或扩展此解决方案。