如何识别我的容器是否在 AWS ECS 上 运行?
How to identify whether my container is running on AWS ECS or not?
如果我 运行 在 AWS ECS(EC2 容器服务)中使用基于 docker 的容器,有没有一种方法可以从应用程序内部识别我的容器是否 运行 是否在 AWS ECS 上运行?这是必需的,因为我的 docker 容器可以 运行 在任何平台上,但是当它在 AWS ECS 上 运行ning 时,我需要执行一些额外的操作。
也许你可以使用 Amazon ECS Container Agent Introspection:
The Amazon ECS container agent provides an API for gathering details about the container instance that the agent is running on and the associated tasks that are running on that instance.
You can use the curl
command from within the container instance to query the Amazon ECS container agent (port 51678) and return container instance metadata or task information.
例如,从您的容器中:
[ec2-user ~]$ curl http://localhost:51678/v1/metadata
输出:
{
"Cluster": "default",
"ContainerInstanceArn": "<container_instance_ARN>",
"Version": "Amazon ECS Agent - v1.14.1 (467c3d7)"
}
OP 提到的另一个标准 , is the Instance MetaData and User Data
Instance metadata is data about your instance that you can use to configure or manage the running instance. Instance metadata is divided into categories.
To view all categories of instance metadata from within a running instance, use the following URI:
http://169.254.169.254/latest/meta-data/
Note that you are not billed for HTTP requests used to retrieve instance metadata and user data.
You can use a tool such as cURL, or if your instance supports it, the GET command; for example:
[ec2-user ~]$ curl http://169.254.169.254/latest/meta-data/
所以一个成功的curl
就够了:
curl -sL -w "%{http_code}\n" "http://169.254.169.254/latest/meta-data/" -o /dev/null
如果OK的话会显示200
参见“Linux script with curl to check webservice is up”
经过反复试验,我发现以下内容最有帮助:
调用 http://169.254.169.254/latest/meta-data/
如果您得到 200 OK,那么您可以假设您在 AWS EC2/ECS.
中 运行
但是,如果你没有得到 200 OK,那么你就不是 运行 in AWS EC2/ECS.
我已经尝试过在此回复之前发布的其他替代方案,但它们都不适用于 ECS。我不知道它是否已经改变,但现在,如果你想验证容器是否在 ECS 上 运行,你应该调用:http://169.254.170.2/v2/metadata/
更多信息:https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-metadata-endpoint-v2.html
如果你想验证容器是否在 EC2 上 运行,你应该调用:http://169.254.169.254/latest/meta-data/
更多信息:https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instancedata-data-retrieval.html
如果我 运行 在 AWS ECS(EC2 容器服务)中使用基于 docker 的容器,有没有一种方法可以从应用程序内部识别我的容器是否 运行 是否在 AWS ECS 上运行?这是必需的,因为我的 docker 容器可以 运行 在任何平台上,但是当它在 AWS ECS 上 运行ning 时,我需要执行一些额外的操作。
也许你可以使用 Amazon ECS Container Agent Introspection:
The Amazon ECS container agent provides an API for gathering details about the container instance that the agent is running on and the associated tasks that are running on that instance.
You can use the
curl
command from within the container instance to query the Amazon ECS container agent (port 51678) and return container instance metadata or task information.
例如,从您的容器中:
[ec2-user ~]$ curl http://localhost:51678/v1/metadata
输出:
{
"Cluster": "default",
"ContainerInstanceArn": "<container_instance_ARN>",
"Version": "Amazon ECS Agent - v1.14.1 (467c3d7)"
}
OP 提到的另一个标准
Instance metadata is data about your instance that you can use to configure or manage the running instance. Instance metadata is divided into categories.
To view all categories of instance metadata from within a running instance, use the following URI:
http://169.254.169.254/latest/meta-data/
Note that you are not billed for HTTP requests used to retrieve instance metadata and user data.
You can use a tool such as cURL, or if your instance supports it, the GET command; for example:
[ec2-user ~]$ curl http://169.254.169.254/latest/meta-data/
所以一个成功的curl
就够了:
curl -sL -w "%{http_code}\n" "http://169.254.169.254/latest/meta-data/" -o /dev/null
如果OK的话会显示200
参见“Linux script with curl to check webservice is up”
经过反复试验,我发现以下内容最有帮助:
调用 http://169.254.169.254/latest/meta-data/
如果您得到 200 OK,那么您可以假设您在 AWS EC2/ECS.
中 运行
但是,如果你没有得到 200 OK,那么你就不是 运行 in AWS EC2/ECS.
我已经尝试过在此回复之前发布的其他替代方案,但它们都不适用于 ECS。我不知道它是否已经改变,但现在,如果你想验证容器是否在 ECS 上 运行,你应该调用:http://169.254.170.2/v2/metadata/
更多信息:https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-metadata-endpoint-v2.html
如果你想验证容器是否在 EC2 上 运行,你应该调用:http://169.254.169.254/latest/meta-data/
更多信息:https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instancedata-data-retrieval.html