Skip to content

ECS scale-in no SIGTERM signal

0

We are running ECS Fargate task that is reading and processing messages from SQS queue, also there is Step Scaling setup based on number of messages in the queue. In our task we have implemented graceful shutdown logic as described in https://aws.amazon.com/blogs/containers/graceful-shutdowns-with-ecs/

In the task event log we can see that after scale-in process 1 task was stopped, it's visible in the logs that SIGTERM signal was handled and task was stopped gracefully. However, after some time in the log we can see that new task was started even though there is no event from autoscaling. Our minimum desired worked count is 3:

Enter image description here

Once task is stopped by autoscaling event log part 1. application handles SIGTERM:

Enter image description here

On the part 2. seems that for some reason ECS starts new task, but there is no event log that currently running task is stopped, ECS still keeps 3 tasks running. We found such task and in it's logs there is no SIGTERM, seems it was just killed?

Enter image description here

Any ideas why it's happening or how to gracefully handle such scenarios?

asked 2 years ago1.1K views

1 Answer
0
Accepted Answer

The behavior you're observing could be related to several factors in how Amazon ECS manages tasks during scale-in events and deployments. Here are some insights and potential explanations for what you're experiencing:

  1. Task Protection: ECS offers a feature called task scale-in protection. If your tasks are processing messages from an SQS queue, you might want to consider implementing this protection. Tasks can mark themselves as protected when they start processing work, preventing ECS from terminating them during scale-in events. After finishing their work, they can clear the protection, making themselves eligible for termination.

  2. Deployment Behavior: During ECS deployments, Application Auto Scaling turns off scale-in processes but allows scale-out processes to continue. This could explain why you're seeing a new task start without an explicit autoscaling event. It's possible that a deployment was in progress, causing this behavior.

  3. Task Replacement: ECS might be replacing tasks for various reasons, such as health checks, instance issues, or as part of its internal management processes. In some cases, ECS might start a new task before stopping an old one to ensure service continuity.

  4. Graceful Shutdown Limitations: While you've implemented graceful shutdown logic, there are scenarios where ECS might need to forcefully stop a task. For instance, if an EC2 instance is being terminated due to an Auto Scaling Group action, tasks on that instance might be forcefully stopped without receiving a SIGTERM signal.

To handle these scenarios more gracefully:

  1. Implement task scale-in protection for critical workloads. This can be done using the ECS container agent endpoint or the ECS API.

  2. Use a stop timeout on your tasks. This can delay force quitting of tasks for up to 48 hours, giving them more time to finish processing.

  3. For mission-critical tasks running on EC2 instances, consider using the ECS metadata endpoint to discover the instance ID and then use the EC2 API to set attributes like disableApiStop or disableApiTermination on the hosting instance. This provides an additional layer of protection against automated instance disruptions.

  4. Ensure your application can handle unexpected terminations gracefully, perhaps by implementing checkpointing or idempotent processing of SQS messages.

By implementing these measures, you can improve the resilience of your ECS tasks and better handle various scaling and deployment scenarios.
Sources
Protect your Amazon ECS tasks from being terminated by scale-in events - Amazon Elastic Container Service
Troubleshooting service auto scaling in Amazon ECS - Amazon Elastic Container Service
Community | Easier EC2 instance maintenance with managed draining for Amazon ECS capacity providers

answered 2 years ago

EXPERT

reviewed 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.