ECS task not starting - STOPPED (CannotPullContainerError: “Error response from daemon request canceled while waiting for connection”

ECS task not starting - STOPPED (CannotPullContainerError: “Error response from daemon request canceled while waiting for connection”

我正在使用 Fargate 在 ECS 中启动一项任务,在处于 PENDING 状态一段时间后,它最终进入 STOPPED 状态,并出现以下错误:

STOPPED (CannotPullContainerError: "Error response from daem

当我展开详细信息时,我看到了

STOPPED (CannotPullContainerError: "Error response from daemon: Get https://id.dkr.ecr.ap-southeast-2.amazonaws.com/v2/: net/http: request canceled while waiting for connection"

原因

(Client.Timeout exceeded while awaiting headers)

所以任务由于某种原因无法访问容器,但我不确定缺少什么权限以及来自什么资源。我已经阅读了一些,我发现的唯一真正的建议是将 AssignPublicIp: ENABLED 添加到 AwsvpcConfiguration 但这没有帮助。

所以看起来错误消息在某个时候发生了变化:https://aws.amazon.com/premiumsupport/knowledge-center/ecs-pull-container-api-error-ecr/ 有步骤要完成,但提到错误 CannotPullContainerError: API error 可能与 CannotPullContainerError: "Error response from daem?[=13 同义=]

至少对我来说,创建 AWS::EC2::VPCEndpoint 似乎让我走得更远。

我通过为在 ECS 上使用我的服务创建的每个 Fargate 实例启用 public IP 来修复此错误。

服务配置:

{
  ...
  "networkConfiguration": {
    "awsvpcConfiguration": {
      "subnets": [
        "my-subnets",
      ],
      "securityGroups": [
        "my-security-group"
      ],
      "assignPublicIp": "ENABLED" // <-- ENABLED HERE
    }
  },
}

我使用 Fargate 找到了适合我的解决方案。他们的文档说明:

  1. If you're running a task using an Amazon Elastic Compute Cloud (Amazon EC2) launch type and your container instance is in a private subnet, or if you're running a task using the AWS Fargate launch type in a private subnet, confirm that your subnet has a route to a NAT gateway in the route table.

简单地说,

  1. 您必须找到您使用的 VPC。
  2. 然后在你有你的VPC的table,你会找到它的主路由table。
  3. 打开路由 table,然后确保您有 link 到互联网网关的条目。

它看起来像 igw-006b1917dc348d10d。完成后,您的 vpc 将可以访问互联网,并且能够获取您的 ECR 映像。

来源:AWS docs

Alan Sereb 的解决方案对我有用。

似乎在 AWS launched Fargate platform version 1.4.0 之后,使用配置了 ECS 服务的 VPC 完成了对远程图像注册表(如我的 Gitlab 注册表)的访问。

所以现在 Fargate 容器网络接口(因此 ECS 使用的 VPC)需要访问互联网,因此必须在 VPC 路由中设置互联网网关 Table。

这是无法拉取镜像时出现的错误..可能有很多原因,比如权限和VPC内部的互联网访问。

如果您的 VPC 是 Public 唯一的子网,那么您需要添加 Internet 网关以访问 Internet。 如果您的 VPC 仅是私有的,那么您需要一个 NAT 网关,以便任务可以到达 docker 镜像进行拉取。

原因是 运行 任务定义的服务未连接到互联网。

我有它是因为我的 vpc 在 public 子网中并且该服务没有 public IP 地址。

建立在 之上,如果您使用 python cdk 创建您的服务,您可以指定服务中的任务是否也应使用​​ public IP 地址作为创建服务时的子网和安全组。

基本上,你应该有这样的东西.. :

service = ecs.FargateService(self,
                             "service-name",
                             cluster=cluster,
                             task_definition=task_definition,
                             service_name="service-name", 
                             assign_public_ip=True, # this is important
                             security_groups=[list of security groups , also important],
                             vpc_subnets=[list of subnets]
                             )

有关FragateService的更多信息,请参阅this

如果您使用的是 cli,则可以使用以下命令更新您的服务:

aws ecs update-service --service service-name --cluster the_Cluster  --network-configuration "{
    \"awsvpcConfiguration\": {
      \"subnets\": [\"subnet-***\",\"subnet-****\",\"subnet-*****\"],
      \"securityGroups\": [\"sg-******\"],
      \"assignPublicIp\": \"ENABLED\"
    }

有关如何更新服务检查的更多信息this

使用 Fargate 拉取镜像 ECS 使用必须具有策略 AmazonECSTaskExecutionRolePolicy.

的任务执行角色(例如:ecsTaskExecutionRole)

从 ECR 外部的私有存储库中提取图像时,此任务执行角色将需要在远程容器注册表中进行身份验证,因此正如 AWS 文档所述 https://docs.aws.amazon.com/AmazonECS/latest/developerguide/private-auth.html 它需要一个带有凭证的秘密,并且,对于访问秘密的任务执行角色,也是一个内联策略 secretsmanager:GetSecretValue.

假设图像 public 可以在任何容器注册表(DockerHub、ECR、GitLab 等)中访问,可能还涉及其他事情。

  1. 确保您的 VPC DNS resolution 设置为 Enable 否则它将无法访问外部 URL
  2. 确保运行 fargate 服务的子网可以访问互联网。如果它们是 public,子网将有一个路由 table 将流量重定向到任何 IP (0.0.0.0/0) 到互联网网关。否则,他们将不得不使用 NAT 网关作为跳转服务来访问互联网。
  3. 确保子网级别的 NACL 和正在使用的安全组允许传出和传入流量。

附带说明一下,VPC 下有一项称为可达性分析器的服务,可让您检查连接路径并检测 NACL 或路由 table 中的任何错误。例如,您可以验证任何子网中的网络接口是否可以访问互联网网关。它用作跟踪路由。

我也遇到了同样的问题。调查后,我看到何时禁用 Auto-assign public IP 我们必须通过与 NAT 网关关联的私有子网将您的服务连接到 public 互联网。

这里的步骤:

1.创建 2 - 3 个私有子网

2。创建新路由 Table 并与这些子网关联

3。创建 NAT 网关

-- 分配您的私有子网之一

-- 使用上述子网创建服务