azure - 如何在 Azure 中一次启动多个虚拟机
问题描述
当我尝试启动多个 Azure VM 时,Terraform 的计数元属性不起作用,而且 Azure 管理控制台上也没有这样的选项。
该代码通过仅启动一个 VM 就可以正常工作,并且在计划或应用执行阶段它甚至不会给出任何错误。
resource "azurerm_virtual_machine" "main" {
name = "terra-vm"
location = "${var.location}"
count = 2
resource_group_name = "${var.resourcegpname}"
network_interface_ids = ["${element(var.netif, count.index)}"]
vm_size = "Standard_B1s"
delete_os_disk_on_termination = true
delete_data_disks_on_termination = true
storage_image_reference {
publisher = "Canonical"
offer = "UbuntuServer"
sku = "16.04-LTS"
version = "latest"
}
storage_os_disk {
name = "myosdisk1"
caching = "ReadWrite"
create_option = "FromImage"
managed_disk_type = "Standard_LRS"
}
os_profile {
computer_name = "hostname"
admin_username = "testadmin"
admin_password = "Password1234!"
custom_data = "sudo apt-get install apache2 -y && sudo systemctl
start apache2"
}
os_profile_linux_config {
disable_password_authentication = "false"
}
tags {
environment = "staging"
}
}
variable "netif" {
type = "list"
}
variable "resourcegpname" {}
variable "location" {}
就像 AWS 我期待它应该启动指定数量的虚拟机。
解决方案
我认为它不会只为您创建一个虚拟机。原因是您只需创建一个 OS 磁盘,然后使用单个 OS 磁盘逐个覆盖 VM,最后只生成一个 VM。
所以你应该为你的虚拟机创建不同的操作系统磁盘名称,其他资源也需要创建更多,这一切都取决于你的虚拟机的数量。你可以这样做:
resource "azurerm_virtual_machine" "test" {
count = 2
name = "acctvm${count.index}"
location = "${azurerm_resource_group.test.location}"
availability_set_id = "${azurerm_availability_set.avset.id}"
resource_group_name = "${azurerm_resource_group.test.name}"
network_interface_ids = ["${element(azurerm_network_interface.test.*.id, count.index)}"]
vm_size = "Standard_DS1_v2"
# Uncomment this line to delete the OS disk automatically when deleting the VM
# delete_os_disk_on_termination = true
# Uncomment this line to delete the data disks automatically when deleting the VM
# delete_data_disks_on_termination = true
storage_image_reference {
publisher = "Canonical"
offer = "UbuntuServer"
sku = "16.04-LTS"
version = "latest"
}
storage_os_disk {
name = "myosdisk${count.index}" <--here is core in your case
caching = "ReadWrite"
create_option = "FromImage"
managed_disk_type = "Standard_LRS"
}
# Optional data disks
storage_data_disk {
name = "datadisk_new_${count.index}"
managed_disk_type = "Standard_LRS"
create_option = "Empty"
lun = 0
disk_size_gb = "1023"
}
storage_data_disk {
name = "${element(azurerm_managed_disk.test.*.name, count.index)}"
managed_disk_id = "${element(azurerm_managed_disk.test.*.id, count.index)}"
create_option = "Attach"
lun = 1
disk_size_gb = "${element(azurerm_managed_disk.test.*.disk_size_gb, count.index)}"
}
os_profile {
computer_name = "hostname"
admin_username = "testadmin"
admin_password = "Password1234!"
}
os_profile_linux_config {
disable_password_authentication = false
}
tags {
environment = "staging"
}
}
您可以通过 Terraform从 Create Multiple VMs 获取整个模板。
推荐阅读
- java - 在arraylist中存储同一父级的多个数据类型
- python - 端点 URL 无法访问 ConnectTimeoutError:端点 URL 上的连接超时:“https://glue.ap-south-1.amazonaws.com/”
- google-bigquery - 如何从 Bigquery 中的 Kmeans 聚类表中获取聚类数?
- html - Vue.js:在子组件的插槽中动态呈现 html
- buildpack - 是否可以自定义使用 Spring Native 生成的 docker 镜像(使用 buildpack)
- excel - 带有 BDH 功能自动化错误的 Bloomberg 文件
- azure - 在 Databricks 上使用 Pyspark 访问 Azure ADLS gen2
- c# - MongoDriver c# 按日期字段中的月份名称过滤
- blazor - Blazor - 我如何设置从父母到孩子的参数
- authentication - SSO 与 JWT 和多个用户帐户 + SPA