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

使用 Terraform 计划部署 Google Cloud Platform Windows 实例,并将其连接到 Azure Arc

本文提供此指南:使用提供的 Terraform 计划部署 Windows Server Google Cloud Platform (GCP) 实例,并将其作为启用了 Azure Arc 的服务器资源进行连接。

先决条件

  1. 克隆 Azure Arc 快速入门存储库。

    git clone https://github.com/microsoft/azure_arc.git
    
  2. 将 Azure CLI 更新到 2.7 及更高版本或者直接安装该版本。 使用以下命令检查你当前安装的版本。

    az --version
    
  3. 安装 Terraform >= 0.12

  4. 将启用计费的 Google Cloud Platform 帐户:创建免费试用帐户。 要创建 Windows Server 虚拟机,你必须升级你的帐户以启用计费。 从菜单中选择“计费”,然后选择右下角的“升级”。

    First screenshot showing how to enable billing on a GCP account.

    Second screenshot showing how to enable billing on a GCP account.

    Third screenshot showing how to enable billing on a GCP account.

    免责声明:为防止意外收费,请按照本文末尾的“删除部署”部分进行操作。

  5. 创建 Azure 服务主体。

    要将 GCP 虚拟机连接到 Azure Arc,需要一个被分配了“参与者”角色的 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://AzureArcGCP" --role contributor --scopes "/subscriptions/00000000-0000-0000-0000-000000000000"
    

    输出应如下所示:

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

    注意

    我们强烈建议你将服务主体的范围限定为特定的 Azure 订阅和资源组

创建新的 GCP 项目

  1. 浏览到 Google API 控制台,并使用 Google 帐户登录。 登录后,创建一个新项目,名为 Azure Arc demo。 创建后,请务必复制项目 ID,因为它通常与项目名称不同。

    First screenshot of the New Project page in the GCP console.

    Second screenshot of the New Project page in the GCP console.

  2. 在页面顶部的下拉列表中创建并选择新项目后,必须为该项目启用计算引擎 API 访问权限。 单击“+ 启用 API 和服务”并搜索“计算引擎”。 然后选择“启用”以启用 API 访问

    First screenshot of Compute Engine API in the GCP console.

    Second screenshot of Compute Engine API in the GCP console.

  3. 接下来,设置一个服务帐户密钥,Terraform 将使用该密钥在 GCP 项目中创建和管理资源。 转到“创建服务帐户密钥”页。 从下拉列表中选择“新建服务帐户”,为其命名,选择项目,然后选择“所有者”作为角色,选择“JSON”作为密钥类型,再选择“创建”。 这将下载一个 JSON 文件,其中包含 Terraform 管理资源所需的所有凭据。 将下载的 JSON 文件复制到 azure_arc_servers_jumpstart/gcp/windows/terraform 目录。

    A screenshot of how to create a service account in the GCP console.

部署

在执行 Terraform 计划之前,必须设置并导出计划将使用的环境变量。 这些变量基于刚创建的 Azure 服务主体、Azure 订阅和租户以及 GCP 项目名称。

  1. 使用 az account list 命令检索你的 Azure 订阅 ID 和租户 ID。

  2. Terraform 计划同时在 Microsoft Azure 和 Google Cloud Platform 中创建资源。 然后它在 GCP 虚拟机上执行脚本以安装 Azure Arc 代理和所有必要的工件。 此脚本需要有关你的 GCP 和 Azure 环境的某些信息。 使用适当的值编辑 scripts/vars.sh 并更新每个变量。

    • TF_VAR_subscription_id = 你的 Azure 订阅 ID
    • TF_VAR_client_id = Azure 服务主体应用程序 ID
    • TF_VAR_client_secret = 你的 Azure 服务主体密码
    • TF_VAR_tenant_id = Azure 租户 ID
    • TF_VAR_gcp_project_id = GCP 项目 ID
    • TF_VAR_gcp_credentials_filename = GCP 凭据 JSON 文件名
  3. 在 CLI 中,导航到克隆的存储库的 azure_arc_servers_jumpstart/gcp/windows/terraform 目录。

  4. 使用源命令运行 scripts/vars.sh,导出编辑的环境变量,如下所示。 Terraform 需要进行这些设置才能使计划正确执行。

    source ./scripts/vars.sh
    
  5. 运行 terraform init 命令,该命令将下载 Terraform AzureRM 提供程序。

    A screenshot of the terraform init command.

  6. 接下来,运行 terraform apply --auto-approve 命令并等待计划完成。 完成 Terraform 脚本后,你将部署 GCP Windows Server 2019 VM 并启动脚本以将 Azure Arc 代理下载到 VM 并将 VM 作为新 Azure 资源组中启用了 Azure Arc 的新服务器连接。 代理需要几分钟才能完成预配,请喝杯咖啡再说。

    A screenshot of the terraform apply command.

  7. 几分钟后,你应该能够打开 Azure 门户并导航到 arc-gcp-demo 资源组。 在 GCP 中创建的 Windows Server 虚拟机将作为资源可见。

    A screenshot of an Azure Arc-enabled server in the Azure portal.

半自动部署(可选)

Terraform 计划会自动安装 Azure Arc 代理,并通过在 VM 首次启动时执行 PowerShell 脚本将 VM 作为托管资源连接到 Azure。

A screenshot of the azcmagent connect command.

如果要演示/控制实际的注册过程,请执行以下操作:

  1. 在运行 terraform apply 命令之前,打开 main.tf 并注释掉 windows-startup-script-ps1 = local-file.install_arc_agent-ps1.content 行,然后保存文件。

    A screenshot showing main.tf being commented out to disable automatic onboarding of an Azure Arc agent.

  2. 按照上面的说明运行 terraform apply --auto-approve

  3. 打开 GCP 控制台并导航到计算实例页,然后选择已创建的 VM。

    A screenshot of a server in the GCP console.

    A screenshot showing how to reset a password for a Windows Server in the GCP console.

  4. 通过选择“设置密码”并指定用户名,为 VM 创建用户和密码。

    A screenshot showing how to set a username and password for a Windows Server in the GCP console.

  5. 通过从 GCP 控制台的 VM 页面中选择 RDP 按钮,RDP 进入 VM,然后使用刚创建的用户名和密码登录。

    A screenshot showing how to RDP into a GCP instance.

  6. 登录后,以管理员身份打开 PowerShell ISE。 确保你运行的是 x64 版本的 PowerShell ISE,而不是 x86 版本。 打开后,选择“文件”>“新建”以创建空文件。.ps1 然后粘贴 ./scripts/install_arc_agent.ps1 的全部内容。 单击播放按钮以执行脚本。 完成后,你应该会看到显示计算机成功载入的输出。

    Screenshot showing the Windows PowerShell integrated scripting environment with an Azure Arc agent connection script.

删除部署

要删除此演示中创建的所有资源,请使用 terraform destroy --auto-approve 命令,如下所示。

A screenshot of the terraform destroy command.

或者,可以直接从 GCP 控制台中删除 GCP VM。

A screenshot showing how to delete a virtual machine from the GCP console.