Elastic Load Balancing automatically distributes incoming application traffic across multiple targets, such as Amazon EC2 instances, containers, IP addresses, and Lambda functions.
Types of Elastic Load Balancers:
ELB supports three types of load balancers. We can select the appropriate load balancer based on our application needs.Classic Load Balancer (CLB):
Classic Load Balancer is just used for round-robin purpose load balancing. Classic Load Balancer is recommended only for EC2 Classic instances. It works on Application Layer.
Fig: Classic Load Balancer (CLB) |
https://aws.amazon.com/elasticloadbalancing/features/#Details_for_Elastic_Load_Balancing_Products
CLB Practical:
In this practical, we are going to maintain load balancing between two instances, whom we are using for Web Services.
Steps we need to follow:
- Make sure your VPC has at least two subnets
- Enable Auto-Assign IP settings of this subnet
- Associate public route table to this subnet for internet access
- Create two Web Servers in Linux
- Create ELB, configure Health Check and provide instances
- Register a domain
- Map IP to DNS via DNS server
- Create Record Set
- Provide nameservers to Freenom.com
- Verify site by using the domain name
Step 1: Make sure your VPC has at least two subnets:
We use subnets to differentiate between Availability Zones (AZ). If we create both of the subnets in the same AZ and in case if that particular AZ is down, then our productivity will hamper. So we use at least two subnets.
AWS ➔ Services ➔ VPC ➔ Subnets ➔ Verify, if we have two subnets or not.
If we do not have multiple subnets, then we need to create them. For creating subnets, kindly go to the link:
In my VPC (webshack-vpc), I have one public and one private subnet.
Fig: Subnets |
AWS ➔ Services ➔ VPC ➔ Subnets ➔ Create Subnet ➔ Name tag: webshack-sub3-pub ➔ VPC: webshack-vpc ➔ Availability Zone: us-east-1c (make sure this AZ is the different one) ➔ IPv4 CIDR block: 10.0.3.0/24 ➔ Create ➔ Close.
Step 2: Enable Auto-Assign IP settings of this subnet:
AWS ➔ Services ➔ VPC ➔ Subnets ➔ Select Subnet 'webshack-sub3-pub' ➔ Actions ➔ Modify auto-assign IP settings ➔ [*] Enable auto-assign public IPv4 address ➔ Save.
Step 3: Associate public route table to this subnet for internet access:
AWS ➔ Services ➔ VPC ➔ Route tables ➔ Select public route table 'webshack-rt-pub' ➔ Subnet Associations ➔ Edit subnet associations ➔ Select your subnet 'webshack-sub2-pub' ➔ Save.
Fig: Subnet Association |
Now, I have two public subnets; one subnet is 'webshack-sub1-pub' which is in us-east-1a and another subnet is 'webshack-sub3-pub' in us-east-1c.
Step 4: Create two Web Servers in Linux:
Create one instance named ' linux-node1':
AWS ➔ Services ➔ EC2 ➔ Instances ➔ Launch Instance ➔ [*] Free Tier Only ➔ Select any Linux AMI ➔ Select ➔ Instance Type: 't2.micro' ➔ Configure Instance: Number of Instances: 1 ➔ Select VPC ➔ Select Public Subnet (I'm selecting us-east-1a) ➔ Auto-assign Public IP: Enable ➔ Advanced Details ➔ User Data: As text ➔ provide a script:
#!/bin/bash
yum install -y httpd
echo "*** This site is coming from Node-1***" >> /var/www/html/index.html
service httpd restart
systemctl enable httpd
Next: Storage ➔ Next Tags: 'Click to add a Name Tag' ➔ Key: Name & Value: linux-node1 ➔ Next: Security Group ➔ Assign a security group: (*) Create a new security group OR ( ) Select an existing security group (but port for SSH (22) and HTTP (80) should be enabled ➔ Review and Launch ➔ Launch ➔ Create or provide an existing key-pair➔ Launch Instances.
Now, if we use public IP of this instance in the browser, we will get the output:
*** This site is coming from Node-1***
Create another instance named 'linux-node2':
AWS ➔ Services ➔ EC2 ➔ Select Instance named 'linux-node1' ➔ Actions ➔ Launch More Like This ➔ Configure Settings ➔ Advanced Details ➔ In Script change Node-2 apart from Node-1 ➔ Add Tags ➔ Name: linux-node2 ➔ Review ➔ Launch ➔ Create or provide an existing key-pair ➔ Launch Instances ➔ View Instances.
Now, if we use public IP of this instance in the browser, we will get the output:
*** This site is coming from Node-2***
SPECIAL NOTE:
As you can see, we are using two different sites in this practical. But this is only for understanding the purpose. Otherwise, we will not understand from where we are getting the access. But on production, both of these sites will be same.
Step 5: Create ELB, configure Health Check and provide instances:
AWS ➔ Services ➔ EC2 ➔ Load Balancers ➔ Create Load Balancer ➔ Create (Classic Load Balancer) ➔ Load Balancer name: webshack-lb ➔ Create LB inside: webshack-vpc (select VPC) ➔ Do not click on 'Create an internal load balancer' ➔ As we did not do any change, then Load Balancer Protocol/Port and Instance Protocol/Port should be HTTP/80 ➔ Select Subnets ➔ Click on plus (+) icon of both Public instances. Now, this will be reflected in Selected Subnets ➔ Next ➔ Assign a security group ➔ to create a new security group OR select an existing one but the port for SSH (22) and HTTP (80) should be enabled ➔ Next ➔ Configure Health Check...
To discover the availability of your EC2 instances, a load balancer periodically sends pings, attempts connections, or sends requests to test the EC2 instances. These tests are called health checks. The status of the instances that are healthy at the time of the health check is InService. The status of any instances that are unhealthy at the time of the health check is OutOfService. The load balancer performs health checks on all registered instances, whether the instance is in a healthy state or an unhealthy state.
For More details kindly go to URL:
https://docs.aws.amazon.com/elasticloadbalancing/latest/classic/elb-healthchecks.html
...Ping Protocol: HTTP ➔ Ping Port: 80 ➔ Ping Path: /index.html ➔ Advanced Details:
To discover the availability of your EC2 instances, a load balancer periodically sends pings, attempts connections, or sends requests to test the EC2 instances. These tests are called health checks. The status of the instances that are healthy at the time of the health check is InService. The status of any instances that are unhealthy at the time of the health check is OutOfService. The load balancer performs health checks on all registered instances, whether the instance is in a healthy state or an unhealthy state.
For More details kindly go to URL:
https://docs.aws.amazon.com/elasticloadbalancing/latest/classic/elb-healthchecks.html
...Ping Protocol: HTTP ➔ Ping Port: 80 ➔ Ping Path: /index.html ➔ Advanced Details:
Response Timeout: 5 seconds (means server should reply within 5 seconds) ➔ Interval: 10 seconds (means after 10 seconds, this should again try to connect) ➔ Unhealthy threshold: 2 (means server will be marked as Unhealthy after no ping 2 times) ➔ Healthy threshold: 2 (means server will be marked as healthy after 2 times continuous ping ➔ Next: Add EC2 Instances ➔ Select both public instances ➔ [*] Enable Cross-Zone load balancing ➔ [*] Enable connection draining: 300 seconds (means do not drop connection for next 5 minutes) ➔ Next: Add Tags (provide any tag if you want) ➔ Review and Create ➔ Create ➔ Close.
In the description, we can see the status as 'out of time' because ELB is performing Health Check. After performing Health Check status will be 'in service'.
As a result, we will get a link in Description ➔ Copy the link ➔ Open Browser ➔ paste URL and hit enter.
Step 6: Register a domain:
Visit the link https://www.freenom.com to register a free domain. Otherwise, we can use AWS and Godaddy, etc. but both of these are paid.
Find a free domain: Search a domain and purchase it as free (right now, I am using domain webshack.cf) ➔ Continue ➔ Checkout ➔ Login via using Gmail or Facebook OR click on login if you have an account ➔ Complete Order.
Step 7: Map IP to DNS via DNS server:
AWS ➔ Services ➔ Route 53 (it’s a paid feature of AWS but if we clear it within 12 hours, there is no charge) ➔ DNS Management: Get Started Now ➔ Create Hosted Zone ➔ Create Hosted Zone ➔ Domain name: webshack.cf (do not provide www) ➔ Comment (provide purpose): Testing s3 web hosting ➔ Type: Public Hosted Zone ➔ Create.
Step 8: Create Record Set:
Create Record Set ➔ Name: www (do not use dot after www) ➔ Type: A-IPv4 address ➔ Alias: Yes ➔ Alias Target: Select ELB link from drop-down list ➔ Routing Policy: Simple ➔ Evaluate Target health: No ➔ Create.
Step 9: Provide nameservers to Freenom.com:
Login to freenom.com ➔ Services ➔ My Domains ➔ Click on Manage Domain in front of our domain 'webshack.cf' ➔ Management Tools ➔ Name Servers ➔ [*] Use custom nameservers (enter below) ➔ (Here, paste all four nameservers from our AWS window one by one WITHOUT DOT) ➔ Change Nameservers.
Step 10: Verify the site by using the domain name:
Use domain in the browser, this will provide home page either from node1 or from node2. On every refresh, we will get a web page from both nodes one by one.
Enjoy!
No comments:
Post a Comment