项目作者: browny

项目描述 :
Scheduling compute instances with Cloud Scheduler
高级语言:
项目地址: git://github.com/browny/gcp-cloud-scheduler-tutorial.git
创建时间: 2020-04-01T01:59:02Z
项目社区:https://github.com/browny/gcp-cloud-scheduler-tutorial

开源协议:MIT License

下载


gcp-cloud-scheduler-tutorial

Open in Cloud
Shell

Scheduling compute instances with Cloud Scheduler

This tutorial demos how to use Cloud Scheduler to schedule Compute Engine instances to start and
stop at specified or periodical time.

The main componentes used in this turial includes Compute Engine instances (with labels), Cloud
Pub/Sub, Cloud Functions and Cloud Scheduler. The event flow looks like as below:

  1. Cloud Scheduler -> Cloud Pub/Sub -> Cloud Functions -> Start/Stop Compute Engine instances

1. Config project

Select the project which you have permissions to access Compute Engine, Cloud Pub/Sub, Cloud
Functions and Cloud Scheduler. Remember to enable these APIs first.

Set up the Compute Engine instance

Create a sample instance with label env=dev which will be used as filter when start/stop action
executed by Cloud Functions.

  1. gcloud compute instances create dev-instance \
  2. --network default \
  3. --zone us-west1-b \
  4. --labels=env=dev

Set up the Cloud Functions functions with Pub/Sub

Create 2 Cloud Pub/Sub topics which will be used as the triggers of following Cloud Functions.

1. Create start-instance and stop-instance event

  1. gcloud pubsub topics create start-instance-event
  1. gcloud pubsub topics create stop-instance-event

Create 2 Cloud Funtions, one for starting instances, the other for stopping instances. They are
triggered by the message of above 2 Cloud Pub/Sub topics.

2. Get code

  1. git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git
  1. cd nodejs-docs-samples/functions/scheduleinstance/

3. Create the start and stop functions

  1. gcloud functions deploy startInstancePubSub \
  2. --trigger-topic start-instance-event \
  3. --runtime nodejs8 \
  4. --allow-unauthenticated
  1. gcloud functions deploy stopInstancePubSub \
  2. --trigger-topic stop-instance-event \
  3. --runtime nodejs8 \
  4. --allow-unauthenticated

Once the functions created, we can manually trigger it to verify the instance being stopped or
started.

4. (Optional) Verify the functions work

  1. gcloud functions call stopInstancePubSub \
  2. --data '{"data":"eyJ6b25lIjoidXMtd2VzdDEtYiIsICJsYWJlbCI6ImVudj1kZXYifQo="}'
  1. gcloud compute instances describe dev-instance \
  2. --zone us-west1-b \
  3. | grep status

Set up the Cloud Scheduler jobs to call Pub/Sub

Now we have 2 Cloud Functions with corresponding Cloud Pub/Sub topics as their triggers ready. Then
we will create 2 Cloud Scheduler jobs, one for starting the instance at 9AM every weekday (Mon. to
Fri.), the other for stopping the instance at 10AM every weekday.

1. Create the jobs (notice: change below --schedule parameter to 0 9 * * 1-5 / 0 10 * * 1-5)

  1. gcloud beta scheduler jobs create pubsub startup-dev-instances \
  2. --schedule '0 9 * * 1-5' \
  3. --topic start-instance-event \
  4. --message-body '{"zone":"us-west1-b", "label":"env=dev"}' \
  5. --time-zone 'Asia/Taipei'
  1. gcloud beta scheduler jobs create pubsub shutdown-dev-instances \
  2. --schedule '0 10 * * 1-5' \
  3. --topic stop-instance-event \
  4. --message-body '{"zone":"us-west1-b", "label":"env=dev"}' \
  5. --time-zone 'Asia/Taipei'

Once the jobs created, we don’t need to wait until the scheduled time, we can manaully run the jobs
to verify the following actions being ran accordingly.

2. (Optional) Verify the jobs work

  1. gcloud beta scheduler jobs run shutdown-dev-instances
  1. gcloud compute instances describe dev-instance \
  2. --zone us-west1-b \
  3. | grep status
  1. gcloud beta scheduler jobs run startup-dev-instances
  1. gcloud compute instances describe dev-instance \
  2. --zone us-west1-b \
  3. | grep status

Clean up

  • Delete the Cloud Scheduler jobs
  • Delete the Pub/Sub topics
  • Delete the Cloud Functions functions
  • Delete the Compute Engine instance