如何从 ECS 容器中获取任务 ID? [英] How to get Task ID from within ECS container?

查看:39
本文介绍了如何从 ECS 容器中获取任务 ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我有兴趣从位于 EC2 主机内部的正在运行的容器中检索 Task ID.

Hello I am interested in retrieving the Task ID from within inside a running container which lives inside of a EC2 host machine.

AWS ECS 文档 声明有一个环境变量 ECS_CONTAINER_METADATA_FILE 与此数据的位置,但只有在集群/EC2 实例创建时 ECS_ENABLE_CONTAINER_METADATA 变量设置为 true 时才会设置/可用.我看不到可以在 aws 控制台中的哪个位置完成此操作.

AWS ECS documentation states there is an environment variable ECS_CONTAINER_METADATA_FILE with the location of this data but will only be set/available if ECS_ENABLE_CONTAINER_METADATA variable is set to true upon cluster/EC2 instance creation. I don't see where this can be done in the aws console.

此外,文档声明这可以通过在主机内将其设置为 true 来完成,但需要重新启动 docker 代理.

Also, the docs state that this can be done by setting this to true inside the host machine but would require to restart the docker agent.

有没有其他方法可以做到这一点,而无需进入 EC2 内部进行设置并重新启动 docker 代理?

Is there any other way to do this without having to go inside the EC2 to set this and restart the docker agent?

推荐答案

我使用的技术是在容器定义中设置环境变量.

The technique I'd use is to set the environment variable in the container definition.

如果您正在管理您的任务 通过Cloudformation,相关的yaml长这样:

If you're managing your tasks via Cloudformation, the relevant yaml looks like so:

  Taskdef:
    Type: AWS::ECS::TaskDefinition
    Properties:
      ...
      ContainerDefinitions:
        - Name: some-name
          ...
          Environment:
            - Name: AWS_DEFAULT_REGION
              Value: !Ref AWS::Region
            - Name: ECS_ENABLE_CONTAINER_METADATA
              Value: 'true'

此技术可帮助您保持一切简单且可重复.

This technique helps you keep everything straightforward and reproducible.

如果您以编程方式需要元数据并且无权访问元数据文件,您可以查询 代理的元数据端点:

If you need metadata programmatically and don't have access to the metadata file, you can query the agent's metadata endpoint:

curl http://localhost:51678/v1/metadata

请注意,如果您将此信息作为正在运行的任务获取,您可能无法连接到环回设备,但您可以连接到 EC2 实例自己的 IP 地址.

Note that if you're getting this information as a running task, you may not be able to connect to the loopback device, but you can connect to the EC2 instance's own IP address.

这篇关于如何从 ECS 容器中获取任务 ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆