如何使用Azure ARM创建公共虚拟机映像?

时间:2016-02-22 10:50:56

标签: azure azure-virtual-machine azure-resource-manager

我想创建一个任何人都可以使用ARM REST API启动的虚拟机。

我该怎么做?我找不到指示。

3 个答案:

答案 0 :(得分:1)

显然,可以在此处创建公共虚拟机映像:https://vmdepot.msopentech.com/help/contribute/vhd.html/

答案 1 :(得分:0)

有几种方法可以做到这一点。假设您在前端有一个网站/应用程序等,它只是您正在寻找的后端通信。

先决条件

此处的选项假定您拥有一个活动的Microsoft Azure帐户,并且能够通过门户网站在那里创建VM。一旦您处于可以执行此操作的阶段,就可以使用REST API来创建计算机。

选项1

您可以使用REST API通过PUT对此URI的请求直接创建VM -

https://management.azure.com/subscriptions/{subscription-id}/resourceGroups/{resource-group-name}/providers/Microsoft.Compute/virtualMachines/{vm-name}?validating={true|false}&api-version={api-version}

您需要将JSON文档附加到该请求,该请求将定义您正在创建的计算机。

{  
  "id":"/subscriptions/{subscription-id}/resourceGroups/myresourcegroup1/providers/Microsoft.Compute/virtualMachines/myvm1",
  "name":"myvm1", 
  "type":"Microsoft.Compute/virtualMachines",
  "location":"westus",
  "tags": {  
    "department":"finance"
  },
  "properties": {  
    "availabilitySet": {  
      "id":"/subscriptions/{subscription-id}/resourceGroups/myresourcegroup1/providers/Microsoft.Compute/availabilitySets/myav1"
    },
    "hardwareProfile": {  
      "vmSize":"Standard_A0"
    },
    "storageProfile": {  
      "imageReference": {  
        "publisher":"MicrosoftWindowsServerEssentials",
        "offer":"WindowsServerEssentials",
        "sku":"WindowsServerEssentials",
        "version":"latest"
      },
      "osDisk": {  
        "name":"myosdisk1",
        "vhd": {  
          "uri":"http://mystorage1.blob.core.windows.net/vhds/myosdisk1.vhd"
        },
        "caching":"ReadWrite",
        "createOption":"FromImage"
      },
      "dataDisks": [ { 
         "name":"mydatadisk1", 
         "diskSizeGB":"1", 
         "lun": 0, 
         "vhd": { 
           "uri" : "http://mystorage1.blob.core.windows.net/vhds/mydatadisk1.vhd" 
         }, 
         "createOption":"Empty" 
       } ]
    },
    "osProfile": {  
      "computerName":"myvm1",
      "adminUsername":"username",
      "adminPassword":"password",
      "customData":"",
      "windowsConfiguration": {  
        "provisionVMAgent":true,
        "winRM": {
          "listeners": [ {
            "protocol": "https",
            "certificateUrl": "url-to-certificate"
          } ]
        },
        "additionalUnattendContent": {  
          "pass":"oobesystem",
          "component":"Microsoft-Windows-Shell-Setup",
          "settingName":"FirstLogonCommands|AutoLogon",
          "content":"<XML unattend content>"
        }  
        "enableAutomaticUpdates":true
      },
      "secrets":[ { 
         "sourceVault": { 
           "id": "/subscriptions/{subscription-id}/resourceGroups/myresourcegroup1/providers/Microsoft.KeyVault/vaults/myvault1" 
         }, 
         "vaultCertificates": [ { 
           "certificateUrl": "https://myvault1.vault.azure.net/secrets/{secretName}/{secretVersion}" 
           "certificateStore": "{certificateStoreName}" 
         } ] 
       } ]
    },
    "networkProfile": {  
      "networkInterfaces": [ {  
        "id":"/subscriptions/{subscription-id}/resourceGroups/myresourceGroup1/providers /Microsoft.Network/networkInterfaces/mynic1"
      } ]
    }
  }
}

有关身份验证和参数的更多详细信息,请参阅Azure虚拟机休息文档 - Create or update a virtual machine

选项2

或者,您可以创建Azure资源管理器模板,例如Azure上的101-vm-simple-linux Github template repository

为要部署的VM定义模板后,您可以PUT对此URI发出另一个请求

https://management.azure.com/subscriptions/ {订阅-ID} / resourcegroups / {资源基团的名称} /providers/microsoft.resources/deployments/ {部署名称}?API-版本= {API-版本}

如果您将该模板文件复制到Azure blob,以及另一个指定所需参数的文件,并使用PUT请求发送此JSON文档

{
  "properties": {
    "templateLink": {
      "uri": "http://mystorageaccount.blob.core.windows.net/templates/template.json",
      "contentVersion": "1.0.0.0",
    },
    "mode": "Incremental",
    "parametersLink": {
      "uri": "http://mystorageaccount.blob.core.windows.net/templates/parameters.json",
      "contentVersion": "1.0.0.0",      
    }
  }
}

您可以在 - Create a template deployment

找到相关文档

答案 2 :(得分:0)

这是详细说明@Michael B的答案:要发现可用的图像,您可以使用VMDepot - 当然 - 或者您可以query for all the marketplace images。首先查看发布者列表,然后从那里您可以决定您想要的图像。

您发现的URN值将是您要在REST调用中使用的值。希望这会有所帮助...