使用 Azure Resource Manager 範本來部署 Azure 虛擬機並將其連線至 Azure Arc

本文提供使用 Azure Resource Manager 範本 (ARM 範本) 將執行 Windows 的 Azure 虛擬機自動上線至 Azure Arc 的指引。提供的 ARM 範本負責建立 Azure 資源,並在 VM 上執行 Azure Arc 上線腳本。

根據預設,Azure VM 會使用 Azure 實例元數據服務 (IMDS)。 藉由將 Azure VM 投影為已啟用 Azure Arc 的伺服器, 就會建立衝突 ,這不允許在使用 IMDS 時將 Azure Arc 伺服器資源表示為一個。 相反地,Azure Arc 伺服器仍會「充當」原生 Azure VM。

本指南可讓您僅針對示範目的,使用 Azure VM 並將 Azure VM 上線至 Azure Arc。 您將能夠模擬部署在 Azure 外部的伺服器,例如內部部署或其他雲端平臺。

注意

Azure VM 不預期為已啟用 Azure Arc 的伺服器。 不支援下列案例,而且只應用於示範和測試目的。

必要條件

  1. 複製 Azure Arc Jumpstart 存放庫。

    git clone https://github.com/microsoft/azure_arc.git
    
  2. 將 Azure CLI 安裝或更新為 2.7 版和更新版本。 使用下列命令來檢查您目前的已安裝版本。

    az --version
    
  3. Azure 訂用帳戶:如果您沒有 Azure 訂用帳戶,您可以 建立免費的 Azure 帳戶

  4. 建立 Azure 服務主體。

    若要使用ARM範本部署 Azure 資源,則需要指派具有參與者角色的 Azure 服務主體。 若要建立它,請登入您的 Azure 帳戶,然後執行下列命令。 您也可以在 Azure Cloud Shell執行此命令。

    az login
    az account set -s <Your Subscription ID>
    az ad sp create-for-rbac -n "<Unique SP Name>" --role contributor --scopes "/subscriptions/<Your Subscription ID>"
    

    例如:

    az ad sp create-for-rbac -n "http://AzureArcServers" --role contributor --scopes "/subscriptions/00000000-0000-0000-0000-000000000000"
    

    輸出應該看起來像這樣:

    {
      "appId": "XXXXXXXXXXXXXXXXXXXXXXXXXXXX",
      "displayName": "http://AzureArcServers",
      "password": "XXXXXXXXXXXXXXXXXXXXXXXXXXXX",
      "tenant": "XXXXXXXXXXXXXXXXXXXXXXXXXXXX"
    }
    

    注意

    強烈建議您將服務主體的範圍設定為特定的 Azure 訂用帳戶和資源群組

自動化流程

若要熟悉自動化和部署流程,以下是說明。

  1. 用戶編輯 ARM 範本參數檔案(一次性編輯)。 這些參數值會在整個部署中使用。

  2. ARM 範本包含 Azure VM 自定義腳本擴充功能,其會部署 install_arc_agent.ps1 PowerShell 腳本。

  3. 為了讓 Azure VM 成功投影為已啟用 Azure Arc 的伺服器,腳本會:

    1. 設定本機OS環境變數。

    2. 產生名為 LogonScript.ps1的本機 OS 登入腳本。 此文稿會:

      • LogonScript.log建立檔案。

      • 停止並停用 Windows Azure 客體代理程序服務。

      • 建立新的 Windows 防火牆規則,以封鎖 Azure IMDS 對遠端地址的 169.254.169.254 輸出流量。

      • 取消註冊登入腳本 Windows 排程工作,使其不會在第一次登入之後執行。

    3. 停用並防止 Windows 伺服器管理員 在啟動時執行。

  4. 用戶會透過 RDP 連線到 Windows VM,這會開始執行 LogonScript.ps1 並將 VM 上線至 Azure Arc。

部署

如前所述,此部署將使用ARM範本。 您將部署單一範本,負責在單一資源群組中建立所有 Azure 資源,並將建立的 VM 上線至 Azure Arc。

  1. 在部署 ARM 範本之前,請使用 Azure CLI 搭配 az login 命令登入 Azure。

  2. 部署使用ARM範本參數檔案。 在起始部署之前,請先編輯 azuredeploy.parameters.json 位於本機複製存放庫資料夾中的檔案。 範例參數檔案位於 這裡

  3. 若要部署 ARM 範本,請瀏覽至本機複製的 部署資料夾 ,然後執行下列命令:

    az group create --name <Name of the Azure resource group> --location <Azure Region> --tags "Project=jumpstart_azure_arc_servers"
    az deployment group create \
    --resource-group <Name of the Azure resource group> \
    --name <The name of this deployment> \
    --template-uri https://raw.githubusercontent.com/microsoft/azure-arc/main/azure_arc_servers_jumpstart/azure/windows/arm_template/azuredeploy.json \
    --parameters <The `azuredeploy.parameters.json` parameters file location>
    

    注意

    請確定您使用的 Azure 資源組名與您在檔案中使用的 azuredeploy.parameters.json 資源組名相同。

    例如:

    az group create --name Arc-Servers-Win-Demo --location "East US" --tags "Project=jumpstart_azure_arc_servers"
    az deployment group create \
    --resource-group Arc-Servers-Win-Demo \
    --name arcwinsrvdemo \
    --template-uri https://raw.githubusercontent.com/microsoft/azure-arc/main/azure_arc_servers_jumpstart/azure/windows/arm_template/azuredeploy.json \
    --parameters azuredeploy.parameters.json
    
  4. 布建 Azure 資源之後,您可以在 Azure 入口網站 中看到它們。

    A screenshot of an output from an ARM template.

    A screenshot resources in the resource group.

Windows 登入和部署後

  1. 現在已建立 Windows Server VM,下一個步驟就是連接它。 使用其公用IP位址,RDP至 VM。

    A screenshot of an Azure VM public IP address.

  2. 第一次登入時,如自動化流程一節中所述,將會執行登入腳本。 此腳本會建立為自動化部署程式的一部分。

  3. 讓腳本執行,不要 關閉 PowerShell工作階段。 工作階段會在完成之後自動關閉。

    注意

    腳本運行時間大約為 1-2 分鐘。

    A screenshot of one type of script output.

    A screenshot of a second type of script output.

    A screenshot of a third type of script output.

    A screenshot of a fourth type of script output.

  4. 成功完成之後,新的已啟用 Azure Arc 的伺服器將會新增至資源群組。

A screenshot of a resource group from an Azure Arc-enabled server.

A screenshot of details from an Azure Arc-enabled server.

清理

若要刪除整個部署,請從 Azure 入口網站 中刪除資源群組。

A screenshot of how to delete the resource group.