本教程介绍如何在 Compute Engine 上部署 .NET Framework Web 应用。
本教程面向对 Microsoft .NET 和 Compute Engine 有基本了解的开发者和 DevOps 工程师。
目标
将使用 .NET Framework 4 并在 Windows 上运行的 ASP.NET 模型-视图-控制器 (MVC) Web 应用部署到单个 Compute Engine 实例。
本教程介绍了如何完成以下任务来实现目标:
- 部署 Compute Engine 虚拟机
- 设置负载均衡
- 部署 ASP.NET 应用
费用
在本文档中,您将使用的以下收费组件: 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.
部署 Compute Engine 虚拟机
本部分介绍如何在 Compute Engine 上创建 Linux 虚拟机或运行 Microsoft Internet Information Services (IIS) Web 服务器 的 Windows Server 虚拟机。
为项目 ID 和 Compute Engine 地区设置默认值。这有助于您节省时间。
gcloud config set project PROJECT_ID gcloud config set compute/zone ZONE
替换以下内容:
- 将
PROJECT_ID替换为您的 Google Cloud 项目 ID。 - 将
ZONE替换为您将用于创建资源的区域的名称。如果不确定要选择哪个区域,请使用地理位置离您最近的区域。
例如:
gcloud config set project test-project-12345 gcloud config set compute/zone us-central1-a
- 将
创建一个虚拟机实例:
如需创建运行 IIS 的 Windows Server 虚拟机,请执行以下操作:
为虚拟机实例创建启动脚本 。此脚本将在虚拟机初始化期间运行并安装 IIS:
"# Install IIS Enable-WindowsOptionalFeature -Online -FeatureName `` NetFx4Extended-ASPNET45, `` IIS-WebServerRole, `` IIS-WebServer, `` IIS-CommonHttpFeatures, `` IIS-HttpErrors, `` IIS-HttpRedirect, `` IIS-ApplicationDevelopment, `` IIS-HealthAndDiagnostics, `` IIS-HttpLogging, `` IIS-LoggingLibraries, `` IIS-RequestMonitor, `` IIS-HttpTracing, `` IIS-Security, `` IIS-RequestFiltering, `` IIS-Performance, `` IIS-WebServerManagementTools, `` IIS-IIS6ManagementCompatibility, `` IIS-Metabase, `` IIS-DefaultDocument, `` IIS-ApplicationInit, `` IIS-NetFxExtensibility45, `` IIS-ISAPIExtensions, `` IIS-ISAPIFilter, `` IIS-ASPNET45, `` IIS-HttpCompressionStatic Install-WindowsFeature Web-Mgmt-Service # Install WebDeploy [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType] 'Ssl3,Tls12' (New-Object Net.WebClient).DownloadFile( 'https://download.microsoft.com/download/0/1/D/01DC28EA-638C-4A22-A57B-4CEF97755C6C/WebDeploy_amd64_en-US.msi', ""$env:Temp\webdeploy.msi"") & msiexec /qb! /i $env:Temp\webdeploy.msi | Out-Default " | Out-File -Encoding ASCII startup.ps1创建虚拟机实例并运行启动脚本
startup.ps1:gcloud compute instances create clouddemo-1 ` --image-family windows-2019 ` --image-project windows-cloud ` --machine-type n1-standard-2 ` --boot-disk-type pd-ssd ` --tags loadbalancer-backend ` --metadata-from-file sysprep-startup-script-ps1=startup.ps1
通过查看虚拟机的串行端口输出来监控其初始化过程:
gcloud compute instances tail-serial-port-output clouddemo-1
等待大约 5 分钟,直到看到输出
Instance setup finished或Startup finished,然后按 Ctrl+C。此时,必备项的安装已完成,虚拟机实例已准备就绪,可以使用了。
设置负载均衡
如需通过互联网提供 ASP.NET 应用,您必须使用 HTTPS 负载均衡器。如需将虚拟机实例 与负载均衡器相关联,请创建一个实例组 并将此实例组分配给负载均衡器:
创建非托管实例组 并添加虚拟机实例:
gcloud compute instance-groups unmanaged create clouddemo-1 gcloud compute instance-groups unmanaged add-instances clouddemo-1 --instances clouddemo-1
创建用于检查 Web 服务器是否正在运行的健康检查:
gcloud compute http-health-checks create clouddemo-health ` --check-interval 5s ` --unhealthy-threshold 2 ` --request-path / gcloud compute instance-groups set-named-ports clouddemo-1 --named-ports=http:80创建一个负载均衡器后端服务,该服务使用您之前创建的 HTTP 健康检查和实例组:
gcloud compute backend-services create clouddemo-backend ` --http-health-checks clouddemo-health ` --port-name http ` --protocol HTTP ` --global gcloud compute backend-services add-backend clouddemo-backend ` --instance-group clouddemo-1 ` --global ` --instance-group-zone $(gcloud config get-value compute/zone)
为负载均衡器创建前端:
gcloud compute url-maps create clouddemo-map --default-service clouddemo-backend gcloud compute target-http-proxies create clouddemo-proxy --url-map clouddemo-map gcloud compute forwarding-rules create clouddemo-frontend --global --target-http-proxy clouddemo-proxy --ports 80
创建一条 防火墙规则,使负载均衡器 将 HTTP 请求发送到已使用
loadbalancer-backend标记注释的实例。gcloud compute firewall-rules create loadbalancer-backend ` --source-ranges "130.211.0.0/22,35.191.0.0/16" ` --target-tags loadbalancer-backend ` --allow tcp:80,tcp:5000
查找负载均衡器的 IP 地址:
gcloud compute forwarding-rules describe clouddemo-frontend --global --format "value(IPAddress)"
记下 IP 地址。以便稍后使用。
部署 ASP.NET 应用
打开 PowerShell 控制台。
下载并解压缩 或从 github 克隆示例存储库:
git clone https://github.com/GoogleCloudPlatform/dotnet-docs-samples.git
构建部署包:
切换到包含示例应用的目录:
cd dotnet-docs-samples\applications\clouddemo\net4
恢复 NuGet 依赖项:
nuget restore
构建解决方案并使用
PackageProfile发布配置文件创建 WebDeploy 部署包:msbuild /t:clean,rebuild CloudDemo.Mvc.sln /p:DeployOnBuild=true /p:PublishProfile=PackageProfile
CloudDemo.Mvc\bin文件夹现在包含文件CloudDemo.Mvc.zip。
将部署软件包复制到虚拟机:
- 为虚拟机实例 创建用户名和密码。
- 使用远程桌面连接到虚拟机 然后使用上一步中创建的用户名和密码登录。
- 将文件
CloudDemo.Mvc.zip从CloudDemo.Mvc\bin文件夹复制到虚拟机实例上的临时位置。 - 在远程桌面会话中,右键点击开始 按钮(或按 Win+X ),然后点击 Windows PowerShell(管理员) 。
- 点击是 以确认提升权限提示。
部署部署包:
&"C:\Program Files\IIS\Microsoft Web Deploy V3\msdeploy.exe" -source:package=
PACKAGE-verb:sync -dest:auto将
PACKAGE替换为部署软件包的路径。
在本地计算机上,打开网络浏览器并前往以下地址:
http://
LOADBALANCER_IP/将
LOADBALANCER_IP替换为您在部署负载均衡器后获得的 IP 地址。您现在会看到演示应用以及标题“此应用在 Compute Engine 上运行”。
清理
完成本教程后,您可以清理您创建的资源,让它们停止使用配额,以免产生费用。以下部分介绍如何删除或关闭这些资源。
删除项目
若要避免产生费用,最简单的方法是删除您为本教程创建的项目。
删除项目: Google Cloud
gcloud projects delete PROJECT_ID
删除各个资源
您需要单独删除针对项目创建的所有资源(例如:实例组、健康检查、后端服务、HTTP 代理和转发规则)。在删除所有这些资源之前,您无法删除虚拟机实例。
后续步骤
- 详细了解如何在 Google 基础架构上创建和运行虚拟机。
- 查看 Google Cloud Well-Architected Framework 中的最佳实践。
- 探索有关 Google Cloud 的参考架构、图表和最佳实践。 查看我们的 Cloud 架构中心。