Blog

Reduce Google Compute Engine costs by 60% with Zorya — GCE Instance Scheduler

1 bimx94i4agv23fzxumvs4a

Running non-production environments in the cloud is great. They are conveniently available to you at any time, right when you need them. But this convenience sometimes comes with a price tag. Usually, non-production instances are mostly used during the daytime and sometimes you are too busy to remember to shut them down during the off hours or the weekends. While each instance costs only a few quarters per hour, the overall cost can accrue very fast over the course of the month to thousands of dollars.

As a Google’s Premier and MSP Partner, we at DoiT International work with hundreds of startups and enterprises running their workloads on Google Cloud Platform in general and Google Compute Engine specifically. At our core, we always aim to reduce our customer’s monthly cloud spend by architecting smarter, more intelligent and cost-effective systems.

With these thoughts exactly, today we are open sourcing Zorya. In Slavic mythology, Zoryas are two guardian goddesses. The Zoryas represent the morning star and the evening star, — if you have read or watched Neil Gaiman’s American Gods, you will probably remember these sisters.

So what is Zorya exactly? It’s a little piece of software that you can run on your own Google Cloud Platform account which will allow you to configure the schedule for your Google Compute Engine instances and establish a policy based across all of your organization’s projects.

We really wanted Zorya to be very easy to install and maintain so we came up with the following design goals :

  • Run Zorya in a serverless manner, reducing management burden
  • Be able to setup multiple schedules and policies
  • Support multiple projects from a single instance of Zorya
  • Low operational cost of running Zorya

We decided to build Zorya and to run it on Google App Engine, Standard Environment. This alone helped us to achieve two of the requirements:

  1. Serverless operation by using App Engine, so we don’t have to manage our servers, and we get high availability and scalability out of the box.
  2. Low operational cost — due to the pricing model of Google App Engine and to the fact that Zorya “wakes up” only once an hour and in the rest of the time it doesn’t consume any resources and therefore keeping the cost low.

Zorya’s backend is written in Python with React based frontend. We have decided to use Google Cloud Datastore as our database to store the configuration data.

Backend

The backend provides a simple RESTful API for getting and setting schedules as well as policies.

Schedule entity is composed from a name , a time zone and a 7x24 matrix that represents each hour in each day of the week. 0 values means that the instance should be in a stopped state and 1 means that the instance should be online.

Policy entity is composed from a name, a list of projects, associated schedule name and a list of Google Compute Engine labels, i.e. pairs of names and values.

{
  "name": "My policy",
  "tags": [
    {
      "dev": "sleeper",
      "staging": "resting"
    }
  ],
  "projetcs": [
    "project-x",
    "y-project"
  ],
  "schedulename": "my schedule name"
}

The API is very simple and have only eight endpoints:

  • add_policy
  • list_schedules
  • del_schedule
  • get_schedule
  • add_policy
  • list_policies
  • get_policy
  • del_policy

Api documentation and a sample consumer code in a few languages can be found here.

Using Google App Engine built-in scheduler, there is a task that checks the policies to see if there is a need to change instances’ state.

If there is a need to change the state, Zorya will loop over the relevant projects and change the state for the instances with the requested label pairs.

Authentication

In order to allow Zorya to manage instances on your behalf in any project within your organization, you’ll need to create a new entry in your Organization IAM and assign Zorya’s service account with a role of “Compute Admin”

First, navigate to https://console.cloud.google.com, then select “IAM & Admin” from the menu and finally select the name of your organization (not just specific project) from the dropdown at the top of the page:

1 b 2mrjt3qw3x93wamm1b0a

The name of the service account you will need to assign permissions to is : [email protected], please just replace project-id with your real project id where you deploy Zorya.

Frontend

Since we anticipate most people will use Zorya’s API, we’ve built a fairly simplistic user-interface which you can use to create schedules and policies. The frontend is based on ReactJS and it’s published on the default module of the App Engine application.

To sign into the app, we are using Cloud Identity-Aware Proxy. Cloud IAP works by verifying a user’s identity and determining if that user should be allowed to access the application. The setup is as simple as heading over to GCP console, enabling IAP on your App Engine application and adding the users or groups which should have access to Zorya.

To start using the user interface, just open your browser at project-id.appspot.com and replace project-id with your own Google Cloud Platform project id.

Using the user interface, it’s easy to setup a new schedule, like in this example — Monday to Friday, 8am to 7pm based on US/Eastern time zone. During that period of time, your instances will be running and outside of this timeframe, Zorya will stop the instances and start them again on the next day.

1 9d5lmvhuyf2pmjtctpcqag
Zorya Schedule Editor

Once you have configured your schedule, you need to setup a policy to apply the schedule to some of the instances. Let’s say you’d like to apply the schedule to all the instances in project doit-playground with labels env:dev, role:frontend and app:reoptimize, so the affected instances would be only these having all three label pairs.

1 pw8la9eyh98tmovrohzzpw
Setting up Zorya Policy

Cost Reduction

We are using reOptimize — a free service from DoiT International to track our Google Cloud Platform costs. reOptimize can extract cost per label in a given GCP Organization and provide a nice visualization.

We have started to use Zorya internally back in December 2017 when our Google Compute Engine costs were about $780/day on average. With Zorya on our development and testing environments, our GCE costs are now only $320/day on average — an almost 60% reduction.

1 toihb7mxeu58exxuvbqkcq (1)
reOptimize.io Dashboard

Closing

If you’d like to also use Zorya to optimize your monthly bill with Google Cloud Platform, please give us a start on GitHub! ;-) We are also very “pull request friendly” and we appreciate your help improving Zorya over time!


Want more stories? Check out our blog, or follow Aviv on Twitter.

Subscribe to updates, news and more.

Related blogs