转:ARM模式下从已有的VHD文件创建新的虚拟机

Azure在ASM模式下可以通过Portal界面的Caputure或Save-AzureVMImage等方式创建VM的Image模板,实现对已有VM的快速重新部署。
在Azure的ARM下,要实现类似的功能,可以通过采用JSON的Template部署的方式实现。本文将介绍这一方法。
1 通过PowerShell列出需要复制的虚拟机,并记录其OSDisk的链接:

复制代码
get-azurermvm
RequestId                         : 984a6daa-c8cf-4f4f-8edc-d9cceabf3d95
StatusCode                        : OK
ResourceGroupName                 : HWNOSQL
Id                                : /subscriptions/xxxx/resourceGroups/HWNOSQL/providers/Microsoft.Compute/virtualMachines/hwnosql
Name                              : hwnosql
Type                              : Microsoft.Rest.Azure.AzureOperationResponse`1[Microsoft.Rest.Azure.IPage`1[Microsoft.Azure.Management.Compute.Models.VirtualMa
chine]]
Location                          : chinaeast
Tags                              : {}
DiagnosticsProfile                :
  BootDiagnostics                 :
    Enabled                       : True
    StorageUri                    : https://hwnosql969.blob.core.chinacloudapi.cn/
Extensions[0]                     :
  Id                              : /subscriptions/xxxx/resourceGroups/HWNOSQL/providers/Microsoft.Compute/virtualMachines/hwnosql
/extensions/Microsoft.Insights.VMDiagnosticsSettings
HardwareProfile                   :
  VmSize                          : Standard_A1
NetworkProfile                    :
  NetworkInterfaces[0]            :
    Id                            : /subscriptions/xxxx/resourceGroups/hwnosql/providers/Microsoft.Network/networkInterfaces/hwnos
ql966
OSProfile                         :
  ComputerName                    : hwnosql
  AdminUsername                   : hengwei
  LinuxConfiguration              :
    DisablePasswordAuthentication : False
ProvisioningState                 : Succeeded
StorageProfile                    :
  ImageReference                  :
    Publisher                     : OpenLogic
    Offer                         : CentOS
    Sku                           : 7.2
    Version                       : latest
  OsDisk                          :
    OsType                        : Linux
    Name                          : hwnosql
    Vhd                           :
      Uri                         : https://hw01sa01.blob.core.chinacloudapi.cn/vhds/hwnosql2016722122351.vhd
    Caching                       : ReadWrite
    CreateOption                  : FromImage
NetworkInterfaceIDs[0]            : /subscriptions/xxxx/resourceGroups/hwnosql/providers/Microsoft.Network/networkInterfaces/hwnos
ql966
复制代码

 
其中https://hw01sa01.blob.core.chinacloudapi.cn/vhds/hwnosql2016722122351.vhd 是其OSDisk的链接。
2 用AzCopy复制此vhd文件到另外一个Container:

复制代码
C:\Users\hengz>set key="xxxxxxxxxxxx=="
C:\Users\hengz>azcopy /source:https://hw01sa01.blob.core.chinacloudapi.cn/vhds /dest:https://hw01sa01.blob.core.chinacloudapi.cn/vhd /destkey:%key% /sourcekey:%key% /pattern:hwnosql2016722122351.vhd
Finished 1 of total 1 file(s).
[2016/08/25 16:55:00] Transfer summary:
-----------------
Total files transferred: 1
Transfer successfully:   1
Transfer skipped:        0
Transfer failed:         0
Elapsed time:            00.00:00:03
复制代码

 
3 在GitHub上的quickstart template找到相应的脚本:
https://github.com/Azure/azure-quickstart-templates/tree/master/101-vm-from-user-image
其中有两个文件是我们需要的,一个是azuredeploy.json, 另外一个是azuredeploy.parameters.json。
浏览azuredeploy.json,没有什么需要我们更改的。
浏览azuredeploy.parameters.json,更改所需内容:

复制代码
{
  "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "customVmName": {
      "value": "hwcopyimageVM"
    },
    "userImageStorageAccountName": {
      "value": "hw01sa01"
    },
    "adminUsername": {
      "value": "hengwei"
    },
    "adminPassword": {
      "value": "xxxxxxxx"
    },
    "osDiskVhdUri": {
      "value": "https://hw01sa01.blob.core.chinacloudapi.cn/vhd/hwnosql2016722122351.vhd"
    },
    "dnsLabelPrefix": {
      "value": "hwcopyimagevm"
    },
    "osType": {
      "value": "Linux"
    },
    "vmSize": {
      "value": "Standard_A1"
    },
    "newOrExistingVnet": {
      "value": "existing"
    },
    "newOrExistingVnetName": {
      "value": "hw01vnet"
    },
    "newOrExistingSubnetName": {
      "value": "Subnet-1"
    }
  }
}
复制代码

 
4 通过PowerShell命令部署新的虚拟机:

复制代码
New-AzureRmResourceGroupDeployment -Name hwdpmt01 -ResourceGroupName hw01 -Mode Incremental -TemplateFile D:\Hengwei\Documents\GitHub\azure-quickstart-templates\101-vm-from-user-image\azuredeploy.json -TemplateParameterFile D:\Hengwei\Documents\GitHub\azure-quickstart-templates\101-vm-from-user-image\azuredeploy.parameters.json
DeploymentName          : hwdpmt01
ResourceGroupName       : hw01
ProvisioningState       : Succeeded
Timestamp               : 2016/8/25 9:10:23
Mode                    : Incremental
TemplateLink            :
Parameters              :
                          Name             Type                       Value
                          ===============  =========================  ==========
                          customVmName     String                     hwcopyimageVM
                          userImageStorageAccountName  String                     hw01sa01
                          osDiskVhdUri     String                     https://hw01sa01.blob.core.chinacloudapi.cn/vhd/hwnosql2016722122351.vhd
                          dnsLabelPrefix   String                     hwcopyimagevm
                          adminUserName    String                     hengwei
                          adminPassword    SecureString
                          osType           String                     Linux
                          vmSize           String                     Standard_A1
                          newOrExistingVnet  String                     existing
                          newOrExistingVnetName  String                     hw01vnet
                          newOrExistingSubnetName  String                     Subnet-1
Outputs                 :
DeploymentDebugLogLevel :
复制代码

 
至此,部署完成。

发表评论