Skip to content

Chris-luiz-16/Docker-container-load-balancing-

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 

Repository files navigation

Docker containers Load balancing

This repo is based on common situation on how to load balance multiple containers either in a single ec2-instance or in a multiple ec2-instance. Let's get started

Table of Contents

Different scenario of Load balancing the containers

Let's get into the diagram so that you can understand it better

ALB


1] Single instance multiple container


As you can see on the left side image where all the setup is in a single ec2-instance. There are three containers running in a single ec2-instance.

Containers are running in the same port 5000 and all are having the same flask enviornment setup. However, I've done a port mapping so that all the 3 containers will have differnt ports assigned while publishing the port publicily.

In this scenario, we'll load balance all the 3 containers and will point to a domain like example.com so that the end-user can access the domain with an SSL on it after load balancing it with the ALB.

2] Multiple instance single container


As you can see on the right side of the image where you have three ec2-instance and each one of them having a one container running in the 5000 port.

This is most common setup of Load balancing multiple Ec2-instance by defining the same on the target group.


Simple flask hello world to test the load balancing

I've created a simple flask hello world project to test the load balancing of the application.

from flask import Flask, request
import os
import socket

app = Flask(__name__)

@app.route('/')
def index():
    hostname = socket.gethostname()
    return f'<h1><center>This is Flask Application running in the Hostname: {hostname}</center></h1>'

flask_port = os.getenv('FLASKPORT', 5000)
app.run(host='0.0.0.0', port=flask_port)

If you guys dont have the time to build the code from the scratch don't worry I got your back. You can simply execute the below docker command to pull from my public docker hub image. It's as simple as that
docker pull chrisluiz16/version


Deploying multiple containers and Load balancing in a single ec2-instance

It's time for my most favourite part to deploy multiple container in a single ec2-instance. Let's go

1] Make a Custom Network Bridge


Making a custome network bridge helps you to communicate with all the containers using just the container name. So it's very important that you don't miss this step

docker network create lb-net

2] Creating the containers


After creating the custom network bridge, now it's time to create some containers to check whether we will be able to execute first infra setup mentioned above on Single instance multiple container

# Make sure to open 8081,8082 and 8083 port in your Ec2-instance Security group

docker container run --name flaskapp1 -d --restart always --network lb-net -p 8081:5000 chrisluiz16/version:latest
docker container run --name flaskapp2 -d --restart always --network lb-net -p 8082:5000 chrisluiz16/version:latest
docker container run --name flaskapp3 -d --restart always --network lb-net -p 8083:5000 chrisluiz16/version:latest

Once that's executed your container's will be listed like this

docker container ls -a
CONTAINER ID   IMAGE                        COMMAND            CREATED         STATUS         PORTS                                       NAMES
fefe57eba914   chrisluiz16/version:latest   "python3 app.py"   8 seconds ago   Up 7 seconds   0.0.0.0:8083->5000/tcp, :::8083->5000/tcp   flaskapp3
a5c87a8b2cc1   chrisluiz16/version:latest   "python3 app.py"   9 seconds ago   Up 8 seconds   0.0.0.0:8082->5000/tcp, :::8082->5000/tcp   flaskapp2
7867ecfa5356   chrisluiz16/version:latest   "python3 app.py"   9 seconds ago   Up 8 seconds   0.0.0.0:8081->5000/tcp, :::8081->5000/tcp   flaskapp1

3] Time to load balance using the ALB


It's time we deploy the ALB for our instance. Before deploying the AWS ALB we need to provision the target group first.Create the target group as below

  1. Create Target group
  2. Choose the target group as Instances
  3. I choose the target group name as lb-tg you can choose any name you wish
  4. Set HTTP as 80 [we can set the redirection rule later]
  5. Set VPC where your instance is placed, mine is default so I'm not changing it
  6. Protocol version as HTTP1
  7. Keep all values default and just click *Next
  8. After clicking Next here comes the tricky part,First tick the instance and then Ports for the selected instances enter the ports that we port published
  9. Change the default port 80 to 8081 and click Include as pending below. Screenshot attached for referenceScreenshot 2023-06-02 005151

  1. Once that's done repeat step 8 and 9 untill 8082 and 8083 is added as the targets. End result will be like this image
  2. Click Create Target group next

Time to Create the ALB and attach it to our target


  1. Create Load balancer
  2. Choose Application Load Balancer and click on Create
  3. Choos whatever name you like to name your ALB
  4. Set Scheme as Internet facing
  5. IP address type as IPv4
  6. Set VPC as default
  7. Mappings select 2 subnet alteash in whichever region you are
  8. Set Security group to allow ALB to have 80 and 443
  9. Listener as HTTP:443 with target group name as the name you set for target name as lb-tg
  10. Default SSL/TLS certificate load the certificate from ACM you can follow the doc Requesting a public certificate 11] Click on create load balancer and that's it job done

Time to allow force http to https redirect


  1. Go to load balancer
  2. Click on your alb
  3. Choose the listner and then click listners option. Screenshot attached for reference. image
  4. After that Default actions from add action to Redirect
  5. Choose protocol HTTPS with the port 443 and status code as 301 permanently moved
  6. Just click add and that's been sorted

Route 53 domain point to ALB endpoint


Point the ALB enpoint by aliasing it with your domain name as shown below image

Verification of single instance multiple container setup



Here you can see the final output where the domain flask.chrisich.fun is loadbalancing by dispyaing the hostnames of the containers

ezgif com-gif-maker



Deploying multiple ec2-instances with a container and load balancing it

Here we should have 3 ec2-instance and all the 3 will be having the same flask container running in 5000 port and setting a port publishing to 80 port and then loadbalancing it as eplained in the right side of the diagram Different scenario of Load balancing the containers

Docker container creation for multiple instance setup


Make sure you run the below docker command on all the 3 ec2-instance

docker container run --name flaskapp1 --restart always -d -p 80:5000 chrisluiz16/version

Load balancing it using the ALB

Time to create the target group in order to attach it to the load balancer

  1. Create Target group
  2. Choose the target group as Instances
  3. I choose the target group name as alb-tg you can choose any name you wish
  4. Set HTTP as 80 [we can set the redirection rule later]
  5. Set VPC where your instance is placed, mine is default so I'm not changing it
  6. Protocol version as HTTP1
  7. Keep all values default and just click *Next
  8. Just select on all the 3 instance without changing the port of Ports for the selected instances. Attaching an image for refernece. image
  9. After that click include as pending below and then click crete target group and that's done


Time to Create the ALB and attach it to our target


  1. Create Load balancer
  2. Choose Application Load Balancer and click on Create
  3. Choos whatever name you like to name your ALB
  4. Set Scheme as Internet facing
  5. IP address type as IPv4
  6. Set VPC as default
  7. Mappings make sure select atleast 2 subnet in whichever region you are provisioning the ALB
  8. Set Security group to allow ALB to have 80 and 443
  9. Listener as HTTP:443 with target group name as the name you set for target name as alb-tg
  10. Default SSL/TLS certificate load the certificate from ACM you can follow the doc Requesting a public certificate 11] Click on create load balancer and that's it job done


Time to allow force http to https redirect


  1. Go to load balancer
  2. Click on your alb
  3. Choose the listner and then click listners option. Screenshot attached for reference. image
  4. After that Default actions from add action to Redirect
  5. Choose protocol HTTPS with the port 443 and status code as 301 permanently moved
  6. Just click add and that's been sorted

Route 53 domain point to ALB endpoint

Point the ALB enpoint by aliasing it with your domain name as shown below image

Verification

I have set the aliasing for lb.chrisch.fun in route53 record. Lets' try calling it in the browser to check whether it's load balancing it or not.

alb_AdobeExpress (1)

About

Managing containers in a single or multiple instance using AWS ALB

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published