首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >启动ECS容器实例,使用userdata集群并运行任务定义。

启动ECS容器实例,使用userdata集群并运行任务定义。
EN

Stack Overflow用户
提问于 2018-05-01 10:47:31
回答 1查看 1.1K关注 0票数 0

我正在尝试启动一个ECS contianer实例,并通过用户数据将其注册到集群中,并开始运行任务定义。

当任务完成时,实例将被终止。

我正在使用AWS文档指南在容器启动时启动一个任务。在userdata下面(省略了集群和任务def参数)

代码语言:javascript
运行
复制
Content-Type: multipart/mixed; boundary="==BOUNDARY=="
MIME-Version: 1.0

--==BOUNDARY==
Content-Type: text/x-shellscript; charset="us-ascii"

#!/bin/bash
# Specify the cluster that the container instance should register into
cluster=my_cluster

# Write the cluster configuration variable to the ecs.config file
# (add any other configuration variables here also)
echo ECS_CLUSTER=$cluster >> /etc/ecs/ecs.config

# Install the AWS CLI and the jq JSON parser
yum install -y aws-cli jq

--==BOUNDARY==
Content-Type: text/upstart-job; charset="us-ascii"

#upstart-job
description "Amazon EC2 Container Service (start task on instance boot)"
author "Amazon Web Services"
start on started ecs

script
    exec 2>>/var/log/ecs/ecs-start-task.log
    set -x
    until curl -s http://localhost:51678/v1/metadata
    do
        sleep 1
    done

    # Grab the container instance ARN and AWS region from instance metadata
    instance_arn=$(curl -s http://localhost:51678/v1/metadata | jq -r '. | .ContainerInstanceArn' | awk -F/ '{print $NF}' )
    cluster=$(curl -s http://localhost:51678/v1/metadata | jq -r '. | .Cluster' | awk -F/ '{print $NF}' )
    region=$(curl -s http://localhost:51678/v1/metadata | jq -r '. | .ContainerInstanceArn' | awk -F: '{print $4}')

    # Specify the task definition to run at launch
    task_definition=my_task_def

    # Run the AWS CLI start-task command to start your task on this container instance
    aws ecs start-task --cluster $cluster --task-definition $task_definition --container-instances $instance_arn --started-by $instance_arn --region $region
end script
--==BOUNDARY==--

当创建实例时,它将被启动到默认集群,而不是我在userdata中指定的集群,并且没有启动任务。

我已经解构了上面的脚本,找出它的失败之处,但我没有运气。

任何帮助都将不胜感激。

EN

回答 1

Stack Overflow用户

发布于 2018-05-01 12:36:26

来自AWS文件

使用用户数据配置Amazon容器实例,例如Amazon容器代理配置中的代理环境变量。当第一次启动该实例时,亚马逊EC2用户数据脚本只执行一次。 默认情况下,容器实例将启动到默认集群中。若要启动到非默认群集,请选择“高级详细信息”列表。然后,将下面的脚本粘贴到用户数据字段中,用集群的名称替换your_cluster_name。

因此,为了能够将这个EC2实例添加到您的ECS集群中,您应该将这个变量更改为集群的名称:

代码语言:javascript
运行
复制
# Specify the cluster that the container instance should register into
cluster=your_cluster_name

将your_cluster_name更改为集群的任何名称。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50115174

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档