GCP - Autoscale - A Simple Approach

Author: Brian Nettles | Date Created: June 7, 2018 | Visits: 147

This tutorial will walk you through setting up a website on Google Cloud Platform that will scale automatically with the demand of the users. I am using a Debian instance with Apache and and a few other necessary packages installed.

The first thing you need to do is to create a server instance in Google Compute Engine. I am using Debian. You need to prepare this instance to prepare this instance to be a template for multiple instances. The database cannot be on this instance. All state information should also not be on the instance.

When creating the instance, make certain your checkbox is not checked for "Delete book disk when instance is deleted". If it is checked and you already created the instance, then edit the instance to uncheck it.
do not delete boot disk image

For the startup script, I followed this advice which I got off of Stack Overflow. It is one of three ways I am aware of to get startup scripts working in this cloud environment. You can optionally define a startup script in the custom metadata section of the instance using the key value pair of - startup-script-url : url in Cloud Storage of a pre-created bash script you want run. You can also use the custom metadata section to define using the key of startup-script and the path within your VM as the value for the startup script.

Copy the python file to /bin:
sudo cp -i /path/to/your_script /bin

Add a new cron job:
sudo crontab -e

scroll to the bottom and add the line:
@reboot python3 /bin/yourscript.py &
So go ahead and test your script by rebooting the machine and making certain Apache properly restarts and all other server specific requirements are in place. Once you are determined that your startup process is good to go and your app is running, now you can get started with the Autoscaling functionilty all done from the GCP Web Panel.

Step 1: Delete the instance.
It is okay to delete the instance. What you need is the disk of the instance. Ensuring the Delete Boot Disk is not checked, go ahead and delete your instance.

Step 2: Snapshot your disk.
Go in the Disks section of Compute Engine. Create the snapshot. This is not a necessary step. Just good practice.

Step 3: Create an image of your disk.

Step 4: Create an instance template. Do not use the default disk. Here you need to change the boot disk. Select from the custom images the image you just created.

Step 5: Create an instance group. The instance group is where you define health checks, thresholds for the health checks, and maximum and minimum instances. Once created, it will immediately span one or more instances into Compute Engine and you will see the instances along with all of your other Compute Engine instances. The multi-zone selection causes the instances to be spread out in different zones within the selected region. This functionality adds more fault tolerance.

Step 6: Add Load Balancing. Go to Network Services -> Load Balancing -> Create Load Balancer -> HTTP(S) Load Balancing -> Start Configuration.

Configure the backend.

Configure the host and path rule.

Configure the frontend. You may need to create a static IP address from the VPC Network section.

Save your load balancer and determine your IP address from the configurations. It may be a good idea to get a static ip address configured here. It is not hard to figure out. And of course, update your DNS.

Now give it about 5 or 10 minutes before you test. It never seems to work immediately. It is not because of the DNS, but it is the load balancer that seems to need some time to get working. In 10 minutes, it should be fine.

Trisummit Technologies
6421 N. Lena Place
Tucson, AZ 85741
© 2018 All Rights Reserved