Running multiple container using single EC2 instances in ECS


Hi, guys.

We are recently want to test on the AWS ECS service. Currently there are a total of 10 containers running on an EC2 which we deploy manually using composer. However, we would like to know if we use ECS service, can we still share the same EC2 or we need to provisioning instances for every services? If possible, we would like to share the similar EC2 across 10 containers. Is that possible?

Besides, we would also like to know how the load balancing would work if we deploy using the similar EC2 instance.

Thanks in advance.

asked 2 years ago4357 views
1 Answer

Hi! So if you register the EC2 instance to join the ECS Cluster (and therefore also be an ECS Instance), you can create services that the cluster will then deploy in the available pool of ECS instances.

You can create deployment constraints, which are rules to say "deploy this service, in that way, on nodes that have these properties", which means it will try only create tasks if there are ECS instances, with available capacity, that match these rules to deploy the containers to.

If you prefer to not have to manage EC2 instances anymore, you can go down the Fargate route. If your use-case is simple enough, that's my go-to recommendation, as the (for on-demand) the maybe extra costs, IMHO, are outweight by simplicity. In that case each service' tasks gets their own host, which further ensures isolation.

If you want to scale in the same or similar way as Fargate does but not have to worry about provisioning the EC2 instances, you can create an Autoscaling Group and give that as a Capacity provider to your ECS cluster. ECS will then drive the autoscaling based on the number of needed containers for your services, and still allows to respect placement rules (strategies) as mentioned above.

When creating your service, you can specify which target groups of a load balancer you would like to register. ECS and ELB then communicate together to automatically register new containers, send traffic to them, or de-register these if unhealthy.

If you want to deploy with something docker-compose compatible which deals with creating all your services definitions, IAM rules, permissions etc, and lots more, try out ECS Compose-X | the labs.

profile picture
answered 2 years ago
  • So, does this mean that if I have a t3.medium instances. I can run all containers inside this instance without provisioning other instances?

  • Technically, yes, if that is the only instance you have. But now, know that the ECS agent will report the CPU/RAM available to the containers. So if you have 10 tasks which require a minimum of .5GB of RAM and .25 CvPUs, then would that all fit in the instance type ? You might run out of space really quickly.

    If you want HA with failover, running a cluster with a single instance is not quite the way to go.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions