你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

使用 Azure Arc 在 VMware vCenter 上创建虚拟机

本文介绍如何在 Azure 门户中使用 vCenter 资源预配 VM。

在 Azure 门户中创建 VM

在你的管理员将 VMware vCenter 连接到 Azure、在 Azure 中表示 VMware vCenter 资源,并向你提供对这些资源的权限后,你将创建一个虚拟机。

先决条件

  • Azure 订阅和资源组,你在其中拥有 Arc VMware VM 参与者角色。
  • 资源池/群集/主机,你在其中拥有 Arc 私有云资源用户角色。
  • 虚拟机模板资源,你在其中拥有 Arc 私有云资源用户角色。
  • 虚拟网络资源,你在其中拥有 Arc 私有云资源用户角色。

按照以下步骤在 Azure 门户中创建 VM:

  1. 在浏览器中转到 Azure 门户。 导航到虚拟机浏览视图。 你将看到 Azure 和 Arc 虚拟机的统一的浏览体验。

    显示 Azure 和 Arc 虚拟机的统一浏览体验的屏幕截图。

  2. 选择“添加”,然后从下拉列表中选择“Azure Arc 计算机”

    显示用于创建 Azure Arc 虚拟机的“基本信息”选项卡的屏幕截图。

  3. 选择要在其中部署 VM 的“订阅”和“资源组”。

  4. 提供“虚拟机名称”,然后选择管理员已与你共享的“自定义位置”。

    如果支持多种 VM,请从“虚拟机种类”下拉列表中选择“VMware”。

  5. 选择要在其中部署 VM 的“资源池/群集/主机”。

  6. 选择要用于存储的数据存储。

  7. 选择“模板”,你将基于此模板创建 VM

    提示

    可以覆盖“CPU 核心”和“内存”的模板默认值。

    如果选择的是 Windows 模板,请为“管理员帐户”提供“用户名”和“密码”。

  8. (可选)更改模板中配置的磁盘。 例如,你可以添加更多磁盘或更新现有磁盘。 所有磁盘和 VM 都将位于步骤 6 中所选的数据存储中。

  9. (可选)更改模板中配置的网络接口。 例如,你可以添加网络接口 (NIC) 卡或更新现有的 NIC。 如果你对网络资源具有适当的权限,还可以更改此 NIC 将连接到的网络。

  10. (可选)如有必要,向 VM 资源添加标记。

  11. 查看所有属性后,选择“创建”。 创建 VM 需要几分钟。

本文介绍如何通过 Bicep 模板使用 vCenter 资源预配 VM。

使用 Bicep 模板创建 Arc VMware 计算机

以下 Bicep 模板可用于创建 Arc VMware 计算机。 此处提供了适用于已启用 Arc 的 VMware 资源的 Azure 资源管理器 (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 计算机。 本文介绍以下两种方案:

  1. 对于在 vCenter 清单中发现的 VM,执行“在 Azure 中启用”操作并安装 Arc 代理。
  2. 使用模板、资源池、数据存储创建新的 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 命令

在运行 Terraform 命令期间,使用 -var-file 标志传递 .tfvars 文件

  1. 初始化 Terraform(如果尚未初始化):terraform init
  2. 验证配置:terraform validate -var-file="CreateVMwareVM.tfvars"
  3. 规划更改:terraform plan -var-file="CreateVMwareVM.tfvars"
  4. 应用更改: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 命令

在运行 Terraform 命令期间,使用 -var-file 标志传递 .tfvars 文件

  1. 初始化 Terraform(如果尚未初始化):terraform init
  2. 验证配置:terraform validate -var-file="CreateVMwareVM.tfvars"
  3. 规划更改:terraform plan -var-file="CreateVMwareVM.tfvars"
  4. 应用更改:terraform apply -var-file="CreateVMwareVM.tfvars"

输入 yes 确认提示,以应用更改。

最佳做法

  • 使用版本控制:使 Terraform 配置文件接受版本控制(例如在 Git 中),以跟踪其在不同时间发生的更改
  • 仔细检查计划:在应用更改之前,请始终检查 Terraform 计划的输出,以确保了解所要做出的更改
  • 状态管理:定期备份 Terraform 状态文件以避免数据丢失

后续步骤

在 Azure 中的 VMware VM 上执行操作