如何在终止时自动从AWS ELB取消注册EC2实例?

时间:2022-12-04 07:53:27

If an EC2 instance is a member of an AWS Elastic Load Balancer (ELB), terminating this EC2 instance won't remove it from the ELB.

如果EC2实例是AWS Elastic Load Balancer(ELB)的成员,则终止此EC2实例不会将其从ELB中删除。

It will be marked as N/A, and out of service. Our monitoring system then notify us of unhealthy instances in the ELB.

它将标记为N / A,并且已停止服务。然后,我们的监控系统会通知我们ELB中的不健康情况。

Our current solution is a boto script that polls and verify every ELB members, on a regular basis. That's working but that's a pull data architecture that bogs our AWS API throttling.

我们当前的解决方案是一个boto脚本,定期轮询和验证每个ELB成员。这是有效的,但这是一个拉动数据架构,使我们的AWS API限制陷入困境。

Is there an option, that automatically removes terminated/missing instances from ELB ? Alternatively, is it possible on a pushed data (SQS, SNS) instead of pulling the data (API, w/boto) ?

是否有一个选项,可以自动从ELB中删除已终止/丢失的实例?或者,是否可以在推送数据(SQS,SNS)上而不是拉动数据(API,w / boto)?

1 个解决方案

#1


I can understand the problem of having the need for polling as you have taken the Pull Design [ poll for changes and pull the boto script to make the changes ].

我可以理解需要轮询的问题,因为你已经采取了拉动设计[轮询更改并拉动boto脚本进行更改]。

The simpler approach would be to use a Push Method i.e. make the instance or remove the instance explicitly on-demand.

更简单的方法是使用推送方法,即按需显示实例或删除实例。

There are couple of ways you can do; few ways I can think of

有几种方法可以做;我能想到的几种方式

  1. I believe the terminate API call will result in Graceful shutdown followed by termination - If my understanding is right then you can use On-Shutdown scripts that can be triggered to remove itself behind the ELB. You can get the help of the EC2 Metadata to identify itself - the instance ID; so you can place the same script in every instance or make an AMI.

    我相信终止API调用将导致正常关闭然后终止 - 如果我的理解是正确的,那么你可以使用可以触发的On-Shutdown脚本来移除ELB后面的自身。您可以获得EC2元数据的帮助来识别自身 - 实例ID;所以你可以在每个实例中放置相同的脚本或制作AMI。

  2. Use Lambda function to Listen for the CLoudTrail Logs using the SNS [ CloudTrail -> SNS -> Lambda Invoke ], parse them for the "eventName": "TerminateInstances" and then look for the instance ID and remove that from the ELB.

    使用Lambda函数使用SNS [CloudTrail - > SNS - > Lambda Invoke]侦听CLoudTrail日志,解析它们以查找“eventName”:“TerminateInstances”,然后查找实例ID并将其从ELB中删除。

  3. This would more like [ 1a + 2a ] - where instead running the boto (or script), you leave a SNS message that would go call the lambda which in turn would remove the instance from the ELB. Would be helpful and easy to implement the logic of conditional removal or changes in use case.

    这更像是[1a + 2a] - 而是运行boto(或脚本),你留下一个SNS消息,它将调用lambda,而lambda又将从ELB中删除实例。将有用且易于实现条件删除或用例更改的逻辑。

#1


I can understand the problem of having the need for polling as you have taken the Pull Design [ poll for changes and pull the boto script to make the changes ].

我可以理解需要轮询的问题,因为你已经采取了拉动设计[轮询更改并拉动boto脚本进行更改]。

The simpler approach would be to use a Push Method i.e. make the instance or remove the instance explicitly on-demand.

更简单的方法是使用推送方法,即按需显示实例或删除实例。

There are couple of ways you can do; few ways I can think of

有几种方法可以做;我能想到的几种方式

  1. I believe the terminate API call will result in Graceful shutdown followed by termination - If my understanding is right then you can use On-Shutdown scripts that can be triggered to remove itself behind the ELB. You can get the help of the EC2 Metadata to identify itself - the instance ID; so you can place the same script in every instance or make an AMI.

    我相信终止API调用将导致正常关闭然后终止 - 如果我的理解是正确的,那么你可以使用可以触发的On-Shutdown脚本来移除ELB后面的自身。您可以获得EC2元数据的帮助来识别自身 - 实例ID;所以你可以在每个实例中放置相同的脚本或制作AMI。

  2. Use Lambda function to Listen for the CLoudTrail Logs using the SNS [ CloudTrail -> SNS -> Lambda Invoke ], parse them for the "eventName": "TerminateInstances" and then look for the instance ID and remove that from the ELB.

    使用Lambda函数使用SNS [CloudTrail - > SNS - > Lambda Invoke]侦听CLoudTrail日志,解析它们以查找“eventName”:“TerminateInstances”,然后查找实例ID并将其从ELB中删除。

  3. This would more like [ 1a + 2a ] - where instead running the boto (or script), you leave a SNS message that would go call the lambda which in turn would remove the instance from the ELB. Would be helpful and easy to implement the logic of conditional removal or changes in use case.

    这更像是[1a + 2a] - 而是运行boto(或脚本),你留下一个SNS消息,它将调用lambda,而lambda又将从ELB中删除实例。将有用且易于实现条件删除或用例更改的逻辑。