本教程介绍如何开始使用 Compute Engine。 请按照本教程向 Compute Engine 部署一个 Hello World .NET Web 应用。如需获取 App Engine 入门方面的帮助,请参阅 App Engine 标准环境。
目标
- 将 Hello World 示例应用部署到单个 Compute Engine 实例。
费用
在本文档中,您将使用 Google Cloud的以下收费组件:
如需根据您的预计使用情况来估算费用,请使用价格计算器。
准备工作
- 登录您的 Google Cloud 账号。如果您是 Google Cloud新手,请 创建一个账号来评估我们的产品在实际场景中的表现。新客户还可获享 $300 赠金,用于运行、测试和部署工作负载。
-
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 theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
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 theserviceusage.services.enablepermission. Learn how to grant roles.-
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 theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
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 theserviceusage.services.enablepermission. Learn how to grant roles.- 在运行 Microsoft Windows 的本地机器上安装 .NET Core SDK 2.1 或更高版本。
- 在运行 Microsoft Windows 的本地机器上安装 Web Deploy 3.6 或更高版本。
在本地运行应用
下载并解压缩或从 github 克隆示例存储库:
git clone https://github.com/GoogleCloudPlatform/getting-started-dotnet.git在 PowerShell 窗口中,进入
HelloWorld目录:cd getting-started-dotnet\HelloWorld启动本地网络服务器:
dotnet run打开网络浏览器,转到
localhost:8080。在您的网络浏览器中,您会看到一些 Hello World 文本,这些文本是从本地机器提供的。
当您准备好继续时,请按 Control+C 停止本地网络服务器。
部署到单个实例
此部分逐步介绍如何在 Compute Engine 上运行应用的单个实例。
创建和配置 Compute Engine 实例
在 Google Cloud Marketplace 中,您可以启动在 Compute Engine 上运行 Microsoft IIS 的 Windows 实例。
在 Google Cloud 控制台中,前往 ASP.NET Framework Cloud Marketplace 页面。
点击启动。
将设置保留为默认值,然后点击部署。
等待 Compute Engine 实例部署。部署通常需要 5 分钟左右。
要显示资源信息,请点击虚拟机实例。
要编辑虚拟机实例,请点击管理资源。
点击设置 Windows 密码,然后记下用户名。
复制新的 Windows 密码,然后点击关闭。
在虚拟机实例详情页面上,记下外部 IP 地址。
在本地 Windows 机器上,修改文件
HelloWorld/Properties/PublishProfiles/ComputeEngine.pubxml。在
<MSDeployServiceURL>和</MSDeployServiceURL>之间输入虚拟机实例的外部 IP 地址。例如<MSDeployServiceURL>203.0.113.22</MSDeployServiceURL>输入您先前在
<UserName>和</UserName>之间记录的实例的用户名。将所做的更改保存到
ComputeEngine.pubxml文件。在 PowerShell 窗口中,将您的应用发布到 Compute Engine 实例:
dotnet publish -c Release ` /p:PublishProfile=Properties\PublishProfiles\ComputeEngine.pubxml ` "/p:Password=YOUR-PASSWORD"
将
YOUR-PASSWORD替换为先前复制的密码。打开网络浏览器,转到您实例的外部 IP 地址。
在您的网络浏览器中,您会看到 Compute Engine 实例提供的一些 Hello World 文本。
管理和监控实例
您可以使用 Google Cloud 控制台来监控和管理您的实例。
-
如需查看 Compute Engine 资源生成的所有日志,请转到Logs Explorer页面。
前往 Logs ExplorerCloud Logging 会自动配置为从各种常用服务(包括
syslog)收集日志。
清除数据
为避免因本教程中使用的资源导致您的 Google Cloud 账号产生费用,请删除包含这些资源的项目,或者保留项目但删除各个资源。
删除项目
- 在 Google Cloud 控制台中,前往管理资源页面。
- 在项目列表中,选择要删除的项目,然后点击删除。
- 在对话框中输入项目 ID,然后点击关闭以删除项目。
逐个删除资源
gcloud compute instances delete my-app-instance --zone=YOUR_ZONE --delete-disks=all gcloud compute firewall-rules delete default-allow-http-80
后续步骤
探索有关 Google Cloud 的参考架构、图表和最佳做法。查看我们的 Cloud Architecture Center。
如需了解构建应用所需的其他 .NET 资源,请参阅以下内容:
- 转到 Compute Engine > 实例组以管理和监控实例组部署。
- 转到网络服务 > 负载平衡以管理负载平衡配置,包括网址映射和后端服务。
- 向 Google Kubernetes Engine 部署应用
- 了解其他Google Cloud 服务。