【问题标题】:Terraform multiple VM with multiple disk GCP使用多磁盘 GCP 对多个 VM 进行 Terraform
【发布时间】:2020-10-16 21:02:24
【问题描述】:

我正在尝试创建需要为每个 VM 创建和附加磁盘列表的 VM 列表。

在下面的示例中,我必须使用 3 个磁盘 test-d01-data 和 test-d01-data-disk 以及 test-d01-commitlog-disk 创建 test-d01 Compute 同理 test-d02 用 2 个磁盘 test-d02-data-01 和 test-d02-data-02 计算。

例如下面的 VM_info 代表所需的配置,它会

    {
      name       = "test-d01"
      zone       = "us-east1-b"    
      disk = [
        {
          disk_name = "test-d01-data"
          disk_type = "pd-ssd"
          disk_size = "60"
        },
        {
          disk_name = "test-d01-data-disk"
          disk_type = "pd-standard"
          disk_size = "15"
        },
        {
          disk_name = "test-d01-commitlog-disk"
          disk_type = "pd-ssd"
          disk_size = "30"
        }
      ]
    },
    {
      name       = "test-d02"
      zone       = "us-east1-b"
           
        disk=[
        {
        disk_name = "test-d02-data"
        disk_type = "pd-ssd"
        disk_size = "60"
        },
        {
        disk_name = "test-d02-data-disk"
        disk_type = "pd-standard"
        disk_size = "15"
        }
        ]
    },
  ]

【问题讨论】:

  • 你有什么问题?你的错误是什么?我们能提供什么帮助?
  • 基本上我需要创建 terraform 共享模块来实现上述要求。现在我可以创建具有相同数量磁盘的虚拟机数量。但是现在我需要更新具有不相等数量磁盘的模块,例如第一个带有 3 个磁盘的虚拟机,第二个带有 2 个磁盘的虚拟机,然后继续......所以有可能以任何集合或任何其他方式在 terraform 中..在这方面需要帮助

标签: google-cloud-platform virtual-machine terraform disk terraform-provider-gcp


【解决方案1】:

好主意。当我使用它时,我确实得到了一个

terraform plan
var.disks
  Enter a value: 2

var.instance_name
  Enter a value: DDVE5


Error: Reference to undeclared resource

  on main.tf line 39, in resource "google_compute_attached_disk" "vm_attached_disk":
  39:   instance = google_compute_instance.vm_instance.self_link

A managed resource "google_compute_instance" "vm_instance" has not been
declared in the root module.

cat main.tf

variable "instance_name" {}
variable "instance_zone" {
  default = "europe-west3-c"
}
variable "instance_type" {
  default = "n1-standard-1"
}
variable "instance_subnetwork" {
  default = "default"
}
variable "disks" {}

provider "google" {
  credentials = file("key.json")
  project     = "ddve50"
  region      = "europe-west3"
  zone        = "europe-west3-a"
}

resource "google_compute_instance" "vm-instance" {
  name         = "ddve-gcp-5-7-2-0-20-65"
  machine_type = "f1-micro"
  tags         = ["creator", "juergen"]
  boot_disk {
    initialize_params {
      image = "ddve"
      type  = "pd-ssd"
    }
  }
  network_interface {
    network = "default"
  }
}

resource "google_compute_attached_disk" "vm_attached_disk" {
  for_each = toset(var.disks)
  disk     = each.key
  instance = google_compute_instance.vm_instance.self_link
}

猫../my_instances.tf

resource "google_compute_disk" "ddve-gcp-5-7-2-0-20-65-nvram" {
  name = "ddve-gcp-5-7-2-0-20-65-nvram"
  type = "pd-ssd"
  size = 10
}

resource "google_compute_disk" "ddve-gcp-5-7-2-0-20-65-m1" {
  name = "ddve-gcp-5-7-2-0-20-65-m1"
  type = "pd-standard"
  size = 1024
}

module "ddve-test-d01" {
  source        = "./instance"
  instance_name = "ddve-test-d01"
  disks = [
    google_compute_disk.ddve-gcp-5-7-2-0-20-65-nvram,
    google_compute_disk.ddve-gcp-5-7-2-0-20-65-m1

  ]
}

【讨论】:

    【解决方案2】:

    Terraform by HashiCorp > Compute Engine > Resources > google_compute_attached_disk:

    永久性磁盘可以使用 attached_disk 计算实例配置中的部分。 但是,可能存在通过以下方式管理附加磁盘的情况 计算实例配置不可取或不可能,例如 使用count 变量附加动态磁盘数

    因此,在这种情况下,使用属性 attached_disk 创建 google_compute_instance 的简单方法可能不适用。

    简而言之,这个想法是先创建永久性磁盘,然后创建 VM 实例并将新磁盘附加到新创建的实例。此测试部署由一个根配置文件my_instances.tf 和可重用模块./instance/main.tf 组成。

    1. 永久磁盘google_compute_disk 可以独立创建。为简单起见,使用了根文件 my_instances.tf 中的 5 个文字块。

    2.调用可重用模块instance/main.tf并将VM属性和磁盘列表传递给模块,以便:

    • 创建VM实例google_compute_instance
    • 使用绑定对象 google_compute_attached_disk 将新的空磁盘附加到新创建的 VM 实例。

    要处理磁盘列表,使用for_each 元参数。
    由于for_each 只接受一个映射或一组字符串,因此toset 函数用于将磁盘列表转换为一组。

    Terraform by HashiCorp > Compute Engine > Data Sources > google_compute_instance
    Terraform by HashiCorp > Compute Engine > Resources > google_compute_disk
    Terraform by HashiCorp > Compute Engine > Resources > google_compute_attached_disk
    Terraform by HashiCorp > Configuration language > Resources > lifecycle.ignore_changes
    Terraform by HashiCorp > Configuration language > Resources > When to Use for_each Instead of count
    Terraform by HashiCorp > Configuration language > Resources > The each Object
    Terraform by HashiCorp > Configuration language > Resources > Using Sets

    $ cat my_instances.tf
    resource "google_compute_disk" "test-d01-data" {
      name  = "test-d01-data"
      type  = "pd-ssd"
      size = 60
      zone = "europe-west3-c"
    }
    resource "google_compute_disk" "test-d01-data-disk" {
      name  = "test-d01-data-disk"
      type  = "pd-standard"
      size = 15
      zone = "europe-west3-c"
    }
    resource "google_compute_disk" "test-d01-commitlog-disk" {
      name  = "test-d01-commitlog-disk"
      type  = "pd-ssd"
      size = 30
      zone = "europe-west3-c"
    }
    
    resource "google_compute_disk" "test-d02-data" {
      name  = "test-d02-data"
      type  = "pd-ssd"
      size = 60
      zone = "europe-west3-c"
    }
    resource "google_compute_disk" "test-d02-data-disk" {
      name  = "test-d02-data-disk"
      type  = "pd-standard"
      size = 15
      zone = "europe-west3-c"
    }
    
    module "test-d01" {
      source           = "./instance"
      instance_name    = "test-d01"
      disks = [
        google_compute_disk.test-d01-data.name,
        google_compute_disk.test-d01-data-disk.name,
        google_compute_disk.test-d01-commitlog-disk.name
      ]
    }
    module "test-d02" {
      source           = "./instance"
      instance_name    = "test-d02"
      disks = [
        google_compute_disk.test-d02-data.name,
        google_compute_disk.test-d02-data-disk.name
      ]
    }
    
    $ cat instance/main.tf
    variable "instance_name" {}
    variable "instance_zone" {
      default = "europe-west3-c"
      }
    variable "instance_type" {
      default = "n1-standard-1"
      }
    variable "instance_subnetwork" {
      default = "default"
      }
    variable "disks" {}
    
    resource "google_compute_instance" "vm_instance" {
      name         = var.instance_name
      zone         = var.instance_zone
      machine_type = var.instance_type
      boot_disk {
        initialize_params {
          image = "debian-cloud/debian-9"
          }
      }
      network_interface {
        subnetwork = "${var.instance_subnetwork}"
        access_config {
          # Allocate a one-to-one NAT IP to the instance
        }
      }
      lifecycle {
        ignore_changes = [attached_disk]
      }
    }
    
    resource "google_compute_attached_disk" "vm_attached_disk" {
      for_each = toset(var.disks)
      disk = each.key
      instance = google_compute_instance.vm_instance.self_link
    }
    
    $ terraform fmt
    $ terraform init 
    $ terraform plan 
    $ terraform apply
    

    【讨论】:

      【解决方案3】:

      您也可以获取附加的磁盘:

      resource "google_compute_disk" "default" {
        name = "test-disk"
        type = "pd-ssd"
        zone = var.instance_zone
        # image = "debian-9-stretch-v20200805"
        labels = {
          environment = "dev"
        }
        physical_block_size_bytes = 4096
      }
      
      resource "google_compute_attached_disk" "vm_attached_disk" {
        count    = var.disks
        disk     = google_compute_disk.default.id
        instance = google_compute_instance.vm-instance.id
      }
      

      【讨论】:

        猜你喜欢
        • 2021-10-19
        • 2022-01-20
        • 2021-05-30
        • 2023-01-16
        • 2021-06-19
        • 2022-11-25
        • 2017-06-18
        • 2020-09-15
        • 2019-03-14
        相关资源
        最近更新 更多