29

How do I automatically restart a preemptible Google Compute Engine instance? I only have one instance that doesn't need 100% uptime but that I would like to restart once the data center becomes unloaded again. The instance/server that I'm trying to automatically restart has its own boot disk that I'd like to use each time it restarts.

3 Answers 3

22
Answer recommended by Google Cloud Collective

You could try using Instance Group Manager to set up a pool of size 1. It will then try to re-create instances after they are preempted.

You should be aware that there is no guarantee that there is going to be capacity for your instance. As the docs say:

Preemptible instances are available from a finite amount of Compute Engine resources, and might not always be available.

Sign up to request clarification or add additional context in comments.

5 Comments

A very strange thing is happening. I'm able to setup the instance group manager to use the persistent disk that I want, and I'm able to have GCE start the instance automatically. But, overnight, the machine has become unreachable, even via SSH, even though the spawned instance is still stoppable and burning 6% CPU.
Update: it seems to be working properly over the last 12 hours. Given the mystery of the failure, I'm inclined to believe that it was simply a ephemeral problem with GCE. That, or it only occurs when it has to restart the instance.
Regarding "Preemptible instances are available from a finite amount of Compute Engine resources, and might not always be available." during the last two weeks there have been several days when no preemptible instances (of any size) could be started in the us-central1-c zone. That's just my personal experience. The caveat in the docs should be taken very seriously.
This is mostly working now, but about once a week the instance becomes unreachable via the web or even the Google ssh keys, even though the GCE GUI shows that it's up. Anyone else have this problem? I'm under the impression that when resources are actually unavailable, that the instance should show up as down, not up. Resetting the instance fixes the problem, but I'm trying to avoid manual steps like that.
A little bit more detail in this answer would be great, e.g. what type of instance group should be created.
11

You could create a f1-micro instance which is free for one instance per month in several data centers and create a cron job

*/10 * * * * /snap/bin/gcloud beta compute instances start --zone "yourzone" "yourinstance" --project "yourproject" 

after you ran gcloud auth login once.

This will restart your instance every 10 minutes. Of course you can set this also to an hour or more. With a bit more scripting also things like exponential back off can be done.

Comments

0

If you'd like to restart it less frequently, you can use Instance schedules that's built in to the Google Cloud Dashboard.

https://cloud.google.com/compute/docs/instances/schedule-instance-start-stop

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.