【问题标题】:Attach NIC to a running VM in Azure using Terraform使用 Terraform 将 NIC 附加到 Azure 中正在运行的 VM
【发布时间】:2023-03-30 15:58:01
【问题描述】:

我编写了一个 terraform 脚本来在 Azure 中部署 VM。我想在不同时间将多个 NIC 附加/分离到该 VM。 由于 VM 在部署后开始运行,当我尝试在 azurerm_virtual_machine 资源块内向 network_interface_ids 添加另一个 NIC Id 时出现错误。 我认为 Terraform 还不能改变 Azure 中 VM 的状态。 由于在 terraform 中看不到任何 VM-NIC 关联资源,我该如何实现?

我是云和 terraform 的初学者,所以这可能是一个基本问题,但我无法在任何地方找到解决方案。任何帮助表示赞赏。

我当前的代码如下所示:

resource "azurerm_network_interface" "nic1" {
  name = "nic1"
  resource_group_name = data.azurerm_resource_group.rg.name
  location = data.azurerm_resource_group.rg.location
  enable_accelerated_networking = true
  ip_configuration {
    name = "nic1-config1"
    # public_ip_address_id = azurerm_public_ip.public_ip.id
    private_ip_address_allocation = "dynamic"
    subnet_id = data.azurerm_subnet.subnet.id
    primary = true

  }
  ip_configuration {
    name = "nic1-config2"
    public_ip_address_id = azurerm_public_ip.public_ip.id
    private_ip_address_allocation = "dynamic"
    subnet_id = data.azurerm_subnet.subnet.id
  }
  tags =  var.TAGS
}



 resource "azurerm_network_interface" "nic2" {
  name = "nic2"
  resource_group_name = data.azurerm_resource_group.rg.name
  location = data.azurerm_resource_group.rg.location
  enable_accelerated_networking = true
  ip_configuration {
    name = "nic2-config1"
    # public_ip_address_id = azurerm_public_ip.public_ip.id
    private_ip_address_allocation = "dynamic"
    subnet_id = data.azurerm_subnet.subnet.id
    primary = true

  }
  ip_configuration {
    name = "nic2-config2"
    # public_ip_address_id = azurerm_public_ip.public_ip.id
    private_ip_address_allocation = "dynamic"
    subnet_id = data.azurerm_subnet.subnet.id
  }
  tags =  var.TAGS
}


resource "azurerm_virtual_machine" "vm" {
  name = "vm"
  resource_group_name = data.azurerm_resource_group.rg.name
  location = data.azurerm_resource_group.rg.location
  network_interface_ids = [azurerm_network_interface.nic1.id]
  vm_size = "Standard_D4S_v3"
  delete_os_disk_on_termination = true
  delete_data_disks_on_termination = true
  storage_os_disk {
    name = "os-disk"
    create_option = "FromImage"
    caching = "ReadWrite"
    managed_disk_type = "Premium_LRS"
    disk_size_gb = 32
  }

  primary_network_interface_id = azurerm_network_interface.nic.id
  
  storage_image_reference {
    id = lookup(var.VMI,data.azurerm_resource_group.rg.location)
  }


  os_profile {
    admin_username = "test"
    computer_name = "test"
    admin_password = "test"
  }
  os_profile_linux_config {
    disable_password_authentication = true


    ssh_keys {
      path = "/home/user/.ssh/authorized_keys"
      key_data = "..."
    }
  }
  tags =  var.TAGS
  
}

我成功部署了上述基础设施。

现在我想将 nic2 附加到这个虚拟机,所以我做了以下更改

resource "azurerm_virtual_machine" "vm" {
....
  network_interface_ids = [azurerm_network_interface.nic1.id, azurerm_network_interface.nic2.id]
...
}

https://azure.microsoft.com/en-us/blog/introducing-the-new-dv3-and-ev3-vm-sizes 说我最多可以将两个 NIC 连接到 Standard_D4s_v3。

我收到此错误,它清楚地表明关闭虚拟机然后尝试附加。

错误:compute.VirtualMachinesClient#CreateOrUpdate Code="AddingOrDeletingNetworkInterfacesOnARunningVirtualMachineNotSupported" Message="具有单个网络接口的虚拟机 vm 必须先停止释放,然后才能更新为具有多个网络接口,反之亦然。"

我想知道是否有办法在 Azure 中将 NIC 热附加到 VM?

我在看这个https://github.com/Azure/azure-powershell/issues/4565

【问题讨论】:

  • 首先您可以发布代码和错误消息。其次,如果您要删除 nic,则需要将其关闭。最后,根据机器的大小,您可以连接的 NIC 数量受到限制。

标签: terraform terraform-provider-azure


【解决方案1】:

当您将另一个 NIC 附加到现有 VM 时,该 VM 必须处于停止(解除分配)状态。它是设计好的。而且 Terraform 没有单独的资源用于 VM 和 NIC 之间的关联。

因此,据我所知,您需要先停止 VM,然后将第二个 nic id 添加到 VM 作为您所做的更改:

resource "azurerm_virtual_machine" "vm" {
....
  network_interface_ids = [azurerm_network_interface.nic1.id, azurerm_network_interface.nic2.id]
...
}

当虚拟机处于停止状态时,应用 Terraform 代码。要通过 Terraform 停止 VM,您可以使用 null_resource with local-exec provisioner 执行 CLI 命令:

resource "null_resource" "example2" {
  provisioner "local-exec" {
    command = "az vm stop --resource-group groupName --name vmName"
    interpreter = ["/bin/bash", "-c"]
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-10
    • 2021-12-21
    • 2020-03-14
    • 2021-07-12
    • 1970-01-01
    • 2019-12-07
    相关资源
    最近更新 更多