GCP has a VM called Preemptible VM that stops in up to 24 hours at the cost of a low price. https://cloud.google.com/compute/docs/instances/preemptible?hl=ja
VMs that are stopped after receiving the preempt notification will not start automatically. The following is a description of the tools that enable automatic startup using shutdown scripts and Cloud Functions.
--Preemptible VM is a tool to start because it does not start automatically after stopping. --It is designed to start only when you receive a preempt notification so that it will not start if you stop it manually. --The execution environment is python3.7.
Download the source code
git clone [email protected]:cloud-mainte/auto-boot-preemtible-vm.git
or
git clone https://gitlab.com/cloud-mainte/auto-boot-preemtible-vm.git
Create a GCS bucket
--Create a bucket to place the shutdown script and place the shutdown script.
--Rewrite the BUCKET
variable in the script as appropriate.
$ gsutil mb -l ASIA-NORTHEAST1 gs://<backet name>
$ gsutil cp shutdown-script.sh gs://<backet name>/
$ gsutil ls -l gs://<backet name>
--Create a bucket to store the status of whether or not you received a preempt notification.
```
$ gsutil mb -l ASIA-NORTHEAST1 gs://<backet name>
```
Set shutdown-script on the target instance
gcloud compute instances add-metadata <instance name> \
--metadata shutdown-script-url=gs://<backet name>/shutdown-script.sh
Set the instance launcher in Cloud Functions
--Rewrite bucket
in main.py as appropriate.
gcloud functions deploy boot_preemptible_instance --region=asia-northeast1 --
entry-point boot_instance --runtime python37 --trigger-event
google.storage.object.finalize --trigger-resource gs://<backet name>
Try stopping the instance manually.
$ gcloud compute instances stop
If stopped manually, it will be " preempted_status ":" FALSE "
.
Check the log.
```
$ gcloud logging read "resource.type=\"cloud_function\" AND resource.labels.function_name=\"boot_preemptible_instance\" AND resource.labels.region=\"asia-northeast1\"" --format="table(timestamp, textPayload)" |head -10
TIMESTAMP TEXT_PAYLOAD
2020-08-11T04:38:26.982711475Z Function execution took 1894 ms, finished with status: 'ok'
2020-08-11T04:38:26.979Z Preempted status is FALSE.
:
2020-08-11T04:38:25.090798409Z Function execution started
```
$ cat preempted_status.out
{"preempted_status":"TRUE", "instance_name":"<instance name>", "zone":"asia-northeast1-a", "project":"<project name>"}
$ gsutil cp preempted_status.out gs://<backet name>/
$ gsutil cat gs://<backet name>/preempted_status.out
{"preempted_status":"TRUE", "instance_name":"<instance name>", "zone":"asia-northeast1-a", "project":"<project name>"}
Check the log.
$ gcloud logging read "resource.type=\"cloud_function\" AND resource.labels.function_name=\"boot_preemptible_instance\" AND resource.labels.region=\"asia-northeast1\"" --format="table(timestamp, textPayload)" |head -12
TIMESTAMP TEXT_PAYLOAD
2020-08-11T05:02:51.640069088Z Function execution took 15066 ms, finished with status: 'ok'
2020-08-11T05:02:51.637Z Instance started.
2020-08-11T05:02:51.637Z Instance starting.. status = RUNNING
2020-08-11T05:02:46.246Z Instance starting.. status = STAGING
2020-08-11T05:02:40.862Z Instance starting.. status = TERMINATED
2020-08-11T05:02:39.373Z Instance status is now TERMINATED
2020-08-11T05:02:38.959Z Preempted status is TRUE. Boot up the instance.
:
2020-08-11T05:02:36.577339221Z Function execution started
The instance has started.
$ gcloud compute instances list
NAME ZONE MACHINE_TYPE PREEMPTIBLE INTERNAL_IP EXTERNAL_IP STATUS
<instance name> asia-northeast1-a g1-small true XX.XX.XX.XX XX.XX.XX.XXX RUNNING
Recommended Posts