Clustering Web Servers Using ELB

In the previous article Introduction to AWS ELB – Part 2 we learned about load balancer types, and their ports and scaling. Here in this article we are going to learn and cover the below topics:
  • Creating AMI of web server.
  • Launch another web server using AMI.
  • Clustering Web servers using ELB
    •  Creating Target group.
    •  Registering instances to target group.
    •  Creating Application Load Balancer.
    • Creating security group for the load balancer.
    • Setting up routing for the load balancer.
    • Checking status of instances in Target Group.
    • Configuring security group of web servers.
So, let’s get started.
 
Here we are going to learn the clustering of web services, and as we saw in one of the previous articles on ELB “Introduction To AWS ELB” , we have created one web server card-web01 which was running successfully, but what if this instance goes down?
 
 
 
So, for that we would like to cluster it and thus we need a minimum of 2 instances (multiple instances) exactly like we created and set up earlier as demonstrated in “Introduction To AWS ELB”.
 

CREATING AMI OF WEB SERVER

 
Now one way to create another instance exactly like web server card-web01 is to set up all the services and configurations like we did earlier, or the other way could be to launch the clone of card-web01 server.
 
That clone is called Image or AMI (Amazon Machine Image). Sometimes, we have a lot of configurations, so in those cases it is really beneficial to set up AMI.
NOTE: Once you have a service created, tested  and approved by everyone then you should create AMI.
 
 
 
As of now we have only 1 web server i.e. card-web01. So now let’s create an image of this instance as shown below.
 
 
 
This instance card-web01 has only root volume, but if it would have other volumes added also then those would be also part of the AMI. 
 
NOTE: When we create AMI then the instance gets rebooted and there is a downtime, but if the instance is already in the cluster then we are good.
 
We can also select the No Reboot option, and this will prevent the instance from rebooting, but it will be a slower process.
 
 
 
So, add the Image name and description and hit create Image and then you are good to have an AMI. We can see that AMI has been created successfully as shown below.
 
 
 
 

LAUNCH INSTANCE USING AMI

 
As we have our AMI ready, so now it’s time to launch another instance using this AMI.
 
So, go to Launch Instance -> Under My AMIs, you will see your AMI.
 
 
 
Select the AMI and follow the same steps as we followed while creating first web server card-web01 and select the same security group and same key pair.Our previous instance of card-web01 was in us-west-1b availability zone, so here for another web server, I have chosen another availability zone i.e. us-west-1c.
 
 
 
And I followed the rest of steps as I did earlier and we can see that both our instances/web servers are up and running.
 
 
 
And one web server is in us-west-1b zone and other in us-west-1c zone. Now let’s copy the public IP of card-web02 server and see if it is up or not. 
 
 
 
Great๐Ÿ˜Š it's up .
 
Note: While setting up card-web01 in Introduction To AWS ELB  we had to restart the apache2 service , but here we did not do this and our website is up and running. This is because in the card-web01 server we have started this and the AMI which we created using card-web01 server already had those configurations set up.
 

CLUSTERING WEB SERVERS USING ELB

 
Now we can see that we have two web servers set up , but to access them we have to access via their separate end points. So now we are going to set up a cluster using ELB.
 
Go to Load Balancer section -> select Application Load Balancer.
 
For this we need Target Group. Target group is a group of instances or we can say that it is used in load balancer so that request can be routed to one or more registered instances.
 
Go to Load Balancing section -> Target Groups -> Create Target group.
 

Creating Target group

 
 
 
I have named my target group as card-web-tg.
 
 
 
We can see that our target group has been crated successfully. Now, next is to select the instances that we want to group in the target group.
 

Registering instances to target group

 
For that go to targets tab -> Click on Edit -> select the instances.
 
 
 
And click on Add to Registered. Please note that the port mentioned here is the BACKEND PORT, this is the port no of the services running on the web servers and we are running apache service which runs on port 80.
 
So now click on Add to registered and then hit save.
 
 
 
As we are now done with setting up the target group, so let’s now created Application Load Balancer.
 

Creating Application Load Balancer

 
For Application Load Balancer (only for HTTP and HTTPS traffic), we are going to set up front end port and the backend target group. So, it is going to route the request to that particular target group.
 
Now, go to Load Balancing section -> Load Balancers -> Create Load Balancer -> Create Application Load Balancer.
 
 
 
A Few Points to Note here
  • Name is what we are going to see in the URL, when hitting the load balancer.
  • We are going to use internet facing load balancer as it is going to receive the HTTP requests.
  • We have to select minimum 2 availability zones.
Next, we will continue with the default options and will create a new security group for the load balancer too.
 

Creating security group for the load balancer

 
 
 

Setting up routing for the load balancer

 
For this we will select the existing target group, which we just created.
 
 
 
So, review all configurations in the end and hit Create.
 
 
 
We can see now that our load balancer is active.
 
AWS ELB works on a round robin algorithm. Load balancer actually distributes your traffic 50%-50% and that’s how the load is balanced.
 
 
 
Now what if web01 is down?
 
In this case load balancer is only going to send requests to web02. So, it is going to send request to only live servers, but how is load balancer deciding which instance is up or which is not?
 
So, the health of the instances is checked by the target group and load balancer is not actually responsible for checking the health. We can edit the health check setting of the target group as well.
 
 
 

Checking status of instances in Target Group

 
 
 
We can see that it is showing both instances as unhealthy, even when I can individually access the instances and they are up and running.
 
This is happening because of the security group of our web servers’ card-web01 and card-web02.
 

Configuring security group of web servers.

 
Now we need to allow the security group of load balancer in the inbound rule for port 80 of the security group of web servers i.e. card-web-sg.
 
 
 
This means that now our web servers can receive requests on port 80 from the load balancer and load balancer will receive HTTP requests from the user on port 80.
 
Now, we can see that the health of our web servers is healthy now. Yay ๐Ÿ˜Š
 
 
 
Accessing Load Balancer via URL
 
As the health of both our instances is good, so now I will simply copy the DNS name of the ELB and will see if it works.
 
card-web-elb-1006025513.us-west-1.elb.amazonaws.com
 
 
We can see that application can be accessed now via Load Balancer. So, we have successfully set up the load balancer.
 

SUMMARY

 
So, in this article we understood the concept of AMI and also learned how we can create and AMI and then further created instances using the AMI. We also learned one of the most important concepts of load balancer and saw how we can configure it using target groups for better load distribution and up time.
 
I hope you find this article helpful. Stay tuned for more … Cheers!!


Similar Articles