單一 VM 堆疊會使用智慧預設值建立個別 VM,但也能自訂虛擬機器。這項功能會使用:
- 運算 - VM - Compute Engine
範例會設定簡單的單一虛擬機器,並允許您透過 SSH 進入該機器。
開始使用
按一下下列連結,即可在 Cloud Shell 中複製原始碼。完成後,只要執行單一指令,即可在專案中啟動應用程式的工作副本。
單一 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 連線並視需要設定。此外,您也應擁有所有程式碼,可修改或擴充這個解決方案,以符合您的環境需求。