简体   繁体   English

如何找出与ALB关联的ECS群集

[英]How to find out which ECS cluster is associated to an ALB

We run an ECS cluster behind an ELB (ALB, to be specific). 我们在ELB后面运行一个ECS集群(ALB,具体而言)。

I have a process that allows me to find out which ECS cluster is associated with the ALB by querying the ALB and tracing the results back through the target group and then instances: 我有一个流程允许我通过查询ALB并通过目标组追溯结果然后实例来找出哪个ECS群集与ALB相关联:


Here is the bash script: 这是bash脚本:

ELB_NAME=$(aws route53 list-resource-record-sets --hosted-zone-id <Zone-ID> | jq -r --arg URL "$URL"'.ResourceRecordSets[]|select(.Name==$URL)|.AliasTarget.DNSName')

ELB_NAME=$(echo $ELB_NAME | cut -f 2- -d "." | rev | cut -f 2- -d "." | rev)

ELB_ARN=$(aws elbv2 describe-load-balancers | jq -r --arg ELB_NAME "$ELB_NAME" '.LoadBalancers[]|select((.DNSName|ascii_downcase)==$ELB_NAME)|.LoadBalancerArn')

TG_ARNS=$(aws elbv2 describe-target-groups | jq -r --arg ELB_ARN "$ELB_ARN" '.TargetGroups[]|select(.LoadBalancerArns[]==$ELB_ARN)|.TG_ARN=$(echo $TG_ARNS | cut -f 1 -d " ")

INSTANCE_ID=$(aws elbv2 describe-target-health --target-group-arn $TG_ARN | jq -r '.TargetHealthDescriptions[].Target.Id' | head -n 1)

CLUSTER=$(aws ec2 describe-instances --instance-ids $INSTANCE_ID | jq -r '.Reservations[].Instances[].Tags[]|select(.Key=="aws:cloudformation:stack-name")|.Value' | cut -f 2 -d "-")

The problem I have is that when there are no running instances associated with the ECS cluster, I can no longer query them for the the tag that returns the Cloudformation stack name, the request for the targets from the target group is empty. 我遇到的问题是,当没有与ECS集群关联的运行实例时,我无法再查询它们返回Cloudformation堆栈名称的标记,来自目标组的目标请求为空。

How can I use the AWS API so that I can determine which ECS cluster the ALB would target if it had running instances? 如何使用AWS API以便我可以确定ALB在运行实例时将针对哪个ECS群集?

It's not really clear what you're asking for, or indeed the purpose you are trying to achieve, but the following should set you on the right track. 目前还不是很清楚你要求的是什么,或者你想要实现的目的,但以下内容应该让你走上正轨。

An ECS "cluster" is really just an Amazon service, when you create a cluster nothing is really provisioned. ECS“集群”实际上只是一个亚马逊服务,当您创建集群时,实际上没有配置任何内容。 You can think of an empty cluster as a record or a placeholder in the ECS service. 您可以将空集群视为ECS服务中的记录或占位符。

In order to do anything with a cluster, it needs instances. 为了对集群做任何事情,它需要实例。 When you boot an EC2 machine from a supported AMI, appropriate IAM role and the cluster name written to a config file, the instance will join the cluster. 从受支持的AMI(适当的IAM角色和写入配置文件的群集名称)引导EC2计算机时,该实例将加入群集。 (If you create a cluster via the AWS console, a CloudFormation template is created that handles the provisioning and orchestration of these steps.) The ECS cluster management can then schedule tasks and services onto that instance as you have defined in the ECS service. (如果通过AWS控制台创建集群,则会创建一个CloudFormation模板,用于处理这些步骤的配置和编排。)然后,ECS集群管理可以按照您在ECS服务中的定义将任务和服务调度到该实例上。

Without any instances, there can be no listening containers, therefore there can be no target groups in your ALB that route to anything. 没有任何实例,就没有监听容器,因此ALB中没有可以路由到任何东西的目标组。 So it is not possible to get from the ELB to the cluster... as you have asked when there are no running instances. 因此,无法get from the ELB to the cluster...因为您已经询问何时没有正在运行的实例。

You might find the following commands are a better way of determining whether or not you have a running cluster. 您可能会发现以下命令是确定是否具有正在运行的集群的更好方法。

First, use the list-clusters command to show which clusters are available: 首先,使用list-clusters命令显示哪些集群可用:

aws ecs list-clusters 
{
    "clusterArns": [
        "arn:aws:ecs:eu-west-1:XXXXXXXXX:cluster/your_cluster"
    ]
}

Then use the output from that to show if there are any EC2 instances registered to the cluster: 然后使用它的输出来显示是否有任何EC2实例注册到集群:

aws ecs describe-clusters --clusters your_cluster
{
    "clusters": [
        {
            "status": "ACTIVE", 
            "statistics": [], 
            "clusterName": "your_cluster", 
            "registeredContainerInstancesCount": 1, 
            "pendingTasksCount": 0, 
            "runningTasksCount": 0, 
            "activeServicesCount": 0, 
            "clusterArn": "arn:aws:ecs:eu-west-1:XXXXXXXXX:cluster/your_cluster"
        }
    ], 
    "failures": []
}

Note the registeredContainerInstancesCount property shows the number of running instances. 请注意, registeredContainerInstancesCount属性显示正在运行的实例数。 I assume you have your ECS services set to register tasks (containers) with the ALB, so when the count is greater than 0 , this will be possible. 我假设您已将ECS服务设置为使用ALB注册任务(容器),因此当计数大于0 ,这将是可能的。

So, querying that property should tell you if your cluster is " on " or not: 因此,查询该属性应该告诉您群集是否“ 打开 ”:

if [[ $(aws ecs describe-clusters --clusters your_cluster | jq -r '.clusters[].registeredContainerInstancesCount') -gt 0 ]] ; then 
  echo "cluster is on"
else 
  echo "cluster is off"
fi

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM