Setting up and Understanding Load Balancer with suitable example

Setting up and Understanding Load Balancer with suitable example

Overview:

In this blog, we will create a load balancer.

Structure :

  • First, we will create a Security Group.

  • We will then create 4 EC2 instances.

  • We will convert these instances into web servers to identify which instance is currently handling the traffic. Traffic can be distributed to any instance so to make things clear we will convert instances into web servers.

  • We will make 2 Target Groups and will place 2 instances in one group, this will be done to not give the complete traffic to one instance. so for this, we will group then.

  • At last, we will make 2 Load Balancers, each load balancer will have one target group.

Creating Security Group:

  • Search for Security Group in the search box.

  • we have to click on Create a security group.

  • Now we will give a name to the group I will give it "My-Group".

  • We will also give a Description of our group.

  • Now below we will click on Add Rule.

  • We will add three rules and it will be -

  • At last, we will click on Create a security group. It is at the bottom and our group will be created.

Creating EC2 instances:

  • Search for EC2 in the search box.

  • Click on the launch instance.

  • Name it as Machine 1, you can give any name but I am going with Machine 1.

  • Select Amazon Linux, in the Amazon Machine Image.

  • In the Instance type select the free tier one. In my case it is t3.micro.

  • Now in the key pair you can create one or if you have already then select it. I have already so I will select my old one.

  • In the Network setting, we will click on Select existing security group and we will select the group which we have created right now.

  • We will select our group named as My-Group.

  • Now in the Advance Details, we will write this

       #!/bin/bash
    
       yum update -y 
    
       yum install httpd -y
    
       systemctl start httpd
    
       systemctl enable httpd
    
       cd /var/www/html
    
       echo "<p>Welcome to Shivam.in</p><h1>This is Machine 1</h1>" > index.html
    

  • These lines make covert this instance into a web server with a paragraph and heading.

  • At last, we will click on Launch instance.

  • When our instance will be ready then we will copy our Pubic IPv4 address and when we will paste it then we can see our webpage will be created.

  • Same we will create 3 more instances same like this.

  • Just we will replace the line "This is Machine 1" with 2,3 and 4. For Machine 2 it will be "This is Machine 2" just like this for Machines 3 and 4.

In the end, we will have 4 different Instances named Machine 1, Machine 2, Machine 3 and Machine 4.

Creating Target Groups:

  • We will search Target Groups in the Search box.

  • Now we will click on Create target group.

  • We will choose the target type as Instances and name our target group. I will name it TG-1.

  • Rest we will leave all things and will click on Next.

  • Now we will get the option to choose instances to target.

  • In this Group, I will choose Machine 1 and Machine 2.

  • After choosing we will click on include as pending below.

  1. At the bottom, we will click on create target group.

  2. Same we will create one more group in which we will have Machine 3 and Machine 4.

In the end, we will have 2 groups named TG-1 and TG-2.

Creating Load Balancer:

  • We will search for load balancers in the search box.

  • Then we will click on Create load balancer.

  • We will have options to select Load balancer types.

  • We will go with Application Load Balancer and click on Create.

  • We will name our load balancer App-LB.

  • Rest we will leave all things but in the Mappings section, we will check all the mappings.

  • Now in the Security groups, we will select the security groups which we have created at the start and we will remove the default security group.

  • Now the main step comes where we have to select our target group, so for this load balancer I will select the machine 1 and machine 2 group which was named TG-1

  • Same We will create one more load balancer and give it to the target group TG-2.

  • In the end, we will have 2 Load balancers with TG-1 and TG-2.

  • Now when we will copy the DNS name of any Load balancer and paste it in the browser we will see that when we keep refreshing the page then we will different pages will render.

  • It means that traffic is passed to different pages.

  • Suppose we copy the DNS name of APP-LB then we will see that while refreshing the page we will see sometimes it will come "This is Machine 1" and sometimes it will come "This is Machine 2".

  • Same with the other Load Balancer also.

  • This is where we will see how Load Balancer works.

Thank You