A Task Definition is a collection of 1 or more container configurations. Some Tasks may need only one container, while other Tasks may need 2 or more potentially linked containers running concurrently.
The Task Definition allows you to specify things like:
- Which Docker image to use
- Which ports to expose
- How much CPU and memory to allot
- How to collect logs
- Define environment variables.
A Task is created when you run a Task directly, which launches container(s) (defined in the task definition) until they are stopped or exit on their own, at which point they are not replaced automatically. Running Tasks directly is ideal for short-running jobs, perhaps as an example of things that were accomplished via CRON.
A task placement strategy is an algorithm for selecting instances for task placement or tasks for termination.
- When a task that uses the EC2 launch type is launched, Amazon ECS must determine where to place the task based on the requirements specified in the task definition, such as CPU and memory.
- Similarly, when you scale down the task count, Amazon ECS must determine which tasks to terminate.
A task placement constraint is a rule that is considered during task placement.
- You can use constraints to place tasks based on Availability Zone or instance type.
- You can also associate attributes, which are name/value pairs, with your container instances and then use a constraint to place tasks based on attribute.
A Service is used to guarantee that you always have some number of Tasks running at all times. If a Task's container exits due to an error, or the underlying EC2 instance fails and is replaced, the ECS Service will replace the failed Task. This is why we create Clusters so that the Service has plenty of resources in terms of CPU, Memory and Network ports to use. To us it doesn't really matter which instance Tasks run on so long as they run. A Service configuration references a Task definition. A Service is responsible for creating Tasks.
Services are typically used for long-running applications like web servers. For example, if I deployed my website powered by Node.JS in Oregon (us-west-2) I would want say at least three Tasks running across the three Availability Zones (AZ) for the sake of High-Availability; if one fails I have another two and the failed one will be replaced (read that as self-healing!). Creating a Service is the way to do this. If I had 6 EC2 instances in my cluster, 2 per AZ, the Service will automatically balance Tasks across zones as best it can while also considering CPU, memory, and network resources.
References
- https://stackoverflow.com/a/42961623/3595388
- https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-placement-constraints.html
- https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-placement.html
- https://aws.amazon.com/blogs/compute/amazon-ecs-task-placement/
Written with StackEdit.