快速入门:使用 ARM 模板在 Azure 公共 MEC 中部署虚拟机

在本快速入门中,你将了解如何使用 Azure 资源管理器 (ARM) 模板在 Azure 公共多访问边缘计算 (MEC) 中部署 Ubuntu Linux 虚拟机 (VM)。

Azure 资源管理器模板是定义项目基础结构和配置的 JavaScript 对象表示法 (JSON) 文件。 模板使用声明性语法。 你可以在不编写用于创建部署的编程命令序列的情况下,描述预期部署。

先决条件

  • 如果没有 Azure 订阅,请在开始之前创建一个免费帐户

  • 将允许列表中的订阅添加到 Azure 帐户,这使你能够在 Azure 公共 MEC 中部署资源。 如果没有有效的支持的订阅,请联系 Azure 公共 MEC 产品团队

注意

Azure CLI 2.26 及更高版本支持 Azure 公共 MEC 部署。

查看模板

  1. 查看以下示例 ARM 模板。

    在 Azure public MEC 中部署的每个资源都有一个名为 extendedLocation 的额外属性,Azure 将其添加到资源提供程序中。 示例 ARM 模板部署下列资源:

    • 虚拟网络
    • 公共 IP 地址
    • Linux
    • 网络安全组
    • 虚拟机

    在这个例子中,ARM 模板:

    • Azure Edge Zone ID 与 Azure 公共 MEC 的显示名称不同。
    • Azure 网络安全组具有一个入站规则,允许从任何位置进行 SSH 和 HTTPS 访问。
    {
        "$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."
                }
            },
            "vmSize": {
                "defaultValue": "Standard_D2s_v3",
                "type": "String",
                "metadata": {
                    "description": "Size of the virtual machine."
                }
            },
            "location": {
                "defaultValue": "[resourceGroup().location]",
                "type": "String",
                "metadata": {
                    "description": "Location for all resources."
                }
            },
            "EdgeZone": {
                "type": "String"
            },
            "publisher": { 
                "type": "string", 
                "defaultValue": "Canonical",
                "metadata" : {
                    "description": "Publisher for the VM Image"
                }
            }, 
            "offer": { 
                "type": "string",
                "defaultValue": "UbuntuServer",
                "metadata" : {
                    "description": "Offer for the VM Image"
                }
            }, 
            "sku": { 
                "type": "string",
                "defaultValue": "18.04-LTS",
                "metadata" : {
                    "description": "SKU for the VM Image"
                }
            }, 
            "osVersion": { 
                "type": "string",
                "defaultValue": "latest",
                "metadata" : {
                    "description": "version for the VM Image"
                }
            },
            "vmName": {
                "defaultValue": "myEdgeVM",
                "type": "String",
                "metadata": {
                    "description": "VM Name."
                }
            }
        },
        "variables": {
            "nicName": "myEdgeVMNic",
            "addressPrefix": "10.0.0.0/16",
            "subnetName": "Subnet",
            "subnetPrefix": "10.0.0.0/24",
            "publicIPAddressName": "myEdgePublicIP",
            "virtualNetworkName": "MyEdgeVNET",
            "subnetRef": "[resourceId('Microsoft.Network/virtualNetworks/subnets', variables('virtualNetworkName'), variables('subnetName'))]",
            "networkSecurityGroupName": "default-NSG"
        },
        "resources": [
            {
                "type": "Microsoft.Network/publicIPAddresses",
                "apiVersion": "2018-11-01",
                "name": "[variables('publicIPAddressName')]",
                "location": "[parameters('location')]",
                "extendedLocation": {
                    "type": "EdgeZone",
                    "name": "[parameters('EdgeZone')]"
                },
                "sku": {
                    "name": "Standard"
                },
                "properties": {
                    "publicIPAllocationMethod": "Static",
                    "dnsSettings": {
                        "domainNameLabel": "[parameters('dnsLabelPrefix')]"
                    }
                }
            },
            {
                "type": "Microsoft.Network/networkSecurityGroups",
                "apiVersion": "2019-08-01",
                "name": "[variables('networkSecurityGroupName')]",
                "location": "[parameters('location')]",
                "properties": {
                    "securityRules": [
                       {
                            "name": "AllowHttps",
                            "properties": {
                                "description": "HTTPS is allowed",
                                "protocol": "*",
                                "sourcePortRange": "*",
                                "destinationPortRange": "443",
                                "sourceAddressPrefix": "*",
                                "destinationAddressPrefix": "*",
                                "access": "Allow",
                                "priority": 130,
                                "direction": "Inbound",
                                "sourcePortRanges": [],
                                "destinationPortRanges": [],
                                "sourceAddressPrefixes": [],
                                "destinationAddressPrefixes": []
                             }
                        },
                        { 
                            "name": "AllowSSH",
                            "properties": {
                                "description": "HTTPS is allowed",
                                "protocol": "*",
                                "sourcePortRange": "*",
                                "destinationPortRange": "22",
                                "sourceAddressPrefix": "*",
                                "destinationAddressPrefix": "*",
                                "access": "Allow",
                                "priority": 140,
                                "direction": "Inbound",
                                "sourcePortRanges": [],
                                "destinationPortRanges": [],
                                "sourceAddressPrefixes": [],
                                "destinationAddressPrefixes": []
                             }
                        }
                    ]
                }
            },
            {
                "type": "Microsoft.Network/virtualNetworks",
                "apiVersion": "2018-11-01",
                "name": "[variables('virtualNetworkName')]",
                "location": "[parameters('location')]",
                "extendedLocation": {
                    "type": "EdgeZone",
                    "name": "[parameters('EdgeZone')]"
                },
                "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')]",
                "extendedLocation": {
                    "type": "EdgeZone",
                    "name": "[parameters('EdgeZone')]"
                },
                "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": "2020-06-01",
                "name": "[parameters('vmName')]",
                "location": "[parameters('location')]",
                "extendedLocation": {
                    "type": "EdgeZone",
                    "name": "[parameters('EdgeZone')]"
                },
                "dependsOn": [
                    "[resourceId('Microsoft.Network/networkInterfaces/', variables('nicName'))]"
                ],
                "properties": {
                    "hardwareProfile": {
                        "vmSize": "[parameters('vmSize')]"
                    },
                    "osProfile": {
                        "computerName": "[parameters('vmName')]",
                        "adminUsername": "[parameters('adminUsername')]",
                        "adminPassword": "[parameters('adminPassword')]"
                    },
                    "storageProfile": {
                        "imageReference": {
                            "publisher": "[parameters('publisher')]",
                            "offer": "[parameters('offer')]",
                            "sku": "[parameters('sku')]",
                            "version": "[parameters('osVersion')]"
                        },
                        "osDisk": {
                            "createOption": "FromImage",
                            "managedDisk": {
                                "storageAccountType": "StandardSSD_LRS"
                            }
                        }
                    },
                    "networkProfile": {
                        "networkInterfaces": [
                            {
                                "id": "[resourceId('Microsoft.Network/networkInterfaces',variables('nicName'))]"
                            }
                        ]
                    }
                }
            }
        ],
        "outputs": {
            "hostname": {
                "type": "String",
                "value": "[reference(variables('publicIPAddressName')).dnsSettings.fqdn]"
            },
            "sshCommand": {
                "type": "string",
                "value": "[format('ssh {0}@{1}', parameters('adminUsername'), reference(resourceId('Microsoft.Network/publicIPAddresses', variables('publicIPAddressName'))).dnsSettings.fqdn)]"
            }
        }
    }
    

使用 Azure CLI 部署 ARM 模板

  1. 将上一节中的示例 ARM 模板的内容保存在名为 azurepublicmecDeploy.json 的文件中。

  2. 使用 az login 命令登录到 Azure,并使用 az account set 命令设置 Azure 订阅。

    az login                                
    az account set --subscription <subscription name>
    
  3. 使用 az group create 命令创建 Azure 资源组。 资源组是在其中部署和管理 Azure 资源的逻辑容器。 以下示例创建名为 myResourceGroup 的资源组:

    az group create --name myResourceGroup --location <location>
    

    注意

    每个 Azure 公共 MEC 站点都与一个 Azure 区域相关联。 根据需要部署资源的 Azure 公共 MEC 位置,为 --location 参数选择适当的区域值。 有关详细信息,请参阅 Azure 公共 MEC 的重要概念

  4. 使用 az deployment group create 命令在资源组中部署 ARM 模板。

    az deployment group create --resource-group myResourceGroup --template-file azurepublicmecDeploy.json
    
    Please provide string value for 'adminUsername' (? for help): <username>
    Please provide securestring value for 'adminPassword' (? for help): <password>
    Please provide string value for 'dnsLabelPrefix' (? for help): <uniqueDnsLabel>
    Please provide string value for 'EdgeZone' (? for help): <edge zone ID>
    
  5. 等待部署运行几分钟。

    命令执行完成后,可以在 myResourceGroup 资源组中看到新的资源。 下面是示例输出:

    { 
    "id": "/subscriptions/xxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.Resources/deployments/edgeZonesDeploy",
      "location": null,
      "name": "edgeZonesDeploy",
      "properties": {
        "correlationId": "<xxxxxxxx>",
        "debugSetting": null,
        "dependencies": [
          {
            "dependsOn": [
              {
                "id": "/subscriptions/xxxxx/resourceGroups/myResourceGroup /providers/Microsoft.Network/networkSecurityGroups/default-NSG",
                "resourceGroup": " myResourceGroup ",
                "resourceName": "default-NSG",
                "resourceType": "Microsoft.Network/networkSecurityGroups"
              }
            ],
            "id": "/subscriptions/xxxxxx/resourceGroups/ myResourceGroup /providers/Microsoft.Network/virtualNetworks/MyEdgeTestVnet",
            "resourceGroup": " myResourceGroup ",
            "resourceName": " MyEdgeTestVnet ",
            "resourceType": "Microsoft.Network/virtualNetworks"
          },
     "outputs": {
          "hostname": {
            "type": "String",
            "value": "xxxxx.cloudapp.azure.com"
          },
          "sshCommand": {
            "type": "String",
            "value": "ssh <adminUsername>@<publicIPFQDN>"
          }
        },
    ...
    }
    

访问虚拟机

若要使用 SSH 连接到 Azure 公共 MEC 中的虚拟机,最佳方法是在 Azure 父区域中部署跳转框。

  1. 按照在区域中创建虚拟机中的说明进行操作。

  2. 使用 SSH 连接到部署在区域中的跳转盒虚拟机。

    ssh <username>@<regionVM_publicIP>
    
  3. 在跳转盒中,使用 SSH 连接到在 Azure 公共 MEC 中创建的虚拟机。

    ssh <username>@<edgezoneVM_publicIP>
    

清理资源

在本快速入门中,你使用 Azure CLI 在 Azure 公共 MEC 中部署了 ARM 模板。 如果将来不需要这些资源,请使用 az group delete 命令删除资源组、规模集和所有相关资源。 使用 --yes 参数,将在不提示确认的情况下删除资源。

az group delete \--name myResourceGroup \--yes

后续步骤

若要使用 Azure CLI 在 Azure 公共 MEC 中部署虚拟机,请转到以下文章: