简体   繁体   English

运行任务时出现 AWS ECS 错误:在您的集群中未找到任何容器实例

[英]AWS ECS Error when running task: No Container Instances were found in your cluster

Im trying to deploy a docker container image to AWS using ECS , but the EC2 instance is not being created.我正在尝试使用ECSdocker容器映像部署到AWS ,但没有创建 EC2 实例。 I have scoured the internet looking for an explanation as to why I'm receiving the following error:我已经在互联网上搜寻了有关我收到以下错误的原因的解释:

"A client error (InvalidParameterException) occurred when calling the RunTask operation: No Container Instances were found in your cluster." “调用 RunTask 操作时发生客户端错误 (InvalidParameterException):在您的集群中未找到任何容器实例。”

Here are my steps:这是我的步骤:

1. Pushed a docker image FROM Ubuntu to my Amazon ECS repo. 1. 将 docker 映像从 Ubuntu 推送到我的 Amazon ECS 存储库。

2. Registered an ECS Task Definition: 2.注册ECS任务定义:

aws ecs register-task-definition --cli-input-json file://path/to/my-task.json 

3. Ran the task: 3.运行任务:

aws ecs run-task --task-definition my-task

Yet, it fails.然而,它失败了。

Here is my task:这是我的任务:

{
  "family": "my-task",
  "containerDefinitions": [
    {
        "environment": [],
        "name": "my-container",
        "image": "my-namespace/my-image",
        "cpu": 10,
        "memory": 500,
        "portMappings": [
            {
                "containerPort": 8080,
                "hostPort": 80
            }
        ],
        "entryPoint": [
            "java",
            "-jar",
            "my-jar.jar"
        ],
        "essential": true
    }
  ]
}

I have also tried using the management console to configure a cluster and services, yet I get the same error.我也尝试使用管理控制台来配置集群和服务,但我得到了同样的错误。 How do I configure the cluster to have ec2 instances, and what kind of container instances do I need to use?如何将集群配置为拥有 ec2 实例,需要使用哪种容器实例? I thought this whole process was to create the EC2 instances to begin with!!我认为这整个过程是从创建 EC2 实例开始的!!

I figured this out after a few more hours of investigating.经过几个小时的调查,我发现了这一点。 Amazon, if you are listening, you should state this somewhere in your management console when creating a cluster or adding instances to the cluster:亚马逊,如果你正在监听,你应该在创建集群或向集群添加实例时在管理控制台的某处说明这一点:

"Before you can add ECS instances to a cluster you must first go to the EC2 Management Console and create ecs-optimized instances with an IAM role that has the AmazonEC2ContainerServiceforEC2Role policy attached" “在将 ECS 实例添加到集群之前,您必须首先转到 EC2 管理控制台并使用附加了AmazonEC2ContainerServiceforEC2Role策略的 IAM 角色创建ecs-optimized实例”

Here is the rigmarole:这是繁文缛节:

1. Go to your EC2 Dashboard , and click the Launch Instance button. 1. 转到您的EC2 控制面板,然后单击Launch Instance按钮。

2. Under Community AMIs , Search for ecs-optimized , and select the one that best fits your project needs. 2. 在Community AMIs ,搜索ecs-optimized ,然后选择最适合您的项目需求的一个。 Any will work.任何将工作。 Click next.点击下一步。

3. When you get to Configure Instance Details, click on the create new IAM role link and create a new role called ecsInstanceRole . 3. 当您进入配置实例详细信息时,单击create new IAM role link并创建一个名为ecsInstanceRole的新角色。

4. Attach the AmazonEC2ContainerServiceforEC2Role policy to that role. 4. 将AmazonEC2ContainerServiceforEC2Role策略附加到该角色。

5. Then, finish configuring your ECS Instance. 5. 然后,完成配置您的 ECS 实例。
NOTE: If you are creating a web server you will want to create a securityGroup to allow access to port 80.注意:如果您正在创建 Web 服务器,您将需要创建一个 securityGroup 以允许访问端口 80。

After a few minutes, when the instance is initialized and running you can refresh the ECS Instances tab you are trying to add instances too.几分钟后,当实例初始化并运行时,您可以刷新您也尝试添加实例的 ECS 实例选项卡。

Currently, the Amazon AWS web interface can automatically create instances with the correct AMI and the correct name so it'll register to the correct cluster.目前,Amazon AWS Web 界面可以使用正确的 AMI 和正确的名称自动创建实例,以便将其注册到正确的集群。

Even though all instances were created by Amazon with the correct settings, my instances wouldn't register.即使所有实例都是由 Amazon 使用正确设置创建的,我的实例也不会注册。 On the Amazon AWS forums I found a clue.亚马逊 AWS 论坛上,我找到了一条线索。 It turns out that your clusters need internet access and if your private VPC does not have an internet gateway, the clusters won't be able to connect.事实证明,您的集群需要访问 Internet,如果您的私有 VPC 没有 Internet 网关,则集群将无法连接。

The fix修复

In the VPC dashboard you should create a new Internet Gateway and connect it to the VPC used by the cluster.在 VPC 仪表板中,您应该创建一个新的 Internet 网关并将其连接到集群使用的 VPC。 Once attached you must update (or create) the route table for the VPC and add as last line附加后,您必须更新(或创建)VPC 的路由表并添加为最后一行

0.0.0.0/0 igw-24b16740  

Where igw-24b16740 is the name of your freshly created internet gateway.其中 igw-24b16740 是您新创建的 Internet 网关的名称。

I ran into this issue when using Fargate.我在使用 Fargate 时遇到了这个问题。 I fixed it when I explicitly defined launchType="FARGATE" when calling run_task .我在调用run_task明确定义了launchType="FARGATE"run_task

Other suggested checks其他建议检查

  1. Selecting the suggested AMI which was specified for the given region solved my problem.选择为给定区域指定的建议 AMI 解决了我的问题。

    To find out the AMI - check Launching an Amazon ECS Container Instance .要查找 AMI - 检查启动 Amazon ECS 容器实例

  2. By default all the ec2 instances are added to default cluster .默认情况下,所有 ec2 实例都添加到默认集群中。 So the name of the cluster also matters.所以集群的名称也很重要。

See point 10 at Launching an Amazon ECS Container Instance .请参阅启动 Amazon ECS 容器实例中的第10 点。

More information available in this thread .此线程中提供了更多信息。

Just in case someone else is blocked with this problem as I was... I've tried everything here and didn't work for me.以防万一其他人像我一样被这个问题阻止......我在这里尝试了所有方法但对我不起作用。

Besides what was said here regards the EC2 Instance Role, as commented here , in my case only worked if I still configured the EC2 Instance with simple information.除了这里所说的关于 EC2 实例角色的内容,正如这里所评论的,在我的情况下,只有在我仍然使用简单信息配置 EC2 实例时才有效。 Using the User Data an initial script like this:使用用户数据的初始脚本如下:

#!/bin/bash
cat <<'EOF' >> /etc/ecs/ecs.config
ECS_CLUSTER=quarkus-ec2
EOF

Informing the related ECS Cluster Name created at this ecs config file, resolved my problem.通知在此 ecs 配置文件中创建的相关 ECS 集群名称,解决了我的问题。 Without this config, the ECS Agent Log at the EC2 Instance was showing an error that was not possible to connect to the ECS, doing this I've got the EC2 Instance visible to the ECS Cluster.如果没有这个配置,EC2 实例上的 ECS 代理日志会显示一个错误,无法连接到 ECS,这样做我让 EC2 实例对 ECS 集群可见。

After doing this, I could get the EC2 Instance available for my EC2 Cluster:执行此操作后,我可以获得可用于我的 EC2 集群的 EC2 实例: 在此处输入图片说明

The AWS documentation said that this part is optional, but in my case, it didn't work without this "optional" configuration. AWS 文档说这部分是可选的,但就我而言,如果没有这个“可选”配置,它就无法工作。

When this happens, you need to look to the following:发生这种情况时,您需要查看以下内容:

  1. Your EC2 instances should have a role with AmazonEC2ContainerServiceforEC2Role managed policy attached to it您的 EC2 实例应该有一个附加有AmazonEC2ContainerServiceforEC2Role托管策略的角色
  2. Your EC2 Instances should be running AMI image which is ecs-optimized (you can check this in EC2 dashboard)您的 EC2 实例应该运行经过ecs-optimized AMI 映像(您可以在 EC2 仪表板中查看)
  3. Your VPC's private subnets don't have public IPs assigned, OR you do not have an interface VPC endpoint configured, OR you don't have NAT gateway set up您的 VPC 的私有子网没有分配公有 IP,或者您没有配置接口 VPC 终端节点,或者您没有设置 NAT 网关

Most of the time, this issue appears because of the misconfigured VPC.大多数情况下,此问题的出现是因为 VPC 配置错误。 According to the Documentation :根据文档

QUOTE: If you do not have an interface VPC endpoint configured and your container instances do not have public IP addresses, then they must use network address translation (NAT) to provide this access.引用:如果您没有配置接口 VPC 终端节点,并且您的容器实例没有公共 IP 地址,则它们必须使用网络地址转换 (NAT) 来提供此访问。

  • To create a VPC endpoint: Follow to the documentation here创建 VPC 端点:按照此处的文档操作
  • To create a NAT gateway: Follow to the documentation here要创建 NAT 网关:请遵循此处的文档

These are the reasons why you don't see the EC2 instances listed in the ECS dashboard.这些是您看不到 ECS 仪表板中列出的 EC2 实例的原因。

If you have come across this issue after creating the cluster如果您在创建集群后遇到此问题

Go the ECS instance in the EC2 instances list and check the IAM role that you have assigned to the instance.转到 EC2 实例列表中的 ECS 实例并检查您分配给该实例的 IAM 角色。 You can identify the instances easily with the instance name starts with ECS Instance您可以使用以ECS Instance开头的实例名称轻松识别ECS Instance

在此处输入图片说明

After that click on the IAM role and it will direct you to the IAM console.之后单击 IAM 角色,它会将您定向到 IAM 控制台。 Select the AmazonEC2ContainerServiceforEC2Role policy from the permission policy list and save the role.从权限策略列表中选择AmazonEC2ContainerServiceforEC2Role策略并保存该角色。

Your instances will be available in the cluster shortly after you save it.您的实例将在您保存后不久在集群中可用。

The real issue is lack of permission.真正的问题是缺乏许可。 As long as you create and assign a IAM Role with AmazonEC2ContainerServiceforEC2Role permission, the problem goes away.只要您创建并分配具有 AmazonEC2ContainerServiceforEC2Role 权限的 IAM 角色,问题就会消失。

我遇到的另一个可能的原因是将我的 ECS 集群 AMI 更新为“Amazon Linux 2”AMI 而不是“Amazon Linux AMI”,这导致我的 EC2 user_data 启动脚本无法运行。

I realize this is an older thread, but I stumbled on it after seeing the error the OP mentioned while following this tutorial .我意识到这是一个较旧的线程,但是在看到 OP 在遵循本教程时提到的错误后,我偶然发现了它。

Changing to an ecs-optimized AMI image did not help.更改为ecs-optimized AMI 映像没有帮助。 My VPC already had a route 0.0.0.0/0 pointing to the subnet.我的 VPC 已经有一个指向子网的路由 0.0.0.0/0。 My instances were added to the correct cluster, and they had the proper permissions.我的实例已添加到正确的集群中,并且它们具有适当的权限。

Thanks to @sanath_p's link to this thread , I found a solution and took these steps:感谢@sanath_p 指向此线程的链接,我找到了解决方案并采取了以下步骤:

  1. Copied my Autoscaling Group's configuration复制了我的 Autoscaling Group 的配置
  2. Set IP address type under the Advanced settings to "Assign a public IP address to every instance"高级设置下的IP address type设置为“为每个实例分配一个公共IP地址”
  3. Updated my Autoscaling Group to use this new configuration.更新了我的 Autoscaling Group 以使用这个新配置。
  4. Refreshed my instances under the Instance refresh tab.实例刷新选项卡下刷新了我的实例。

for other than ecs-optimized instance image.除了 ecs 优化的实例图像。 Please do below step请执行以下步骤

ECS_CLUSTER=REPLACE_YOUR_CLUSTER_NAME ECS_CLUSTER=REPLACE_YOUR_CLUSTER_NAME

  • add above content to /etc/ecs/ecs.config将以上内容添加到 /etc/ecs/ecs.config

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

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