从映像创建VM并添加到现有可用性集

时间:2016-03-08 08:41:55

标签: azure azure-virtual-machine

如何添加我在门户中创建或从vmdepot导入到现有可用性集的现有VM?它似乎不可能来自门户网站,它是否在Powershell中运行?它是否适用于Resource Manager部署模型?

enter image description here

1 个答案:

答案 0 :(得分:1)

目前,对于ARM部署,尚不支持设置可用性集。可用性集只能在创建期间添加。因此,对于您的情况,您需要删除当前实例,并使用旧的OSDisk,Vnet和其他一些设置创建新部署。

这可以通过使用ARM模板来实现。以下示例说明如何使用现有VHD和Vnet部署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": "Please enter the location where you want to deploy this VM"
      }
    },
    "vmName": {
      "type": "string",
      "metadata": {
        "description": "Name of the VM"
      }
    },
    "osType": {
      "type": "string",
      "allowedValues": [
        "Windows",
        "Linux"
      ],
      "metadata": {
        "description": "Type of OS on the existing vhd"
      }
    },
    "osDiskVhdUri": {
      "type": "string",
      "metadata": {
        "description": "Uri of the existing VHD in ARM standard or premium storage"
      }
    },
    "vmSize": {
      "type": "string",
      "metadata": {
        "description": "Size of the VM"
      }
    },
    "existingVirtualNetworkName": {
      "type": "string",
      "metadata": {
        "description": "Name of the existing VNET"
      }
    },
    "existingVirtualNetworkResourceGroup": {
      "type": "string",
      "metadata": {
        "description": "Name of the existing VNET resource group"
      }
    },
    "subnetName": {
      "type": "string",
      "metadata": {
        "description": "Name of the subnet in the virtual network you want to use"
      }
    },
    "dnsNameForPublicIP": {
      "type": "string",
      "metadata": {
        "description": "Unique DNS Name for the Public IP used to access the Virtual Machine."
      }
    },
    "availabilitySetName": {
      "type": "string",
      "metadata": {
        "description": "The name of your Availability Set."
      }
    }
  },
  "variables": {
    "api-version": "2015-06-15",
    "publicIPAddressType": "Dynamic",
    "vnetID": "[resourceId(parameters('existingVirtualNetworkResourceGroup'), 'Microsoft.Network/virtualNetworks', parameters('existingVirtualNetworkName'))]",
    "subnetRef": "[concat(variables('vnetID'),'/subnets/', parameters('subnetName'))]",
    "nicName": "[parameters('vmName')]",
    "publicIPAddressName": "[parameters('vmName')]"
  },
  "resources": [
    {
      "apiVersion": "[variables('api-version')]",
      "type": "Microsoft.Network/publicIPAddresses",
      "name": "[variables('publicIPAddressName')]",
      "location": "[parameters('location')]",
      "tags": {
        "displayName": "PublicIPAddress"
      },
      "properties": {
        "publicIPAllocationMethod": "[variables('publicIPAddressType')]",
        "dnsSettings": {
          "domainNameLabel": "[parameters('dnsNameForPublicIP')]"
        }
      }
    },
    {
      "apiVersion": "[variables('api-version')]",
      "type": "Microsoft.Network/networkInterfaces",
      "name": "[variables('nicName')]",
      "location": "[parameters('location')]",
      "dependsOn": [
        "[concat('Microsoft.Network/publicIPAddresses/', variables('publicIPAddressName'))]"
      ],
      "tags": {
        "displayName": "NetworkInterface"
      },
      "properties": {
        "ipConfigurations": [
          {
            "name": "ipconfig1",
            "properties": {
              "privateIPAllocationMethod": "Dynamic",
              "publicIPAddress": {
                "id": "[resourceId('Microsoft.Network/publicIPAddresses',variables('publicIPAddressName'))]"
              },
              "subnet": {
                "id": "[variables('subnetRef')]"
              }
            }
          }
        ]
      }
    },
    {
      "type": "Microsoft.Compute/availabilitySets",
      "name": "[parameters('availabilitySetName')]",
      "apiVersion": "2015-06-15",
      "location": "[parameters('location')]",
      "properties": {
        "platformFaultDomainCount": "3",
        "platformUpdateDomainCount": "20"
      }
    },
    {
      "apiVersion": "[variables('api-version')]",
      "type": "Microsoft.Compute/virtualMachines",
      "name": "[parameters('vmName')]",
      "location": "[parameters('location')]",
      "tags": {
        "displayName": "VirtualMachine"
      },
      "dependsOn": [
        "[concat('Microsoft.Compute/availabilitySets/', parameters('availabilitySetName'))]",
        "[concat('Microsoft.Network/networkInterfaces/', variables('nicName'))]"
      ],
      "properties": {
        "hardwareProfile": {
          "vmSize": "[parameters('vmSize')]"
        },
        "AvailabilitySet" : {
          "id": "[resourceId('Microsoft.Compute/availabilitySets', parameters('availabilitySetName'))]"
        },
        "storageProfile": {
          "osDisk": {
            "name": "[concat(parameters('vmName'))]",
            "osType": "[parameters('osType')]",
            "caching": "ReadWrite",
            "vhd": {
              "uri": "[parameters('osDiskVhdUri')]"
            },
            "createOption": "Attach"
          }
        },
        "networkProfile": {
          "networkInterfaces": [
            {
              "id": "[resourceId('Microsoft.Network/networkInterfaces',variables('nicName'))]"
            }
          ]
        }
      }
    }
  ]
}

如果要将VM添加到现有可用性集,可以删除

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

        "[concat('Microsoft.Compute/availabilitySets/', parameters('availabilitySetName'))]",

有关创建ARM模板的更多详细信息,请参阅Authoring Azure Resource Manager templates

有关如何部署ARM模板的详细信息,请参阅Deploy a Resource Group with Azure Resource Manager template

以上模板是从GitHub的this sample template修改的。





在我发布这个答案后,我一直在考虑使用REST API来更新带有可用性集的vm。我认为它可能有效,所以我试了一下,这是我得到的错误信息:

Invoke-RestMethod : {
  "error": {
    "code": "PropertyChangeNotAllowed",
    "target": "availabilitySet.id",
    "message": "Changing property 'availabilitySet.id' is not allowed."
  }
}
At C:\Users\v-dazen\Documents\setVMRestAPI.ps1:13 char:1
+ Invoke-RestMethod -Method Put -Uri "https://management.azure.com/subs ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

这意味着无法为现有的ARM部署的VM设置可用性集。