Compute Engine 上的 .NET 使用入门

本教程介绍如何开始使用 Compute Engine。 请按照本教程向 Compute Engine 部署一个 Hello World .NET Web 应用。如需获取 App Engine 入门方面的帮助,请参阅 App Engine 标准环境

目标

  • 将 Hello World 示例应用部署到单个 Compute Engine 实例。

费用

在本文档中,您将使用 Google Cloud的以下收费组件:

如需根据您的预计使用情况来估算费用,请使用价格计算器

新 Google Cloud 用户可能有资格申请免费试用

准备工作

  1. 登录您的 Google Cloud 账号。如果您是 Google Cloud新手,请 创建一个账号来评估我们的产品在实际场景中的表现。新客户还可获享 $300 赠金,用于运行、测试和部署工作负载。
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. Enable the Compute Engine API.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the API

  5. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  6. Verify that billing is enabled for your Google Cloud project.

  7. Enable the Compute Engine API.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the API

  8. 在运行 Microsoft Windows 的本地机器上安装 .NET Core SDK 2.1 或更高版本。
  9. 在运行 Microsoft Windows 的本地机器上安装 Web Deploy 3.6 或更高版本。

在本地运行应用

  1. 下载并解压缩或从 github 克隆示例存储库:

    git clone https://github.com/GoogleCloudPlatform/getting-started-dotnet.git
    
  2. 在 PowerShell 窗口中,进入 HelloWorld 目录:

    cd getting-started-dotnet\HelloWorld
    
  3. 启动本地网络服务器:

    dotnet run
    
  4. 打开网络浏览器,转到 localhost:8080

    在您的网络浏览器中,您会看到一些 Hello World 文本,这些文本是从本地机器提供的。

  5. 当您准备好继续时,请按 Control+C 停止本地网络服务器。

部署到单个实例

此部分逐步介绍如何在 Compute Engine 上运行应用的单个实例。

单实例部署。

创建和配置 Compute Engine 实例

在 Google Cloud Marketplace 中,您可以启动在 Compute Engine 上运行 Microsoft IIS 的 Windows 实例。

  1. 在 Google Cloud 控制台中,前往 ASP.NET Framework Cloud Marketplace 页面。

    转到 Cloud Marketplace

  2. 点击启动

  3. 将设置保留为默认值,然后点击部署

    等待 Compute Engine 实例部署。部署通常需要 5 分钟左右。

  4. 要显示资源信息,请点击虚拟机实例

  5. 要编辑虚拟机实例,请点击管理资源

  6. 点击设置 Windows 密码,然后记下用户名。

  7. 复制新的 Windows 密码,然后点击关闭

  8. 虚拟机实例详情页面上,记下外部 IP 地址。

  9. 在本地 Windows 机器上,修改文件 HelloWorld/Properties/PublishProfiles/ComputeEngine.pubxml

  10. <MSDeployServiceURL></MSDeployServiceURL> 之间输入虚拟机实例的外部 IP 地址。例如 <MSDeployServiceURL>203.0.113.22</MSDeployServiceURL>

  11. 输入您先前在 <UserName></UserName> 之间记录的实例的用户名。

  12. 将所做的更改保存到 ComputeEngine.pubxml 文件。

  13. 在 PowerShell 窗口中,将您的应用发布到 Compute Engine 实例:

    dotnet publish -c Release `
      /p:PublishProfile=Properties\PublishProfiles\ComputeEngine.pubxml `
      "/p:Password=YOUR-PASSWORD"
    

    YOUR-PASSWORD 替换为先前复制的密码。

  14. 打开网络浏览器,转到您实例的外部 IP 地址。

    在您的网络浏览器中,您会看到 Compute Engine 实例提供的一些 Hello World 文本。

管理和监控实例

您可以使用 Google Cloud 控制台来监控和管理您的实例。

  1. 如需查看 Compute Engine 资源生成的所有日志,请转到Logs Explorer页面。

    前往 Logs Explorer

    Cloud Logging 会自动配置为从各种常用服务(包括 syslog)收集日志。

清除数据

为避免因本教程中使用的资源导致您的 Google Cloud 账号产生费用,请删除包含这些资源的项目,或者保留项目但删除各个资源。

删除项目

  1. 在 Google Cloud 控制台中,前往管理资源页面。

    转到“管理资源”

  2. 在项目列表中,选择要删除的项目,然后点击删除
  3. 在对话框中输入项目 ID,然后点击关闭以删除项目。

逐个删除资源

gcloud compute instances delete my-app-instance --zone=YOUR_ZONE --delete-disks=all
gcloud compute firewall-rules delete default-allow-http-80

后续步骤