How to scale in/out EC2 instances based on ECS cluster resources availability?

泄露秘密 提交于 2019-12-07 10:51:13

问题


I have multiple services running in my ECS cluster. Each service contains one or more tasks based on CPU utilization or a number of users. I have deployed these containers with EC2 launch type. Now, I want to increase/decrease the number of EC2 instances based on available resources in the cluster. Let's say there are four ECS tasks running in two m5.large instances.

Now, if an ECS service increases the number of tasks and there aren't enough resources available in the cluster, how can I spin up an instance and add to the cluster?

And same goes for vice versa. If there is instance running with no ecs task in it, how can I destroy it automatically?

PS - I was using Fargate. Since it's cost is very high, I moved to EC2 instances.


回答1:


you need to setup your ecs cluster instances in a ASG as @Nitesh says, second you need to set up a cloudwatch alert based in a key metric, with ecs is complex because you need to set up two autoscaling policies one by service another one to scale up your instances, for ec2 the metric that you could use is Cluster CPU reservation and /or Cluster memory reservation.

The scheme works like this your service increases the number of the desired container by an autoscaling rule using a key metric for your service as could be de CPU usage or the number the request in a load balancer and in consequence the Cluster CPU reservation increase this triggers the cloudwatch alert and your ASG increase the number of instaces.

Some tips scale up fast, and scale down slow this could by handle by setting up the time of the alerts

For the containers use Service Auto Scaling and Target tracking policies for more info see

https://docs.aws.amazon.com/AmazonECS/latest/developerguide/cloudwatch-metrics.html#cluster_reservation

https://docs.aws.amazon.com/AmazonECS/latest/developerguide/service-auto-scaling.html

https://aws.amazon.com/blogs/compute/automatic-scaling-with-amazon-ecs/

I hope this help

Regards



来源:https://stackoverflow.com/questions/52720556/how-to-scale-in-out-ec2-instances-based-on-ecs-cluster-resources-availability

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!