Scaling on Compute Engine with .NET

This tutorial shows how to add scaling features to the Bookshelf application in .NET.

This page is part of a multi-page tutorial. To start from the beginning and see instructions for setting up, go to .NET Bookshelf App.

The other sections in this tutorial showed how easy it is to get your application up and running on Google Cloud Platform (GCP) using a single Google Compute Engine VM instance. This approach works well for small applications. However, what happens when your application gets 1000 queries per second at noon, but only 10 queries per second at midnight? Compute Engine handles scenarios like this by using Instance Groups to automatically scale the number of instances running your application.

Horizontal scaling with multiple instances

Multiple-instance deployment with instance groups

In this step, you deploy the Bookshelf app to GCP in an autoscaled and load balanced configuration using instance groups. This tutorial step is optional and intended for users who want to explore deployment options available with GCP. This tutorial step demonstrates:

  • Horizontally scaling your application using Compute Engine instance groups.
  • Serving traffic using the Compute Engine HTTP(S) load balancer.
  • Responding to traffic changes using the Compute Engine autoscaler.

Overview of creating an Instance Group

Instance groups are a group of homogeneous instances based on the same instance template. An instance template defines the configuration of your instance, including the source image, the disk size, the scopes, and any metadata, including startup scripts.

Instance group creation from instance template

Before you can create the instance group, create a template. The instance group uses the template to create new instances.

Creating the Instance Group template

Create a VM image from your running Compute Engine instance. Because you can't create a VM image from a disk that's attached to an instance, start the image creation process by generating a snapshot of the existing instance running your ASP.NET application. This snapshot can then be used to create a new instance specifically for the purpose of generating a VM image.

Creating a Snapshot

In the Google Cloud Platform Console, in the project where you have deployed your ASP.NET application, go to the Compute Engine > Snapshots section and complete the following steps to create a snapshot:

  1. Click the Create snapshot button.
  2. Enter a Name for the snapshot.
  3. For the Source Disk, select the persistent disk that is attached to your ASP.NET VM instance.
  4. Click the Create button.

Snapshot creation form

Creating an instance and persistent disk for generating a VM image

In the Cloud Platform Console, in the project where you just created the snapshot, go to the Compute Engine > Instances section and complete the following steps to a create a new instance:

  1. Click the Create Instance button.
  2. Enter a Name for the VM instance.
  3. For the Boot Disk:
    1. Click the Change button.
    2. Select the Snapshot tab in the Boot Disk dialog.
    3. Click the name of the snapshot you just created and click the Select button.
  4. In the Identity and API access section:
  5. Select the Set access for each API radio button.
  6. Set the following values for these APIs:
    • Set Cloud Datastore to "Enabled"
    • Set Storage to "Read Write"
    • Set User Info to "Enabled"
  7. In the Firewall section, select the Allow HTTP traffic and the Allow HTTPS traffic checkboxes.
  8. Click Management, disk, networking, SSH keys and then click the Disks tab.
  9. Under the Deletion rule setting, deselect the Delete boot disk when instance is deleted checkbox.
  10. Click the Create button at the bottom of the page to create the instance.

VM instance creation form

Preparing the persistent disk for VM image creation

To create a VM image from the VM instance and persistent disk you just created, you must first perform the following steps to prepare the instance's persistent disk for image creation. In the Cloud Platform Console, in the project where you just created the VM Instance, go to the Compute Engine Instances section:

  1. Click the name of the newly created VM Instance to open the instance's details page.
  2. On the instance details page, click Create or reset Windows password.
  3. In the Set new Windows password dialog, add your username, and then click Set to create the user account on your instance.
  4. Make a note of the provided password, and close the dialog.
  5. Click the RDP button next to the VM instance you just created to start a remote desktop connection to the newly created VM instance.
  6. Sign in with the username and provided password you noted in step 4.
  7. When connected to the instance using RDP, right-click the Powershell icon in the menu bar at the bottom of the screen.

    Select Powershell Icon

  8. Select Run as Administrator. If this option isn't visible, wait another minute to allow the VM's login process to fully complete, then try again.

    Open Powershell as an Administrator

  9. A User Account Control dialog window opens asking if you want to allow Powershell to make changes to the computer. Click Yes.

  10. In the Powershell window, run the following command, which prepares the VM's persistent disk for imaging. This closes the remote desktop connection and shuts down the instance:

    GCESysprep
    
  11. Close the RDP window and return to the Compute Engine > Instances section

  12. Click the checkbox next to the name of the instance, which is now shut down, and then click the Delete button at the top of the page to delete the instance. This operation leaves only its detached persistent disk, ready for imaging.

Creating a VM image from a detached persistent disk

In the Cloud Platform Console, go to the Compute Engine > Images section and complete the following steps to create a VM image:

  1. Click the Create Image button.
  2. Enter a Name for VM image.
  3. For the Source Disk, select the detached persistent disk you just created.
  4. Click Create.

VM Image creation form

Creating an instance group template with a VM image

In the Cloud Platform Console, go to the Compute Engine > Instance templates section and complete the following steps to create an instance group template:

  1. Click the Create instance template button.
  2. Enter a Name for the Instance template.
  3. For the Boot Disk:
  4. Click the Change button
  5. Select the Your image tab in the Boot Disk dialog window.
  6. Click the name of the image you just created and click the Select button.
  7. In the Identity and API access section:
  8. Select the Set access for each API radio button.
  9. Set the following values for these APIs:
    • Set Cloud Datastore to "Enabled".
    • Set Storage to "Read Write".
    • Set User Info to "Enabled".
  10. In the Firewall section select the Allow HTTP traffic and Allow HTTPS traffic checkboxes.
  11. Click Create to create the instance template.

Instance template creation form

Creating an instance group with an instance group template

In the Cloud Platform Console, go to the Compute Engine > Instance groups section and complete the following steps to create an instance group:

  1. Click Create Instance Group.
  2. Enter a Name for the instance group.
  3. For Instance definition select the name of the instance template you just created.
  4. Set Autoscaling to "On".
  5. For Autoscale based on select "HTTP load balancing usage".
  6. Set the Cool-down period to 410 seconds.

    This setting tells the autoscaler how many seconds to wait after a new instance has started before the autoscaler starts collecting usage information. This should be at least the time it takes to initialize a new instance. For your app, the cool-down period could be more or less depending on the machine type and the app being run. Test creating a new instance from your custom image to determine the optimal amount of time to specify for the cool-down period.

  7. For the Health check section:

  8. Select the Create a Health check option.
  9. Enter a Name for the health check.
  10. Set the Request path to "/Books", which is the ASP.NET Bookshelf sample app's default path.
  11. Set Check interval to 30 seconds. This setting controls how often the health check runs.
  12. Set Timeout to 10 seconds. This setting specifies the amount of time to wait before a health check is considered to be unsuccessful.
  13. Click Save to create the health check.
  14. Set the Initial delay to 400 seconds.

    This value specifies the length of the period during which the instance is known to be initializing. This includes the time from when the instance first starts up to when the instance has booted up its operating system and other software required to start serving the app. Test creating a new instance from your custom image to determine the optimal about of time to specify for the initial delay period.

  15. Click Save to create the instance group.

  16. A dialog displays the message "Autoscaling configuration is not complete" because you still have to create and configure a load balancer. You will set up the load balancer in the next section, so click OK to continue with the creation of the instance group.

Instance group creation form

Creating an HTTP(S) load balancer for an instance group

Next, create and configure a load balancer to increase or decrease the number of running instances in the instance group based on the number of HTTP requests sent to your application.

In the Cloud Platform Console, go to the Networking > Load balancing section and complete the following steps to create a load balancer for your newly created instance group:

  1. Click Create load balancer.
  2. Click Start configuration in the HTTP(S) Load Balancing option.
  3. Enter a Name for the load balancer.
  4. Click the Backend configuration heading.
  5. Click the Create or select a backend service drop-down menu and select "Create a backend service".
  6. Enter a Name for the backend service.
  7. Under New Backend:
    • For Instance group, select the instance group you just created.
    • Set the Balancing mode to "Rate".
    • Set the Maximum RPS to 100 requests per second per instance.
  8. Click the Health check drop-down menu and select the same health check you created when you created the instance group.
  9. Click the Session affinity drop-down menu and select "Generated cookie".

    This setting will generate a cookie for each visitor to the application and then the load balancer will send all requests with that cookie to the same instance in the instance group. This feature is currently in Beta release. See the session affinity documentation for details.

  10. Click the Host and path rules heading. The default setting is to accept all requests made to the load balancer and send them to the backend you just created. No further changes are required for this setting.

  11. Click the Frontend configuration heading. The default settings are "HTTP" for Protocol, "Ephemeral" for IP and 80 for the Port. No further changes are required for these settings in order to run the Bookshelf sample application. If you would prefer to reserve a static IP address, then click the IP drop-down menu and select "Create IP Address" to create an external IP address for your load balancer that will not change.
  12. Click Review and finalize to review the configuration of the load balancer that will be created.
  13. Click Create to create the HTTP(S) load balancer.

Load Balancer creation details

Viewing your application

When the load balancer creation is complete, find the newly created load balancer listed in the Cloud Platform Console Networking > Load balancing section. Click the name of the load balancer to view its configuration details. Copy the IP address listed in the Frontend details under the IP:Port column heading. In a browser, browse to the IP address you just copied.

Your app is now running on the load-balanced, autoscaled, Cloud Platform!

Cleaning up

To avoid incurring charges to your Google Cloud Platform account for the resources used in this tutorial:

Delete the project

The easiest way to eliminate billing is to delete the project you created for the tutorial.

To delete the project:

  1. In the Cloud Platform Console, go to the Projects page.

    Go to the Projects page

  2. In the project list, select the project you want to delete and click Delete project. After selecting the checkbox next to the project name, click
      Delete project
  3. In the dialog, type the project ID, and then click Shut down to delete the project.

Delete your Compute Engine instances

To delete a Compute Engine instance:

  1. In the Cloud Platform Console, go to the VM Instances page.

    Go to the VM Instances page

  2. Click the checkbox next to the instance you want to delete.
  3. Click the Delete button at the top of the page to delete the instance.

Delete your Cloud SQL instance

To delete a Cloud SQL instance:

  1. In the Cloud Platform Console, go to the SQL Instances page.

    Go to the SQL Instances page

  2. Click the name of the SQL instance you want to delete.
  3. Click the Delete button at the top of the page to delete the instance.

Delete your Cloud Storage bucket

To delete a Cloud Storage bucket:

  1. In the Cloud Platform Console, go to the Cloud Storage browser.

    Go to the Cloud Storage browser

  2. Click the checkbox next to the bucket you want to delete.
  3. Click the Delete button at the top of the page to delete the bucket.

Delete your SQL Server instance

To delete a SQL Server instance:

  1. In the Cloud Platform Console, go to the VM Instances page.

    Go to the VM Instances page

  2. Select the checkbox next to the name of SQL Server instance you want to delete.

  3. Click the Delete button at the top of the page.

Delete your instance groups

To delete a Compute Engine instance group:

  1. In the Cloud Platform Console, go to the Instance Groups page.

    Go to the Instance Groups page

  2. Select the checkbox next to the name of the instance group you want to delete.

  3. Click the Delete button at the top of the page.

Delete your load balancer

To delete a load balancer:

  1. In the Cloud Platform Console, go to the Load Balancing page.

    Go to the Load Balancing page

  2. Click the trash can button next to the load balancer you want to delete.

  3. In the Delete Load Balancer dialog, select the associated backend service, listed in the "Backend services" section, to also delete the backend service.

  4. Click the Delete load balancer and the selected resources button.

Delete your instance templates

To delete an instance template:

  1. In the Cloud Platform Console, go to the Instance Templates page.

    Go to the Instance Templates page

  2. Select the checkbox next to the name of the instance template you want to delete.

  3. Click the Delete button at the top of the page.

Delete your images

To delete an image:

  1. In the Cloud Platform Console, go to the Images page.

    Go to the Images page

  2. Select the checkbox next to the name of the image you want to delete.

  3. Click the Delete button at the top of the page.

Delete your persistent disks

To delete a persistent disk:

  1. In the Cloud Platform Console, go to the Disks page.

    Go to the Disks page

  2. Select the checkbox next to the name of the disk you want to delete.

  3. Click the Delete button at the top of the page.

Delete your snapshots

To delete a snapshot:

  1. In the Cloud Platform Console, go to the Snapshots page.

    Go to the Snapshots page

  2. Select the checkbox next to the name of the snapshot you want to delete.

  3. Click the Delete button at the top of the page.

What's next

Try out other Google Cloud Platform features for yourself. Have a look at our tutorials.

Send feedback about...