Devansh Sharma
Devansh Sharma

Reputation: 331

AWS ECS: Load Balancing multiple containers under a single service

Suppose I have a task definition with 2 nginx containers. Now when I make a service (EC2 launch type) using this task definition and try to add an ALB to it I am able to only choose one "container to load balance". How can I set it up so I am able to load balance the other container too using the same ALB ?

What I have tried and understood is: Suppose I am running 2 tasks using above definition under a single service. So there will be 4 containers running (2 each of container_1 and container_2). When I choose the container_1 to load balance during service ALB creation, ECS will create a target group and put the instance and the dynamic ports of container_1 as targets. This target group will then be mapped to the ALB's rules. This works for me.

But now if I want to have a rule setup for my other set of containers the only way I see is to make the target group myself by hardcoding the dynamic ports of container_2's containers, which is less than ideal.

A use case that I can think of right now is suppose one container is running the frontend and another is running the backend. So I want url's like /my-app/pages/* to go to frontend containers and /my-app/apis/* to go to backend containers. I'm sure there are better examples/use cases but this is all I can think of right now.

So, how would I go about setting this up ?

Thanks!

Upvotes: 2

Views: 2976

Answers (1)

Hussain Mansoor
Hussain Mansoor

Reputation: 3124

Let's follow the documentation and recommendations from AWS on why more than 1 containers are allowed in a task. From the documentation [1]

When the following conditions are required, we recommend that you deploy your containers in a single task definition:

  • Your containers share a common lifecycle (that is, they are launched and terminated together).

  • Your containers must run on the same underlying host (that is, one container references the other on a localhost port).

  • You require that your containers share resources.

  • Your containers share data volumes.

Otherwise, you should define your containers in separate tasks definitions so that you can scale, provision, and deprovision them separately.

[1] https://docs.aws.amazon.com/AmazonECS/latest/developerguide/application_architecture.html

For your use-case of frontend and backend applications, two different services is the right way. 2 ECS Services, with 1 TaskDefinition and 1 Container each.

As to how both should integrate with each other, via URL or service names (Microservice Mesh) is another topic and discussion.

Upvotes: 3

Related Questions