简体   繁体   English

AWS ECS 中的任务和服务有什么区别?

[英]What is the difference between a task and a service in AWS ECS?

It appears that one can either run a Task or a Service based on a Task Definition.似乎可以根据任务定义运行任务或服务。 What are the differences and similarities between Task and Service?任务和服务之间有什么区别和相似之处? Is there a clue in the fact that one can specify "Task Group" when creating Task but not Service?在创建任务而不是服务时可以指定“任务组”这一事实是否有线索? Are Task and Service hierarchically equal instantiations of Task Definition, or is Service composed of Tasks?任务和服务在层次上是任务定义的实例化,还是由任务组成的服务?

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.有些任务可能只需要一个容器,而其他任务可能需要 2 个或更多潜在链接的容器同时运行。 The Task definition allows you to specify which Docker image to use, which ports to expose, how much CPU and memory to allot, how to collect logs, and define environment variables.任务定义允许您指定要使用的 Docker 映像、要公开的端口、要分配多少 CPU 和内存、如何收集日志以及定义环境变量。

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 .当您直接运行Task 时会创建一个 Task,它会启动容器(在任务定义中定义),直到它们停止或自行退出,此时它们不会被自动替换 Running Tasks directly is ideal for short-running jobs, perhaps as an example of things that were accomplished via CRON.直接运行任务非常适合短期运行的作业,也许作为通过 CRON 完成的事情的一个例子。

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.如果 Task 的容器因错误退出,或者底层 EC2 实例失败并被替换,ECS 服务将替换失败的 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.这就是我们创建集群的原因,以便服务在 CPU、内存和网络端口方面有足够的资源可供使用。 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.服务通常用于长时间运行的应用程序,如 Web 服务器。 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;例如,如果我在俄勒冈州 (us-west-2) 部署了由 Node.JS 提供支持的网站,为了高可用性,我想说至少有三个任务跨三个可用区 (AZ) 运行; 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.如果我的集群中有 6 个 EC2 实例,每个可用区 2 个,该服务将在考虑 CPU、内存和网络资源的同时,尽可能地自动平衡跨区域的任务。

UPDATE:更新:

I'm not sure it helps to think of these things hierarchically.我不确定分层思考这些事情是否有帮助。

Another very important point is that a Service can be configured to use a load balancer, so that as it creates the Tasks—that is it launches containers defined in the Task Definition—the Service will automatically register the container's EC2 instance with the load balancer.另一个非常重要的一点是,可以将 Service 配置为使用负载均衡器,以便在它创建任务时(即启动任务定义中定义的容器),该 Service 将自动向负载均衡器注册容器的 EC2 实例。 Tasks cannot be configured to use a load balancer, only Services can.任务不能配置为使用负载均衡器,只有服务可以。

Beautifully explained in words by @talentedmrjones. @talentedmrjones 用文字进行了精美的解释。 Picture below will help you visualize it easily :)下面的图片将帮助您轻松地将其可视化:)

集群、服务、EC2 实例和任务在行动

Task Definition:任务定义:

This is the blueprint describing which Docker containers to run and represents your application.这是描述要运行哪些 Docker 容器并代表您的应用程序的蓝图。 It includes several tasks.它包括几个任务。

任务定义

Service:服务:

An instance of Task Definition.任务定义的一个实例。 It also defines the minimum and maximum Tasks from one Task Definition run at any given time, autoscaling, and load balancing.它还定义了在任何给定时间运行的一个任务定义中的最小和最大任务、自动缩放和负载平衡。

ECS Container Instances: ECS 容器实例:

This is an EC2 instance that has Docker and an ECS Container Agent running on it.这是一个 EC2 实例,上面运行着 Docker 和 ECS 容器代理。 The Agent takes care of the communication between ECS and the instance, providing the status of running containers and managing running new ones. Agent 负责 ECS 和实例之间的通信,提供运行容器的状态并管理正在运行的新容器。

Relationship:关系:

在此处输入图片说明

ECS 对象及其关联关系图

Task Definition : (It is a configuration) A task definition is a blueprint for your application and describes one or more containers through attributes.任务定义:(它是一种配置)任务定义是您的应用程序的蓝图,并通过属性描述一个或多个容器。 Some attributes are configured at the task level, but the majority of attributes are configured per container.一些属性是在任务级别配置的,但大多数属性是按容器配置的。

You are defining your containers and how to launch them via Task definitions.您正在定义容器以及如何通过任务定义启动它们。 You describe how containers should be provisioned (link to ECR's saved container images, CPU units, Memory, Container ports to expose, network type).您描述了应如何配置容器(链接到 ECR 保存的容器映像、CPU 单元、内存、要公开的容器端口、网络类型)。

Task definitions specify the container information for your application (web), such as how many containers are part of your task, what resources they will use, how they interact with each other and which host port they will use.任务定义为您的应用程序 (web) 指定容器信息,例如您的任务中有多少容器、它们将使用哪些资源、它们如何相互交互以及它们将使用哪个主机端口。 It can be of Fargate and EC2 type.它可以是 Fargate 和 EC2 类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM