问题
I need to run a clean up script every time my spot instance is scheduled for termination. I have two reasons for termination:
- A price for spot instance exceeds my bid
- Downscaling caused by scaling policies of autoscaling group (based on average cluster cpu usage in 5 mins, random instance is terminated).
I did like the simple script below I found on this blog. It covers first case and gives me 2 minutes to clean up before shutdown.
Is there an easy way to get such 2-minute notification for second case?
May be the warning will be available via same url?
#!/bin/bash
while true
do
if [ -z $(curl -Is http://169.254.169.254/latest/meta-data/spot/termination-time | head -1 | grep 404 | cut -d \ -f 2) ]
then
logger "Running shutdown hook."
# Call your shutdown script here.
break
else
# Spot instance not yet marked for termination.
sleep 5
fi
done
回答1:
Auto Scaling Hooks allow you to create an event notification that an instance is going to be terminated (or launched).
The up-side: you get a lot more time. The default is 60 minutes, and you can send a heartbeat message to extend the time, or you can notify the service when your cleanup is done. If you don't respond, the event times out and finishes anyway.
The down-side: the notifications don't go to the instance itself, as the spot termination warnings do. They go to SNS or SQS. You'll either have to subscribe the instance to the notifications (subscribe each instance to the SNS topic on instance startup, for example) or you'll need to roll your own system to manage the messages and communicate with the instances.
来源:https://stackoverflow.com/questions/32613600/ec2-spot-instance-termination-notice-when-downscaling