A Network Load Balancer functions at the fourth layer of the OSI model (Open Systems Interconnection). It can handle millions of requests per second. After the load balancer receives a connection request, it selects a target from the target group for the default rule. It attempts to open a TCP connection to the selected target on the port specified in the listener configuration.

When you enable an Availability Zone for the load balancer, Elastic Load Balancing creates a load balancer node in the Availability Zone. By default, each load balancer node distributes traffic across the registered targets in its Availability Zone only. If you enable cross-zone load balancing, each load balancer node distributes traffic across the registered targets in all enabled Availability Zones.

In this post we going to create a Network load Balancer, and we going to associate an Elastic IP to it and test it, for this we going to launch the instances in the same AZ as you need to attach an Elastic IP for each AZ.

Requirements

  • AWS Account

Hands-On

  1. In the AWS Management Console go to EC2

2. Go to instances

3. Click on Launch instances

4. Select Amazon Linux 2 AMI - 64 bits

5. Select t2.micro and click on Next

6. In the next window make sure you have the Auto-assign Public IP enabled, select 2 instances,  select a subnet and leave everything else on defaults and click Next

7. In the Add Storage step you can leave it as default, and click on Next

8. In Add tags, leave as default and click Next

9. In the next window, create a new security group and give it a name, you will have as default the port 22 open to connect via ssh, click on Add Rule and add the port 80 for the nginx server (type HTTP), and click Review and Launch

10. Review the information and click on Launch

11. In the next window, select Create a new key pair, give it a name, download and click on Launch instances

12. You will see the Launch status, if everything is correct, you will have a confirmation, click on View Instances

13. In the instances, select the first instance and click on Connect

14. On the EC2 Instance Connect, copy the IP address for later and click on Connect

15. This will open a new window with an emulated terminal in the instance, now install nginx

sudo su -
amazon-linux-extras install -y nginx1

16. Check the status of nginx

systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: di
sabled)
   Active: inactive (dead)

Dec 14 21:04:22 ip-172-31-29-155.us-east-2.compute.internal systemd[1]: Unit nginx...
Hint: Some lines were ellipsized, use -l to show in full.

17. Start the nginx server

systemctl start nginx

18. Open in a new browser tab the public IP to check is working

19. Now lets customize the landing page, then use exit to close the connection, and close the tab or window.

echo "This is the first server" > /usr/share/nginx/html/index.html
exit
exit

20. Refresh and you will see the custom message

21. Repeat the steps with the second instance, click on instances

22. Select the second instance and click on Connect

23. On the EC2 Instance Connect tab, copy the public IP address for later, click on Connect, this will open a emulated terminal on a new browser window.

24. Install nginx

sudo su -
amazon-linux-extras install -y nginx1

25. Check the status of the nginx server

systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: di
sabled)
   Active: inactive (dead)

Dec 14 21:21:11 ip-172-31-24-113.us-east-2.compute.internal systemd[1]: Unit nginx...
Hint: Some lines were ellipsized, use -l to show in full.

26. Start the nginx server

systemctl start nginx

27. Open a new tab o browser window and open the public IP address to check the nginx installation

28. Now lets customize the message in the second server, and use exit close the connection.

echo "This is the second server" > /usr/share/nginx/html/index.html
exit
exit

29. Refresh the browser to see the second message

30. Now lets create the Elastic IP and assign to the load balancer, go to Elastic IP's

31. Click on Allocate Elastic IP address

32. Click on Allocate

33. Go to EC2, Load Balancers

34. Click on Create Load Balancer

35. Click on Create on the Network Load Balancer

36. Give it a name, and leave the port 80

34. Select the AZ where your instances are, select Choose an Elastic IP, and select the IP we just allocate, and click on Next

35. You can leave the next part at it is, as we don't going to use the port 443, and click Next

36. In the next window, select new target group, give it a name, select IP as target type, and click on Next

37. Add the two private IP's from the instances we start earlier and click Next

38. Review the configuration and click on Create

39. You will see the Creation status, click on close

40. Wait until the status is active and select the network load balancer to copy the DNS name

41. Test it on an Internet browser, and refresh, you will see the two servers responding after you refresh some times

42. Now try to use the Elastic IP, to get the same results.

Great, you learn how to create a Network Load Balancer with an EIP, in the next post we going deeper into the AWS Services.

Clean-Up

  1. In Load Balancers, Select the load balancer we create and click on Actions, Delete

2. Confirm

3. Go to elastic IPs and select the EIP and click on Actions and Release Elastic IP address

4. Confirm

5. Go to instances, select the two instances we create and click on Instance state and click on Terminate

6. Confirm

7. Go to Load Balancing Target Groups, select the target group and click on Actions and select Delete

8. Confirm

9. Go to Key Pairs, select the key pair and click on Actions, Delete

10. Confirm

11. Go to Security Groups, select the security group, and click on Actions, Delete security grup

12. Confirm