Scaling on Compute Engine with .NET

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

This page is part of a multi-page tutorial. To start from the beginning and read the setup instructions, go to .NET Bookshelf app.

The other sections in this tutorial showed how to get your app up and running on Google Cloud Platform (GCP) using a single Compute Engine VM instance. This approach works well for small apps. However, what happens when your app 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 app.

Horizontal scaling with multiple instances

The following diagram illustrates how a multiple-instance deployment is grouped into instance groups in Compute Engine.

Multiple-instance deployment with instance groups

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

  • Horizontally scaling your app 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

An instance group is 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 app. You can use this snapshot to create a new instance specifically for the purpose of generating a VM image.

  1. In the Google Cloud Platform Console, in the project where you deployed your ASP.NET app, go to the Snapshots page.

    GO TO SNAPSHOTS

  2. Click Create snapshot.

  3. Enter a Name for the snapshot.

  4. From the Source Disk drop-down list, select the persistent disk that is attached to your ASP.NET VM instance.

  5. Click Create.

    Snapshot creation form

Creating an instance and persistent disk for generating a VM image

  1. In the GCP Console, in the project where you created the snapshot, go to the Instances page.

    GO TO INSTANCES

  2. Click Create Instance.

  3. Enter a Name for the VM instance.

  4. For the Boot Disk section, complete the following steps:

    1. Click Change.
    2. On the Snapshot tab, click the name of the snapshot you created, and then click Select.
  5. Click Set access for each API.

  6. From the Cloud Datastore drop-down list, select Enabled.

  7. From the Storage drop-down list, select Read Write.

  8. From the User Info drop-down list, select Enabled.

  9. In the Firewall section, select the Allow HTTP traffic and the Allow HTTPS traffic checkboxes.

  10. Click Management, security, disks, networking, sole tenancy, and then click the Disks tab.

  11. Under the Deletion rule setting, clear the Delete boot disk when instance is deleted checkbox.

  12. To create the instance, at the bottom of the page, click Create.

    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 created, you must first prepare the instance's persistent disk for image creation.

  1. In the GCP Console, in the project where you created the VM instance, go to the Instances page.

    GO TO INSTANCES

  2. Click the name of the newly created VM instance.

  3. In the Instance Details window, click Set Windows password.

  4. To create the user account on your instance, in the Set new Windows password dialog, add your username, and then click Set.

  5. Make a note of the provided password, and then close the dialog.

  6. To start a remote desktop connection to the newly created VM instance, click RDP next to the VM instance you created.

  7. Sign in with the username and provided password.

  8. When connected to the instance using RDP, right-click the PowerShell icon, and then select Run as Administrator. If this option isn't visible, wait another minute to allow the VM's login process to fully complete, and then try again.

    Open PowerShell as an Admin

  9. In the User Account Control dialog window that opens asking if you want to allow PowerShell to make changes to the computer, click Yes.

  10. To prepare the VM's persistent disk for imaging, in the PowerShell window, run the following command. This command closes the remote desktop connection and shuts down the instance.

    GCESysprep
    
  11. Close the RDP window and return to the Instances page.

    GO TO INSTANCES

  12. To delete the instance, select the name of the shut down instance checkbox, and then click Delete. This operation leaves only its detached persistent disk, ready for imaging.

Creating a VM image from a detached persistent disk

  1. In the GCP Console, go to the Images page.

    GO TO IMAGES

  2. Click Create Image.

  3. Enter a Name for VM image.

  4. From the Source Disk drop-down list, select the detached persistent disk you created.

  5. Click Create.

    VM Image creation form

Creating an instance group template with a VM image

  1. In the GCP Console, go to the Instance templates page.

    GO TO INSTANCE TEMPLATES

  2. Click Create instance template.

  3. Enter a Name for the instance template.

  4. For the Boot Disk section, complete the following steps:

    1. Click Change.
    2. On the Your image tab, click the name of the image you created, and then click Select.
  5. Click Set access for each API.

  6. From the Cloud Datastore drop-down list, select Enabled.

  7. From the Storage drop-down list, select Read Write.

  8. From the User Info drop-down list, select Enabled.

  9. In the Firewall section, select the Allow HTTP traffic and Allow HTTPS traffic checkboxes.

  10. To create the instance template, click Create.

    Instance template creation form

Creating an instance group with an instance group template

  1. In the GCP Console, go to the Instance groups page.

    GO TO INSTANCE GROUPS

  2. Click Create Instance Group.

  3. Enter a Name for the instance group.

  4. In the Creation method section, click Use instance template, and then from the instance template drop-down list, select the name of the instance template you created.

  5. From the Autoscaling drop-down list, select On.

  6. From the Autoscale based on drop-down list, select HTTP load balancing usage.

  7. In the Cool-down period field, enter 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 you're running. Test creating a new instance from your custom image to determine the optimal amount of time to specify for the cool-down period.

  8. From the Health check drop-down list, select Create a Health check and complete the following steps:

    1. Enter a Name for the health check.
    2. In the Request path field, enter /Books, which is the ASP.NET Bookshelf sample app's default path.
    3. To control how often the health check runs, in the Check interval field, enter 30 seconds.
    4. To specify the amount of time to wait before a health check is considered to be unsuccessful, in the Timeout field, enter 10 seconds.
    5. To create the health check, click Save.
  9. In the Initial delay field, enter 400 seconds.

    This value specifies the length of the period during which the instance is 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 amout of time to specify for the initial delay period.

  10. Leave all other fields at their default values. To create the new instance group, click Save.

  11. A dialog displays the message "Autoscaling configuration is not complete" because you still have to create and configure a load balancer. This steps is completed 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 app.

  1. In the GCP Console, go to the Load balancing page.

    GO TO LOAD BALANCING

  2. Click Create load balancer.

  3. For HTTP(S) Load Balancing, click Start configuration.

  4. Enter a Name for the load balancer.

  5. Click Backend configuration, and complete the following steps:

    1. From the Create or select a backend service drop-down list, select Create a backend service.
    2. Enter a Name for the backend service.
    3. From the Instance group drop-down list, select the instance group you created.
    4. From the Balancing mode drop-down list, select Rate.
    5. From the Maximum RPS drop-down list, select 100 requests per second per instance.
    6. From the Health check drop-down list, select the same health check you created when you created the instance group.
    7. From the Session affinity drop-down list, select Generated cookie.

      This setting generates a cookie for each visitor to the app and then the load balancer sends 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.

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

  7. Click Frontend configuration. To run the Bookshelf sample app, no further changes are required. If you would prefer to reserve a static IP address, then from the IP drop-down list, select Create IP Address to create an external IP address for your load balancer that doesn't change.

  8. To review the configuration of the load balance, click Review and finalize.

  9. To create the HTTP(S) load balancer, click Create.

    Load Balancer creation details

Viewing your app

When you finish creating the load balancer, the newly created load balancer is listed in the GCP Console.

  1. Go to the Load balancing page.

    GO TO LOAD BALANCING

  2. To view the configuration details, click the name of the load balancer.

  3. In the Frontend details under the IP:Port column heading, copy the listed IP address.

  4. In a browser, browse to the IP address you copied.

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

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 GCP 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 .
  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 GCP Console, go to the VM Instances page.

    Go to the VM Instances page

  2. Click the checkbox for the instance you want to delete.
  3. Click Delete to delete the instance.

Delete your Cloud SQL instance

To delete a Cloud SQL instance:

  1. In the GCP 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 Delete to delete the instance.

Delete your Cloud Storage bucket

To delete a Cloud Storage bucket:

  1. In the GCP Console, go to the Cloud Storage Browser page.

    Go to the Cloud Storage Browser page

  2. Click the checkbox for the bucket you want to delete.
  3. Click Delete to delete the bucket.

Delete your SQL Server instance

To delete a SQL Server instance:

  1. In the GCP 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

  1. In the GCP Console, go to the Instance groups page.

    Go to the Instance groups page

  2. Click the checkbox for the instance group you want to delete.
  3. Click Delete to delete the instance group.

Delete your load balancer

To delete a load balancer:

  1. In the GCP Console, go to the Load Balancing page.

    Go to the Load Balancing page

  2. Click the Delete 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 Delete load balancer and the selected resources.

Delete your instance templates

To delete an instance template:

  1. In the GCP 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 GCP 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

  1. In the GCP Console, go to the Disks page.

    Go to the Disks page

  2. Click the checkbox for the disk you want to delete.
  3. Click Delete to delete the disk.

Delete your snapshots

To delete a snapshot:

  1. In the GCP 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.

Var denne side nyttig? Giv os en anmeldelse af den: