đź’ˇApplication Load Balancer
đź“ŚThe load balancer distributes incoming application traffic across multiple target groups, such as EC2 instances, in multiple Availability Zones. This increases the scalability and availability of the application.
đź“ŚBy distributing network traffic and information flows across multiple servers, a load balancer ensures no single server bears too much demand. This improves application responsiveness and availability, enhances user experiences, and can protect from distributed denial-of-service (DDoS) attacks.
📌ALBs have three components — listeners, load balancer, and the target group. After receiving a request, the load balancer evaluates the listener rules in priority order (to choose which rule to execute). It then selects a target from the target group for the rule action.
đź“ŚBased on the below diagram , we are implementing the Application Load Balancer.
đź“ŚImplementation
1.Create VPC , 2 public subnets and 2 private subnets
2.Launch 2 EC2 instances in 2 private subnets and select the pre-configured AMIs
3.Create EC2 target group and attach the above 2 ec2 instances into the target group.
4.Create Application load balancer , use 2 public subnets , configure the listener at port 80, create a security group and allow traffic from port 80 anywhere , attach to target group.
5.Take the load balance DNS name and access over the browser and refresh the page and see the traffic is passing to each servers simultaneously
6.Go to target groups and add stickiness to implement the traffic passage to one server only in the specified time.
❎Creating VPC
đź“ŚGo to AWS console search option and type VPC.Then click on create VPC. Fill the below details and click on create VPC.
đź“ŚCreate Internet Gateway.Go to Internet Gateway from LHS menu and click on create internet gateway
đź“ŚThen go to Actions from Internet Gateway home page and click on Attach VPC.Select the VPC created and click on Attach internet gateway above as below
đź“ŚGo to subnets from LHS , as per the diagram we need 4 subnets.So attach the VPC into the subnet
đź“ŚAnd create 4 subnets as per below images: Give subnet name, select an AZ and give the IP , then click on create subnet.
📌Go to Routing Tables from LHS and we can see a default and one unnamed routing table.The unnamed routing table we can use either for public or private.So here I’m going to name the unnamed routing table as public.
đź“ŚNow go to create route table and give inputs as below
đź“ŚGo to subnet allocations and click on edit subnet allocations
đź“ŚSelect both public subnets and click on save associations
đź“ŚSame way do for private-alb-route as well by selecting the private subnets. Now click on public-alb-route and click on edit routes , add as below and click on save changes.
Now all done from VPC end.
❎Go to AWS EC2 console , we need to launch 2 instances as per the diagram.
📌Launch instance with all default settings except in Network Settings → Select existing security group which had port 22 and 80 open.
📌Please refer this link to know how to configure this EC2 — https://lnkd.in/g-P4ePXT and in Advanced details add the below commands and launch instance.
#!/bin/bash
yum install httpd -y
service httpd start
chkconfig httpd on
echo “ Ohio Server1 — Try fail smash it” > /var/www/html/index.html
đź“ŚOnce the instance is 2/2 checks passed, then follow like below and click on create image
đź“ŚGive an image name like below
đź“ŚTerminate the instance created and go to LHS AMIs and find the AMI which we created just now. Same way create another AMI image also and named and webserver2.
đź“ŚNow we have our own AMI, create instance with the our own AMI and select the VPC which we have created and select the private-A subnet.All other settings are default.
đź“ŚAnd in Rules , add ssh and http with custom IP as 10.10.0.0/16 .All other settings are default.
đź“ŚCreate second instance as per the diagram and select the AMI name as Webserver2 ,select the VPC and subnet as private-B. Select the security group we have just created above alb-servers and under advance details paste the same command as below and launch instance with all default settings.
#!/bin/bash
yum install httpd -y
service httpd start
chkconfig httpd on
echo “ Ohio Server2 — Try fail smash it” > /var/www/html/index.html
❎Creating Target Group
đź“ŚGo to LHS and select Target Groups and click on Create target group.Give a name and select the VPC which we have created above.
📌Once created → go to Targets , click on edit and select the instances and below and click on Add to registered and click on save.
đź“ŚNow the target group has the instances which we have created.
❎Go to Load balancer → Create load balancer → Application load balancer
Give name to the load balancer and under Availability Zones
đź“ŚSelect the newly created VPC and select the 2 AZs and select the subnet as PublicA and PublicB as the traffic first needs to go to public IPs and then private IPs as per the diagram.
đź“ŚCreate new load balancer with below functionalities
đź“ŚSelect the existing target group and create the load balancer
đź“ŚNow select the alb-lb1 load balancer and under Security Groups click on the link of the security group which will open a new window.
đź“ŚGo to inbound rules and click on edit inbound rules and remove the rules already added and select anywhere IPv4 from source and click on save rules.
❎Take the DNS name from the load balancer and try to access it in a window. So we will get the output as below
đź“ŚIf we refresh the browser , we will get the output of 2 instances.
❎But what if we want to pass the traffic only to webserver1
đź“ŚThen go to target groups and under Attributes , click on Edit attributes. Select it Enable and give 5 seconds as below
📌Now go to the browser and refresh the page, we will get the server1 page for 5–6 seconds , we will get the server2 page once the cookie expires.