AWS recently launched a new Application Load Balancer (ALB) that supports Dynamic Port Mapping with ECS. It allows you to run two containers of a service on a single server on dynamic ports which ALB automatically detects and reconfigures itself.
Amazon EC2 Container Service ECS is a managed container service that allows you to run your application on docker container and manage cluster on EC2 instances. ALB works as a load balancer and distributes traffic on multiple running containers. ALB continuously monitors health check of containers, and if any container fails the health check, ALB terminates that container and starts a new one to maintain desired no of containers.
While working on a project, we were using an Elastic Load Balancer with ECS for container heath check and distribute traffic on containers. In task definition, we defined host port on which container accepts the request and same port was used as an instance port in ELB. For example, If you have a service with two containers, you need at least two ECS container instance because multiple containers can’t run on the same port on the same server, each container is hosted on a separate server.
Below are the steps we took to create an ALB and configure an ECS Service using that ALB:
1. Create an ALB and select application load balancer in the load balancer type.
2. Give a name to your ALB, select a scheme – private or public, select a port on which the ALB will accept requests, select VPC & Subnet and click on next to configure security group of your ALB.
3. Select any existing security group or create a new security group and define port and source to allow traffic on your ALB.
4. Create a target group that will be attached to the ALB and route traffic from ALB to your container instances. You need to define port & protocol that ALB uses to route traffic to your targets in your target group and perform health checks on your targets (Instances).
5. Now your ALB is ready and it accepts request on port 80, now create a task, service and add ALB for load balancing between multiple containers.
6. Create a new Task or a new version of the existing task and set host port 0. It will dynamically assign any available port when it runs a docker container. You can use any public Docker image or your own Docker image.
7. Create a new service from the task and in ‘Configure ELB’ section, select application load balancer in ELB type section, select your ALB & target group and create service. If your ECS cluster has single ECS instance and tasks count is two, it will start two new containers in your instance on two different dynamic ports.
8. Check targets in your target group, you would see that same instance is registered two times with two different ports.
9. You can check the same on your server using the ‘docker ps’ command. Two different containers are running on two random ports by the same docker image that you mentioned in your task definition.
So If you are running multiple containers of a single service, you don’t need multiple servers for them. ALB allows to maximize the usage of servers and offers you a high-performance load balancing option. It gives you the flexibility of running multiple containers of a service on a single server by using the random available port.
Same will help in deployment also. You don’t need to run extra servers for deployment. The new version will be deployed on the same server on some different port if resources (Core & RAM) and new containers are in-service under ALB target, old containers will be terminated.