共用方式為


使用 ARM 範本建立 Azure 計算機群

本文將逐步解說如何使用 ARM 範本來建立 Azure 計算機群。

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

必要條件

  • 如果您沒有 Azure 訂用帳戶,請在開始前建立免費帳戶
  • 在使用計算機群之前,請先完成功能註冊並設定角色型存取控制 (RBAC)。

功能註冊

使用 PowerShell 或 Azure 入口網站向您的訂用帳戶註冊 Azure 計算機群資源提供者。 註冊最多可能需要 30 分鐘才能成功顯示為已註冊。

Register-AzResourceProvider -ProviderNamespace Microsoft.AzureFleet

角色型存取控制權限

指派適當的 RBAC 權限以使用 Azure 計算機群。

  1. Azure 入口網站中,瀏覽到訂用帳戶。
  2. 選取您要調整 RBAC 權限的訂用帳戶。
  3. 選取存取控制 (IAM)
  4. 選取 [新增],然後選取 [新增角色指派]
  5. 搜尋 [虛擬機器參與者] 並反白顯示。 選取 [下一步]。
  6. 按一下 [+ 選取成員]
  7. 搜尋 [Azure 機群資源提供者] 角色。
  8. 選取 [Azure 機群資源提供者],然後選取 [檢閱 + 指派]
  9. 針對網路參與者角色和受控識別操作員角色重複上述步驟。

如果您在部署計算機群時使用儲存在計算資源庫中的映像,也請針對計算資源庫共用管理員角色重複上述步驟。

如需指派角色的詳細資訊,請參閱使用 Azure 入口網站指派 Azure 角色

ARM 範本

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

ARM 範本可讓您部署相關資源的群組。 在單一範本中,您可以建立虛擬機器擴展集、安裝應用程式,並設定自動縮放規則。 使用變數和參數,就可以重複使用此範本來更新現有的、或建立額外的擴展集。 您可以透過 Azure 入口網站、Azure CLI 或 Azure PowerShell,或從持續整合 / 持續傳遞 (CI/CD) 管線部署範本。

檢閱範本

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "fleetName": {
            "type": "string",
            "metadata": {
                "description": "String used as a base for naming resources. Must be 3-61 characters in length and globally unique across Azure. A hash is prepended to this string for some resources, and resource-specific information is appended."
            }
        },
        "adminUsername": {
            "type": "string",
            "defaultValue": "testusername",
            "metadata": {
                "description": "Admin username on all VMs."
            }
        },
        "adminPassword": {
            "type": "string",
            "metadata": {
                "description": "Admin password on all VMs."
            }
        },
        "location": {
            "type": "string",
            "defaultValue": "[resourceGroup().location]",
            "metadata": {
                "description": "Location for all resources."
            }
        }
    },
    "variables": {
        "vnName": "[format('{0}-vnet', parameters('fleetName'))]",
        "lbName": "[format('{0}-lb', parameters('fleetName'))]",
        "imageReference": {
            "publisher": "MicrosoftWindowsServer",
            "offer": "WindowsServer",
            "sku": "2019-DataCenter-GS",
            "version": "latest"
        }
    },
    "resources": [
        {
            "type": "Microsoft.Network/virtualNetworks",
            "apiVersion": "2022-07-01",
            "name": "[variables('vnName')]",
            "location": "[parameters('location')]",
            "properties": {
                "addressSpace": {
                    "addressPrefixes": [
                        "10.0.0.0/16"
                    ]
                },
                "subnets": [
                    {
                        "name": "default",
                        "properties": {
                            "addressPrefix": "10.0.0.0/24"
                        }
                    }
                ]
            }
        },
        {
            "type": "Microsoft.Network/loadBalancers",
            "apiVersion": "2022-07-01",
            "name": "[variables('lbName')]",
            "location": "[parameters('location')]",
            "sku": {
                "name": "Standard"
            },
            "properties": {
                "frontendIPConfigurations": [
                    {
                        "name": "[variables('lbName')]",
                        "properties": {
                            "privateIPAddress": "10.0.0.4",
                            "privateIPAllocationMethod": "Dynamic",
                            "subnet": {
                                "id": "[reference(resourceId('Microsoft.Network/virtualNetworks', variables('vnName'))).subnets[0].id]"
                            }
                        }
                    }
                ],
                "backendAddressPools": [
                    {
                        "name": "[variables('lbName')]"
                    }
                ]
            },
            "dependsOn": [
                "[resourceId('Microsoft.Network/virtualNetworks', variables('vnName'))]"
            ]
        },
        {
            "type": "Microsoft.AzureFleet/fleets",
            "apiVersion": "2024-05-01-Preview",
            "name": "[toLower(parameters('fleetName'))]",
            "location": "[parameters('location')]",
            "properties": {
                "vmSizesProfile": [
                    {
                        "name": "Standard_F2s_v2"
                    },
                    {
                        "name": "Standard_DS1_v2"
                    },
                    {
                        "name": "Standard_DS2_v2"
                    }
                ],
                "spotPriorityProfile": {
                    "capacity": 10,
                    "evictionPolicy": "Delete",
                    "allocationStrategy": "CapacityOptimized",
                    "maintain": false
                },
                "regularPriorityProfile": {
                    "capacity": 50,
                    "minCapacity": 30,
                    "allocationStrategy": "LowestPrice"
                },
                "computeProfile": {
                    "platformFaultDomainCount": 1,
                    "computeApiVersion": "2024-03-01",
                    "baseVirtualMachineProfile": {
                        "storageProfile": {
                            "osDisk": {
                                "osType": "Windows",
                                "createOption": "fromImage",
                                "caching": "ReadWrite",
                                "managedDisk": {
                                    "storageAccountType": "Standard_LRS"
                                }
                            },
                            "imageReference": "[variables('imageReference')]"
                        },
                        "osProfile": {
                            "computerNamePrefix": "[parameters('fleetName')]",
                            "adminUsername": "[parameters('adminUsername')]",
                            "adminPassword": "[parameters('adminPassword')]"
                        },
                        "networkProfile": {
                            "networkApiVersion": "2022-07-01",
                            "networkInterfaceConfigurations": [
                                {
                                    "name": "[variables('vnName')]",
                                    "properties": {
                                        "primary": true,
                                        "enableIPForwarding": true,
                                        "enableAcceleratedNetworking": false,
                                        "ipConfigurations": [
                                            {
                                                "name": "[variables('vnName')]",
                                                "properties": {
                                                    "subnet": {
                                                        "id": "[reference(resourceId('Microsoft.Network/virtualNetworks', variables('vnName'))).subnets[0].id]"
                                                    },
                                                    "primary": true,
                                                    "applicationGatewayBackendAddressPools": [],
                                                    "loadBalancerBackendAddressPools": [
                                                        {
                                                            "id": "[format('{0}/backendAddressPools/{1}', resourceId('Microsoft.Network/loadBalancers', variables('lbName')), variables('lbName'))]"
                                                        }
                                                    ]
                                                }
                                            }
                                        ]
                                    }
                                }
                            ]
                        }
                    }
                }
            },
            "dependsOn": [
                "[resourceId('Microsoft.Network/virtualNetworks', variables('vnName'))]",
                "[resourceId('Microsoft.Network/loadBalancers', variables('lbName'))]"
            ]
        }
    ]
}

範本中定義了下列資源:

清除資源

若不再需要,您可以使用 az group delete 移除資源群組、擴展集和所有相關資源,如下所示。 --no-wait 參數不會等待作業完成,就會將控制項傳回給提示字元。 --yes 參數能確認您想要刪除資源,而不需再透過另一個提示確認。

az group delete --name myResourceGroup --yes --no-wait

下一步