Skip to content

Automatic startup and shutdown of compute instances in Google Cloud Platform

License

Notifications You must be signed in to change notification settings

lmaczulajtys/gcp-instance-scheduler

Repository files navigation

GCP Instance Scheduler

Automatic startup and shutdown of compute instances in Google Cloud Platform. Define and manage in one place schedules for VMs in multiple projects and reduce cost of your CGP cloud.

Components

Solution's architecture is similar to described in th article: Scheduling compute instances with Cloud Scheduler

Component Service Description
configuration Datastore Scheduler configuration
instance-scheduler Cloud Functions Function starting/stopping Compute Instances
instance-scheduler-topic Pub/Sub Topic Topic for events triggering function
instance-scheduler-job Cloud Scheduler Cron job for function triggering

Getting started

Prerequisites

  1. Install gcloud
  2. Create a GCP project, set up billing
  3. Enable required APIs using this link or manually:
  1. Cloud Build API
  2. Cloud Datastore API
  3. Cloud Functions API
  4. Cloud Pub/Sub API
  5. Cloud Scheduler API

Installation

Instalation is similat to presented in Google Cloud tutorial.

  1. Prepare environment
export GCP_PROJECT="Project id where scheduler will be installed"
export GCP_REGION="Region where scheduler will be installed"
export ENV_PROJECTS_LIST="You have to enable scheduling for every project separately. Set comma separated projects list with scheduling enabled e.g. project1,project2,project3"
export ENV_TIMEZONE="Scheduler timezone e.g. Europe/Warsaw"
  1. Create role for scheduler
gcloud iam roles create InstanceSchedulerRole \
    --description "Instance Scheduler for automatic start/stop of instances" \
    --permissions compute.instances.list,compute.instances.start,compute.instances.stop \
    --stage GA --project $GCP_PROJECT
  1. Create service account for function
gcloud iam service-accounts create instance-scheduler \
    --description="Instance scheduler" \
    --display-name="instance-scheduler"
  1. Assign roles to service account
gcloud projects add-iam-policy-binding $GCP_PROJECT \
    --member="serviceAccount:instance-scheduler@$GCP_PROJECT.iam.gserviceaccount.com" \
    --role="projects/$GCP_PROJECT/roles/InstanceSchedulerRole"
gcloud projects add-iam-policy-binding $GCP_PROJECT \
    --member="serviceAccount:instance-scheduler@$GCP_PROJECT.iam.gserviceaccount.com" \
    --role="roles/datastore.viewer"
  1. Create Pub/Sub topic for function triggering
gcloud pubsub topics create instance-scheduler-topic
  1. Clone this project and and open function folder:
git clone [email protected]:lmaczulajtys/gcp-instance-scheduler.git
cd gcp-instance-scheduler/functions/instance_scheduler
  1. Create function with pub/sub trigger
gcloud functions deploy instance-scheduler \
    --trigger-topic=instance-scheduler-topic \
    --region=$GCP_REGION \
    --runtime=python38 \
    --entry-point=main \
    --service-account=instance-scheduler@$GCP_PROJECT.iam.gserviceaccount.com \
    --set-env-vars=^:^ENV_PROJECTS_LIST=$ENV_PROJECTS_LIST:ENV_TIMEZONE=$ENV_TIMEZONE \
    --max-instances=1
  1. Create Cloud Scheduler Job
gcloud scheduler jobs create pubsub instance-scheduler-job --schedule="*/5 * * * *" --topic=instance-scheduler-topic --message-body=run

Configuration

Configuring schedules

Currently, you have to create configuration manually. CLI is planned.

Scheduling configuration is stored in Datastore in gcp-instance-scheduler namespace in two entity types:

  • Period - information when instance should be started or stopped
  • name - period unique name
  • description - period description
  • beginTime - instance start time hour e.g. 00:00, 08:30, 18:45
  • endTime - instance stop time hour e.g. 00:00, 08:30, 18:45
  • weekdays - integer array od week days numbers (monday - 0, sunday - 6) where period is active
  • Schedule - schedule with one or multiple periods. Instances are assigned to schedules - not periods.
  • name - schedule unique name
  • description - schedule description
  • periods - string array od periods in schedule

To create Period

  1. Create entity
  2. Set napespace to gcp-instance-scheduler
  3. Set Kind to Period
  4. Set Key identifier to Custom name
  5. Set another values. Be careful with weekdays!!! Use example below for whole week:
{
  "values": [
    {
      "integerValue": "0"
    },
    {
      "integerValue": "1"
    },
    {
      "integerValue": "2"
    },
    {
      "integerValue": "3"
    },
    {
      "integerValue": "4"
    },
    {
      "integerValue": "5"
    },
    {
      "integerValue": "6"
    }
  ]
}
  1. See example below. Create Period entity

To create Schedule

  1. Create entity
  2. Set napespace to gcp-instance-scheduler
  3. Set Kind to Schedule
  4. Set Key identifier to Custom name
  5. Set another values. Be careful with schedules!!! Use example below:
{
  "values": [
    {
      "stringValue": "business-days"
    }
  ]
}
  1. See example below. Create Schedule entity

Setting schedule on instance

To assign instance to configured schedule, you only have to add label schedule to your instance with schedule name as a value. For example: schedule=business-days or schedule=dev.

To disable scheduling, remove label schedule from your instance.

Enabling scheduling in another project

You have to enable scheduling in every project separately.

  1. Setup
export SCHEDULER_PROJECT="project id with scheduling function"
export NEW_PROJECT="project id where you want to enable scheduling"
  1. Create role for scheduler in project and assign it to service account from scheduler project.
gcloud iam roles create InstanceSchedulerRole \
    --description "Instance Scheduler for automatic start/stop of instances" \
    --permissions compute.instances.list,compute.instances.start,compute.instances.stop \
    --stage GA --project $NEW_PROJECT
gcloud projects add-iam-policy-binding $GCP_PROJECT \
    --member="serviceAccount:instance-scheduler@$SCHEDULER_PROJECT.iam.gserviceaccount.com" \
    --role="projects/$NEW_PROJECT/roles/InstanceSchedulerRole"
  1. Edit instance-scheduler function and extend PROJECTS_LIST environment variable. Add comma and new project id e.g. project1,project2,new-project
  2. Click next and deploy function.

License

This project is licensed under the Apache 2.0 License - see the LICENSE file for details.

Acknowledgments

Main inspiration: awslabs/aws-instance-scheduler

About

Automatic startup and shutdown of compute instances in Google Cloud Platform

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages