Back when we were managing a huge marquee campaign site on Google Cloud, we had to make sure that the web layer can scale automagically to withstand sudden spikes in traffic due to marketing campaigns in twitter and TV channels etc. A step by step guide to autoscaling for AWS are dime a dozen but we couldn't find similar articles for Google Cloud. But Google Cloud's documentation is pretty awesome, next only to that of AWS. Since we already configured autoscaling for a challenging site and have seen it in action, we thought documenting it would help someone in future. What follows is a series of screenshots with minimum commentary that explains how to configure autoscaling in Google cloud.
Prepare an instance for Image
Autoscaler requires an image to be available from which it can spin instances dynamically when certain conditions are met. In this example, we will consider a simple Apache/PHP server for autoscaling and prepare an image accordingly.
- Launch an instance type of your preference
- Install Apache, PHP and other necessary packages
- Deploy code and test if it works
- Add startup scripts if you need your instance to do certain tasks during startup. Example, you might want to pull the latest code from a git repository and deploy during startup.
Once all of these is done, go to instance settings and disable
Delete Boot Disk option when the instance is deleted. Yup, we are going to delete this instance and make an image out of it.
Now delete this instance without deleting the boot disk that powers it.
Create a new image from this disk.
Check if the image is created.
Create An Instance Template
So far we have a boot disk from which we can spin instances, but Google Cloud doesn't know what kind of instances you want to spin. What size, what type? This is where Instance Templates step in. We need to create an instance template according to our needs.
In GCE console, go to
Instance templates page. Click on
Create a New Instance Template. Populate the following information for instance template:
- Machine type
- Boot disk (this is the one we created in the previous step)
- Allow traffic. HTTP/HTTPS or something else, according to your needs.
- Allow API access if you want to communicate with other Google Cloud Services
- Create the template
Create an Instance Group
Google Cloud has instance groups that are either managed or unmanaged groups. In the case of Compute Engine, we need managed instance groups that have common characteristics and can scale depending upon certain conditions. An instance group depends on an instance template and we took care of the template already. So lets create the instance group now
Create a loadbalancer
Scaling out generally depends on loadbalancing and we need one for this. So lets go ahead and configure one. Google Compute's loadbalancer is orders of magnitudes advanced compared to the basic AWS ELB or Azure loadbalancer. More about this later. For the sake of this article, I will go ahead with a Network Loadbalancer -
That's about it. Your application will now scale up and down depending upon how you configured it. Hope this was useful!