快速入门:使用 ARM 模板创建 Windows 虚拟机Quickstart: Create a Windows virtual machine using an ARM template

本快速入门介绍如何使用 Azure 资源管理器模板(ARM 模板)在 Azure 中部署 Windows 虚拟机 (VM)。This quickstart shows you how to use an Azure Resource Manager template (ARM template) to deploy a Windows virtual machine (VM) in Azure.

ARM 模板是定义项目基础结构和配置的 JavaScript 对象表示法 (JSON) 文件。An ARM template is a JavaScript Object Notation (JSON) file that defines the infrastructure and configuration for your project. 该模板使用声明性语法,使你可以声明要部署的内容,而不需要编写一系列编程命令来进行创建。The template uses declarative syntax, which lets you state what you intend to deploy without having to write the sequence of programming commands to create it.

如果你的环境满足先决条件,并且你熟悉如何使用 ARM 模板,请选择“部署到 Azure”按钮。If your environment meets the prerequisites and you're familiar with using ARM templates, select the Deploy to Azure button. Azure 门户中会打开模板。The template will open in the Azure portal.

部署到 Azure Deploy to Azure

先决条件Prerequisites

如果没有 Azure 订阅,可在开始前创建一个试用帐户If you don't have an Azure subscription, create a trial account before you begin.

查看模板Review the template

本快速入门中使用的模板来自 Azure 快速启动模板The template used in this quickstart is from Azure Quickstart Templates.

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "adminUsername": {
      "type": "string",
      "metadata": {
        "description": "Username for the Virtual Machine."
      }
    },
    "adminPassword": {
      "type": "securestring",
      "metadata": {
        "description": "Password for the Virtual Machine."
      }
    },
    "dnsLabelPrefix": {
      "type": "string",
      "metadata": {
        "description": "Unique DNS Name for the Public IP used to access the Virtual Machine."
      }
    },
    "windowsOSVersion": {
      "type": "string",
      "defaultValue": "2016-Datacenter",
      "allowedValues": [
        "2008-R2-SP1",
        "2012-Datacenter",
        "2012-R2-Datacenter",
        "2016-Nano-Server",
        "2016-Datacenter-with-Containers",
        "2016-Datacenter",
        "2019-Datacenter"
      ],
      "metadata": {
        "description": "The Windows version for the VM. This will pick a fully patched image of this given Windows version."
      }
    },
    "vmSize": {
      "type": "string",
      "defaultValue": "Standard_D2_v3",
      "metadata": {
        "description": "Size of the virtual machine."
      }
    },
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]",
      "metadata": {
        "description": "Location for all resources."
      }
    }
  },
  "variables": {
    "storageAccountEndPoint": "https://core.chinacloudapi.cn/",
    "storageAccountName": "[concat(uniquestring(resourceGroup().id), 'sawinvm')]",
    "nicName": "myVMNic",
    "addressPrefix": "10.0.0.0/16",
    "subnetName": "Subnet",
    "subnetPrefix": "10.0.0.0/24",
    "publicIPAddressName": "myPublicIP",
    "vmName": "SimpleWinVM",
    "virtualNetworkName": "MyVNET",
    "subnetRef": "[resourceId('Microsoft.Network/virtualNetworks/subnets', variables('virtualNetworkName'), variables('subnetName'))]",
    "networkSecurityGroupName": "default-NSG"
  },
  "resources": [
    {
      "type": "Microsoft.Storage/storageAccounts",
      "apiVersion": "2018-11-01",
      "name": "[variables('storageAccountName')]",
      "location": "[parameters('location')]",
      "sku": {
        "name": "Standard_LRS"
      },
      "kind": "Storage",
      "properties": {}
    },
    {
      "type": "Microsoft.Network/publicIPAddresses",
      "apiVersion": "2018-11-01",
      "name": "[variables('publicIPAddressName')]",
      "location": "[parameters('location')]",
      "properties": {
        "publicIPAllocationMethod": "Dynamic",
        "dnsSettings": {
          "domainNameLabel": "[parameters('dnsLabelPrefix')]"
        }
      }
    },
    {
      "comments":  "Default Network Security Group for template",
      "type":  "Microsoft.Network/networkSecurityGroups",
      "apiVersion":  "2019-08-01",
      "name":  "[variables('networkSecurityGroupName')]",
      "location":  "[parameters('location')]",
      "properties": {
        "securityRules": [
          {
            "name":  "default-allow-3389",
            "properties": {
              "priority":  1000,
              "access":  "Allow",
              "direction":  "Inbound",
              "destinationPortRange":  "3389",
              "protocol":  "Tcp",
              "sourcePortRange":  "*",
              "sourceAddressPrefix":  "*",
              "destinationAddressPrefix":  "*"
            }
          }
        ]
      }
    },
    {
      "type": "Microsoft.Network/virtualNetworks",
      "apiVersion": "2018-11-01",
      "name": "[variables('virtualNetworkName')]",
      "location": "[parameters('location')]",
      "dependsOn": [
        "[resourceId('Microsoft.Network/networkSecurityGroups', variables('networkSecurityGroupName'))]"
      ],
      "properties": {
        "addressSpace": {
          "addressPrefixes": [
            "[variables('addressPrefix')]"
          ]
        },
        "subnets": [
          {
            "name": "[variables('subnetName')]",
            "properties": {
              "addressPrefix": "[variables('subnetPrefix')]",
              "networkSecurityGroup": {
                "id": "[resourceId('Microsoft.Network/networkSecurityGroups', variables('networkSecurityGroupName'))]"
              }
            }
          }
        ]
      }
    },
    {
      "type": "Microsoft.Network/networkInterfaces",
      "apiVersion": "2018-11-01",
      "name": "[variables('nicName')]",
      "location": "[parameters('location')]",
      "dependsOn": [
        "[resourceId('Microsoft.Network/publicIPAddresses/', variables('publicIPAddressName'))]",
        "[resourceId('Microsoft.Network/virtualNetworks/', variables('virtualNetworkName'))]"
      ],
      "properties": {
        "ipConfigurations": [
          {
            "name": "ipconfig1",
            "properties": {
              "privateIPAllocationMethod": "Dynamic",
              "publicIPAddress": {
                "id": "[resourceId('Microsoft.Network/publicIPAddresses',variables('publicIPAddressName'))]"
              },
              "subnet": {
                "id": "[variables('subnetRef')]"
              }
            }
          }
        ]
      }
    },
    {
      "type": "Microsoft.Compute/virtualMachines",
      "apiVersion": "2018-10-01",
      "name": "[variables('vmName')]",
      "location": "[parameters('location')]",
      "dependsOn": [
        "[resourceId('Microsoft.Storage/storageAccounts/', variables('storageAccountName'))]",
        "[resourceId('Microsoft.Network/networkInterfaces/', variables('nicName'))]"
      ],
      "properties": {
        "hardwareProfile": {
          "vmSize": "[parameters('vmSize')]"
        },
        "osProfile": {
          "computerName": "[variables('vmName')]",
          "adminUsername": "[parameters('adminUsername')]",
          "adminPassword": "[parameters('adminPassword')]"
        },
        "storageProfile": {
          "imageReference": {
            "publisher": "MicrosoftWindowsServer",
            "offer": "WindowsServer",
            "sku": "[parameters('windowsOSVersion')]",
            "version": "latest"
          },
          "osDisk": {
            "createOption": "FromImage"
          },
          "dataDisks": [
            {
              "diskSizeGB": 1023,
              "lun": 0,
              "createOption": "Empty"
            }
          ]
        },
        "networkProfile": {
          "networkInterfaces": [
            {
              "id": "[resourceId('Microsoft.Network/networkInterfaces',variables('nicName'))]"
            }
          ]
        },
        "diagnosticsProfile": {
          "bootDiagnostics": {
            "enabled": true,
            "storageUri": "[reference(resourceId('Microsoft.Storage/storageAccounts/', variables('storageAccountName'))).primaryEndpoints.blob]"
          }
        }
      }
    }
  ],
  "outputs": {
    "hostname": {
      "type": "string",
      "value": "[reference(variables('publicIPAddressName')).dnsSettings.fqdn]"
    }
  }
}

该模板中定义了多个资源:Several resources are defined in the template:

  • Microsoft.Network/virtualNetworks/subnets:创建子网。Microsoft.Network/virtualNetworks/subnets: create a subnet.
  • Microsoft.Storage/storageAccounts:创建存储帐户。Microsoft.Storage/storageAccounts: create a storage account.
  • Microsoft.Network/publicIPAddresses:创建公共 IP 地址。Microsoft.Network/publicIPAddresses: create a public IP address.
  • Microsoft.Network/networkSecurityGroups:创建网络安全组。Microsoft.Network/networkSecurityGroups: create a network security group.
  • Microsoft.Network/virtualNetworks:创建虚拟网络。Microsoft.Network/virtualNetworks: create a virtual network.
  • Microsoft.Network/networkInterfaces:创建 NIC。Microsoft.Network/networkInterfaces: create a NIC.
  • Microsoft.Compute/virtualMachines:创建虚拟机。Microsoft.Compute/virtualMachines: create a virtual machine.

部署模板Deploy the template

  1. 选择下图登录到 Azure 并打开一个模板。Select the following image to sign in to Azure and open a template. 该模板将创建 Key Vault 和机密。The template creates a key vault and a secret.

    部署到 Azure Deploy to Azure

  2. 选择或输入以下值。Select or enter the following values. 使用可用的默认值。Use the default values, when available.

    • 订阅:选择一个 Azure 订阅。Subscription: select an Azure subscription.
    • 资源组:从下拉列表中选择现有资源组,或选择“新建”并输入资源组的唯一名称,然后单击“确定” 。Resource group: select an existing resource group from the drop-down, or select Create new, enter a unique name for the resource group, and then click OK.
    • 位置:选择一个位置。Location: select a location. 例如,中国北部For example, China North.
    • 管理员用户名:提供用户名,如 azureuser。Admin username: provide a username, such as azureuser.
    • 管理员密码:提供用于管理员帐户的密码。Admin password: provide a password to use for the admin account. 密码必须至少 12 个字符长,且符合定义的复杂性要求The password must be at least 12 characters long and meet the defined complexity requirements.
    • DNS 标签前缀:输入要用作 DNS 标签一部分的唯一标识符。DNS label prefix: enter a unique identifier to use as part of the DNS label.
    • Windows OS 版本:选择想要在 VM 上运行的 Windows 版本。Windows OS version: select which version of Windows you want to run on the VM.
    • VM 大小:选择要用于 VM 的大小VM size: select the size to use for the VM.
    • 位置:默认与资源组位置相同(如果已存在)。Location: the default is the same location as the resource group, if it already exists.
  3. 选择“查看 + 创建”。Select Review + create. 验证完成后,选择“创建”以创建和部署 VM。After validation completes, select Create to create and deploy the VM.

使用 Azure 门户部署模板。The Azure portal is used to deploy the template. 除了 Azure 门户,还可以使用 Azure PowerShell、Azure CLI 和 REST API。In addition to the Azure portal, you can also use the Azure PowerShell, Azure CLI, and REST API. 若要了解其他部署方法,请参阅部署模板To learn other deployment methods, see Deploy templates.

查看已部署的资源Review deployed resources

你可以使用 Azure 门户来检查 VM 和创建的其他资源。You can use the Azure portal to check on the VM and other resource that were created. 部署完成后,请选择“转到资源组”以查看 VM 和其他资源。After the deployment is finished, select Go to resource group to see the VM and other resources.

清理资源Clean up resources

如果不再需要资源组,可以将其删除,这将删除 VM 和资源组中的所有资源。When no longer needed, delete the resource group, which deletes the VM and all of the resources in the resource group.

  1. 选择“资源组”。Select the Resource group.
  2. 在资源组页上,选择“删除”。On the page for the resource group, select Delete.
  3. 出现提示时,键入资源组的名称,然后选择“删除”。When prompted, type the name of the resource group and then select Delete.

后续步骤Next steps

在本快速入门中,你已使用 ARM 模板部署了一个简单的虚拟机。In this quickstart, you deployed a simple virtual machine using an ARM template. 若要详细了解 Azure 虚拟机,请继续学习 Linux VM 的教程。To learn more about Azure virtual machines, continue to the tutorial for Linux VMs.