How can I spread 2 tasks between 2 ec2 instances in 2 different Availability zones?

637 Views Asked by At

I have a cluster, lets say my-cluster. It has 2 services running. Lets call them my-services. Each services has 1 tasks running. my-task1 and my-task2.

The EC2 instances are launched by an autoscaling group which launches 2 EC2 instances. I am using an application load balancer.

When I created the service I selected AZ-balance-spread which is described: "This template will spread tasks across availability zones and within the availability zone spread tasks across instances. Learn more. Strategy: spread(attribute:ecs.availability-zone), spread(instanceId)"

My EC2 instances are on different Availability Zones. But when I click on my-cluster and select the ECS-instances tab, I see the 2 tasks running both on 1 container instance. The other container instance says 0 tasks running.

I was expecting 2 container instances, and each having 1 task running.

My question is, if I selected AZ-balance-spread shouldn't these tasks be spread between the 2 EC2 instances which are on separate AZ's?

I want to set it up so all tasks are spread out between AZ's so if an instance goes down the other task on the other instance in the other AZ is still up. But I can't seem to figure out how to do this. That being said if I create 4 tasks, it does spread them between the two instances. 2 tasks for one and 2 tasks for another. But when I just have 2 tasks, it will put them all in one EC2 instance.

Your help is much appreciated in solving this mystery!

Thanks!

1

There are 1 best solutions below

0
On

This is something that Amazon didn't make to work perfectly. The best workaround that I found for that is configuring the Placement Strategic to spread randomly between the ec2. The problem is that all placement strategics is setup in each service, so if we configure to spread using the InstanceID, the tasks inside this service will use this setup, but other service does not use this info and spread using the same strategy, launching in the same InstanceID (maybe because of the number order).

So I've configured all the services to be random, this is not a good way if these services are scalable, but in my case, I have 24 services just with 1 task each. Using the random strategic, they spread between all the cluster instances.

"placementStrategy": [
    {
        "type": "random"
    }
]

taks spread between instances