Lets Build this Architecture where we are deploying Node JS apps on EC2 instances in different availability Zones
Let’s start with an example, consider a scenario where you have more than one Ec2 Instances sitting behind a Web Server to serve your application. Now, how can you ensure that the traffic load is equally shared between all the Ec2 Instances to avoid imbalance(as shown in the above figure) ? To meet this need we have ELB i.e. Elastic Load Balancer that helps to load balance incoming application traffic across Amazon EC2 instances in a single Availability Zone or multiple Availability Zones. Let’s create one Elastic Load Balancer.
Login to AWS Console and ensure you have at least two Ec2 Instances serving any Web Application (as simple as “Hello World :) “) because we will need these instances at a later stage to attach to our ELB. Though it is possible to have a single server behind a load balancer, it is best to have a pool of servers behind an ELB. Under Ec2, go to Load Balancers and click on Create Load Balancer. AWS provides 2 types of Load Balancers -
Classic Load Balancer (CLB) — It does routing decision at Layer 4 of the OSI Model i.e. at TCP Layer. CLB are best in use when routing and load-balancing decisions are based on IP addresses and TCP ports
Application Load Balancer (ALB) — It does routing decision at layer 7 of the OSI Model i.e. at Application Layer. Multiple services can share a single ALB using path-based routing which can be enabled by adding rules for Listeners and multiple target groups. This feature differentiates an ALB from CLB.
Start with creating a Classic Load Balancer first-
Select Classic Load Balancer and Click on continue. Configure your Load Balancer by giving a name. We can create a Load Balancer inside a VPC as per our needs which can restrict the traffic to our web server and provide security.
Next, set Listener Configuration:-
Load Balancer Protocol and Port — Specify the protocol and port which a user will use to access the Web Server through the Load Balancer. It’s a common practice to keep the LB Protocol and port same as Instance’s Protocol and port
Instance Protocol and Port — Specify the protocol and port number used by our Ec2 Instances to serve the Web Application. For e.g. in my case, My “Hello World” app is running on port 80(HTTP) of the instance so I made the entries as shown below-
Fig 1: CLB Configuration
Next, assign a security group to the ELB keeping in mind the ports you want/don’t want to open for the world.
If you choose HTTP protocol for the Load Balancer in the previous step then you’ll see a warning message indicating that we aren’t using HTTPS, also not using any kind of SSL certificates. We can ignore this message at this point in time because our focus is to understand the working of Load Balancer. Click on Configure Health Check, this is an important step.The Load Balancer will perform health checks on your EC2 instances based on the Ping Protocol, Port and Path provided in this step. I’ll perform a health check on the index.html for this article but keep in mind that you can provide any Ping Protocol, Port and Path which ensures that your ec2 Instance is actively serving the Web Application.
There are also other parameters(described below) to set and in case if you forget what they mean, just scroll over them to see their description( A tip for the exam!)
Response Time — Amount of time the LB is gonna wait for response from health check(2 sec-60 sec).
Interval — Time between two consecutive Health Checks(5 sec — 300 sec)
Unhealthy Threshold — Number of Consecutive health check failures before declaring an Ec2 Instance as being Unhealthy.
Healthy Threshold — Number of Consecutive successful health check before declaring an Ec2 Instance as being healthy. Check the image below for example entries:-
Fig 2: CLB Health Check
Next, move forward to add the Ec2 Instances serving the Web Application to this Load Balancer. ELB provides us with features:-1. Enable Cross-Zone Load Balancing and 2. Enable Connection Draining
Click on Review and Create to create this Load Balancer. Now once the Load Balancer is ready, notice some important points -
The Load Balancer is provided with a DNS name(not public IP) and if everything is working fine then you can see the web application serving at this DNS name.
Go to instances under the Load Balancer section, there are two Instance states-
a. Out Of Service — Instance does not pass health check
b. In Service — Successful health checks are indicated.
3. Once an Ec2 Instance is Out of Service, the Load Balancer will not send traffic to
that unhealthy Instance till the time it becomes healthy again.
This was all about the basics of Classic Load Balancer required for Certification Exam. Let’s move Application Load Balancer.
Fig 3 : Cross-zone load balancing and Availability Zones.
Application Load Balancer (ALB) — Application Load Balancer is very similar to CLB, the main difference is that it does path based routing. For routing to different paths, create a target group for each application and create a different rule for each target group. For example:-
Target Group A → Instances 1 and 2, port 8084
Target Group B → Instances 1 and 2, port 8086
Then on the ALB, you would create 2 rules for port 80, like so:
Path /application1 → Target Group A
Path /application2 → Target Group B
If you want to read more on this then refer to this link
Containerized Application Support with EC2 Container Service
HTTP/2 Support, HTTPS Support
Native IPv6 Support