GCP dispose d'une VM appelée Preemptible VM qui s'arrête jusqu'à 24 heures au prix d'un prix modique. https://cloud.google.com/compute/docs/instances/preemptible?hl=ja
Les machines virtuelles qui sont arrêtées après avoir reçu la notification de préemption ne démarreront pas automatiquement. Voici une description des outils permettant le démarrage automatique à l'aide de scripts d'arrêt et de Cloud Functions.
--Preemptible VM est un outil pour démarrer car il ne démarre pas automatiquement après l'arrêt.

Téléchargez le code source
git clone [email protected]:cloud-mainte/auto-boot-preemtible-vm.git
or 
git clone https://gitlab.com/cloud-mainte/auto-boot-preemtible-vm.git
Créez un bucket GCS --Créez un compartiment pour placer le script d'arrêt et placer le script d'arrêt.
Réécrivez la variable BUCKET dans le script comme il convient.
$ gsutil mb -l ASIA-NORTHEAST1 gs://<backet name>
$ gsutil cp shutdown-script.sh gs://<backet name>/
$ gsutil ls -l gs://<backet name>
--Créez un compartiment pour stocker l'état de la réception ou non d'une notification de préemption.
```
$ gsutil mb -l ASIA-NORTHEAST1 gs://<backet name>
```
Définissez shutdown-script sur l'instance cible
gcloud compute instances add-metadata <instance name> \
    --metadata shutdown-script-url=gs://<backet name>/shutdown-script.sh
Définissez le lanceur d'instance dans Cloud Functions
Réécrivez bucket dans main.py selon le cas.
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>
Essayez d'arrêter l'instance manuellement.
$ gcloud compute instances stop 
S'il est arrêté manuellement, il sera "" preempted_status ":" FALSE "`. 
Vérifiez le journal.
```
$ 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>"}
Vérifiez le journal.
$ 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
L'instance a démarré.
$ 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