How to save state when preempted on a Google preem

2019-06-08 06:13发布

问题:

I need to be able to save the state of a Centos-based Google Compute instance when it first receives a preemption signal. The documentation very clearly indicates that a "ACPI G2 Soft Off" signal is sent 30 seconds before a preemptible shutdown. Unfortunately, Google has given no examples to demonstrate how to capture this signal and I have not yet found a way to capture it.

My initial attempts have all been focused around the ACPI Interface using the acpid daemon. Using it, I have been able to capture the shutdown (button/power) event without any problems. But no matter what event I try to capture (button, power, sleep) I have not been able to capture the state transition to the G2 state, which I presume is what I am looking for.

Has anyone successfully managed to capture the preemption signal in a Centos (or any Linux) environment?

回答1:

You may want to use shutdown scripts for this:

Create and run shutdown scripts that allow you to execute commands right before an instance is terminated or restarted, on a best-effort basis. This is useful if you rely on automated scripts to start up and shut down instances, allowing your instances time to clean up or perform tasks, such as exporting logs, or syncing with other systems.

If you have a managed instance group that is being autoscaled, shutdown scripts can be useful because if an instance is shut down due to autoscaling, the shutdown script performs any actions you define before the instance is terminated. For example, your shutdown script might copy processed data or back up any logs.

Shutdown scripts function very similarly to startup scripts; much of the documentation for startup scripts also applies for shutdown scripts.

However, since you have limited time to do the work, consider using persistent external storage systems which won't be deleted when your instance is preempted, e.g., Google Cloud Storage, Google Cloud Bigtable, Google Cloud Datastore, or others.