Hi everyone,
I am struggling with a problem with ECS and Auto-Scaling. Scaling up my cluster is trivial. My current issue is scaling down. Now I’ve read a number of articles on the subject and tried a couple of things (for example: A better solution to ECS AutoScaling | Philipps Blog ). And it seems to work, however I’m wondering if there is an easy way to tell which instance the ASG should terminate in response to scale down alarm.
What I’m basically trying to do is the following: Scale down alarm triggers, and the ASG desired capacity is reduced by 1, however I want to terminate only an instance which has no tasks running on it, not an instance which the ASG picks up according to default policies.
I’m thinking of creating a lambda which goes over my cluster, finds an empty instance, disables the Launch process of the ASG, manually terminates the empty instance, adjusts the desired capacity of the ASG, and then enables the Launch process again. This sounds like it should work, but has anyone any other ideas or suggestions?
The way I “solved” this issue was a jenkins job on a 1min schedule that marks all instances in the ASG that is currently running a task with scale-in protection. That’s probably safer than manual termination. You could easily do the same from lambda.
I would suggest letting AWS drain the instance it picks since there might be a chance there is not an instance without a task running it. You set a lifecycle rule on the asg, which marks the instance for draining and the tasks get rescheduled to the remaining machines. This is described in this article from AWS. We combined this with the custom scaling metric described in the link you posted and it seems to do quite well.
I thought about this, but I would rather not move running tasks around if possible. I saw boto3 can remove and adjust asg desired capacity in one call, so I’m currently working on a lambda to do this.
I do this aws post and the blog post you linked. It works really well and I highly suggest it. Moving running tasks around, if theyre things that can drain in <10 seconds, is really not a big deal.
One problem with waiting for it to hit zero is the default placement policy is spread, so you end up with one container on each host, and if its a small container you will end up being massively underutilized.
It sounds like you found the autoscaling remove instance api call, which if you really have to do this is the way to remove and decrease as one.
I guess for me it doesn’t make sense to stop an instance potentially running 10s of services when there is an instance running 1,2 or 0 potentially services. I am really surprised there is no way to add/configure such termination policy. It seems like a pretty obvious thing.
Sure that host is running 10s of services. But maybe it’s older, or has an older launch config or any number of other factors. When you scale it in all of the tasks should be moved to the other hosts within your ECS cluster. It’s not like the services just stop and you have downtime, it should all be graceful.
For me personally, I have my task placement set to make sure they are at the very least running in different azs, and then different hosts. After that I don’t care how AWS takes care of scaling the tasks up and down on my available hosts. I have other things to worry about.
Can’t you just not give it any scale in policies? And use lambda or some other external api caller to deregister instances when their tasks are done? The way I’m looking to solve a similar problem is to have each host publish a cw metric when they know they need to die (app logic) and that cw metric kicks off a lambda func to go deregister the host and give it the should decrement desired capacity.