简体   繁体   English

AWS ECS SDK。使用SDK为ECS集群注册新的容器实例(EC2)

[英]AWS ECS SDK.Register new container instance (EC2) for ECS Cluster using SDK

I've faced with the problem while using AWS SDK. 使用AWS开发工具包时遇到了问题。 Currently I am using SDK for golang, but solutions from other languages are welcome too! 目前,我正在将SDK用于golang,但也欢迎使用其他语言的解决方案!

I have ECS cluster created via SDK 我通过SDK创建了ECS集群

Now I need to add EC2 containers for this cluster. 现在,我需要为该集群添加EC2容器。 My problem is that I can't use Amazon ECS Agent to specify cluster name via config: 我的问题是我无法使用Amazon ECS代理通过配置指定集群名称:

#!/bin/bash
echo ECS_CLUSTER=your_cluster_name >> /etc/ecs/ecs.config

or something like that. 或类似的东西。 I can use only SDK . 我只能使用SDK

I found method called RegisterContainerInstance . 我找到了名为RegisterContainerInstance的方法。

But it has note: 但要注意:

This action is only used by the Amazon ECS agent, and it is not intended for use outside of the agent. 此操作仅由Amazon ECS代理使用,并且不适用于该代理以外的用途。

It doesn't look like working solution. 它看起来不像是可行的解决方案。

I need to understand how (if it's possible) to create working ECS cluster using SDK only. 我需要了解如何(如果可能)仅使用SDK创建有效的ECS cluster

UPDATE: My main target is that I need to start specified count of servers from my Docker image. 更新:我的主要目标是我需要从Docker映像中启动指定数量的服务器。 While I am investigating this task i've found that I need: 在研究此任务时,我发现我需要:

  • create ECS cluster 创建ECS集群

  • assign to it needed count of ec2 instances. 为其分配所需的ec2实例数。

  • create Task with my Docker image. 用我的Docker镜像创建Task

  • run it on cluster manually or as service. 手动或作为服务在群集上运行它。

So I: 所以我:

  1. Created new cluster via CreateCluster method with name "test-cluster". 通过名称为“ test-cluster”的CreateCluster方法创建了新集群。

  2. Created new task via RegisterTaskDefinition 通过RegisterTaskDefinition创建了新任务

  3. Created new EC2 instance with ecsInstanceRole role with ecs-optimized AMI type, that is correct for my region. 创建具有ecsInstanceRole角色且具有经ecs优化的AMI类型的新EC2实例,该实例对我的区域是正确的。 And there place where problems had started. 那里开始出现问题。

Actual result: All new ec2 instances had attached to "default" cluster (AWS created it and attach instance to it). 实际结果:所有新的ec2实例都已附加到“默认”群集(AWS创建了该实例并将其附加到实例)。 If I am using ECS agent I can specify cluster name by using ECS_CLUSTER config env. 如果使用的是ECS代理,则可以使用ECS_CLUSTER config env指定群集名称。 But I am developing tool that use only SDK (without any ability of using ECS agent). 但是我正在开发仅使用SDK的工具(没有使用ECS代理的任何功能)。 With RegisterTaskDefinition I haven't any possibility to specify cluster, so my question, how I can assign new EC2 instance exactly to specified cluster? 使用RegisterTaskDefinition,我无法指定集群,所以我的问题是,如何将新的EC2实例完全分配给指定的集群?

When I had tried to just start my task via RunTask method (with hoping that AWS somehow create instances for me or something like that) I receive an error: 当我尝试仅通过RunTask方法启动任务(希望AWS以某种方式为我创建实例或类似的实例)时,我收到一条错误消息:

InvalidParameterException: No Container Instances were found in your cluster.

I actually can't sort out which question you are asking. 我实际上无法弄清您在问哪个问题。 Do you need to add containers to the cluster, or add instances to the cluster? 您是否需要将容器添加到集群,或将实例添加到集群? Those are very different. 这些是非常不同的。

Add instances to the cluster 将实例添加到集群

This is not done with the ECS API, it is done with the EC2 API by creating EC2 instances with the correct ecsInstanceRole. ECS API不能做到这一点,而EC2 API可以通过使用正确的ecsInstanceRole创建EC2实例来实现。 See the Launching an Amazon ECS Container Instance documentation for more information. 有关更多信息,请参阅启动Amazon ECS容器实例文档。

Add containers to the cluster 将容器添加到集群

This is done be defining a task definition, then running those tasks manually or as services. 可以先定义任务定义,然后手动或作为服务运行这些任务。 See the Amazon ECS Task Definitions for more information. 有关更多信息,请参阅Amazon ECS任务定义

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

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