aws ecs autoscaling instances - amazon-web-services

Currently I am struggling with AWS auto scaling.
Do I need to create instance Auto Scaling Group in parallel to ECS Service Auto Scaling to provision necessary resources for services/tasks, or it is managed automatically by the latter one?
edit:
I am using api/cloudformation only, no web interface.

ECS service scaling and And EC2 instance scaling are independent entities. EC2 instance scaling is not handled if you configure ECS service scaling. It is required(Mandotory) to configure EC2 Instance scaling and maintain enough resources in the cluster if you are opting ECS Service scaling, otherwise ECS service fails to place new Tasks during service scaling activities. In general, it is recommended to scale EC2 Instances based on CPUReservation and MemoryReservation cloudwatch alarms.
EC2 Container Instance scaling tutorial: https://docs.aws.amazon.com/AmazonECS/latest/developerguide/cloudwatch_alarm_autoscaling.html

Related

Why do I need to enable instance scale-in protection in my Auto Scaling Group when enabling a capacity provider within a cluster?

I am making an AWS ECS cluster using EC2 and trying to use capacity providers. I don't really understand why do I need to enable instance scale-in protection inside my AWS Auto Scaling group.
Isn't the point of Auto Scaling the termination of needless EC2 instances?
why do I need to enable instance scale-in protection
This is only needed when you chose to use managed scaling:
When managed scaling is enabled, Amazon ECS manages the scale-in and scale-out actions of the Auto Scaling group used when creating the capacity provider. On your behalf, Amazon ECS creates an AWS Auto Scaling scaling plan with a target tracking scaling policy based on the target capacity value you specify.
The managed scaling ensures that ECS controlled when instances are removed. By doing this it protects any instances that have some tasks running on it from being terminated:
When managed termination protection is enabled, Amazon ECS prevents Amazon EC2 instances that contain tasks and that are in an Auto Scaling group from being terminated during a scale-in action.
The entire idea is that you enable instance scale-in protection on your ASG so that ECS has control over which instances to terminate based on tasks they run. Without this, your ASG could terminate instances based on other criteria, not nervelessly related to "needless EC2 instances". For example, ASG can choose to terminate instances based due to AZRebalance process. This could lead to ASG terminating instances with running tasks, which may not be what you want.

Differences between EC2 Auto Scaling Group (EC2 ASG) and Elastic Container Service (ECS)

From what I've read so far:
EC2 ASG is a simple solution to scale your server with more copies of it with a load balancer in front of the EC2 instance pool
ECS is more like Kubernetes, which is used when you need to deploy multiple services in docker containers that works with each other internally to form a service, and auto scaling is a feature of ECS itself.
Are there any differences I'm missing here? Because ECS is almost always a superior choice to go with if they work as I understand.
You are right, in a very simple sense, EC2 Autoscaling Groups is a way to add/remove (register/unregister) EC2 instances to a Classic Load Balancer or Target Groups (ALB/NLB).
ECS has two type of scaling as does any Container orchestration platform:
Cluster Autoscaling: Add remove EC2 instances in a Cluster when tasks are pending to run
Service Autoscaling: Add/remove tasks in a service based on demand, uses Application AutoScaling service behind the scenes

How to scale ecs cluster with autoscaling?

I think you have 2 scaling
1 for ec2 instance
1 for ecs task
So I think flow goes like
You get more requests
Your tasks need more resources (cpu/memory)
Your ecs task autoscaling kicks in and creates more task
your ec2 autoscaling also kicks in and creates more ec2
Questions
your ecs needs to know newly launched ec2 (from ec2 autoscaling group) is the host that it can run on (how?)
I want the number of ec2 instance starts from one.
I think one instance is already running in the ecs cluster
Do I make ec2 autoscaling group start from 0?
ec2 autoscaling group has pricing option to use spot instance, but I didn't see such option when I created ecs cluster instances. To be clear, can I make ecs cluster with 0 ecs-instance, and let ec2 autoscaling group starts with 1 instance?
Your ecs task autoscaling kicks in and creates more task
your ec2 autoscaling also kicks in and creates more ec2
Your ECS task scale if there is a memory so fine it will scale and launch new container, if not enough memory then it will not launch an instance, you need to configure cloud watch alarm to scale ECS cluster based on the task scaling.
Tutorial: Scaling Container Instances with CloudWatch Alarms
your ecs needs to know newly launched ec2 (from ec2 autoscaling group)
is the host that it can run on (how?)
ECS agent registers the new instance with the same cluster, and the new task can be placed in any instance that have available resources to handle and launch the container.
I want the number of ec2 instance starts from one. I think one
instance is already running in the ecs cluster Do I make ec2
autoscaling group start from 0?
You should start from 1 otherwise no container will launch and will throw error no instance found in the cluster. if you do not want run container for now then you can start from 0.
ec2 autoscaling group has pricing option to use spot instance, but I
didn't see such option when I created ecs cluster instances. To be
clear, can I make ecs cluster with 0 ecs-instance, and let ec2
autoscaling group starts with 1 instance?
You can read about spot ECS cluster instance here.
Amazon ECS is a highly scalable, high performance, container
management service that supports Docker containers and allows you to
run applications on a managed cluster of Amazon EC2 instances easily.
ECS already handles the placement and scheduling of containers on EC2
instances. When combined with Spot fleet, ECS can deliver significant
savings over EC2 on-demand pricing.

How to do auto scaling for Rancher and Kubernetes clusters on AWS EC2?

Using AWS EC2 to install Rancher cluster. Then setup Kubernetes cluster from Rancher server.
About auto scaling, there are some ways to do:
Use Rancher cattle webhook service
https://rancher.com/docs/rancher/v1.6/en/cattle/webhook-service/
This way should use monitoring tool Prometheus to monitor CPU usage, then add or delete nodes due to alerting.
Use terraform to generate rancher-master-ha, rancher-nodes, networking, database dynamictly
http://rancher.com/aws-rancher-building-resilient-stack/
This can well done for Rancher cluster.
Horizontal Pod Autoscaling Walkthrough
https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale-walkthrough/
It's official horizontal pod autoscaling way.
Kubernetes Autoscaler
https://github.com/kubernetes/autoscaler
It's also a official auto scaling way for Kubernets cluster.
Use AWS Auto Scaling
https://aws.amazon.com/autoscaling/
About this way, how to connect it to Rancher cluster and Kubernetes cluster running on EC2?
There are many ways to do the auto scaling, but which is the best way? And the very important thing is, how to use AWS Auto Scaling for this architecture?
Since you deployed Kubernetes with Rancher, you should use Rancher webhooks for this operation.
Use Prometheus/Grafana to set up webhook when CPU utilization is over some %.

Deploying CodeStar application to Spot Instance

Is there an easy way to automatically deploy a CodeStar application to a persistent spot instance every time the request is fulfilled? The pipeline only runs when the code is changed and requires that the codedeploy agent already be installed. I've searched online and can't seem to find anything regarding using CodeStar with spot instances.
Seems totally possible, please read Deploying AWS CodeDeploy Applications to Auto Scaling Groups
AWS CodeDeploy supports Auto Scaling, an AWS service that can launch Amazon EC2 instances automatically according to conditions you define. These conditions can include limits exceeded in a specified time interval for CPU utilization, disk reads or writes, or inbound or outbound network traffic. Auto Scaling terminates the instances when they are no longer needed. For more information, see What Is Auto Scaling?.
When new Amazon EC2 instances are launched as part of an Auto Scaling
group, AWS CodeDeploy can deploy your revisions to the new instances
automatically. You can also coordinate deployments in AWS CodeDeploy
with Amazon EC2 instances registered with Elastic Load Balancing load
balancers. For more information, see Integrating AWS CodeDeploy with
Elastic Load Balancing and Set Up a Classic Load Balancer in Elastic
Load Balancing for AWS CodeDeploy Deployments.
Good luck