共用方式為


快速入門:使用 ARM 範本在 Azure 公用 MEC 中部署虛擬機器

在本快速入門中,您將了解如何使用 Azure Resource Manager (ARM) 範本,在 Azure 公用多接取邊緣運算 (MEC) 中部署 Ubuntu Linux 虛擬機器 (VM)。

Azure Resource Manager 範本是一個 JavaScript 物件標記法 (JSON) 檔案,會定義專案的基礎結構和設定。 範本使用宣告式語法。 您可以描述預期的部署,而不需要撰寫程式設計命令順序來建立部署。

必要條件

  • 如果您沒有 Azure 訂用帳戶,請在開始前建立免費帳戶

  • 將加入允許清單的訂用帳戶新增至您的 Azure 帳戶,這可讓您在 Azure 公用 MEC 中部署資源。 如果您沒有作用中的允許訂用帳戶,請連絡 Azure 公用 MEC 產品小組

注意

Azure CLI 2.26 版和更新版本支援 Azure 公用 MEC 部署。

檢閱範本

  1. 請檢閱下列範例 ARM 範本。

    您在 Azure 公用 MEC 中部署的每個資源都有 Azure 新增至資源提供者的一個名為 extendedLocation 的額外屬性。 範例 ARM 範本會部署這些資源:

    • 虛擬網路
    • 公用 IP 位址
    • 網路介面
    • 網路安全性群組
    • 虛擬機器

    在此範例 ARM 範本中:

    • Azure Edge Zone 識別碼與 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 中部署虛擬機器,請參閱下列文章: