LiJell's 성장기

Amazon ECS managed instance draining 본문

Cloud

Amazon ECS managed instance draining

All_is_LiJell 2024. 1. 31. 15:47
반응형

Abstract

최근 ECS에 ECS managed instance draining 기능이 추가되어 관련 내용을 공유하고자 이 글을 작성하게 됐는데요, AWS EC2 instance를 capacity provider로 사용하는 분들에게 이 글이 도움이 될 것 같습니다.

 

그 동안 AWS EC2 instance를 capacity provider로 사용하게되면 draining process 최적화를 위해 Auto Scaling lifecycle hooks 로 커스텀 컨트롤이 필요했습니다.

 

How to Automate Container Instance Draining in Amazon ECS | Amazon Web Services

Update 24 Aug 2023: The approach described in this post relies on a recursive AWS Lambda function. Lambda announced a recursion control to detect and stop Lambda functions in July 2023. Accounts having recursive Lambda functions were automatically opted-ou

aws.amazon.com

하지만 이제는 Amazon ECS에서 capacitor provider의 ECS managed instance draining 기능을 이용해 새로운 버전의ECS agent로 업데이트를 하는 등 workloads에 영향 없이 seamlessly하게 Auto Scaling Group (이하 ASG) instance를 refresh할 수 있습니다.

 

따라서 저는,  ECS managed instance draining를 이용해서 기존 Lambda를 사용하지 않고 spot instance interruption 상황에서 task draining을 optimizing 할 수 있었습니다.

Instance Refresh and Draining

ASG의 EC2 instnace들은 Launch Template으로 AMI, 인스턴스 종류 등이 설정됩니다. ASG instance refresh는  새로운 버전의 AMI로 업데이트 하는 방법 중 하나입니다. 

 

Instance를 refresh해주면 ASG의 EC2 instance가 replaced 되면서 tasks들 또한 멈추게 됩니다. 이 때, ECS는 손실된 tasks를 감지하고 다시 원하는 개수의 tasks 수를 맞추기 위해서 healthy한 EC2 instance에 tasks들을 배치시키지만, reactive 하기 때문에  desired count of tasks 보다 적은 상태가 되는 경우가 생깁니다.

 

하지만, Managed Instance Draining 기능은 반응형이 아닌 proactive입니다. 기존 task들이 멈추기 전에 다른 task들이 먼저 뜰 수 있게 해줍니다. 이게 가능한 이유는, ASG이 EC2 instance를 terminate 시키면 Amazone ECS는 일시적으로 instance termination을 delay 시키고 (최대 48시간) draining mode로 바뀌게 됩니다. Drain mode인 instance는 새로운 task가 실행되지 않게되고, delay 시간동안 새로운 task들이 다른 healthy한 instance로 재배치가 가능해집니다. 

 

결과적으로 Managed Instance Draining이 workloads에 영향이 없게 instance를 refresh 할 수 있죠.

Amazon EC2 Auto Scaling lifecycle hooks with managed instance draining

Auto Scaling lifecycle hooks은 사용자가 instance lifecycle의 특정 이벤트에 triggered되는 solution을 만들거나 특정 이벤트 때 custom action을 할 수 있게 도와줍니다. ASG은 최대 50개의 hooks를 지원하죠. Abstract에서 언급했던 draining process 최적화를 위한 Auto Scaling lifecycle hooks 로 커스텀 컨트롤하는것도 이에 해당됩니다. 이제는 Managed Instance Draining를 이용하면 AWS 만들어주기 때문에 직접 만들 필요가 없어졌습니다! Managed Instance Draining은 ECS capacity provider과 함께할 때 사용할 수 있고, ASG capacity provider을 생성하거나 업데이트 할 때 활성화 할 수 있습니다.

                 The lifecycle of instances using lifecycle hooks.

Managed Instance Draining를 활성하고나면, ASG의 instance management 섹션을 확인하면 아래와 같은 그림을 확인 할 수 있습니다.

 

 

 

위 그림에서 Heartbeat timeout은 일시적으로 instance termination을 delay시키는 시간의 timeout을 말해줍니다. 저 시간내에 task stop을 graceful하게 진행이 완료되면, 저 시간에 도달하지 않아도 delay가 종료되게 됩니다.

 

주의할 점은 task stop timeout은 제한이 없지만 Heartbeat timeout은 최대 48시간이기 때문에, task stop timeout도 48시간을 넘지 않아야 합니다.

Summary

결과적으로 Amazon ECS managed instance draining을 이용하면 task draining control을 최적화 하기 위해서 따로 람다와 custom ASG lifecycle hooks를 준비하지 않아도 됐습니다.

 

Amazon ECS managed instance draining를 활성화 하면서 생성된 lifecycle hooks은 원하는데로 조정이 가능하니 참고하시면 될 것 같네요.

 

이 글의 내용이 틀린 점이 있거나 추가 의견이 있으면 댓글로 남겨주세요. 감사합니다.

 

Reference

https://docs.aws.amazon.com/autoscaling/ec2/userguide/lifecycle-hooks-overview.html

https://community.aws/content/2bMN99heQOCAAkJDC7wwB6ObVyx/manage

https://docs.aws.amazon.com/AmazonECS/latest/developerguide/managed-instance-draining.html

 

Amazon ECS managed instance draining - Amazon Elastic Container Service

Amazon ECS managed instance draining Managed instance draining facilitates graceful termination of Amazon EC2 instances. This allows your workloads to stop safely and be rescheduled to non-terminating instances. Infrastructure maintenance and updates are p

docs.aws.amazon.com

 

Easier EC2 instance maintenance with managed draining for Amazon ECS capacity providers

Learn how Amazon ECS capacity providers can help you to gracefully maintain your fleet of Amazon EC2 instances without causing downtime for your application containers.

community.aws

 

How lifecycle hooks work - Amazon EC2 Auto Scaling

How lifecycle hooks work An Amazon EC2 instance transitions through different states from the time it launches until it is terminated. You can create custom actions for your Auto Scaling group to act when an instance transitions into a wait state due to a

docs.aws.amazon.com

 

반응형
Comments