單一 VM

+

架構

單一 VM 堆疊會建立個別 VM,並提供智慧型預設值,同時也能自訂虛擬機器。使用方式如下:

  • 運算 - VM - Compute Engine

這個範例會設定單一簡單的虛擬機器,並允許您透過 SSH 連線至該機器。


開始使用

按一下以下連結,前往 Cloud Shell 中的原始碼副本。完成後,您只需執行單一指令,即可在專案中啟動應用程式的可用副本。

在 Cloud Shell 開啟

在 GitHub 上查看原始碼


單一 VM 元件

單一 VM 架構會使用一個主要產品。以下內容會強調該產品,並提供更多資訊,包括相關影片、產品說明文件和互動式操作說明的連結。
影片 文件 逐步操作說明
Compute Engine Compute Engine 是 Google Cloud 的虛擬技術。您可以使用這項功能啟動多種不同的 VM 設定,以符合各種運算需求。

指令碼

安裝指令碼會使用以 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
}

建立虛擬機器

建立 VM。

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]
    }

結論

執行後,您應該會看到單一 VM。您可以透過 SSH 連線並視需要設定。此外,您應該擁有所有程式碼,以便修改或擴充這個解決方案,以便符合您的環境。