Tech Junkie Blog - Real World Tutorials, Happy Coding!: Iaas With AWS: Create A Network Load Balancer

Wednesday, September 29, 2021

Iaas With AWS: Create A Network Load Balancer

 In the previous post we went over how to create a Classic Load Balancer, in this post we are going to create one of types of load balancer AWS offers.  We are going to create a Network Load Balancer, this balancer  is for websites that require high performance and low latency websites, think of streaming data.  If your website needs real time streaming data, this is probably the load balancer for you. It supports layer 4 protocols such as UDP, TLS and TCP protocols. If you need a static IP or Elastic IP assigned to your load balancer this is your only choice because the other two load balancer does not give you the option to assign Elastic IPs.

Before we create the load balancer we need to create more than one instances with a web server because we need to test that the load balancer is able to switch.

1. Create two instances with the user data to create Apache Web Servers with these commands in the User Data for instance, if you need the full instruction on how to create instances with User Data you can read this post

#Use this for your user data (script without newlines)
# install httpd (Linux 2 version)

yum update -y 
yum install -y httpd.x86_64 
systemctl start httpd.service 
systemctl enable httpd.service 
echo "Hello world from $(hostname -f)" > /var/www/html/index.html

We just created an index.html file to write out the hostname for testing later on

2. For the Security Groups setting make sure there's an inbound rule for HTTP traffic

3. On the create the two instances, if you copy and paste the public DNS or public IP into the address bar of the browser you see the hostname being printed out for the instance, so if the load balancer is successfully the request should change to a different host when a user requests the site
Now its time to create the Network Load Balancer

4. On EC2 Dashboard scroll down to "LOAD BALANCING" and click on "Load Balancers",  then click on the "Create Load Balancer" button you will be presented with three choices.  Choose the "Network Load Balancer" by clicking on the "Create" button

5.  On the next page give your load balancer a name and select "internet-facing" for the "Scheme"

6. For the "Listeners" accept the default, since HTTP is part of TCP it will accepts HTTP requests on port 80 if we choose TCP

7.  For Availability Zones you have to assign at least two AZs, so pick two AZ, select your subnets and let AWS assign the IP address for you by selecting "Assigned by AWS". click "Next: Configure Security Settings", click "Next" again

8.  In the "Step 3: Configure Routing" you have to specify the target group for your Network Load Balancer.  We  going to create a new target group, and assign the target group type to instance so that we can assign the instances we created. Accept the default because we want to protocol to be TCP and port 80 still.  Then click "Next"

9. On "Step 4: Register Targets" page, select the two instances and click on the "Add to registered" button to add the instances to the Network Load Balancer

Select "Next" until you see the "Create" button, click on the "Create button

Once the Load Balancer has been created you can copy and paste the Load Balancer's DNS into the browser's address bar and it would alternate between the two instances.  You will have to wait for a while for it to work, the instances will have a status of "healthy" when it's ready

Here is result you should get when everything is finished.

The Network Load Balancer seems to implement sticky IP by default so you will probably stay with one instance for your whole session, that's actually a good thing.  So you might not get a new instance on every request.  Don't quote me on that though.


Search This Blog