# 設定步驟 ## VM Instance 設定 - 加入 label , Key1 輸入 env , Value1 輸入dev, 之後scheduler會針對 env=dev的VM執行排程 ![image](https://hackmd.io/_uploads/HJ_mjAKV6.png) ## Cloud Function 設定 - 進入 Cloud Functions 畫面, 新增二個 function : startInstancePubSub 和 stopInstancePubSub - 依照[本網頁 Create and deploy the functions](https://cloud.google.com/scheduler/docs/start-and-stop-compute-engine-instances-on-a-schedule#create_and_deploy_the_functions)設定. 注意 Region可以和 VM不同, Runtime 改成 Node.js 2.0, 設定完成如下圖 ![image](https://hackmd.io/_uploads/SyG5iAK4T.png) - 在 stopInstance PubSub 的 index.js 最後加入這段code, 這是為了做到 graceful shutdown ``` process.on('SIGTERM', function () { console.log('helloworld: received SIGTERM, exiting gracefully'); process.exit(0); }); ``` 如下圖 ![image](https://hackmd.io/_uploads/SkWiID2Vp.png) - 測試 Function 是否可以把 env=dev 的VM 開關機 - 執行方式如[本網頁 (Optional) Verify the functions work](https://cloud.google.com/scheduler/docs/start-and-stop-compute-engine-instances-on-a-schedule#optional_verify_the_functions_work), 注意 {"zone":"us-west1-b", "label":"env=dev"} 的 zone 要修改為 VM 所在的 zone, 再將此字串貼到[轉換網頁](https://www.google.com/search?q=base64+encoding+tool)轉成 base64 - 如果執行stopInstancePubSub, 確認 VM status 狀態從 start 變成 stop. - 如果執行startInstancePubSub, 確認 VM status 狀態從 stop 變成 start. ## Cloud Scheduler 設定 - 進入 Cloud Scheduler 畫面, 建立二個 job : startup-dev-instances, shutdown-dev-instances, - 建立的方式參考[本網頁 Create the jobs](https://cloud.google.com/scheduler/docs/start-and-stop-compute-engine-instances-on-a-schedule#create_the_jobs), 注意 zone要指定VM所在位置, 建立完成如下圖 ![image](https://hackmd.io/_uploads/r1OhjCKEa.png) - 測試 Job 是否可以把 env=dev 的VM 開關機 - 執行 shutdown-dev-instances 把VM關機 - 執行 startup-dev-instances 把VM開機 # 查詢執行紀錄 - 從 VM 的 OBSERVABILITY -> LOGS -> 選擇適當時間區間, 可以看到 scheduled job 的紀錄 ![image](https://hackmd.io/_uploads/rJzQPxs4T.png) # gcloud 執行 - 執行關機 scheduler gcloud beta scheduler jobs run shutdown-dev-instances --location=us-central1 - 執行開機 scheduler gcloud beta scheduler jobs run startup-dev-instances --location=us-central1 - 列出 label env=dev 的 VM gcloud compute instances list --filter='labels.env:dev' - 顯示指定字串的 base64編碼 echo ‘{“zone”:”asia-east1-b”, “label”:”env=dev”}’ | base64 # 參考資料 - [Graceful shutdowns on Cloud Run: Deep dive](https://cloud.google.com/blog/topics/developers-practitioners/graceful-shutdowns-cloud-run-deep-dive) - [Schedule a Compute Engine VM to start or stop](https://cloud.google.com/scheduler/docs/start-and-stop-compute-engine-instances-on-a-schedule) - [Cloud Scheduler 統一關機VM Instance](https://joehuang-pop.github.io/2020/02/12/GCP-Cloud-Scheduler-%E7%B5%B1%E4%B8%80%E9%97%9C%E6%A9%9FVM-Instance-Cloud-Scheduler-Saving-GCP-Cost/) - [Trap termination signal (SIGTERM) sent to the container instance](https://cloud.google.com/run/docs/samples/cloudrun-sigterm-handler?hl=zh-cn#cloudrun_sigterm_handler-nodejs) - [Stop and start a VM](https://cloud.google.com/compute/docs/instances/stop-start-instance) - [Running shutdown scripts](https://cloud.google.com/compute/docs/shutdownscript) - [Scheduling a VM instance to start and stop](https://cloud.google.com/compute/docs/instances/schedule-instance-start-stop)