ARM模板:如何在一个资源组中创建具有私有IP的多个VM

时间:2018-05-29 22:00:37

标签: azure templates azure-resource-manager azure-virtual-network

在ARM模板下面,我试图在一个Resource组中创建3个VM。 从Azure门户网站错误中我发现该模板在VirtualNetwork创建时失败。

   "name": "[variables('virtualNetworkName')]",
    "type": "Microsoft.Network/virtualNetworks"

我需要的是在同一资源组中简单扩展~900个具有私有IP 的相同虚拟机。不需要DNS或加入域。

我发现要创建虚拟机,我需要虚拟网络和网络安全组。我想我不需要存储帐户,或者至少我可以使用一个存储帐户来支持所有存储帐户。 在Azure的快速入门模板中,我没有找到这样的模板。 非常感谢您的帮助,使其成功。

virtualMachineSize:Basic_A0

{
  "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": { 
        "virtualMachineName": {
            "type": "string",
			"defaultValue": "uservm0010"
        }, 
        "virtualMachineSize": {
            "type": "string"
			
        },
        "adminUsername": {
            "type": "string",
			"defaultValue": "user"
        },
           "adminPassword": {
            "type": "securestring",
			 "value": null
        }
  },
   "variables": {
       
		"nicName": "myVMNic",
		"addressPrefix": "10.0.0.0/16",
		"subnetName": "Subnet",
		"subnetPrefix": "10.0.0.0/24",
		"vmName": "SimpleLinVM",
		"virtualNetworkName": "MyVNET",			  
		"subnetRef": "[resourceId('Microsoft.Network/virtualNetworks/subnets', variables('virtualNetworkName'), variables('subnetName'))]"
				
    },
  "resources": [
      {
           "apiVersion": "2017-12-01",
           "name": "[concat(variables('vmName'), padLeft(copyIndex(), 2, '0'))]",
            "type": "Microsoft.Compute/virtualMachines",				            
            "location": "[parameters('location')]",
            "properties": {
                "osProfile": {
                   "computerName": "[variables('vmName')]",
					"adminUsername": "[parameters('adminUsername')]",
					"adminPassword": "[parameters('adminPassword')]"
                },
                "hardwareProfile": {
                    "vmSize": "Basic_A0"
                },
                "storageProfile": {
                    "imageReference": {
                        "publisher": "credativ",
                        "offer": "Debian",
                        "sku": "9",
                        "version": "latest"
                    },
                    
                    "dataDisks": []
                },
                "networkProfile": {
                    "networkInterfaces": [
                        {
							"id": "[resourceId('Microsoft.Network/networkInterfaces',variables('nicName'))]"
						}
                    ]
                }

            }
      },
      {  
            "name": "[variables('virtualNetworkName')]",
            "type": "Microsoft.Network/virtualNetworks",
            "apiVersion": "2018-02-01",
            "location": "[parameters('location')]",
            "properties": {
                "addressSpace": {
                    "addressPrefixes": [
                        "[variables('addressPrefix')]"
                    ]
                },
                "subnets": [
                    {
                        "name": "[variables('subnetName')]",
                        "properties": {
                            "addressPrefix": "[variables('subnetPrefix')]"
                        }
                    }
                ]
            }
        },
        { 
			"name": "[variables('nicName')]",	
			"type": "Microsoft.Network/networkInterfaces",
			"apiVersion": "2016-09-01",
			"location": "[parameters('location')]",
			            
            "properties": {
                "ipConfigurations": [
                    {
                        "name": "ipconfig1",
                        "properties": {
                            "subnet": {
                                "id": "[variables('subnetRef')]"
                            },
                            "privateIPAllocationMethod": "Dynamic"
                        }
                    }
                ]
            
            }
        },
        {"copy":{
			"name": "[parameters('virtualMachineName')]",
			"count": 3
			}
        }
  ]
}

1 个答案:

答案 0 :(得分:1)

您可以使用我在下面测试过的模板。该模板包含一个可用性集和一个虚拟网络以及三个VM,无NSG。我建议你这么多可用性。你可以随意改变一些东西。

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "adminUsername": {
            "type": "string",
            "metadata": {
                "description": "Admin username for VM"
            }
        },
        "adminPassword": {
            "type": "securestring",
            "metadata": {
                "description": "Admin password for VMs"
            }
        },
        "numberOfInstances": {
            "type": "int",
            "defaultValue": 2,
            "minValue": 2,
            "maxValue": 5,
            "metadata": {
                "description": "Number of VMs to deploy, limit 5 since this sample is using a single storage account"
            }
        },
        "OS": {
            "type": "string",
            "defaultValue": "Ubuntu",
            "allowedValues": [
                "Ubuntu",
                "Windows"
            ],
            "metadata": {
                "description": "OS Platform for the VM"
            }
        },
        "location": {
            "type": "string",
            "defaultValue": "[resourceGroup().location]",
            "metadata": {
                "description": "Location for all resources."
            }
        },
        "vmSizes": {
            "type": "string",
            "allowedValues": [
                "Basic_A0",
                "Standard_D1_v2"
            ],
            "defaultValue": "Standard_D1_v2",
            "metadata": {
                    "description": "The type of replication to use for the VM size."
            }
        }
    },
    "variables": {
        "virtualNetworkName": "myVNET",
        "addressPrefix": "10.0.0.0/16",
        "subnet1Name": "Subnet-1",
        "subnet1Prefix": "10.0.0.0/24",
        "subnet1Ref": "[resourceId('Microsoft.Network/virtualNetworks/subnets',variables('virtualNetworkName'),variables('subnet1Name'))]",
        "availabilitySetName": "myAvSet",
        "Ubuntu": {
            "publisher": "Canonical",
            "offer": "UbuntuServer",
            "sku": "16.04.0-LTS",
            "version": "latest"
        },
        "Windows": {
            "publisher": "MicrosoftWindowsServer",
            "offer": "WindowsServer",
            "sku": "2016-Datacenter",
            "version": "latest"
        },
        "imageReference": "[variables(parameters('OS'))]"
    },
    "resources": [
        {
            "type": "Microsoft.Compute/availabilitySets",
            "name": "[variables('availabilitySetName')]",
            "apiVersion": "2016-04-30-preview",
            "location": "[parameters('location')]",
            "properties": {
                "platformFaultDomainCount": 2,
                "platformUpdateDomainCount": 2,
                "managed": true
            }
        },
        {
            "type": "Microsoft.Network/virtualNetworks",
            "name": "[variables('virtualNetworkName')]",
            "apiVersion": "2016-03-30",
            "location": "[parameters('location')]",
            "properties": {
                "addressSpace": {
                    "addressPrefixes": [
                        "[variables('addressPrefix')]"
                    ]
                },
                "subnets": [
                    {
                        "name": "[variables('subnet1Name')]",
                        "properties": {
                            "addressPrefix": "[variables('subnet1Prefix')]"
                        }
                    }
                ]
            }
        },
        {
            "type": "Microsoft.Network/networkInterfaces",
            "name": "[concat('nic', copyindex())]",
            "apiVersion": "2016-03-30",
            "location": "[parameters('location')]",
            "copy": {
                "name": "nicLoop",
                "count": "[parameters('numberOfInstances')]"
            },
            "dependsOn": [
                "[variables('virtualNetworkName')]"
            ],
            "properties": {
                "ipConfigurations": [
                    {
                        "name": "ipconfig1",
                        "properties": {
                            "privateIPAllocationMethod": "Dynamic",
                            "subnet": {
                                "id": "[variables('subnet1Ref')]"
                            }
                        }
                    }
                ]
            }
        },
        {
            "type": "Microsoft.Compute/virtualMachines",
            "name": "[concat('myvm', copyIndex())]",
            "apiVersion": "2016-04-30-preview",
            "location": "[parameters('location')]",
            "copy": {
                "name": "virtualMachineLoop",
                "count": "[parameters('numberOfInstances')]"
            },
            "dependsOn": [
                "nicLoop"
            ],
            "properties": {
                "availabilitySet": {
                    "id": "[resourceId('Microsoft.Compute/availabilitySets', variables('availabilitySetName'))]"
                },
                "hardwareProfile": {
                    "vmSize": "[parameters('vmSizes')]"
                },
                "osProfile": {
                    "computerName": "[concat('vm', copyIndex())]",
                    "adminUsername": "[parameters('adminUsername')]",
                    "adminPassword": "[parameters('adminPassword')]"
                },
                "storageProfile": {
                    "imageReference": "[variables('imageReference')]",
                    "osDisk": {
                        "createOption": "FromImage"
                    }
                },
                "networkProfile": {
                    "networkInterfaces": [
                        {
                            "id": "[resourceId('Microsoft.Network/networkInterfaces',concat('nic', copyindex()))]"
                        }
                    ]
                }
            }
        }
    ]
}

如果模板可以帮助您,请告诉我。