【问题标题】:Adding Availability Set To Azure Virtual Machine Template Creation将可用性集添加到 Azure 虚拟机模板创建
【发布时间】:2016-04-07 13:53:46
【问题描述】:

我可以使用下面的模板创建具有特定 VHD 的 Azure VM,但如何将其添加到可用性集中。创建 VM 后我无法执行此操作,因此我需要在此处执行此操作。

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "location": {
            "type": "string",
            "metadata": {
                "description": "Location to create the VM in"
            }
        },
        "osDiskVhdUri": {
            "type": "string",
            "metadata": {
                "description": "Uri of the existing VHD"
            }
        },
        "osType": {
            "type": "string",
            "allowedValues": [
                "Windows",
                "Linux"
            ],
            "metadata": {
                "description": "Type of OS on the existing vhd"
            }
        },
        "vmSize": {
            "type": "string",
            "defaultValue": "Standard_D2",
            "metadata": {
                "description": "Size of the VM"
            }
        },
        "vmName": {
            "type": "string",
            "metadata": {
                "description": "Name of the VM"
            }
        }
    },
    "variables": {
        "api-version": "2015-06-15",
        "addressPrefix": "10.0.0.0/16",
        "subnetName": "Subnet",
        "subnetPrefix": "10.0.0.0/24",
        "publicIPAddressName": "specializedVMPublicIP",
        "publicIPAddressType": "Dynamic",
        "virtualNetworkName": "specializedVMVNET",
        "vnetID": "[resourceId('Microsoft.Network/virtualNetworks',variables('virtualNetworkName'))]",
        "subnetRef": "[concat(variables('vnetID'),'/subnets/',variables('subnetName'))]",
        "nicName": "specializedVMNic"
    },
    "resources": [
        {
            "apiVersion": "[variables('api-version')]",
            "type": "Microsoft.Network/virtualNetworks",
            "name": "[variables('virtualNetworkName')]",
            "location": "[parameters('location')]",
            "properties": {
                "addressSpace": {
                    "addressPrefixes": [
                        "[variables('addressPrefix')]"
                    ]
                },
                "subnets": [
                    {
                        "name": "[variables('subnetName')]",
                        "properties": {
                            "addressPrefix": "[variables('subnetPrefix')]"
                        }
                    }
                ]
            }
        },
        {
            "apiVersion": "[variables('api-version')]",
            "type": "Microsoft.Network/networkInterfaces",
            "name": "[variables('nicName')]",
            "location": "[parameters('location')]",
            "dependsOn": [
                "[concat('Microsoft.Network/publicIPAddresses/', variables('publicIPAddressName'))]",
                "[concat('Microsoft.Network/virtualNetworks/', variables('virtualNetworkName'))]"
            ],
            "properties": {
                "ipConfigurations": [
                    {
                        "name": "ipconfig1",
                        "properties": {
                            "privateIPAllocationMethod": "Dynamic",
                            "publicIPAddress": {
                                "id": "[resourceId('Microsoft.Network/publicIPAddresses',variables('publicIPAddressName'))]"
                            },
                            "subnet": {
                                "id": "[variables('subnetRef')]"
                            }
                        }
                    }
                ]
            }
        },
        {
            "apiVersion": "[variables('api-version')]",
            "type": "Microsoft.Network/publicIPAddresses",
            "name": "[variables('publicIPAddressName')]",
            "location": "[parameters('location')]",
            "properties": {
                "publicIPAllocationMethod": "[variables('publicIPAddressType')]"
            }
        },
        {
            "apiVersion": "[variables('api-version')]",
            "type": "Microsoft.Compute/virtualMachines",
            "name": "[parameters('vmName')]",
            "location": "[parameters('location')]",
            "dependsOn": [
                "[concat('Microsoft.Network/networkInterfaces/', variables('nicName'))]"
            ],
            "properties": {
                "hardwareProfile": {
                    "vmSize": "[parameters('vmSize')]"
                },
                "storageProfile": {
                    "osDisk": {
                        "name": "[concat(parameters('vmName'),'-osDisk')]",
                        "osType": "[parameters('osType')]",
                        "caching": "ReadWrite",
                        "vhd": {
                            "uri": "[parameters('osDiskVhdUri')]"
                        },
                        "createOption": "Attach"
                    }
                },
                "networkProfile": {
                    "networkInterfaces": [
                        {
                            "id": "[resourceId('Microsoft.Network/networkInterfaces', variables('nicName'))]"
                        }
                    ]
                }
            }
        }
    ]
}

【问题讨论】:

    标签: azure azure-virtual-machine azure-resource-manager arm-template azure-availability-set


    【解决方案1】:

    解决此类问题的最佳方法是浏览templates,直到找到所需内容!

    所以根据this模板,你创建一个这样的可用性集

    "resources": [
      {
        "type": "Microsoft.Compute/availabilitySets",
        "name": "availabilitySet1",
        "apiVersion": "2015-06-15",
        "location": "[parameters('location')]",
        "properties": {
          "platformFaultDomainCount": "3",
          "platformUpdateDomainCount": "20"
        }
      }
    ]
    

    然后(根据this)你像这样使用它

    "apiVersion": "[variables('apiVersion')]",
    "type": "Microsoft.Compute/virtualMachines",
    "name": "[concat('myvm', copyIndex())]",
    "location": "[variables('location')]",
    "copy": {
      "name": "virtualMachineLoop",
      "count": "[parameters('numberOfInstances')]"
    },
    "dependsOn": [
      "[concat('Microsoft.Network/networkInterfaces/', 'nic', copyindex())]",
      "[concat('Microsoft.Storage/storageAccounts/', variables('storageAccountName'))]"
    ],
    "properties": {
      "availabilitySet": {
        "id": "[resourceId('Microsoft.Compute/availabilitySets', variables('availabilitySetName'))]"
      },
    

    【讨论】:

      【解决方案2】:

      您需要查看 Microsoft.Compute/availabilitySets 资源提供程序,这是来自我的模板之一的一些示例 JSON。

      "resources": [
      {
        "type": "Microsoft.Compute/availabilitySets",
        "name": "availabilitySet1",
        "apiVersion": "2015-06-15",
        "location": "[parameters('location')]",
        "properties": {
          "platformFaultDomainCount": "3",
          "platformUpdateDomainCount": "20"
        }
      }
      ]
      

      然后,您需要使用 virtualMachines 资源提供程序的 availabilitySet 属性将 VM 添加到可用性集。确保使用 dependsOn 以确保在 VM 之前创建可用性集。例如,如果您按名称引用它:

      "properties": {
              "hardwareProfile": { "vmSize": "Standard_A0" },
              "networkProfile": ...,
              "availabilitySet": { "id": "availabilitySet1" },
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-09-06
        • 2019-05-10
        • 1970-01-01
        • 1970-01-01
        • 2019-11-08
        • 1970-01-01
        • 2019-09-17
        • 2021-07-15
        相关资源
        最近更新 更多