使用 Azure 编排模板部署 Azure 学生订阅 VM

本文简述了使用 Azure Template 部署一台 Linux 虚拟机的过程,此模板针对 Azure for Students 订阅,但是您也可以修改参数来用于其他订阅。

此方法相比手动创建,具有以下优势:

  • 命名一致:使用一致的驼峰命名方法,避免系统使用默认的随机数命名资源。
  • 快速部署:只需粘贴模板和参数即可完成部署,无需手动完成长达 5 页的虚拟机部署向导,同时避免了手动操作可能带来的错误。

默认的模板参数已经针对 Azure for Students 订阅进行了修改以防止不必要的扣费,无需后续修改磁盘、IP等参数。

此模板包括以下资源:

  • 创建一个 Resource Group,名为 Example;所有资源都将部署到此 Resource Group 中。
  • 虚拟机Example-VM (计算机名:localhost)
  • 虚拟网络Example-VNet
  • 虚拟子网Example-Subnet
  • 网卡Example-NIC
  • 网络配置Example-IPConfig
  • 公网IPExample-IP (动态,基本SKU)
  • 网络安全组Example-NSG (已允许 SSH 连接)
  • 硬盘Example-OSDisk (已修改为 Azure for Students 订阅免费的 64G P6 性能)

打开 Azure 控制台,在顶部搜索框输入 「Deploy a custom template」,点击「Build your own template in the editor」,粘贴以下模板内容

1
2
3
4
5
6
7
8
{
"$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {"location":{"type":"string"},"networkInterfaceName":{"type":"string"},"networkIpconfigName":{"type":"string"},"networkSecurityGroupName":{"type":"string"},"networkSecurityGroupRules":{"type":"array"},"subnetName":{"type":"string"},"virtualNetworkName":{"type":"string"},"addressPrefixes":{"type":"array"},"subnets":{"type":"array"},"publicIpAddressName":{"type":"string"},"publicIpAddressType":{"type":"string"},"publicIpAddressSku":{"type":"string"},"pipDeleteOption":{"type":"string"},"virtualMachineName":{"type":"string"},"virtualMachineComputerName":{"type":"string"},"virtualMachineRG":{"type":"string"},"osDiskType":{"type":"string"},"osDiskSizeGiB":{"type":"int"},"osDiskDeleteOption":{"type":"string"},"osDiskName":{"type":"string"},"virtualMachineSize":{"type":"string"},"nicDeleteOption":{"type":"string"},"hibernationEnabled":{"type":"bool"},"adminUsername":{"type":"string"},"adminPublicKey":{"type":"secureString"},"securityType":{"type":"string"},"secureBoot":{"type":"bool"},"vTPM":{"type":"bool"}},
"variables": {"nsgId":"[resourceId(resourceGroup().name, 'Microsoft.Network/networkSecurityGroups', parameters('networkSecurityGroupName'))]","vnetName":"[parameters('virtualNetworkName')]","vnetId":"[resourceId(resourceGroup().name,'Microsoft.Network/virtualNetworks', parameters('virtualNetworkName'))]","subnetRef":"[concat(variables('vnetId'), '/subnets/', parameters('subnetName'))]"},
"resources": [{"name":"[parameters('networkInterfaceName')]","type":"Microsoft.Network/networkInterfaces","apiVersion":"2022-11-01","location":"[parameters('location')]","dependsOn":["[concat('Microsoft.Network/networkSecurityGroups/', parameters('networkSecurityGroupName'))]","[concat('Microsoft.Network/virtualNetworks/', parameters('virtualNetworkName'))]","[concat('Microsoft.Network/publicIpAddresses/', parameters('publicIpAddressName'))]"],"properties":{"ipConfigurations":[{"name":"[parameters('networkIpconfigName')]","properties":{"subnet":{"id":"[variables('subnetRef')]"},"privateIPAllocationMethod":"Dynamic","publicIpAddress":{"id":"[resourceId(resourceGroup().name, 'Microsoft.Network/publicIpAddresses', parameters('publicIpAddressName'))]","properties":{"deleteOption":"[parameters('pipDeleteOption')]"}}}}],"networkSecurityGroup":{"id":"[variables('nsgId')]"}}},{"name":"[parameters('networkSecurityGroupName')]","type":"Microsoft.Network/networkSecurityGroups","apiVersion":"2020-05-01","location":"[parameters('location')]","properties":{"securityRules":"[parameters('networkSecurityGroupRules')]"}},{"name":"[parameters('virtualNetworkName')]","type":"Microsoft.Network/virtualNetworks","apiVersion":"2024-01-01","location":"[parameters('location')]","properties":{"addressSpace":{"addressPrefixes":"[parameters('addressPrefixes')]"},"subnets":"[parameters('subnets')]"}},{"name":"[parameters('publicIpAddressName')]","type":"Microsoft.Network/publicIpAddresses","apiVersion":"2020-08-01","location":"[parameters('location')]","properties":{"publicIpAllocationMethod":"[parameters('publicIpAddressType')]"},"sku":{"name":"[parameters('publicIpAddressSku')]"}},{"name":"[parameters('virtualMachineName')]","type":"Microsoft.Compute/virtualMachines","apiVersion":"2024-03-01","location":"[parameters('location')]","dependsOn":["[concat('Microsoft.Network/networkInterfaces/', parameters('networkInterfaceName'))]"],"properties":{"hardwareProfile":{"vmSize":"[parameters('virtualMachineSize')]"},"storageProfile":{"osDisk":{"name":"[parameters('osDiskName')]","createOption":"fromImage","managedDisk":{"storageAccountType":"[parameters('osDiskType')]"},"diskSizeGB":"[parameters('osDiskSizeGiB')]","deleteOption":"[parameters('osDiskDeleteOption')]"},"imageReference":{"publisher":"debian","offer":"debian-12","sku":"12-gen2","version":"latest"}},"networkProfile":{"networkInterfaces":[{"id":"[resourceId('Microsoft.Network/networkInterfaces', parameters('networkInterfaceName'))]","properties":{"deleteOption":"[parameters('nicDeleteOption')]"}}]},"securityProfile":{"securityType":"[parameters('securityType')]","uefiSettings":{"secureBootEnabled":"[parameters('secureBoot')]","vTpmEnabled":"[parameters('vTPM')]"}},"additionalCapabilities":{"hibernationEnabled":false},"osProfile":{"computerName":"[parameters('virtualMachineComputerName')]","adminUsername":"[parameters('adminUsername')]","linuxConfiguration":{"disablePasswordAuthentication":true,"ssh":{"publicKeys":[{"path":"[concat('/home/', parameters('adminUsername'), '/.ssh/authorized_keys')]","keyData":"[parameters('adminPublicKey')]"}]}}},"diagnosticsProfile":{"bootDiagnostics":{"enabled":true}}}}],
"outputs": {"adminUsername":{"type":"string","value":"[parameters('adminUsername')]"}}
}

点击「Save」,在跳出的页面点击「Edit parameters」,粘贴以下参数内容。

根据自己的需要修改参数,特别是注意记得填入SSH密钥,然后点击「Review + create」。如果提示错误找不到资源组,请先返回上一步并创建对应的资源组。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"location": { "value": "westus" },
"networkInterfaceName": { "value": "Example-NIC" },
"networkIpconfigName": { "value": "Example-IPConfig" },
"networkSecurityGroupName": { "value": "Example-NSG" },
"networkSecurityGroupRules": {
"value": [
{
"name": "AllowSSH",
"properties": {
"priority": 300,
"protocol": "TCP",
"access": "Allow",
"direction": "Inbound",
"sourceAddressPrefix": "*",
"sourcePortRange": "*",
"destinationAddressPrefix": "*",
"destinationPortRange": "22"
}
}
]
},
"subnetName": { "value": "Example-Subnet" },
"virtualNetworkName": { "value": "Example-VNet" },
"addressPrefixes": { "value": [ "10.0.0.0/16" ] },
"subnets": {
"value": [{
"name": "Example-Subnet",
"properties": { "addressPrefix": "10.0.0.0/24" }
}]
},
"publicIpAddressName": { "value": "Example-IP" },
"publicIpAddressType": { "value": "Dynamic" },
"publicIpAddressSku": { "value": "Basic" },
"pipDeleteOption": { "value": "Delete" },
"virtualMachineName": { "value": "Example-VM" },
"virtualMachineComputerName": { "value": "localhost" },
"virtualMachineRG": { "value": "Example" },
"osDiskType": { "value": "Premium_LRS" },
"osDiskSizeGiB": { "value": 64 },
"osDiskDeleteOption": { "value": "Delete" },
"osDiskName": { "value": "Example-OSDisk" },
"virtualMachineSize": { "value": "Standard_B1s" },
"nicDeleteOption": { "value": "Delete" },
"hibernationEnabled": { "value": false },
"adminUsername": { "value": "azure" },
"adminPublicKey": { "value": null },
"securityType": { "value": "TrustedLaunch" },
"secureBoot": { "value": true },
"vTPM": { "value": true }
}
}