Use a Load Balancer on Google Cloud to Host Web Applications
This article was published as a part of the Data Science Blogathon.
What is a Load Balancer, and why do we need it?
Load Balancer is a must component when we want to scale our systems horizontally. Horizontal scaling means the addition of extra servers and machines to the existing infrastructure so that it can cope with the new demands. So there is a requirement for an infrastructure that can distribute the load between these servers, so Load Balancer (LB) comes into the picture.
Load Balancer on works between the client-server. It takes requests from the client and distributes these requests to the server using various algorithms. It reduces the load on individual systems and also improves fault tolerance.
Nowadays, a load balancer has become a critical infrastructure. Every company wants more reliable systems, fewer downtimes, and more availability and responsiveness. Load Balancer also keeps track of the active and healthy servers. If one server fails, the Load Balancer stops sending requests to that server. It also automatically adds or removes the servers according to the demand.
This tutorial shows how we can implement these Load Balancers practically.
Creating VM Instance Templates
1. Log in to Google Cloud Platform.
2. Create a new project inside it.
Note: You need a billing account to use a Load Balancer as it is a paid service.
3. Navigate to Compute Engine >> Instance templates
4. Click on Create Instance Template button
5. Name your instance template and choose the configurations for your VM.
Note: You can also see your monthly cost of the VM according to your configurations.
6. Select the Disk Size.
7. Choose your Service Account, or you can choose the default one also. Also, allow both HTTP and HTTP(s) traffic.
We are now ready to create Instance Groups using these Instance Templates.
Creating Instance Groups
In this section, we will create a group of servers. A Load Balancer requires that all instances in the network be from the same group.
1. Navigate to Compute Engine >> Instance Groups
2. Click on Create Instance Group button
3. Go to New managed instance group(stateless)
- Enter the name of your instance group
- Select the instance template that we created earlier.
- Select the region of the servers. You can also select multiple zones to keep different servers at different locations.
4. Keep the autoscaling mode as off. Autoscaling can automatically add or remove servers depending on the load condition. But we keep it off and only use a fixed amount of servers.
5. Select the number of instances that you want to use. For this tutorial, we keep them as two.
6. Select the Port Name and Port Numbers you want your application to listen to. Finally, click on Create button.
7. Your instance group is now created, having two running instances.
Creating a Load Balancer
There are three types of Load Balancers-
HTTP(s) is best to divide the traffic of a web application.
1. Navigate to Network Services >> Load Balancer
2. Click on Create Load Balancer button
3. Choose HTTP(s) Load Balancing service.
4. Keep the default settings and click on Continue.
1. Backend Configuration
Name your load balancer, and navigate to Create a Backend Service.
Enter the name of your backend service, and choose backend type as Instance group.
Select the instance group we created earlier and enter the same port number you have previously entered in your instance group (Port Number can also appear automatically).
2. Creating Health Checks
Health checks ensure that the instances are correctly working or not. It periodically sends requests to our instances and checks whether the backend is responding or not.
Enter the Name of your Health Check and select HTTP Protocol with the Port that you have chosen earlier.
Keep these settings as it is and click on Save.
Now, select your newly created Health Check and click on Create button to complete your backend.
Leave the Frontend Configurations as it is and click on Create button.
Your Load Balancer is now ready to use
You get your Load Balancer’s Frontend IP address. When you hit that IP address, the load balancer can internally divide the request between the two instances. You may also link the Load Balancer’s IP address to a domain name.
Hurray🎉, your Load Balancer is finally created. Now you can host your web applications on the two instances.
One of Google Cloud’s functions is the Load Balancer. We learned how to establish a Load Balancer in the Google Cloud Platform in this lesson. You may also experiment with different services such as Vision AI, Cloud Storage, Content Delivery Networks, Kubernetes Engine, etc.
A dedicated load balancer like this will cost you money. Still, you can also set up web servers like Nginx and Apache, providing load balancing and a reverse proxy. They aid in enhancing the performance, scalability, and stability of your online applications, and they are open source and free to use.
Some of the key takeaways are as follows:
1. Firstly, we create a template of the VM Instance on Google Cloud, so we don’t have to fill up the configurations of the VM again and again.
2. We have created a group of instances, load balancer only works within the group.
3. Then, we set up the backend configurations of the VM and also created health checks.
4. Finally, we can use our load balancer by hitting its frontend IP address. The load balancer would automatically distribute the load among all the servers in that particular group.
I hope you have enjoyed my article on Google Cloud. If you have any doubts or suggestions, feel free to comment below. Or you can also connect with me on LinkedIn. I will be delighted to get associated with you. You can check my other articles also.
The media shown in this article is not owned by Analytics Vidhya and is used at the Author’s discretion.