使用 Azure Arc 在 VMware vCenter 上建立虛擬機器
本文說明如何從 Azure 入口網站使用 vCenter 資源佈建 VM。
在 Azure 入口網站中建立 VM
一旦系統管理員將 VMware vCenter 連線至 Azure,代表成為 Azure 中的 VMware vCenter 資源,並提供您使用這些資源的權限,您就可以建立虛擬機器。
必要條件
- 您擁有 Arc VMware VM 參與者角色的 Azure 訂用帳戶和資源群組。
- 您擁有 Arc 私人雲端資源使用者角色的資源集區/叢集/主機。
- 您擁有 Arc 私人雲端資源使用者角色的虛擬機器範本資源。
- 您擁有 Arc 私人雲端資源使用者角色的虛擬網路資源。
請遵循下列步驟,在 Azure 入口網站中建立 VM:
在瀏覽器中,移至 Azure 入口網站。 導覽至虛擬機器瀏覽檢視。 您會看到 Azure 和 Arc 虛擬機器的整合瀏覽體驗。
選取 [新增],然後從下拉式清單中選取 [Azure Arc 機器]。
選取您要部署 VM 的 [訂用帳戶] 和 [資源群組]。
提供 [虛擬機器名稱],然後選取管理員與您共用的 [自訂位置]。
如果支援多種 VM,請從 [虛擬機器種類] 下拉式清單中選取 [VMware]。
選取應部署 VM 的 [資源集區/叢集/主機]。
選取要用於儲存的資料存放區。
依據您要建立的 VM,選取 [範本]。
提示
您可以覆寫 [CPU 核心] 和 [記憶體] 的範本預設值。
如果您已選取 Windows 範本,請提供[系統管理員帳戶] 的 [使用者名稱] 和 [密碼]。
(選用) 變更範本中設定的磁碟。 例如,您可以新增更多磁碟,或更新現有的磁碟。 所有磁碟和 VM 都會位於步驟 6 中選取的資料存放區。
(選用) 變更範本中設定的網路介面。 例如,您可以新增網路介面 (NIC) 卡或更新現有的 NIC。 在具備適當的網路資源權限情況下,您也可以變更此 NIC 將連結的網路。
(選用) 必要時,請新增標籤至 VM 資源。
檢閱所有屬性之後,選取 [建立]。 系統應該需要幾分鐘的時間來建立 VM。
本文說明如何運用 Bicep 範本,使用 vCenter 資源佈建 VM。
使用 Bicep 範本建立 Arc VMware 機器
下列 Bicep 範本可用來建立 Arc VMware 機器。 此處的清單列出已啟用 Arc 之 VMware 資源的可用 Azure Resource Manager (ARM)、Bicep 和 Terraform 範本。 若要觸發任何其他 Arc 作業,請將對應的 ARM 範本轉換為 Bicep 範本。
// Parameters
param vmName string = 'contoso-vm'
param vmAdminPassword string = 'examplepassword!#'
param vCenterId string = '/subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourceGroups/contoso-rg/providers/Microsoft.ConnectedVMwarevSphere/vcenters/contoso-vcenter'
param templateId string = '/subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourceGroups/contoso-rg/providers/Microsoft.ConnectedVMwarevSphere/VirtualMachineTemplates/contoso-template-win22'
param resourcePoolId string = '/subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourceGroups/contoso-rg/providers/Microsoft.ConnectedVMwarevSphere/ResourcePools/contoso-respool'
param datastoreId string = '/subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourceGroups/contoso-rg/providers/Microsoft.ConnectedVMwarevSphere/Datastores/contoso-datastore'
param networkId string = '/subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourceGroups/contoso-rg/providers/Microsoft.ConnectedVMwarevSphere/VirtualNetworks/contoso-network'
param extendedLocation object = {
type: 'customLocation'
name: '/subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourceGroups/contoso-rg/providers/Microsoft.ExtendedLocation/customLocations/contoso-customlocation'
}
param ipSettings object = {
allocationMethod: 'static'
gateway: ['172.24.XXX.1']
ipAddress: '172.24.XXX.105'
subnetMask: '255.255.255.0'
dnsServers: ['172.24.XXX.9']
}
resource contosoMachine 'Microsoft.HybridCompute/machines@2023-10-03-preview' = {
name: vmName
location:'westeurope'
kind:'VMware'
properties:{}
tags: {
foo: 'bar'
}
}
resource vm 'Microsoft.ConnectedVMwarevSphere/virtualMachineInstances@2023-12-01' = {
name: 'default'
scope: contosoMachine
extendedLocation: extendedLocation
properties: {
hardwareProfile: {
memorySizeMB: 4096
numCPUs: 2
}
osProfile: {
computerName: vmName
adminPassword: vmAdminPassword
}
placementProfile: {
resourcePoolId: resourcePoolId
datastoreId: datastoreId
}
infrastructureProfile: {
templateId: templateId
vCenterId: vCenterId
}
networkProfile: {
networkInterfaces: [
{
nicType: 'vmxnet3'
ipSettings: ipSettings
networkId: networkId
name: 'VLAN103NIC'
powerOnBoot: 'enabled'
}
]
}
}
}
// Outputs
output vmId string = vm.id
本文說明如何運用 Terraform 範本,使用 vCenter 資源佈建 VM。
使用 Terraform 建立 Arc VMware 機器
必要條件
- Azure 訂用帳戶:請確定您有作用中的 Azure 訂用帳戶。
- Terraform:在機器上安裝 Terraform。
- Azure CLI:安裝 Azure CLI 以驗證和管理資源。
請遵循下列步驟,使用 Terraform 建立 Arc VMware 機器。 本文涵蓋下列兩種案例:
- 針對在 vCenter 清查中探索到的 VM,請在 Azure 作業中執行啟用並安裝 Arc 代理程式。
- 使用範本、資源集區、資料存放區建立新的 Arc VMware VM 及安裝 Arc 代理程式。
案例 1
針對在 vCenter 清查中探索到的 VM,請在 Azure 作業中執行啟用並安裝 Arc 代理程式。
步驟 1:定義 variables.tf 檔案中的變數
建立名為 variables.tf 的檔案,並定義所有必要的變數。
variable "subscription_id" {
description = "The subscription ID for the Azure account."
type = string
}
variable "resource_group_name" {
description = "The name of the resource group."
type = string
}
variable "location" {
description = "The location/region where the resources will be created."
type = string
}
variable "machine_name" {
description = "The name of the machine."
type = string
}
variable "inventory_item_id" {
description = "The ID of the Inventory Item for the VM."
type = string
}
variable "custom_location_id" {
description = "The ID of the custom location."
type = string
}
variable "vm_username" {
description = "The admin username for the VM."
type = string
}
variable "vm_password" {
description = "The admin password for the VM."
type = string
}
variable "resource_group_name" {
description = "The name of the resource group."
type = string
}
variable "location" {
description = "The location/region where the resources will be created."
type = string
}
variable "machine_name" {
description = "The name of the machine."
type = string
}
variable "vm_username" {
description = "The admin username for the VM."
type = string
}
variable "vm_password" {
description = "The admin password for the VM."
type = string
}
variable "inventory_id" {
description = "The Inventory ID for the VM."
type = string
}
variable "vcenter_id" {
description = "The ID of the vCenter."
type = string
}
variable "custom_location_id" {
description = "The ID of the custom location."
type = string
}
步驟 2:建立 tfvars 檔案
建立名為 CreateVMwareVM.tfvars 的檔案,並提供變數的範例值。
subscription_id = "your-subscription-id"
resource_group_name = "your-resource-group"
location = "eastus"
machine_name = "test_machine0001"
inventory_item_id = "/subscriptions/your-subscription-id/resourceGroups/your-resource-group/providers/Microsoft.ConnectedVMwarevSphere/VCenters/your-vcenter-id/InventoryItems/your-inventory-item-id"
custom_location_id = "/subscriptions/your-subscription-id/resourceGroups/your-resource-group/providers/Microsoft.ExtendedLocation/customLocations/your-custom-location-id"
vm_username = "Administrator"
vm_password = " The admin password for the VM "
步驟 3:修改組態以使用變數
建立名為 main.tf 的檔案,並插入下列程式碼。
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = ">= 3.0"
}
azapi = {
source = "azure/azapi"
version = ">= 1.0.0"
}
}
}
# Configure the AzureRM provider with the subscription ID
provider "azurerm" {
features {}
subscription_id = var.subscription_id
}
# Configure the AzAPI provider with the subscription ID
provider "azapi" {
subscription_id = var.subscription_id
}
# Retrieve the resource group details
data "azurerm_resource_group" "example" {
name = var.resource_group_name
}
# Create a VMware machine resource in Azure
resource "azapi_resource" "test_machine0001" {
schema_validation_enabled = false
parent_id = data.azurerm_resource_group.example.id
type = "Microsoft.HybridCompute/machines@2023-06-20-preview"
name = var.machine_name
location = data.azurerm_resource_group.example.location
body = jsonencode({
kind = "VMware"
identity = {
type = "SystemAssigned"
}
})
}
# Create a Virtual Machine instance using the VMware machine and Inventory Item ID
resource "azapi_resource" "test_inventory_vm0001" {
schema_validation_enabled = false
type = "Microsoft.ConnectedVMwarevSphere/VirtualMachineInstances@2023-10-01"
name = "default"
parent_id = azapi_resource.test_machine0001.id
body = jsonencode({
properties = {
infrastructureProfile = {
inventoryItemId = var.inventory_item_id
}
}
extendedLocation = {
type = "CustomLocation"
name = var.custom_location_id
}
})
depends_on = [azapi_resource.test_machine0001]
}
# Install Arc agent on the VM
resource "azapi_resource" "guestAgent" {
type = "Microsoft.ConnectedVMwarevSphere/virtualMachineInstances/guestAgents@2023-10-01"
parent_id = azapi_resource.test_inventory_vm0001.id
name = "default"
body = jsonencode({
properties = {
credentials = {
username = var.vm_username
password = var.vm_password
}
provisioningAction = "install"
}
})
schema_validation_enabled = false
ignore_missing_property = false
depends_on = [azapi_resource.test_inventory_vm0001]
}
步驟 4:執行 Terraform 命令
使用 -var-file 旗標,在 Terraform 命令期間傳遞 .tfvars 檔案。
- 初始化 Terraform (如果尚未初始化):
terraform init
- 驗證組態:
terraform validate -var-file="CreateVMwareVM.tfvars"
- 規劃變更:
terraform plan -var-file="CreateVMwareVM.tfvars"
- 套用變更:
terraform apply -var-file="CreateVMwareVM.tfvars"
輸入 yes 確認提示來套用變更。
最佳作法
- 使用版本控制:讓 Terraform 組態檔保持在版本控制 (例如 Git) 之下,以追蹤一段時間的變更。
- 仔細檢閱計劃:在套用變更之前,請務必先檢閱 terraform 計劃的輸出,以確保您了解將進行哪些變更。
- 狀態管理:定期備份 Terraform 狀態檔案,以避免資料遺失。
您可以依照下列步驟,使用 Terraform 在 Azure 上有效建立和管理 HCRP 和 Arc VMware VM,並在建立的 VM 上安裝客體代理程式。
案例 2
使用範本、資源集區、資料存放區建立新的 Arc VMware VM 及安裝 Arc 代理程式。
步驟 1:定義 variables.tf 檔案中的變數
建立名為 variables.tf 的檔案,並定義所有必要的變數。
variable "subscription_id" {
description = "The subscription ID for the Azure account."
type = string
}
variable "resource_group_name" {
description = "The name of the resource group."
type = string
}
variable "location" {
description = "The location/region where the resources will be created."
type = string
}
variable "machine_name" {
description = "The name of the machine."
type = string
}
variable "vm_username" {
description = "The admin username for the VM."
type = string
}
variable "vm_password" {
description = "The admin password for the VM."
type = string
}
variable "template_id" {
description = "The ID of the VM template."
type = string
}
variable "vcenter_id" {
description = "The ID of the vCenter."
type = string
}
variable "resource_pool_id" {
description = "The ID of the resource pool."
type = string
}
variable "datastore_id" {
description = "The ID of the datastore."
type = string
}
variable "custom_location_id" {
description = "The ID of the custom location."
type = string
}
步驟 2:建立 tfvars 檔案
建立名為 CreateVMwareVM.tfvars 的檔案,並提供變數的範例值。
subscription_id = "your-subscription-id"
resource_group_name = "your-resource-group"
location = "eastus"
machine_name = "test_machine0002"
vm_username = "Administrator"
vm_password = "*********"
template_id = "/subscriptions/your-subscription-id/resourceGroups/your-resource-group/providers/Microsoft.ConnectedVMwarevSphere/virtualmachinetemplates/your-template-id"
vcenter_id = "/subscriptions/your-subscription-id/resourceGroups/your-resource-group/providers/Microsoft.ConnectedVMwarevSphere/VCenters/your-vcenter-id"
resource_pool_id = "/subscriptions/your-subscription-id/resourceGroups/your-resource-group/providers/Microsoft.ConnectedVMwarevSphere/resourcepools/your-resource-pool-id"
datastore_id = "/subscriptions/your-subscription-id/resourceGroups/your-resource-group/providers/Microsoft.ConnectedVMwarevSphere/datastores/your-datastore-id"
custom_location_id = "/subscriptions/your-subscription-id/resourceGroups/your-resource-group/providers/Microsoft.ExtendedLocation/customLocations/your-custom-location-id"
步驟 3:修改組態以使用變數
建立名為 main.tf 的檔案,並插入下列程式碼。
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = ">= 3.0"
}
azapi = {
source = "azure/azapi"
version = ">= 1.0.0"
}
}
}
# Configure the AzureRM provider with the subscription ID
provider "azurerm" {
features {}
subscription_id = var.subscription_id
}
# Configure the AzAPI provider with the subscription ID
provider "azapi" {
subscription_id = var.subscription_id
}
# Retrieve the resource group details
data "azurerm_resource_group" "example" {
name = var.resource_group_name
}
# Create a VMware machine resource in Azure
resource "azapi_resource" "test_machine0002" {
schema_validation_enabled = false
parent_id = data.azurerm_resource_group.example.id
type = "Microsoft.HybridCompute/machines@2023-06-20-preview"
name = var.machine_name
location = data.azurerm_resource_group.example.location
body = jsonencode({
kind = "VMware"
identity = {
type = "SystemAssigned"
}
})
}
# Create a Virtual Machine instance using the VMware machine created above
resource "azapi_resource" "test_vm0002" {
schema_validation_enabled = false
type = "Microsoft.ConnectedVMwarevSphere/VirtualMachineInstances@2023-10-01"
name = "default"
parent_id = azapi_resource.test_machine0002.id
body = jsonencode({
properties = {
infrastructureProfile = {
templateId = var.template_id
vCenterId = var.vcenter_id
}
placementProfile = {
resourcePoolId = var.resource_pool_id
datastoreId = var.datastore_id
}
osProfile = {
adminPassword = var.vm_password
}
}
extendedLocation = {
type = "CustomLocation"
name = var.custom_location_id
}
})
depends_on = [azapi_resource.test_machine0002]
}
# Create a guest agent for the VM instance
resource "azapi_resource" "guestAgent" {
type = "Microsoft.ConnectedVMwarevSphere/virtualMachineInstances/guestAgents@2023-10-01"
parent_id = azapi_resource.test_vm0002.id
name = "default"
body = jsonencode({
properties = {
credentials = {
username = var.vm_username
password = var.vm_password
}
provisioningAction = "install"
}
})
schema_validation_enabled = false
ignore_missing_property = false
depends_on = [azapi_resource.test_vm0002]
}
步驟 4:執行 Terraform 命令
使用 -var-file 旗標,在 Terraform 命令期間傳遞 .tfvars 檔案。
- 初始化 Terraform (如果尚未初始化):
terraform init
- 驗證組態:
terraform validate -var-file="CreateVMwareVM.tfvars"
- 規劃變更:
terraform plan -var-file="CreateVMwareVM.tfvars"
- 套用變更:
terraform apply -var-file="CreateVMwareVM.tfvars"
輸入 yes 確認提示來套用變更。
最佳作法
- 使用版本控制:讓 Terraform 組態檔保持在版本控制 (例如 Git) 之下,以追蹤一段時間的變更。
- 仔細檢閱計劃:在套用變更之前,請務必先檢閱 terraform 計劃的輸出,以確保您了解將進行哪些變更。
- 狀態管理:定期備份 Terraform 狀態檔案,以避免資料遺失。