This tutorial is intended for developers and DevOps engineers who have basic knowledge of Microsoft .NET and Compute Engine.
Objectives
Deploy a sample app to a single Compute Engine instance. For the purpose of this tutorial, you can select between two example applications:
- An ASP.NET Core web application that uses .NET 6 and runs on Linux
- An ASP.NET Model-View-Controller (MVC) web-application that uses .NET Framework 4 and runs on Windows
This tutorial shows you how to complete the following tasks to reach your objective:
- Deploy a Compute Engine VM
- Set up load balancing
- Deploy the ASP.NET application
Costs
In this document, you use the following billable components of Google Cloud:
To generate a cost estimate based on your projected usage,
use the pricing calculator.
When you finish this tutorial, you can avoid continued billing by deleting the resources you created. For more information, see Clean up.
Before you begin
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project. Learn how to check if billing is enabled on a project.
-
Enable the Compute Engine API.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project. Learn how to check if billing is enabled on a project.
-
Enable the Compute Engine API.
Deploy a Compute Engine VM
This section shows you how to create a Linux VM, or a Windows Server VM that runs Microsoft Internet Information Services (IIS) web servers on Compute Engine.
Set default values for your project ID and Compute Engine zone. This helps you save time.
gcloud config set project PROJECT_ID gcloud config set compute/zone ZONE
Replace the following:
PROJECT_ID
with the ID of your Google Cloud project.ZONE
with the name of the zone that you're going to use for creating resources. If you are unsure about which zone to pick, use the zone geographically located closest to you.
For example:
gcloud config set project test-project-12345 gcloud config set compute/zone us-central1-a
Create a VM instance:
.NET/Linux
To create a Linux VM, do the following:
Create a startup script for the VM instance. The script runs during VM initialization and install the .NET runtime:
"if ! dpkg-query -W aspnetcore-runtime-6.0 then curl https://packages.microsoft.com/config/debian/11/packages-microsoft-prod.deb -O sudo dpkg -i packages-microsoft-prod.deb rm packages-microsoft-prod.deb sudo apt-get update && sudo apt-get install -y aspnetcore-runtime-6.0 fi " | Out-File -Encoding ASCII startup.sh
Create the VM instance and use
startup.sh
as startup script:gcloud compute instances create clouddemo-1 ` --image-family debian-11 ` --image-project debian-cloud ` --machine-type n1-standard-2 ` --boot-disk-type pd-ssd ` --tags loadbalancer-backend ` --metadata-from-file startup-script=startup.sh
.NET Framework/Windows
To create a Windows Server VM that runs IIS, do the following:
Create a startup script for the VM instance. This script will run during VM initialization and install 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
Create the VM instance and run the startup script
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
Monitor the initialization process of the VM by viewing its serial port output:
gcloud compute instances tail-serial-port-output clouddemo-1
Wait about 5 minutes until you see the output
Instance setup finished
orStartup finished
, and then press Ctrl+C. At this point, the installation of prerequisites is complete and the VM instance is ready to be used.
Set up load balancing
To make your ASP.NET app available over the internet, you have to use an HTTPS load balancer. To associate your VM instance with the load balancer, create an instance group and assign this instance group to the load balancer:
Create an unmanaged instance group and add the VM instance:
gcloud compute instance-groups unmanaged create clouddemo-1 gcloud compute instance-groups unmanaged add-instances clouddemo-1 --instances clouddemo-1
Create a health check that checks if the web server is running:
.NET/Linux
gcloud compute http-health-checks create clouddemo-health ` --check-interval 5s ` --unhealthy-threshold 2 ` --request-path / ` --port 5000 gcloud compute instance-groups set-named-ports clouddemo-1 --named-ports=http:5000
.NET Framework/Windows
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
Create a load balancer backend service that uses the HTTP health check and the instance group that you created previously:
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)
Create a front-end for the load balancer:
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
Create a firewall rule that allows the load balancer to send HTTP requests to instances that have been annotated with the
loadbalancer-backend
tag.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
Lookup the IP address of the load balancer:
gcloud compute forwarding-rules describe clouddemo-frontend --global --format "value(IPAddress)"
Take note of the IP address. You will need it later.
Deploy the ASP.NET application
Open a PowerShell console.
Download and unzip or clone the sample repository from github:
git clone https://github.com/GoogleCloudPlatform/dotnet-docs-samples.git
Build the deployment package:
.NET/Linux
Switch to the directory that contains the sample application:
cd dotnet-docs-samples\applications\clouddemo\netcore
Build the solution:
dotnet publish -c Release
.NET Framework/Windows
Switch to the directory that contains the sample application:
cd dotnet-docs-samples\applications\clouddemo\net4
Restore NuGet dependencies:
nuget restore
Build the solution and use the
PackageProfile
publishing profile to create a WebDeploy deployment package:msbuild /t:clean,rebuild CloudDemo.Mvc.sln /p:DeployOnBuild=true /p:PublishProfile=PackageProfile
The
CloudDemo.Mvc\bin
folder now contains a fileCloudDemo.Mvc.zip
.
Copy the deployment package to the VM:
.NET/Linux
Copy the contents of the
publish
folder to your home directory on the VM:gcloud compute scp --recurse CloudDemo.MvcCore\bin\Release\net6.0\publish clouddemo-1:
On the VM, create a folder
/var/www/clouddemo
and copy the application files into this folder:sudo mkdir -p /var/www/clouddemo sudo chown -R www-data:www-data /var/www/clouddemo sudo cp -r publish/* /var/www/clouddemo
Register the application as a systemd unit:
cat <<EOF > kestrel-clouddemo.service [Unit] Description=Cloud Demo ASP.NET app [Service] WorkingDirectory=/var/www/clouddemo ExecStart=/usr/bin/dotnet /var/www/clouddemo/CloudDemo.MvcCore.dll Restart=always Environment=ASPNETCORE_ENVIRONMENT=Production Environment=ASPNETCORE_URLS=http://0.0.0.0:5000 [Install] WantedBy=multi-user.target EOF sudo mv kestrel-clouddemo.service /etc/systemd/system/ sudo systemctl daemon-reload sudo systemctl start kestrel-clouddemo
.NET Framework/Windows
- Create a username and password for the VM instance.
- Connect to the VM by using Remote Desktop and log in using the username and password created in the previous step.
- Copy the file
CloudDemo.Mvc.zip
from theCloudDemo.Mvc\bin
folder to a temporary location on the VM instance. - In the Remote Desktop session, right-click on the Start button (or press Win+X) and click Windows PowerShell (Admin).
- Confirm the elevation prompt by clicking Yes.
Deploy the deployment package:
&"C:\Program Files\IIS\Microsoft Web Deploy V3\msdeploy.exe" -source:package=
PACKAGE
-verb:sync -dest:autoReplace
PACKAGE
with the path to the deployment package.
On your local computer, open a web browser and navigate to the following address:
http://
LOADBALANCER_IP
/Replace
LOADBALANCER_IP
with the IP address that you obtained after deploying the load balancer.You now see the demo application and the title This app is running on Compute Engine.
Clean up
After you finish the tutorial, you can clean up the resources that you created so that they stop using quota and incurring charges. The following sections describe how to delete or turn off these resources.
Delete the project
The easiest way to eliminate billing is to delete the project that you created for the tutorial.
Delete a Google Cloud project:
gcloud projects delete PROJECT_ID
Delete individual resources
You'll need to individually delete all the resources created for the project (for example: instance groups, health checks, backend services, http proxy, and forwarding rules). You can't delete the VM instances until delete all these resources.
What's next
- Learn more about creating and running virtual machines on Google infrastructure.
- Review the best practices in the Google Cloud Architecture Framework.
- Explore reference architectures, diagrams, and best practices about Google Cloud. Take a look at our Cloud Architecture Center.