【问题标题】:Launch ECS container instance to cluster and run task definition using userdata启动 ECS 容器实例到集群并使用 userdata 运行任务定义
【发布时间】:2018-05-01 10:47:31
【问题描述】:

我正在尝试启动 ECS contianer 实例并通过 userdata 将其注册到集群并开始运行任务定义。

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

我正在使用 AWS 文档上的指南在容器启动时启动任务。 用户数据下方(集群和任务定义参数省略)

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==--

创建实例后,它会启动到默认集群,而不是我在用户数据中指定的集群,并且不会启动任何任务。

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

任何帮助将不胜感激。

【问题讨论】:

    标签: amazon-web-services docker amazon-ec2 amazon-ecs user-data


    【解决方案1】:

    来自AWS Documentation

    使用用户数据配置您的 Amazon ECS 容器实例,例如 来自 Amazon ECS 容器代理的代理环境变量 配置。 Amazon EC2 用户数据脚本只执行一个 实例首次启动的时间。

    默认情况下,您的容器实例启动到您的默认值 簇。要启动到非默认集群,请选择 Advanced 详细列表。然后,将以下脚本粘贴到用户数据中 字段,将 your_cluster_name 替换为您的集群名称。

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

    # Specify the cluster that the container instance should register into
    cluster=your_cluster_name
    

    将 your_cluster_name 更改为集群的名称。

    【讨论】:

    • 我刚刚从指南中复制并粘贴了该代码。我正在更改集群和任务定义参数。
    • 你能改变你的问题并包含修改后的文件吗?
    猜你喜欢
    • 2019-11-08
    • 1970-01-01
    • 2016-07-31
    • 2022-01-11
    • 2023-03-30
    • 1970-01-01
    • 2021-08-02
    • 1970-01-01
    • 2020-07-22
    相关资源
    最近更新 更多