Skip to content

Instantly share code, notes, and snippets.

@isaacarnault
Last active July 30, 2019 19:43
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save isaacarnault/a34c481ca96936cd29b983d3255d9d7a to your computer and use it in GitHub Desktop.
Save isaacarnault/a34c481ca96936cd29b983d3255d9d7a to your computer and use it in GitHub Desktop.
Enabling Elastic Load Balancing on AWS
________ ________ ___ __ ___
|\_____ \|\ __ \|\ \|\ \ |\ \
\|___/ /\ \ \|\ \ \ \/ /|\ \ \
/ / /\ \ __ \ \ ___ \ \ \
/ /_/__\ \ \ \ \ \ \\ \ \ \ \
|\________\ \__\ \__\ \__\\ \__\ \__\
\|_______|\|__|\|__|\|__| \|__|\|__|
Other cloud platforms such as GCP, Azure.

Testing Elastic Load Balancing on AWS - Hands-on

Project Status: Concept – Minimal or no implementation has been done yet, or the repository is only intended to be a limited example, demo, or proof-of-concept.

I'm currently preparing AWS Certified Solution Architect - Associate certificate.
The following gist is intended to AWS users to learn how to enable Elastic Load Balancing on two EC2 instances.
I performed this setup on my Ubuntu 18.04.2 LTS.
To check your OS version, execute $ lsb_release -a in your Terminal.

Installations

None. Just log into your AWS management console, https://console.aws.amazon.com.
You'll need to perform several tasks in your CLI regarding SSH keygen, so make sure you check the following prerequisites.

Prerequisites

First, make sure Oracle jdk is installed. I recommend java 1.8.0
To uninstall effectively your current jdk, perform this:
$ sudo apt-get remove openjdk*
$ sudo apt-get remove --auto-remove openjdk*
$ sudo apt-get purge openjdk*
$ sudo apt-get purge --auto-remove openjdk*

To install java 1.8.0, open Terminal Ctrl+Alt+T and run the command:
$ sudo add-apt-repository ppa:webupd8team/java // adds PPA repository
$ sudo apt-get update // updates package list
$ sudo apt-get install openjdk-8-jdk // installs openjdk

java-8.png
$ javac -version // shows your new java version

Author

  • Isaac Arnault - AWS Cloud series - Related tags: #EC2 #TLS #AWSCLI #Linux

Part 1 : create two EC2 instances

  • We log into our AWS management console using $ https://console.aws.amazon.com.

I'm using MFA to secure my root account access coupled with Google Authenticator on my Android smartphone.

We can bypass this step and login normally to AWS Management Console.

πŸ”΄ See output

isaac-arnault-AWS-1.jpg

  • We go to Services > EC2 > Launch Instance > choose t2.micro > Next: Configure Instance Details

  • Subnet: select "Default in us-east-1a"

πŸ”΄ See output

isaac-arnault-AWS-81.png

  • We are going to use a custom script to launch our EC2 instance:

  • Click "Advanced Details"

Ues the following Script

πŸ”΅ See script

#!/bin/bash
yum update -y
yum install httpd -y
service httpd start
chkconfig httpd on
cd /var/www/html
echo "This is Web Server 1" > index.html

πŸ”΄ See output

isaac-arnault-AWS-82.png

  • Next: Add Storage (leave as it is by default) > Next: Add Tags

Key: Name > Value: my-WEB-Server-1 > Next: Configure Security Group > Create a new security group

Security group name: SG-ELB > Description: Security Group of my ELB > Add rule: enable HTTP and HTTPS

πŸ”΄ See output

isaac-arnault-AWS-87.png

You can create a new key pair or Choose an existing key pair. I'll choose an existing Key Pair since I've already got one.

The Key Pair is saved in a folder named SSH on my Desktop.

Verify that my-WEB-Server-1 is running.

πŸ”΄ See output

isaac-arnault-AWS-85.png

Let's create a second EC2 instance before we enable ELB.

Click on "Launch instance"

Select Amazon Linux 2 AMI > t2.micro > Configure Instance Details > Subnet: choose us-east-1b

πŸ”΄ See output

isaac-arnault-AWS-86.png

Advanced details: use custom script below

πŸ”΅ See script

#!/bin/bash
yum update -y
yum install httpd -y
service httpd start
chkconfig httpd on
cd /var/www/html
echo "This is Web Server 2" > index.html

  • Next: Add Storage (leave as it is by default) > Next: Add Tags

Key: Name > Value: my-WEB-Server-2 > Next: Configure Security Group > Use an existing Security Group

We choose our existing Security Group which is SG-ELB then "Review and Launch" > Launch.

πŸ”΄ See output

isaac-arnault-AWS-87.png

We choose an existing key pair since we've already got one.

Verify that my-WEB-Server-2 is running.

πŸ”΄ See output

isaac-arnault-AWS-88.png

To check that our 2 web servers my-WEB-Server-1 and my-WEB-Server-2 are running:

  • we can connect to Server 1 using it's IPv4 address in our browser.

  • we can do the same as for Server 2.

πŸ”΄ See output

isaac-arnault-AWS-88.png

Part 2 : create an Elastic Load Balancing (ELB)

2.1 classic way

On your EC2 dashboard, on Load balancing section click on Load balancers > Create Load Balancer

Create Classic Load Balancer > Load Balancer name: myELB

πŸ”΄ See output

isaac-arnault-AWS-89.png

  • Next: Assign Security Groups > select an existing Security Group: SG-ELB

  • Next: Configure Health Check. Here we are using a custom configuration, but you can leave the set up as it is by default.

. Response timeout: 2s
. Interval: 3s
. Unhealthy threshold: 2s
. Healthy threshold: 3s

πŸ”΄ See output

isaac-arnault-AWS-90.png

  • Next: Add EC2 Instances: let's select our two EC2 instances that we've created earlier.
πŸ”΄ See output

isaac-arnault-AWS-91.png

  • Next: Add Tags > Key: Name > Value: myELB > Review and Create > Create

To check that our ELB is working, we should click on Instances tab and wait until our two EC2 instances status are "InService".

πŸ”΄ See output

isaac-arnault-AWS-92.png

We can also try our load balancing on our web browser, by copy/paste DNS Name into our web browser.

πŸ”΄ See output

isaac-arnault-AWS-93.png

When refreshing our browser, Web Server 1 & 2 should alternate upon page refresh, which means that our ELB is running.

πŸ”΄ See output

isaac-arnault-AWS-94.png

2.1 advanced way

Go to EC2 > Load Balancing > Target Groups > Create target group > Target group name: myTG-1 > Path: /index.html

Advanced health check settings:

Healthy threshold: 2 > Unhealthy threshold: 3 > Timeout: 5 > Interval: 6 > Success codes: 200 > Create

πŸ”΄ See output

isaac-arnault-AWS-96.png

We should add our EC2 instances to our Target Group.

Go to Target Groups > Select myTG-1 > Click on Targets tab > Edit > Select both instances and click Save

πŸ”΄ See output

isaac-arnault-AWS-97.png

Now let's create a Load Balancer in a more robust way. Go to Load Balancers > Create Load Balancer

Create a Application Load Balancer

πŸ”΄ See output

isaac-arnault-AWS-98.png

Name: myALB-EC2 > Availability Zones: You can select the one that fits your needs. For this gist I selected:

  • us-east-1a
  • us-east-1b
  • us-east-1c
πŸ”΄ See output

isaac-arnault-AWS-97.png

Next: Configure Security Settings > Next: Configure Security Groups > Select the SG we've created earlier
SG-ELB

πŸ”΄ See output

isaac-arnault-AWS-99.png

  • Next: Configure routing > Select existing target group
πŸ”΄ See output

isaac-arnault-AWS-99.png

  • Next: Register Targets > Next: Review > Create

Our Application Load Balancer myALB-EC2 should now appear in Load Balancers.

Go to Target Groups > click on Targets tab > Edit > select all EC2 instances and click "Add to registered"

πŸ”΄ See output

isaac-arnault-AWS-101.png

Our Application Load Balancer is now active. Go to Description and copy DNS name into your clipboard.

Paste it into your browser and do some refreshing of the page. You should see it alternating between web server 1 and 2.

This mean our myALB-EC2 is running fine.

πŸ”΄ See output

isaac-arnault-AWS-102.png

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment