在容器实例启动时启动任务 - Amazon Elastic Container Service
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

在容器实例启动时启动任务

根据您的应用程序架构设计,您可能需要在每个容器实例上运行一个特定容器以处理操作或安全问题,例如监控、安全性、指标、服务发现或日志记录。

为此,可以将容器实例配置为在启动时或在某些 init 系统(如 Upstart 或 systemd)中使用用户数据脚本调用 docker run 命令。虽然此方法可行,但它有一些缺点,因为 Amazon ECS 不了解容器并且无法监控 CPU、内存、端口或已使用的任何其他资源。要确保 Amazon ECS 可正确了解所有任务资源,请为要在容器实例上运行的容器创建任务定义。然后,使用 Amazon ECS 在启动时利用 Amazon EC2 用户数据放置任务。

以下过程中的 Amazon EC2 用户数据脚本使用 Amazon ECS 自检 API 来确定容器实例。然后,它使用 Amazon CLI 和start-task命令在启动期间自行运行指定的任务。

在容器实例启动时启动任务
  1. 如果您尚未这样做,请按照使用控制台创建任务定义中的过程操作以使用要在启动时在容器实例上运行的容器创建任务定义。

  2. 修改您的 ecsInstanceRole IAM 角色以便为 StartTask API 操作添加权限。有关更多信息,请参阅 Amazon ECS 容器实例 IAM 角色

    1. 通过以下网址打开 IAM 控制台:https://console.aws.amazon.com/iam/

    2. 在导航窗格中,选择角色

    3. 选择 ecsInstanceRole。如果角色不存在,请使用 Amazon ECS 容器实例 IAM 角色中的过程来创建角色并返回此过程。如果角色存在,请选择角色以查看附加的策略。

    4. 权限选项卡中,选择 添加内联策略

    5. 对于 Service (服务),请依次选择 Choose a service (选择服务)Elastic Container Service

    6. 对于 “操作”,StartTask在搜索字段中键入,然后选择StartTask

    7. 对于资源,请选择所有资源,然后选择查看策略

    8. 查看策略页面上,输入策略的名称,例如 ecs-start-task,然后选择创建策略

  3. 通过执行 启动 Amazon ECS Linux 容器实例 中的过程(但在 步骤 7 中,将以下 MIME 多部分用户数据脚本复制并粘贴到 User data 字段中)使用经 Amazon ECS 优化的 Amazon Linux 2 AMI 启动一个或多个容器实例。将 your_cluster_name 替换为要注册到的容器实例的集群,并将 my_task_def 替换为要在启动时在实例上运行的任务定义。

    注意

    以下 MIME 分段内容使用 Shell 脚本设置配置值和安装程序包。它还将使用 Upstart 作业在 ecs 服务已在运行且自检 API 可用后启动任务。

    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=your_cluster_name # 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 START_TASK_SCRIPT_FILE="/etc/ecs/ecs-start-task.sh" cat <<- 'EOF' > ${START_TASK_SCRIPT_FILE} exec 2>>/var/log/ecs/ecs-start-task.log set -x # Install prerequisite tools yum install -y jq aws-cli # Wait for the ECS service to be responsive until curl -s http://localhost:51678/v1/metadata do sleep 1 done # Grab the container instance ARN and Amazon 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 Amazon 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 EOF # Write systemd unit file UNIT="ecs-start-task.service" cat <<- EOF > /etc/systemd/system/${UNIT} [Unit] Description=ECS Start Task Requires=ecs.service After=ecs.service [Service] Restart=on-failure RestartSec=30 ExecStart=/usr/bin/bash ${START_TASK_SCRIPT_FILE} [Install] WantedBy=default.target EOF # Enable our ecs.service dependent service with `--no-block` to prevent systemd deadlock # See https://github.com/aws/amazon-ecs-agent/issues/1707 systemctl enable --now --no-block "${UNIT}" --==BOUNDARY==--
  4. 验证您的容器实例是否启动到正确的集群中以及您的任务是否已启动。

    1. https://console.aws.amazon.com/ecs/v2 打开控制台。

    2. 在导航栏中,选择您的集群所在的区域。

    3. 在导航窗格中,选择 Clusters 并选择托管您的容器实例的集群。

    4. 集群页面上,选择任务,然后选择您的任务。

      您启动的每个容器实例都应运行您的任务。

      如果没有看到任务,可以使用 SSH 登录容器实例,在 /var/log/ecs/ecs-start-task.log 文件中查看调试信息。