如何使用Azure资源管理器从自定义映像创建VM

时间:2016-12-15 20:01:22

标签: azure

我一直在使用Azure Classic门户,我可以从我的自定义映像创建VM而不会出现问题。 现在我正在尝试使用新门户,当我去创建一个新的VM时,我没有看到使用我的图像的选项。如何从我的某个图像创建VM?

3 个答案:

答案 0 :(得分:1)

这是怎么做的...... 假设您的资源组已创建,我的infra配置如下 -

custom template uri path- https://myvmstore.blob.core.windows.net/vhds/CustomVHD.vhd  
RG Name - myVMsRG 
VNet Name - myVNET 
VmName = mytestvm  
userImageStorageAccountName = myvmstore 
adminUsername = adminuser  
adminPassword = PassWord123#  
osDiskVhdUri = https://myvmstore.blob.core.windows.net/vhds/CustomVHD.vhd 
dnsLabelPrefix = mytestvm 
osType = Windows  
vmSize = Standard_D2  
newOrExistingVnet = existing 
newOrExistingVnetName = myVNET  
newOrExistingSubnetName = mySubnet

首先你需要json模板。我已经为这种格式配置了自己。您可以复制相同的代码,并在D驱动器中使用名称deployvm.json进行保存。注意:如果您的VNET名称与myVNET不同,请在相同位置的模板中对其进行硬编码。

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "customVmName": {
      "type": "string",
      "metadata": {
        "description": "This is the name of the your VM"
      }
    },
    "userImageStorageAccountName": {
      "type": "string",
      "metadata": {
        "description": "This is the name of the your storage account"
      }
    },
    "osDiskVhdUri": {
      "type": "string",
      "metadata": {
        "description": "Uri of the your user image"
      }
    },
    "dnsLabelPrefix": {
      "type": "string",
      "metadata": {
        "description": "DNS Label for the Public IP. Must be lowercase. It should match with the following regular expression: ^[a-z][a-z0-9-]{1,61}[a-z0-9]$ or it will raise an error."
      }
    },
    "adminUserName": {
      "type": "string",
      "metadata": {
        "description": "User Name for the Virtual Machine"
      }
    },
    "adminPassword": {
      "type": "securestring",
      "metadata": {
        "description": "Password for the Virtual Machine"
      }
    },
    "osType": {
      "type": "string",
      "allowedValues": [
        "Windows",
        "Linux"
      ],
      "metadata": {
        "description": "This is the OS that your VM will be running"
      }
    },
    "vmSize": {
      "type": "string",
      "metadata": {
        "description": "This is the size of your VM"
      }
    },
    "newOrExistingVnet": {
      "allowedValues": [ "new", "existing" ],
      "type": "string",
      "metadata": {
        "description":  "Select if this template needs a new VNet or will reference an existing VNet"
      }
    },
    "newOrExistingVnetName": {
      "type": "string",
      "defaultValue": "",
      "metadata": {
        "description": "New or Existing VNet Name"
      }
    },
    "newOrExistingSubnetName": {
      "type": "string",
      "defaultValue": "Subnet1",
      "metadata": {
        "description": "Subnet Name"
      }
    }
  },
  "variables": {
    "publicIPAddressName": "[parameters('customVmName')]",
    "vmName": "[parameters('customVmName')]",
    "nicName": "[parameters('customVmName')]",
    "publicIPAddressType": "Dynamic",
    "apiVersion": "2015-06-15",
    "vnetID": "[resourceId('myVNET', 'Microsoft.Network/virtualNetworks', parameters('newOrExistingVnetName'))]",
    "subnetRef": "[concat(variables('vnetID'),'/subnets/', parameters('newOrExistingSubnetName'))]",
},
  "resources": [
    {
      "apiVersion": "[variables('apiVersion')]",
      "type": "Microsoft.Network/publicIPAddresses",
      "name": "[variables('publicIPAddressName')]",
      "location": "[resourceGroup().location]",
      "properties": {
        "publicIPAllocationMethod": "[variables('publicIPAddressType')]",
        "dnsSettings": {
          "domainNameLabel": "[parameters('dnsLabelPrefix')]"
        }
      }
    },
    {
      "apiVersion": "2016-03-30",
      "type": "Microsoft.Network/networkInterfaces",
      "name": "[variables('nicName')]",
      "location": "[resourceGroup().location]",
      "dependsOn": [
        "[concat('Microsoft.Network/publicIPAddresses/', variables('publicIPAddressName'))]"
      ],
      "properties": {
        "ipConfigurations": [
          {
            "name": "ipconfig1",
            "properties": {
              "privateIPAllocationMethod": "Dynamic",
              "publicIPAddress": {
                "id": "[resourceId('Microsoft.Network/publicIPAddresses',variables('publicIPAddressName'))]"
              },
              "subnet": {
                "id": "[variables('subnetRef')]"
              }
            }
          }
        ]
      }
    },
    {
      "apiVersion": "[variables('apiVersion')]",
      "type": "Microsoft.Compute/virtualMachines",
      "name": "[variables('vmName')]",
      "location": "[resourceGroup().location]",
      "dependsOn": [
        "[concat('Microsoft.Network/networkInterfaces/', variables('nicName'))]"
      ],
      "properties": {
        "hardwareProfile": {
          "vmSize": "[parameters('vmSize')]"
        },
        "osProfile": {
          "computerName": "[variables('vmName')]",
          "adminUsername": "[parameters('adminUsername')]",
          "adminPassword": "[parameters('adminPassword')]"
        },
        "storageProfile": {
          "osDisk": {
            "name": "[concat(variables('vmName'),'-osDisk')]",
            "osType": "[parameters('osType')]",
            "caching": "ReadWrite",
            "createOption": "FromImage",
            "image": {
              "uri": "[parameters('osDiskVhdUri')]"
            },
            "vhd": {
              "uri": "[concat(reference(concat('Microsoft.Storage/storageAccounts/', parameters('userImageStorageAccountName')), variables('apiVersion')).primaryEndpoints.blob, 'vhds/',variables('vmName'), uniquestring(resourceGroup().id), 'osDisk.vhd')]"
            }
          }
        },
        "networkProfile": {
          "networkInterfaces": [
            {
              "id": "[resourceId('Microsoft.Network/networkInterfaces',variables('nicName'))]"
            }
          ]
        },
        "diagnosticsProfile": {
          "bootDiagnostics": {
            "enabled": "true",
            "storageUri": "[concat(reference(concat('Microsoft.Storage/storageAccounts/', parameters('userImageStorageAccountName')), variables('apiVersion')).primaryEndpoints.blob)]"
          }
        }
      }
    }
  ]
}

现在使用以下powershell代码创建您的VM -

$paramList = 
     @{
        "Params1" = @{ customVmName = "mytestvm" ; userImageStorageAccountName = "myvmstore" ; adminUsername = "adminuser" ; adminPassword = "PassWord123#" ; osDiskVhdUri = "https://myvmstore.blob.core.windows.net/vhds/CustomVHD.vhd" ; dnsLabelPrefix = "mytestvm" ; osType ="Windows" ; vmSize = "Standard_D2" ; newOrExistingVnet = "existing" ;  newOrExistingVnetName = "myVPN" ; newOrExistingSubnetName = "mySubnet"}
       }

        foreach ($keys in $paramList.Keys)
        {   
            $paramvalues = $paramList.$keys
            New-AzureRmResourceGroupDeployment -ResourceGroupName "myVMsRG" -TemplateFile "D:\deployvm.json" -TemplateParameterObject $paramValues   
        }

参考 - https://github.com/Azure/azure-quickstart-templates/tree/master/101-vm-from-user-image

答案 1 :(得分:0)

您可以使用PowershellARM Template。这里粘贴的代码真的太多了,但即使我发布的链接会发生变化,也很容易搜索。

答案 2 :(得分:0)

将自定义映像从Azure Classic门户复制到新门户(此处所有内容都存储在资源组下)存储帐户。

现在,您将能够使用Azure CLI或PowerShell或ARM模板中的自定义映像创建新VM。